forked from davestephens/ansible-nas
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdevenv.nix
186 lines (159 loc) · 4.31 KB
/
devenv.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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')";
};
}