blob: d7f2242040f44d92816a5d96f67e3b4bda239f7f [file] [log] [blame]
Mykyta Karpindb737652018-02-05 11:11:31 +02001#!/usr/bin/env bash
2
3###
4# Script requirments:
Mykyta Karpin4f30fff2018-02-23 15:46:10 +02005#apt-get install -y python-yaml virtualenv git
6
Mykyta Karpindb737652018-02-05 11:11:31 +02007set -e
8[ -n "$DEBUG" ] && set -x
9
10CURDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
11METADATA=${CURDIR}/../metadata.yml
12FORMULA_NAME=$(cat $METADATA | python -c "import sys,yaml; print yaml.load(sys.stdin)['name']")
13FORMULA_META_DIR=${CURDIR}/../${FORMULA_NAME}/meta
14
15## Overrideable parameters
16PILLARDIR=${PILLARDIR:-${CURDIR}/pillar}
17BUILDDIR=${BUILDDIR:-${CURDIR}/build}
18VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv}
19MOCK_BIN_DIR=${MOCK_BIN_DIR:-${CURDIR}/mock_bin}
20DEPSDIR=${BUILDDIR}/deps
21SCHEMARDIR=${SCHEMARDIR:-"${CURDIR}/../${FORMULA_NAME}/schemas/"}
22
23SALT_FILE_DIR=${SALT_FILE_DIR:-${BUILDDIR}/file_root}
24SALT_PILLAR_DIR=${SALT_PILLAR_DIR:-${BUILDDIR}/pillar_root}
25SALT_CONFIG_DIR=${SALT_CONFIG_DIR:-${BUILDDIR}/salt}
26SALT_CACHE_DIR=${SALT_CACHE_DIR:-${SALT_CONFIG_DIR}/cache}
27SALT_CACHE_EXTMODS_DIR=${SALT_CACHE_EXTMODS_DIR:-${SALT_CONFIG_DIR}/cache_master_extmods}
28
29SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null"
30
Mykyta Karpin4f30fff2018-02-23 15:46:10 +020031if [ "x${SALT_VERSION}" != "x" ]; then
Mykyta Karpindb737652018-02-05 11:11:31 +020032 PIP_SALT_VERSION="==${SALT_VERSION}"
33fi
34
Mykyta Karpindb737652018-02-05 11:11:31 +020035## Functions
36log_info() {
37 echo -e "[INFO] $*"
38}
39
40log_err() {
41 echo -e "[ERROR] $*" >&2
42}
43
44setup_virtualenv() {
45 log_info "Setting up Python virtualenv"
Mykyta Karpin4f30fff2018-02-23 15:46:10 +020046 dependency_check virtualenv
Mykyta Karpindb737652018-02-05 11:11:31 +020047 virtualenv $VENV_DIR
48 source ${VENV_DIR}/bin/activate
Mykyta Karpin4f30fff2018-02-23 15:46:10 +020049 python -m pip install salt${PIP_SALT_VERSION}
50 python -m pip install jsonschema
Mykyta Karpindb737652018-02-05 11:11:31 +020051 if [[ -f ${CURDIR}/pip_requirements.txt ]]; then
Mykyta Karpin4f30fff2018-02-23 15:46:10 +020052 python -m pip install -r ${CURDIR}/pip_requirements.txt
Mykyta Karpindb737652018-02-05 11:11:31 +020053 fi
54}
55
56setup_mock_bin() {
57 # If some state requires a binary, a lightweight replacement for
58 # such binary can be put into MOCK_BIN_DIR for test purposes
59 if [ -d "${MOCK_BIN_DIR}" ]; then
60 PATH="${MOCK_BIN_DIR}:$PATH"
61 export PATH
62 fi
63}
64
65setup_pillar() {
66 [ ! -d ${SALT_PILLAR_DIR} ] && mkdir -p ${SALT_PILLAR_DIR}
67 echo "base:" > ${SALT_PILLAR_DIR}/top.sls
68 for pillar in ${PILLARDIR}/*; do
69 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
70 state_name=$(basename ${pillar%.sls})
71 echo -e " ${state_name}:\n - ${state_name}" >> ${SALT_PILLAR_DIR}/top.sls
72 done
73}
74
75setup_salt() {
76 [ ! -d ${SALT_FILE_DIR} ] && mkdir -p ${SALT_FILE_DIR}
77 [ ! -d ${SALT_CONFIG_DIR} ] && mkdir -p ${SALT_CONFIG_DIR}
78 [ ! -d ${SALT_CACHE_DIR} ] && mkdir -p ${SALT_CACHE_DIR}
79 [ ! -d ${SALT_CACHE_EXTMODS_DIR} ] && mkdir -p ${SALT_CACHE_EXTMODS_DIR}
80
81 echo "base:" > ${SALT_FILE_DIR}/top.sls
82 for pillar in ${PILLARDIR}/*.sls; do
83 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
84 state_name=$(basename ${pillar%.sls})
85 echo -e " ${state_name}:\n - ${FORMULA_NAME}" >> ${SALT_FILE_DIR}/top.sls
86 done
87
88 cat << EOF > ${SALT_CONFIG_DIR}/minion
89file_client: local
90cachedir: ${SALT_CACHE_DIR}
91extension_modules: ${SALT_CACHE_EXTMODS_DIR}
92verify_env: False
93minion_id_caching: False
94
95file_roots:
96 base:
97 - ${SALT_FILE_DIR}
98 - ${CURDIR}/..
99
100pillar_roots:
101 base:
102 - ${SALT_PILLAR_DIR}
103 - ${PILLARDIR}
104EOF
105}
106
107fetch_dependency() {
108 # example: fetch_dependency "linux:https://github.com/salt-formulas/salt-formula-linux"
109 dep_name="$(echo $1|cut -d : -f 1)"
110 dep_source="$(echo $1|cut -d : -f 2-)"
111 dep_root="${DEPSDIR}/$(basename $dep_source .git)"
112 dep_metadata="${dep_root}/metadata.yml"
113
Mykyta Karpin4f30fff2018-02-23 15:46:10 +0200114 dependency_check git
Mykyta Karpindb737652018-02-05 11:11:31 +0200115 [ -d $dep_root ] && { log_info "Dependency $dep_name already fetched"; return 0; }
116
117 log_info "Fetching dependency $dep_name"
118 [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
119 git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git)
120 ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name}
121
122 METADATA="${dep_metadata}" install_dependencies
123}
124
125link_modules(){
126 # Link modules *.py files to temporary salt-root
127 local SALT_ROOT=${1:-$SALT_FILE_DIR}
128 local SALT_ENV=${2:-$DEPSDIR}
129
130 mkdir -p "${SALT_ROOT}/_modules/"
131 # from git, development versions
132 find ${SALT_ENV} -maxdepth 3 -mindepth 3 -path '*_modules*' -iname "*.py" -type f -print0 | while read -d $'\0' file; do
133 ln -fs $(readlink -e ${file}) "$SALT_ROOT"/_modules/$(basename ${file}) ;
134 done
135 salt_run saltutil.sync_all
136}
137
138install_dependencies() {
139 grep -E "^dependencies:" ${METADATA} >/dev/null || return 0
140 (python - | while read dep; do fetch_dependency "$dep"; done) << EOF
141import sys,yaml
142for dep in yaml.load(open('${METADATA}', 'ro'))['dependencies']:
143 print '%s:%s' % (dep["name"], dep["source"])
144EOF
145}
146
147clean() {
148 log_info "Cleaning up ${BUILDDIR}"
149 [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0
150}
151
152salt_run() {
153 [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
Mykyta Karpin4f30fff2018-02-23 15:46:10 +0200154 python $(which salt-call) ${SALT_OPTS} $*
Mykyta Karpindb737652018-02-05 11:11:31 +0200155}
156
157prepare() {
158 [ -d ${BUILDDIR} ] && mkdir -p ${BUILDDIR}
159
160 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
161 setup_mock_bin
162 setup_pillar
163 setup_salt
164 install_dependencies
165}
166
167lint_releasenotes() {
168 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
169 source ${VENV_DIR}/bin/activate
Mykyta Karpin4f30fff2018-02-23 15:46:10 +0200170 python -m pip install reno
Mykyta Karpindb737652018-02-05 11:11:31 +0200171 reno lint ${CURDIR}/../
172}
173
174lint() {
175# lint_releasenotes
176 log_err "TODO: lint_releasenotes"
177}
178
179run() {
180 for pillar in ${PILLARDIR}/*.sls; do
181 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
182 state_name=$(basename ${pillar%.sls})
183 salt_run grains.set 'noservices' False force=True
184
185 echo "Checking state ${FORMULA_NAME}.${state_name} ..."
186 salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
187
188 # Check that all files in 'meta' folder can be rendered using any valid pillar
189 for meta in `find ${FORMULA_META_DIR} -type f`; do
190 meta_name=$(basename ${meta})
191 echo "Checking meta ${meta_name} ..."
192 salt_run --out=quiet --id=${state_name} cp.get_template ${meta} ${SALT_CACHE_DIR}/${meta_name} \
193 || { log_err "Failed to render meta ${meta} using pillar ${FORMULA_NAME}.${state_name}"; exit 1; }
194 cat ${SALT_CACHE_DIR}/${meta_name}
195 done
196 done
197}
198
199real_run() {
200 for pillar in ${PILLARDIR}/*.sls; do
201 state_name=$(basename ${pillar%.sls})
202 salt_run --id=${state_name} state.sls ${FORMULA_NAME} || { log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1; }
203 done
204}
205
206run_model_validate(){
Mykyta Karpin4f30fff2018-02-23 15:46:10 +0200207 if [ -d ${SCHEMARDIR} ]; then
208 # model validator require py modules
209 fetch_dependency "salt:https://github.com/salt-formulas/salt-formula-salt"
210 link_modules
211 # Rendered Example:
212 # python $(which salt-call) --local -c /test1/maas/tests/build/salt --id=maas_cluster modelschema.model_validate maas cluster
213 for role in ${SCHEMARDIR}/*.yaml; do
214 state_name=$(basename "${role%*.yaml}")
215 minion_id="${state_name}"
216 # in case debug-reruns, usefull to make cleanup
217 [ -n "$DEBUG" ] && { salt_run saltutil.clear_cache; salt_run saltutil.refresh_pillar; salt_run saltutil.sync_all; }
218 salt_run -m ${DEPSDIR}/salt-formula-salt --id=${minion_id} modelschema.model_validate ${FORMULA_NAME} ${state_name} || { log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1 ; }
219 done
220 else
221 log_info "${SCHEMARDIR} not found!";
222 fi
223}
224
225dependency_check() {
226 local DEPENDENCY_COMMANDS=$*
227
228 for DEPENDENCY_COMMAND in $DEPENDENCY_COMMANDS; do
229 which $DEPENDENCY_COMMAND > /dev/null || ( log_err "Command \"$DEPENDENCY_COMMAND\" can not be found in default path."; exit 1; )
230 done
Mykyta Karpindb737652018-02-05 11:11:31 +0200231}
232
233_atexit() {
234 RETVAL=$?
235 trap true INT TERM EXIT
236
237 if [ $RETVAL -ne 0 ]; then
238 log_err "Execution failed"
239 else
240 log_info "Execution successful"
241 fi
242 return $RETVAL
243}
244
245## Main
246trap _atexit INT TERM EXIT
247
248case $1 in
249 clean)
250 clean
251 ;;
252 prepare)
253 prepare
254 ;;
255 lint)
256 lint
257 ;;
258 run)
259 run
260 ;;
261 real-run)
262 real_run
263 ;;
264 model-validate)
265 prepare
266 run_model_validate
267 ;;
268 *)
269 prepare
270# lint
271 run
272# run_model_validate
273 ;;
Mykyta Karpin4f30fff2018-02-23 15:46:10 +0200274esac