blob: ef199ed0570ed79e97d707d26e45e6c656eb563a [file] [log] [blame]
Petr Ruzickaba0a49d2018-01-31 22:16:20 +01001#!/usr/bin/env bash
2
3###
4# Script requirments:
Petr Ruzicka42ac5592018-02-04 08:58:24 +01005#apt-get install -y python-yaml virtualenv git
6which virtualenv &> /dev/null || apt-get install -y virtualenv
Petr Ruzicka9a75c662018-02-02 15:52:50 +01007
Petr Ruzickaba0a49d2018-01-31 22:16:20 +01008set -e
9[ -n "$DEBUG" ] && set -x
10
11CURDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
12METADATA=${CURDIR}/../metadata.yml
13FORMULA_NAME=$(cat $METADATA | python -c "import sys,yaml; print yaml.load(sys.stdin)['name']")
14FORMULA_META_DIR=${CURDIR}/../${FORMULA_NAME}/meta
15
16## Overrideable parameters
17PILLARDIR=${PILLARDIR:-${CURDIR}/pillar}
18BUILDDIR=${BUILDDIR:-${CURDIR}/build}
19VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv}
20MOCK_BIN_DIR=${MOCK_BIN_DIR:-${CURDIR}/mock_bin}
21DEPSDIR=${BUILDDIR}/deps
22SCHEMARDIR=${SCHEMARDIR:-"${CURDIR}/../${FORMULA_NAME}/schemas/"}
23
24SALT_FILE_DIR=${SALT_FILE_DIR:-${BUILDDIR}/file_root}
25SALT_PILLAR_DIR=${SALT_PILLAR_DIR:-${BUILDDIR}/pillar_root}
26SALT_CONFIG_DIR=${SALT_CONFIG_DIR:-${BUILDDIR}/salt}
27SALT_CACHE_DIR=${SALT_CACHE_DIR:-${SALT_CONFIG_DIR}/cache}
28SALT_CACHE_EXTMODS_DIR=${SALT_CACHE_EXTMODS_DIR:-${SALT_CONFIG_DIR}/cache_master_extmods}
29
30SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null"
31
32if [ "x${SALT_VERSION}" != "x" ]; then
33 PIP_SALT_VERSION="==${SALT_VERSION}"
34fi
35
36## Functions
37log_info() {
38 echo -e "[INFO] $*"
39}
40
41log_err() {
42 echo -e "[ERROR] $*" >&2
43}
44
45setup_virtualenv() {
46 log_info "Setting up Python virtualenv"
47 virtualenv $VENV_DIR
48 source ${VENV_DIR}/bin/activate
49 pip install salt${PIP_SALT_VERSION}
50 pip install jsonschema
51 if [[ -f ${CURDIR}/pip_requirements.txt ]]; then
52 pip install -r ${CURDIR}/pip_requirements.txt
53 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
114 [ -d $dep_root ] && { log_info "Dependency $dep_name already fetched"; return 0; }
115
116 log_info "Fetching dependency $dep_name"
117 [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
118 git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git)
119 ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name}
120
121 METADATA="${dep_metadata}" install_dependencies
122}
123
124link_modules(){
125 # Link modules *.py files to temporary salt-root
126 local SALT_ROOT=${1:-$SALT_FILE_DIR}
127 local SALT_ENV=${2:-$DEPSDIR}
128
129 mkdir -p "${SALT_ROOT}/_modules/"
130 # from git, development versions
131 find ${SALT_ENV} -maxdepth 3 -mindepth 3 -path '*_modules*' -iname "*.py" -type f -print0 | while read -d $'\0' file; do
132 ln -fs $(readlink -e ${file}) "$SALT_ROOT"/_modules/$(basename ${file}) ;
133 done
134 salt_run saltutil.sync_all
135}
136
137install_dependencies() {
138 grep -E "^dependencies:" ${METADATA} >/dev/null || return 0
139 (python - | while read dep; do fetch_dependency "$dep"; done) << EOF
140import sys,yaml
141for dep in yaml.load(open('${METADATA}', 'ro'))['dependencies']:
142 print '%s:%s' % (dep["name"], dep["source"])
143EOF
144}
145
146clean() {
147 log_info "Cleaning up ${BUILDDIR}"
148 [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0
149}
150
151salt_run() {
152 [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
153 salt-call ${SALT_OPTS} $*
154}
155
156prepare() {
157 [ -d ${BUILDDIR} ] && mkdir -p ${BUILDDIR}
158
159 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
160 setup_mock_bin
161 setup_pillar
162 setup_salt
163 install_dependencies
164}
165
166lint_releasenotes() {
167 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
168 source ${VENV_DIR}/bin/activate
169 pip install reno
170 reno lint ${CURDIR}/../
171}
172
173lint() {
174# lint_releasenotes
175 log_err "TODO: lint_releasenotes"
176}
177
178run() {
179 for pillar in ${PILLARDIR}/*.sls; do
180 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
181 state_name=$(basename ${pillar%.sls})
182 salt_run grains.set 'noservices' False force=True
183
184 echo "Checking state ${FORMULA_NAME}.${state_name} ..."
185 salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
186
187 # Check that all files in 'meta' folder can be rendered using any valid pillar
188 for meta in `find ${FORMULA_META_DIR} -type f`; do
189 meta_name=$(basename ${meta})
190 echo "Checking meta ${meta_name} ..."
191 salt_run --out=quiet --id=${state_name} cp.get_template ${meta} ${SALT_CACHE_DIR}/${meta_name} \
192 || { log_err "Failed to render meta ${meta} using pillar ${FORMULA_NAME}.${state_name}"; exit 1; }
193 cat ${SALT_CACHE_DIR}/${meta_name}
194 done
195 done
196}
197
198real_run() {
199 for pillar in ${PILLARDIR}/*.sls; do
200 state_name=$(basename ${pillar%.sls})
201 salt_run --id=${state_name} state.sls ${FORMULA_NAME} || { log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1; }
202 done
203}
204
205run_model_validate(){
206 [[ -d ${SCHEMARDIR} ]] || { log_err "${SCHEMARDIR} not found!"; return 1; }
207 # model validator require py modules
208 fetch_dependency "salt:https://github.com/salt-formulas/salt-formula-salt"
209 link_modules
210 # Rendered Example:
211 # salt-call --local -c /test1/maas/tests/build/salt --id=maas_cluster modelschema.model_validate maas cluster
212 for role in ${SCHEMARDIR}/*.yaml; do
213 state_name=$(basename "${role%*.yaml}")
214 minion_id="${state_name}"
215 # in case debug-reruns, usefull to make cleanup
216 [ -n "$DEBUG" ] && { salt_run saltutil.clear_cache; salt_run saltutil.refresh_pillar; salt_run saltutil.sync_all; }
217 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 ; }
218 done
219}
220
221_atexit() {
222 RETVAL=$?
223 trap true INT TERM EXIT
224
225 if [ $RETVAL -ne 0 ]; then
226 log_err "Execution failed"
227 else
228 log_info "Execution successful"
229 fi
230 return $RETVAL
231}
232
233## Main
234trap _atexit INT TERM EXIT
235
236case $1 in
237 clean)
238 clean
239 ;;
240 prepare)
241 prepare
242 ;;
243 lint)
244 lint
245 ;;
246 run)
247 run
248 ;;
249 real-run)
250 real_run
251 ;;
252 model-validate)
253 prepare
254 run_model_validate
255 ;;
256 *)
257 prepare
258# lint
259 run
260 run_model_validate
261 ;;
262esac