From 1a22d4521fb0c5b41bf607141b97056908a72ee1 Mon Sep 17 00:00:00 2001 From: Anarion Dunedain Date: Tue, 14 Jan 2025 12:52:46 +0100 Subject: [PATCH 1/3] Update .gitignore --- .gitignore | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.gitignore b/.gitignore index 12c7ba201..74500df4b 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,12 @@ Session.vim *.lock +# Devenv +.devenv* +devenv.local.nix + +# direnv +.direnv + +# pre-commit +.pre-commit-config.yaml From aefefb6cd5496d1dc368e997de325571b0ae806c Mon Sep 17 00:00:00 2001 From: Anarion Dunedain Date: Tue, 14 Jan 2025 12:53:22 +0100 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=A7=91=F0=9F=92=BB=20Add=20devenv=20n?= =?UTF-8?q?ix=20support?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .envrc | 3 + devenv.nix | 188 ++++++++++++++++++++++++++++++++++++++++++++++++++++ devenv.yaml | 9 +++ 3 files changed, 200 insertions(+) create mode 100644 .envrc create mode 100644 devenv.nix create mode 100644 devenv.yaml diff --git a/.envrc b/.envrc new file mode 100644 index 000000000..894571bf4 --- /dev/null +++ b/.envrc @@ -0,0 +1,3 @@ +source_url "https://raw.githubusercontent.com/cachix/devenv/82c0147677e510b247d8b9165c54f73d32dfd899/direnvrc" "sha256-7u4iDd1nZpxL4tCzmPG0dQgC5V+/44Ba+tHkPob1v2k=" + +use devenv diff --git a/devenv.nix b/devenv.nix new file mode 100644 index 000000000..eb42cdd26 --- /dev/null +++ b/devenv.nix @@ -0,0 +1,188 @@ +{ + pkgs, + lib, + config, + ... +}: { + name = "ansible-nas"; + + # https://devenv.sh/basics/ + env = { + GREET = "ansible-nas devenv"; + VIRTUAL_ENV = "ansible-nas"; + VIRTUAL_ENV_PROMPT = "(ansiblenas)"; + }; + + # https://devenv.sh/packages/ + packages = [ + pkgs.git + ]; + + # https://devenv.sh/languages/ + languages = { + python = { + enable = true; + version = "3.12"; + uv.enable = true; + venv.enable = true; + venv.requirements = '' + pre-commit>=4.0.1 + molecule>=24.9.0 + pytest>=8.3.4 + pytest-molecule>=2.0.0 + molecule-plugins[docker]>=23.5.3 + yamllint>=1.35.1 + ansible-lint>=24.10.0 + pymarkdownlnt>=0.9.25 + ansible>=11.1.0 + jmespath>=1.0.1 + ''; + }; + ansible = { + enable = true; + }; + }; + + enterShell = '' + echo Entering development environment for ansible-nas... + echo + echo 🦾 Available scripts: + echo 🦾 + ${pkgs.gnused}/bin/sed -e 's| |••|g' -e 's|=| |' <] [-h|--host ]" + echo "Options:" + echo " -t, --tag Specify the Ansible tag" + echo " -H, --host Specify the target host" + echo " -h, --help Show this help message" + exit 1 + } + + # Parse named arguments + while [[ "$#" -gt 0 ]]; do + case $1 in + -t|--tag) tag="$2"; shift ;; + -H|--host) host="$2"; shift ;; + -h|--help) usage ;; + *) echo "Unknown parameter: $1"; usage ;; + esac + shift + done + + # Base command + cmd="ansible-playbook -i inventories/my-ansible-nas/inventory nas.yml -b -K" + + # Echo the run message + echo '🏃 Run Playbook' + + # Add tag if provided + if [ ! -z "$tag" ]; then + cmd="$cmd -t $tag" + fi + + # Add host limit if provided + if [ ! -z "$host" ]; then + cmd="$cmd --limit $host" + fi + + # Print the command being executed (optional, for debugging) + echo "Executing: $cmd" + + # Execute the command + eval $cmd + ''; + description = "Run Playbook (add '-t tag' to only run certain role, and '-H host' to limit the playbook run to only that host from your inventory"; + }; + + scripts.check = { + exec = '' + echo '🏃 Run Playbook Syntax Check' + ansible-playbook -i inventory nas.yml --syntax-check + ''; + description = "Run Playbook Syntax Check"; + }; + + scripts.precommit = { + exec = '' + echo '🏃 Run pre-commit checks' + pre-commit run --all-files + ''; + description = "Run pre-commit checks"; + }; + + scripts.pytest = { + exec = '' + echo '🏃 Run pytest' + pytest --molecule-base-config=../../tests/molecule/base.yml + ''; + description = "Run pytest"; + }; + + scripts.roletest = { + exec = '' + # Initialize variables + role="" + + # Function to display usage + usage() { + echo "Usage: $0 [-r|--role ]" + echo "Options:" + echo " -r, --role Specify the role to test (required)" + echo " -h, --help Show this help message" + exit 1 + } + + # Parse named arguments + while [[ "$#" -gt 0 ]]; do + case $1 in + -r|--role) role="$2"; shift ;; + -h|--help) usage ;; + *) echo "Unknown parameter: $1"; usage ;; + esac + shift + done + + # Check if tag is provided + if [ -z "$role" ]; then + echo "Error: Role parameter is required" + usage + fi + + # Base command + cmd="molecule -c ../../tests/molecule/base.yml test" + + # Echo the run message + echo '🏃 Running Molecule Tests' + + # Add role if provided + if [ ! -z "$role" ]; then + cd roles/$role + fi + + # Print the command being executed (optional, for debugging) + echo "Executing: $cmd" + + # Execute the command + eval $cmd + + # Go back to root + if [ ! -z "$role" ]; then + cd ../../ + fi + + ''; + description = "Run molecule role test (provide '-r role')"; + }; +} diff --git a/devenv.yaml b/devenv.yaml new file mode 100644 index 000000000..1e1bcee1b --- /dev/null +++ b/devenv.yaml @@ -0,0 +1,9 @@ +--- +inputs: + nixpkgs: + url: github:cachix/devenv-nixpkgs/rolling + nixpkgs-python: + url: github:cachix/nixpkgs-python + inputs: + nixpkgs: + follows: nixpkgs From 65cc5f7ca20fefc816314df9faf4755895368524 Mon Sep 17 00:00:00 2001 From: Anarion Dunedain Date: Tue, 14 Jan 2025 13:07:14 +0100 Subject: [PATCH 3/3] =?UTF-8?q?=E2=9A=B0=EF=B8=8F=20Remove=20unnecessary?= =?UTF-8?q?=20env=20variables?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- devenv.nix | 2 -- 1 file changed, 2 deletions(-) diff --git a/devenv.nix b/devenv.nix index eb42cdd26..309c12511 100644 --- a/devenv.nix +++ b/devenv.nix @@ -9,8 +9,6 @@ # https://devenv.sh/basics/ env = { GREET = "ansible-nas devenv"; - VIRTUAL_ENV = "ansible-nas"; - VIRTUAL_ENV_PROMPT = "(ansiblenas)"; }; # https://devenv.sh/packages/