Skip to content

Commit

Permalink
Merge pull request #198 from anarion80/add_devenv
Browse files Browse the repository at this point in the history
Add devenv
  • Loading branch information
anarion80 authored Jan 14, 2025
2 parents d8d6994 + 65cc5f7 commit 234e916
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source_url "https://raw.githubusercontent.com/cachix/devenv/82c0147677e510b247d8b9165c54f73d32dfd899/direnvrc" "sha256-7u4iDd1nZpxL4tCzmPG0dQgC5V+/44Ba+tHkPob1v2k="

use devenv
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,12 @@ Session.vim

*.lock

# Devenv
.devenv*
devenv.local.nix

# direnv
.direnv

# pre-commit
.pre-commit-config.yaml
186 changes: 186 additions & 0 deletions devenv.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
{
pkgs,
lib,
config,
...
}: {
name = "ansible-nas";

# https://devenv.sh/basics/
env = {
GREET = "ansible-nas devenv";
};

# 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|=| |' <<EOF | ${pkgs.util-linuxMinimal}/bin/column -t | ${pkgs.gnused}/bin/sed -e 's|^|🦾 |' -e 's|••| |g'
${lib.generators.toKeyValue {} (lib.mapAttrs (name: value: value.description) config.scripts)}
EOF
echo
'';

scripts.run = {
exec = ''
# Initialize variables
tag=""
host=""
# Function to display usage
usage() {
echo "Usage: $0 [-t|--tag <tag>] [-h|--host <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 <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')";
};
}
9 changes: 9 additions & 0 deletions devenv.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
inputs:
nixpkgs:
url: github:cachix/devenv-nixpkgs/rolling
nixpkgs-python:
url: github:cachix/nixpkgs-python
inputs:
nixpkgs:
follows: nixpkgs

0 comments on commit 234e916

Please sign in to comment.