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