blob: a348912d9069d7fbb31e51dd704626a5c1729a62 [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
azvyagintsev4e404702018-05-30 20:02:03 +030031IGNORE_MODELVALIDATE_MASK=${IGNORE_MODELVALIDATE_MASK:-"novalidate"}
32
Adam Tenglerd98367f2016-01-26 11:56:27 +010033if [ "x${SALT_VERSION}" != "x" ]; then
34 PIP_SALT_VERSION="==${SALT_VERSION}"
35fi
36
37## Functions
38log_info() {
azvyagintsevbee57b92017-12-08 16:46:09 +020039 echo -e "[INFO] $*"
Adam Tenglerd98367f2016-01-26 11:56:27 +010040}
41
42log_err() {
azvyagintsevbee57b92017-12-08 16:46:09 +020043 echo -e "[ERROR] $*" >&2
Adam Tenglerd98367f2016-01-26 11:56:27 +010044}
45
46setup_virtualenv() {
47 log_info "Setting up Python virtualenv"
Ondrej Smolad3c67032018-04-04 12:16:05 +020048 dependency_check virtualenv
Adam Tenglerd98367f2016-01-26 11:56:27 +010049 virtualenv $VENV_DIR
50 source ${VENV_DIR}/bin/activate
Jakub Josef482a8552018-02-08 13:22:18 +010051 python -m pip install salt${PIP_SALT_VERSION}
Ondrej Smolad3c67032018-04-04 12:16:05 +020052 if [[ -f ${CURDIR}/test-requirements.txt ]]; then
53 python -m pip install -r ${CURDIR}/test-requirements.txt
azvyagintsevbee57b92017-12-08 16:46:09 +020054 fi
Adam Tenglerd98367f2016-01-26 11:56:27 +010055}
56
Dennis Dmitriev5e72a352017-07-06 21:07:28 +030057setup_mock_bin() {
58 # If some state requires a binary, a lightweight replacement for
59 # such binary can be put into MOCK_BIN_DIR for test purposes
60 if [ -d "${MOCK_BIN_DIR}" ]; then
61 PATH="${MOCK_BIN_DIR}:$PATH"
62 export PATH
63 fi
64}
65
Adam Tenglerd98367f2016-01-26 11:56:27 +010066setup_pillar() {
67 [ ! -d ${SALT_PILLAR_DIR} ] && mkdir -p ${SALT_PILLAR_DIR}
68 echo "base:" > ${SALT_PILLAR_DIR}/top.sls
69 for pillar in ${PILLARDIR}/*; do
Dennis Dmitriev5e72a352017-07-06 21:07:28 +030070 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Adam Tenglerd98367f2016-01-26 11:56:27 +010071 state_name=$(basename ${pillar%.sls})
72 echo -e " ${state_name}:\n - ${state_name}" >> ${SALT_PILLAR_DIR}/top.sls
73 done
74}
75
76setup_salt() {
77 [ ! -d ${SALT_FILE_DIR} ] && mkdir -p ${SALT_FILE_DIR}
78 [ ! -d ${SALT_CONFIG_DIR} ] && mkdir -p ${SALT_CONFIG_DIR}
79 [ ! -d ${SALT_CACHE_DIR} ] && mkdir -p ${SALT_CACHE_DIR}
azvyagintsevbee57b92017-12-08 16:46:09 +020080 [ ! -d ${SALT_CACHE_EXTMODS_DIR} ] && mkdir -p ${SALT_CACHE_EXTMODS_DIR}
Adam Tenglerd98367f2016-01-26 11:56:27 +010081
82 echo "base:" > ${SALT_FILE_DIR}/top.sls
83 for pillar in ${PILLARDIR}/*.sls; do
Dennis Dmitriev5e72a352017-07-06 21:07:28 +030084 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Adam Tenglerd98367f2016-01-26 11:56:27 +010085 state_name=$(basename ${pillar%.sls})
86 echo -e " ${state_name}:\n - ${FORMULA_NAME}" >> ${SALT_FILE_DIR}/top.sls
87 done
88
89 cat << EOF > ${SALT_CONFIG_DIR}/minion
90file_client: local
91cachedir: ${SALT_CACHE_DIR}
azvyagintsevbee57b92017-12-08 16:46:09 +020092extension_modules: ${SALT_CACHE_EXTMODS_DIR}
Adam Tenglerd98367f2016-01-26 11:56:27 +010093verify_env: False
Dennis Dmitriev5e72a352017-07-06 21:07:28 +030094minion_id_caching: False
Adam Tenglerd98367f2016-01-26 11:56:27 +010095
96file_roots:
97 base:
98 - ${SALT_FILE_DIR}
99 - ${CURDIR}/..
100
101pillar_roots:
102 base:
103 - ${SALT_PILLAR_DIR}
104 - ${PILLARDIR}
105EOF
106}
107
108fetch_dependency() {
azvyagintsevbee57b92017-12-08 16:46:09 +0200109 # example: fetch_dependency "linux:https://github.com/salt-formulas/salt-formula-linux"
Filip Pytloun4ebe36d2016-04-14 11:51:09 +0200110 dep_name="$(echo $1|cut -d : -f 1)"
Filip Pytloun9bf88cc2016-04-14 16:39:28 +0200111 dep_source="$(echo $1|cut -d : -f 2-)"
Filip Pytloun4ebe36d2016-04-14 11:51:09 +0200112 dep_root="${DEPSDIR}/$(basename $dep_source .git)"
Adam Tenglerd98367f2016-01-26 11:56:27 +0100113 dep_metadata="${dep_root}/metadata.yml"
114
Ondrej Smolad3c67032018-04-04 12:16:05 +0200115 dependency_check git
azvyagintsevbee57b92017-12-08 16:46:09 +0200116 [ -d $dep_root ] && { log_info "Dependency $dep_name already fetched"; return 0; }
Adam Tenglerd98367f2016-01-26 11:56:27 +0100117
Filip Pytloun4ebe36d2016-04-14 11:51:09 +0200118 log_info "Fetching dependency $dep_name"
Adam Tenglerd98367f2016-01-26 11:56:27 +0100119 [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
Filip Pytloun445dde32016-04-14 12:46:30 +0200120 git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git)
Adam Tenglerd98367f2016-01-26 11:56:27 +0100121 ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name}
122
123 METADATA="${dep_metadata}" install_dependencies
124}
125
azvyagintsevbee57b92017-12-08 16:46:09 +0200126link_modules(){
127 # Link modules *.py files to temporary salt-root
128 local SALT_ROOT=${1:-$SALT_FILE_DIR}
129 local SALT_ENV=${2:-$DEPSDIR}
130
131 mkdir -p "${SALT_ROOT}/_modules/"
132 # from git, development versions
133 find ${SALT_ENV} -maxdepth 3 -mindepth 3 -path '*_modules*' -iname "*.py" -type f -print0 | while read -d $'\0' file; do
134 ln -fs $(readlink -e ${file}) "$SALT_ROOT"/_modules/$(basename ${file}) ;
135 done
136 salt_run saltutil.sync_all
137}
138
Adam Tenglerd98367f2016-01-26 11:56:27 +0100139install_dependencies() {
140 grep -E "^dependencies:" ${METADATA} >/dev/null || return 0
141 (python - | while read dep; do fetch_dependency "$dep"; done) << EOF
142import sys,yaml
143for dep in yaml.load(open('${METADATA}', 'ro'))['dependencies']:
Filip Pytloun4ebe36d2016-04-14 11:51:09 +0200144 print '%s:%s' % (dep["name"], dep["source"])
Adam Tenglerd98367f2016-01-26 11:56:27 +0100145EOF
146}
147
148clean() {
149 log_info "Cleaning up ${BUILDDIR}"
150 [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0
151}
152
153salt_run() {
Jakub Josefe7fc6ab2017-12-15 16:58:30 +0100154 [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
Jakub Josef482a8552018-02-08 13:22:18 +0100155 python $(which salt-call) ${SALT_OPTS} $*
Adam Tenglerd98367f2016-01-26 11:56:27 +0100156}
157
158prepare() {
azvyagintsev4e404702018-05-30 20:02:03 +0300159 if [[ -f ${BUILDDIR}/.prepare_done ]]; then
160 log_info "${BUILDDIR}/.prepare_done exist, not rebuilding BUILDDIR"
161 return
162 fi
163 [[ -d ${BUILDDIR} ]] && mkdir -p ${BUILDDIR}
Adam Tenglerd98367f2016-01-26 11:56:27 +0100164
azvyagintsevbee57b92017-12-08 16:46:09 +0200165 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
Dennis Dmitriev5e72a352017-07-06 21:07:28 +0300166 setup_mock_bin
Adam Tenglerd98367f2016-01-26 11:56:27 +0100167 setup_pillar
168 setup_salt
169 install_dependencies
Ondrej Smolad3c67032018-04-04 12:16:05 +0200170 link_modules
azvyagintsev4e404702018-05-30 20:02:03 +0300171 touch ${BUILDDIR}/.prepare_done
Adam Tenglerd98367f2016-01-26 11:56:27 +0100172}
173
azvyagintsevbee57b92017-12-08 16:46:09 +0200174lint_releasenotes() {
175 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
Ondrej Smolad3c67032018-04-04 12:16:05 +0200176 source ${VENV_DIR}/bin/activate
azvyagintsevbee57b92017-12-08 16:46:09 +0200177 reno lint ${CURDIR}/../
178}
179
180lint() {
181# lint_releasenotes
182 log_err "TODO: lint_releasenotes"
183}
184
Adam Tenglerd98367f2016-01-26 11:56:27 +0100185run() {
186 for pillar in ${PILLARDIR}/*.sls; do
Dennis Dmitriev5e72a352017-07-06 21:07:28 +0300187 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Adam Tenglerd98367f2016-01-26 11:56:27 +0100188 state_name=$(basename ${pillar%.sls})
Dennis Dmitriev5e72a352017-07-06 21:07:28 +0300189 salt_run grains.set 'noservices' False force=True
190
191 echo "Checking state ${FORMULA_NAME}.${state_name} ..."
Adam Tenglerd98367f2016-01-26 11:56:27 +0100192 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 +0300193
194 # Check that all files in 'meta' folder can be rendered using any valid pillar
195 for meta in `find ${FORMULA_META_DIR} -type f`; do
196 meta_name=$(basename ${meta})
197 echo "Checking meta ${meta_name} ..."
198 salt_run --out=quiet --id=${state_name} cp.get_template ${meta} ${SALT_CACHE_DIR}/${meta_name} \
azvyagintsevbee57b92017-12-08 16:46:09 +0200199 || { log_err "Failed to render meta ${meta} using pillar ${FORMULA_NAME}.${state_name}"; exit 1; }
Dennis Dmitriev5e72a352017-07-06 21:07:28 +0300200 cat ${SALT_CACHE_DIR}/${meta_name}
201 done
202 done
203}
204
205real_run() {
206 for pillar in ${PILLARDIR}/*.sls; do
207 state_name=$(basename ${pillar%.sls})
azvyagintsevbee57b92017-12-08 16:46:09 +0200208 salt_run --id=${state_name} state.sls ${FORMULA_NAME} || { log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1; }
209 done
210}
211
212run_model_validate(){
Ondrej Smolad3c67032018-04-04 12:16:05 +0200213 # Run modelschema.model_validate validation.
214 # TEST iterateble, run for `each formula ROLE against each ROLE_PILLARNAME`
215 # Pillars should be named in conviend ROLE_XXX.sls or ROLE.sls
216 # Example:
217 # client.sls client_auth.sls server.sls server_auth.sls
218 if [ -d ${SCHEMARDIR} ]; then
azvyagintsevbee57b92017-12-08 16:46:09 +0200219 # model validator require py modules
220 fetch_dependency "salt:https://github.com/salt-formulas/salt-formula-salt"
221 link_modules
Ondrej Smolad3c67032018-04-04 12:16:05 +0200222 salt_run saltutil.clear_cache; salt_run saltutil.refresh_pillar; salt_run saltutil.sync_all;
azvyagintsevbee57b92017-12-08 16:46:09 +0200223 for role in ${SCHEMARDIR}/*.yaml; do
Ondrej Smolad3c67032018-04-04 12:16:05 +0200224 role_name=$(basename "${role%*.yaml}")
azvyagintsev4e404702018-05-30 20:02:03 +0300225 for pillar in $(ls pillar/${role_name}*.sls | grep -v ${IGNORE_MODELVALIDATE_MASK} ); do
Ondrej Smolad3c67032018-04-04 12:16:05 +0200226 pillar_name=$(basename "${pillar%*.sls}")
227 local _message="FORMULA:${FORMULA_NAME} ROLE:${role_name} against PILLAR:${pillar_name}"
228 log_info "model_validate ${_message}"
229 # Rendered Example:
230 # python $(which salt-call) --local -c /test1/maas/tests/build/salt --id=maas_cluster modelschema.model_validate maas cluster
231 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 ; }
232 done
Adam Tenglerd98367f2016-01-26 11:56:27 +0100233 done
Ondrej Smolad3c67032018-04-04 12:16:05 +0200234 else
235 log_info "${SCHEMARDIR} not found!";
236 fi
237}
238
239dependency_check() {
240 local DEPENDENCY_COMMANDS=$*
241
242 for DEPENDENCY_COMMAND in $DEPENDENCY_COMMANDS; do
243 which $DEPENDENCY_COMMAND > /dev/null || ( log_err "Command \"$DEPENDENCY_COMMAND\" can not be found in default path."; exit 1; )
244 done
Adam Tenglerd98367f2016-01-26 11:56:27 +0100245}
246
247_atexit() {
248 RETVAL=$?
249 trap true INT TERM EXIT
250
251 if [ $RETVAL -ne 0 ]; then
252 log_err "Execution failed"
253 else
254 log_info "Execution successful"
255 fi
256 return $RETVAL
257}
258
259## Main
260trap _atexit INT TERM EXIT
261
262case $1 in
263 clean)
264 clean
265 ;;
266 prepare)
267 prepare
268 ;;
azvyagintsevbee57b92017-12-08 16:46:09 +0200269 lint)
270 lint
271 ;;
Adam Tenglerd98367f2016-01-26 11:56:27 +0100272 run)
273 run
274 ;;
Dennis Dmitriev5e72a352017-07-06 21:07:28 +0300275 real-run)
276 real_run
277 ;;
azvyagintsevbee57b92017-12-08 16:46:09 +0200278 model-validate)
279 prepare
280 run_model_validate
281 ;;
Adam Tenglerd98367f2016-01-26 11:56:27 +0100282 *)
283 prepare
azvyagintsevbee57b92017-12-08 16:46:09 +0200284# lint
Adam Tenglerd98367f2016-01-26 11:56:27 +0100285 run
azvyagintsevbee57b92017-12-08 16:46:09 +0200286 run_model_validate
Adam Tenglerd98367f2016-01-26 11:56:27 +0100287 ;;
288esac