Skip to content

Commit 9f6afdb

Browse files
committed
Create a python venv to run the ansible playbooks
The patch creates a python virtual environment where required python modules and ansible collections are installed without interfering or polluting the system. Signed-off-by: Xavi Hernandez <[email protected]>
1 parent 2df0a4b commit 9f6afdb

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ansible
2+
netaddr

site

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
#!/bin/bash
2+
3+
set -eu
4+
5+
function check_version() {
6+
local v
7+
8+
for v in "${VERSION[@]}"; do
9+
[[ ${v} -ne ${1-0} ]] && break
10+
unset v
11+
shift
12+
done
13+
14+
[[ ${v-0} -ge ${1-0} ]]
15+
}
16+
17+
function template() {
18+
local file="${SITE_HOME}/${1}"
19+
local mode="${2}"
20+
local content="${3}"
21+
22+
sed -n 's/^\s*|//p' <<<"${content}" >"${file}"
23+
chmod "${mode}" "${file}"
24+
}
25+
26+
function wrapper() {
27+
bash --rcfile "${SITE_HOME}/.siterc" -i "${@}"
28+
}
29+
30+
function run() {
31+
wrapper -c "${*}"
32+
}
33+
34+
function cmd_show() {
35+
find "${SITE_USER}" -mindepth 1 -maxdepth 1 -type d |
36+
xargs -i{} bash -c '
37+
if [[ -f "{}/.siterc" ]]; then
38+
echo "$(basename "{}") $(grep -o "EXTRA_VARS.*" "{}/.siterc")"
39+
fi
40+
'
41+
}
42+
43+
function cmd_yamllint() {
44+
yamllint -c .yamllint .
45+
}
46+
47+
function cmd_statedump() {
48+
run make nodes.statedump
49+
}
50+
51+
function cmd_shell() {
52+
wrapper
53+
}
54+
55+
function cmd_create() {
56+
local opts
57+
58+
opts=(
59+
"--symlinks"
60+
"--clear"
61+
)
62+
check_version 3 6 && opts+=("--prompt" "SITE:${SITE_NAME}")
63+
check_version 3 9 && opts+=("--upgrade-deps")
64+
65+
${PYTHON} -m venv "${opts[@]}" "${SITE_HOME}"
66+
template ".siterc" 0644 "
67+
|if [[ -f ~/.bashrc ]]; then
68+
| source ~/.bashrc
69+
|fi
70+
|
71+
|export EXTRA_VARS='${EXTRA_VARS-}'
72+
|
73+
|alias ssh=\"ssh -F \\\"\${SITE_HOME}/ssh-config-host\\\"\"
74+
|alias scp=\"scp -F \\\"\${SITE_HOME}/ssh-config-host\\\"\"
75+
|alias sftp=\"sftp -F \\\"\${SITE_HOME}/ssh-config-host\\\"\"
76+
|
77+
|source \"\${SITE_HOME}/bin/activate\"
78+
|
79+
|cd \"\${SITE_HOME}\"
80+
"
81+
82+
template "Makefile" 0644 "
83+
|PHONY: \$(MAKECMDGOALS) all
84+
|
85+
|\$(or \$(MAKECMDGOALS), all):
86+
| @make -C \"\${SITE_ROOT}/playbooks\" \$(MAKECMDGOALS)
87+
"
88+
89+
cp "${SITE_ROOT}/requirements.txt" "${SITE_HOME}"
90+
91+
run pip install --upgrade -r requirements.txt
92+
}
93+
94+
function cmd_build() {
95+
run make
96+
}
97+
98+
function cmd_clean() {
99+
run make clean
100+
}
101+
102+
function cmd_destroy() {
103+
cmd_clean
104+
rm -rf "${SITE_HOME}"
105+
}
106+
107+
export SITE_USER="$(realpath "${HOME}/.site")"
108+
109+
mkdir -p "${SITE_USER}"
110+
111+
CMD="${1-show}"
112+
113+
if [[ "${CMD}" == "show" ]]; then
114+
cmd_show
115+
exit 0
116+
elif [[ "${CMD}" == "yamllint" ]]; then
117+
cmd_yamllint
118+
exit 0
119+
fi
120+
121+
PYTHON="$(echo -n "${PATH}" |
122+
tr ':' '\n' |
123+
xargs -i{} find {} -maxdepth 1 -type f -regex '.*/python[0-9.]*' 2>/dev/null |
124+
xargs -i{} bash -c "echo \$({} --version | tr -dc '0-9.') {}" |
125+
LC_ALL=C sort -Vr |
126+
head -n 1 |
127+
cut -d" " -f 2-)"
128+
129+
VERSION=($(${PYTHON} --version | tr -dc '0-9.' | tr '.' ' '))
130+
131+
if ! check_version 3 4; then
132+
echo "Python 3.4+ is required" >&2
133+
exit 1
134+
fi
135+
136+
echo "Using ${PYTHON} ($(IFS=.; echo "${VERSION[*]}"))"
137+
138+
export SITE_HOME="$(realpath "${SITE_USER}/${2?Environment name is not specified}")"
139+
export SITE_NAME="$(basename "${SITE_HOME}")"
140+
export SITE_ROOT="$(realpath "$(dirname "${0}")")"
141+
142+
if [[ "${CMD}" == "create" ]]; then
143+
cmd_create
144+
exit 0
145+
fi
146+
147+
if [[ ! -d "${SITE_HOME}" ]]; then
148+
if [[ "${CMD}" != "destroy" ]]; then
149+
echo "Directory doesn't exist" >&2
150+
exit 1
151+
fi
152+
exit 0
153+
fi
154+
155+
if [[ ! -f "${SITE_HOME}/.siterc" ]]; then
156+
echo "Directory is not a SITE configuration" >&2
157+
exit 1
158+
fi
159+
160+
case "${CMD}" in
161+
"statedump")
162+
cmd_statedump
163+
;;
164+
"shell")
165+
cmd_shell
166+
;;
167+
"build")
168+
cmd_build
169+
;;
170+
"clean")
171+
cmd_clean
172+
;;
173+
"destroy")
174+
cmd_destroy
175+
;;
176+
*)
177+
echo "Unknown command" >&2
178+
exit 1
179+
esac

0 commit comments

Comments
 (0)