blob: 70931615797ee16ffb61a71cf45848b5a1f5cc9b [file] [log] [blame]
Adam Tenglerd98367f2016-01-26 11:56:27 +01001#!/usr/bin/env bash
2
azvyagintsevbee57b92017-12-08 16:46:09 +02003###
4# Script requirments:
Ondrej Smolad3c67032018-04-04 12:16:05 +02005#apt-get install -y python-yaml virtualenv git
6
Adam Tenglerd98367f2016-01-26 11:56:27 +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']")
Dennis Dmitriev5e72a352017-07-06 21:07:28 +030013FORMULA_META_DIR=${CURDIR}/../${FORMULA_NAME}/meta
Adam Tenglerd98367f2016-01-26 11:56:27 +010014
15## Overrideable parameters
16PILLARDIR=${PILLARDIR:-${CURDIR}/pillar}
17BUILDDIR=${BUILDDIR:-${CURDIR}/build}
18VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv}
Dennis Dmitriev5e72a352017-07-06 21:07:28 +030019MOCK_BIN_DIR=${MOCK_BIN_DIR:-${CURDIR}/mock_bin}
Adam Tenglerd98367f2016-01-26 11:56:27 +010020DEPSDIR=${BUILDDIR}/deps
azvyagintsevbee57b92017-12-08 16:46:09 +020021SCHEMARDIR=${SCHEMARDIR:-"${CURDIR}/../${FORMULA_NAME}/schemas/"}
Adam Tenglerd98367f2016-01-26 11:56:27 +010022
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}
azvyagintsevbee57b92017-12-08 16:46:09 +020027SALT_CACHE_EXTMODS_DIR=${SALT_CACHE_EXTMODS_DIR:-${SALT_CONFIG_DIR}/cache_master_extmods}
Adam Tenglerd98367f2016-01-26 11:56:27 +010028
Dennis Dmitriev5e72a352017-07-06 21:07:28 +030029SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null"
Adam Tenglerd98367f2016-01-26 11:56:27 +010030
31if [ "x${SALT_VERSION}" != "x" ]; then
32 PIP_SALT_VERSION="==${SALT_VERSION}"
33fi
34
35## Functions
36log_info() {
azvyagintsevbee57b92017-12-08 16:46:09 +020037 echo -e "[INFO] $*"
Adam Tenglerd98367f2016-01-26 11:56:27 +010038}
39
40log_err() {
azvyagintsevbee57b92017-12-08 16:46:09 +020041 echo -e "[ERROR] $*" >&2
Adam Tenglerd98367f2016-01-26 11:56:27 +010042}
43
44setup_virtualenv() {
45 log_info "Setting up Python virtualenv"
Ondrej Smolad3c67032018-04-04 12:16:05 +020046 dependency_check virtualenv
Adam Tenglerd98367f2016-01-26 11:56:27 +010047 virtualenv $VENV_DIR
48 source ${VENV_DIR}/bin/activate
Jakub Josef482a8552018-02-08 13:22:18 +010049 python -m pip install salt${PIP_SALT_VERSION}
Ondrej Smolad3c67032018-04-04 12:16:05 +020050 if [[ -f ${CURDIR}/test-requirements.txt ]]; then
51 python -m pip install -r ${CURDIR}/test-requirements.txt
azvyagintsevbee57b92017-12-08 16:46:09 +020052 fi
Adam Tenglerd98367f2016-01-26 11:56:27 +010053}
54
Dennis Dmitriev5e72a352017-07-06 21:07:28 +030055setup_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
Adam Tenglerd98367f2016-01-26 11:56:27 +010064setup_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
Dennis Dmitriev5e72a352017-07-06 21:07:28 +030068 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Adam Tenglerd98367f2016-01-26 11:56:27 +010069 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}
azvyagintsevbee57b92017-12-08 16:46:09 +020078 [ ! -d ${SALT_CACHE_EXTMODS_DIR} ] && mkdir -p ${SALT_CACHE_EXTMODS_DIR}
Adam Tenglerd98367f2016-01-26 11:56:27 +010079
80 echo "base:" > ${SALT_FILE_DIR}/top.sls
81 for pillar in ${PILLARDIR}/*.sls; do
Dennis Dmitriev5e72a352017-07-06 21:07:28 +030082 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Adam Tenglerd98367f2016-01-26 11:56:27 +010083 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}
azvyagintsevbee57b92017-12-08 16:46:09 +020090extension_modules: ${SALT_CACHE_EXTMODS_DIR}
Adam Tenglerd98367f2016-01-26 11:56:27 +010091verify_env: False
Dennis Dmitriev5e72a352017-07-06 21:07:28 +030092minion_id_caching: False
Adam Tenglerd98367f2016-01-26 11:56:27 +010093
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() {
azvyagintsevbee57b92017-12-08 16:46:09 +0200107 # example: fetch_dependency "linux:https://github.com/salt-formulas/salt-formula-linux"
Filip Pytloun4ebe36d2016-04-14 11:51:09 +0200108 dep_name="$(echo $1|cut -d : -f 1)"
Filip Pytloun9bf88cc2016-04-14 16:39:28 +0200109 dep_source="$(echo $1|cut -d : -f 2-)"
Filip Pytloun4ebe36d2016-04-14 11:51:09 +0200110 dep_root="${DEPSDIR}/$(basename $dep_source .git)"
Adam Tenglerd98367f2016-01-26 11:56:27 +0100111 dep_metadata="${dep_root}/metadata.yml"
112
Ondrej Smolad3c67032018-04-04 12:16:05 +0200113 dependency_check git
azvyagintsevbee57b92017-12-08 16:46:09 +0200114 [ -d $dep_root ] && { log_info "Dependency $dep_name already fetched"; return 0; }
Adam Tenglerd98367f2016-01-26 11:56:27 +0100115
Filip Pytloun4ebe36d2016-04-14 11:51:09 +0200116 log_info "Fetching dependency $dep_name"
Adam Tenglerd98367f2016-01-26 11:56:27 +0100117 [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
Filip Pytloun445dde32016-04-14 12:46:30 +0200118 git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git)
Adam Tenglerd98367f2016-01-26 11:56:27 +0100119 ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name}
120
121 METADATA="${dep_metadata}" install_dependencies
122}
123
azvyagintsevbee57b92017-12-08 16:46:09 +0200124link_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
Adam Tenglerd98367f2016-01-26 11:56:27 +0100137install_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']:
Filip Pytloun4ebe36d2016-04-14 11:51:09 +0200142 print '%s:%s' % (dep["name"], dep["source"])
Adam Tenglerd98367f2016-01-26 11:56:27 +0100143EOF
144}
145
146clean() {
147 log_info "Cleaning up ${BUILDDIR}"
148 [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0
149}
150
151salt_run() {
Jakub Josefe7fc6ab2017-12-15 16:58:30 +0100152 [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
Jakub Josef482a8552018-02-08 13:22:18 +0100153 python $(which salt-call) ${SALT_OPTS} $*
Adam Tenglerd98367f2016-01-26 11:56:27 +0100154}
155
156prepare() {
157 [ -d ${BUILDDIR} ] && mkdir -p ${BUILDDIR}
158
azvyagintsevbee57b92017-12-08 16:46:09 +0200159 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
Dennis Dmitriev5e72a352017-07-06 21:07:28 +0300160 setup_mock_bin
Adam Tenglerd98367f2016-01-26 11:56:27 +0100161 setup_pillar
162 setup_salt
163 install_dependencies
Ondrej Smolad3c67032018-04-04 12:16:05 +0200164 link_modules
Adam Tenglerd98367f2016-01-26 11:56:27 +0100165}
166
azvyagintsevbee57b92017-12-08 16:46:09 +0200167lint_releasenotes() {
168 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
Ondrej Smolad3c67032018-04-04 12:16:05 +0200169 source ${VENV_DIR}/bin/activate
azvyagintsevbee57b92017-12-08 16:46:09 +0200170 reno lint ${CURDIR}/../
171}
172
173lint() {
174# lint_releasenotes
175 log_err "TODO: lint_releasenotes"
176}
177
Adam Tenglerd98367f2016-01-26 11:56:27 +0100178run() {
179 for pillar in ${PILLARDIR}/*.sls; do
Dennis Dmitriev5e72a352017-07-06 21:07:28 +0300180 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Adam Tenglerd98367f2016-01-26 11:56:27 +0100181 state_name=$(basename ${pillar%.sls})
Dennis Dmitriev5e72a352017-07-06 21:07:28 +0300182 salt_run grains.set 'noservices' False force=True
183
184 echo "Checking state ${FORMULA_NAME}.${state_name} ..."
Adam Tenglerd98367f2016-01-26 11:56:27 +0100185 salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
Dennis Dmitriev5e72a352017-07-06 21:07:28 +0300186
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} \
azvyagintsevbee57b92017-12-08 16:46:09 +0200192 || { log_err "Failed to render meta ${meta} using pillar ${FORMULA_NAME}.${state_name}"; exit 1; }
Dennis Dmitriev5e72a352017-07-06 21:07:28 +0300193 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})
azvyagintsevbee57b92017-12-08 16:46:09 +0200201 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(){
Ondrej Smolad3c67032018-04-04 12:16:05 +0200206 # Run modelschema.model_validate validation.
207 # TEST iterateble, run for `each formula ROLE against each ROLE_PILLARNAME`
208 # Pillars should be named in conviend ROLE_XXX.sls or ROLE.sls
209 # Example:
210 # client.sls client_auth.sls server.sls server_auth.sls
211 if [ -d ${SCHEMARDIR} ]; then
azvyagintsevbee57b92017-12-08 16:46:09 +0200212 # model validator require py modules
213 fetch_dependency "salt:https://github.com/salt-formulas/salt-formula-salt"
214 link_modules
Ondrej Smolad3c67032018-04-04 12:16:05 +0200215 salt_run saltutil.clear_cache; salt_run saltutil.refresh_pillar; salt_run saltutil.sync_all;
azvyagintsevbee57b92017-12-08 16:46:09 +0200216 for role in ${SCHEMARDIR}/*.yaml; do
Ondrej Smolad3c67032018-04-04 12:16:05 +0200217 role_name=$(basename "${role%*.yaml}")
218 for pillar in pillar/${role_name}*.sls; do
219 pillar_name=$(basename "${pillar%*.sls}")
220 local _message="FORMULA:${FORMULA_NAME} ROLE:${role_name} against PILLAR:${pillar_name}"
221 log_info "model_validate ${_message}"
222 # Rendered Example:
223 # python $(which salt-call) --local -c /test1/maas/tests/build/salt --id=maas_cluster modelschema.model_validate maas cluster
224 salt_run -m ${DEPSDIR}/salt-formula-salt --id=${pillar_name} modelschema.model_validate ${FORMULA_NAME} ${role_name} || { log_err "Execution of model_validate ${_message} failed"; exit 1 ; }
225 done
Adam Tenglerd98367f2016-01-26 11:56:27 +0100226 done
Ondrej Smolad3c67032018-04-04 12:16:05 +0200227 else
228 log_info "${SCHEMARDIR} not found!";
229 fi
230}
231
232dependency_check() {
233 local DEPENDENCY_COMMANDS=$*
234
235 for DEPENDENCY_COMMAND in $DEPENDENCY_COMMANDS; do
236 which $DEPENDENCY_COMMAND > /dev/null || ( log_err "Command \"$DEPENDENCY_COMMAND\" can not be found in default path."; exit 1; )
237 done
Adam Tenglerd98367f2016-01-26 11:56:27 +0100238}
239
240_atexit() {
241 RETVAL=$?
242 trap true INT TERM EXIT
243
244 if [ $RETVAL -ne 0 ]; then
245 log_err "Execution failed"
246 else
247 log_info "Execution successful"
248 fi
249 return $RETVAL
250}
251
252## Main
253trap _atexit INT TERM EXIT
254
255case $1 in
256 clean)
257 clean
258 ;;
259 prepare)
260 prepare
261 ;;
azvyagintsevbee57b92017-12-08 16:46:09 +0200262 lint)
263 lint
264 ;;
Adam Tenglerd98367f2016-01-26 11:56:27 +0100265 run)
266 run
267 ;;
Dennis Dmitriev5e72a352017-07-06 21:07:28 +0300268 real-run)
269 real_run
270 ;;
azvyagintsevbee57b92017-12-08 16:46:09 +0200271 model-validate)
272 prepare
273 run_model_validate
274 ;;
Adam Tenglerd98367f2016-01-26 11:56:27 +0100275 *)
276 prepare
azvyagintsevbee57b92017-12-08 16:46:09 +0200277# lint
Adam Tenglerd98367f2016-01-26 11:56:27 +0100278 run
azvyagintsevbee57b92017-12-08 16:46:09 +0200279 run_model_validate
Adam Tenglerd98367f2016-01-26 11:56:27 +0100280 ;;
281esac