blob: 8b7a6680aae6780f816f8ea9b10a96f771bf8df3 [file] [log] [blame]
jan kaufmanfd7a9ec2016-01-25 23:41:58 +01001#!/usr/bin/env bash
2
azvyagintsevfd555032017-12-18 15:50:25 +02003###
4# Script requirments:
5# apt-get install -y python-yaml virtualenv git
jan kaufmanfd7a9ec2016-01-25 23:41:58 +01006set -e
7[ -n "$DEBUG" ] && set -x
8
9CURDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
10METADATA=${CURDIR}/../metadata.yml
11FORMULA_NAME=$(cat $METADATA | python -c "import sys,yaml; print yaml.load(sys.stdin)['name']")
Dennis Dmitriev4f6d8db2017-07-06 21:07:28 +030012FORMULA_META_DIR=${CURDIR}/../${FORMULA_NAME}/meta
jan kaufmanfd7a9ec2016-01-25 23:41:58 +010013
14## Overrideable parameters
15PILLARDIR=${PILLARDIR:-${CURDIR}/pillar}
16BUILDDIR=${BUILDDIR:-${CURDIR}/build}
17VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv}
Dennis Dmitriev4f6d8db2017-07-06 21:07:28 +030018MOCK_BIN_DIR=${MOCK_BIN_DIR:-${CURDIR}/mock_bin}
jan kaufmanfd7a9ec2016-01-25 23:41:58 +010019DEPSDIR=${BUILDDIR}/deps
azvyagintsevfd555032017-12-18 15:50:25 +020020SCHEMARDIR=${SCHEMARDIR:-"${CURDIR}/../${FORMULA_NAME}/schemas/"}
jan kaufmanfd7a9ec2016-01-25 23:41:58 +010021
22SALT_FILE_DIR=${SALT_FILE_DIR:-${BUILDDIR}/file_root}
23SALT_PILLAR_DIR=${SALT_PILLAR_DIR:-${BUILDDIR}/pillar_root}
24SALT_CONFIG_DIR=${SALT_CONFIG_DIR:-${BUILDDIR}/salt}
25SALT_CACHE_DIR=${SALT_CACHE_DIR:-${SALT_CONFIG_DIR}/cache}
azvyagintsevfd555032017-12-18 15:50:25 +020026SALT_CACHE_EXTMODS_DIR=${SALT_CACHE_EXTMODS_DIR:-${SALT_CONFIG_DIR}/cache_master_extmods}
jan kaufmanfd7a9ec2016-01-25 23:41:58 +010027
Dennis Dmitriev4f6d8db2017-07-06 21:07:28 +030028SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null"
jan kaufmanfd7a9ec2016-01-25 23:41:58 +010029
30if [ "x${SALT_VERSION}" != "x" ]; then
31 PIP_SALT_VERSION="==${SALT_VERSION}"
32fi
33
34## Functions
35log_info() {
azvyagintsevfd555032017-12-18 15:50:25 +020036 echo -e "[INFO] $*"
jan kaufmanfd7a9ec2016-01-25 23:41:58 +010037}
38
39log_err() {
azvyagintsevfd555032017-12-18 15:50:25 +020040 echo -e "[ERROR] $*" >&2
jan kaufmanfd7a9ec2016-01-25 23:41:58 +010041}
42
43setup_virtualenv() {
44 log_info "Setting up Python virtualenv"
45 virtualenv $VENV_DIR
46 source ${VENV_DIR}/bin/activate
Jakub Josef51020392018-02-08 13:22:19 +010047 python -m pip install salt${PIP_SALT_VERSION}
azvyagintsevfd555032017-12-18 15:50:25 +020048 if [[ -f ${CURDIR}/test-requirements.txt ]]; then
Jakub Josef51020392018-02-08 13:22:19 +010049 python -m pip install -r ${CURDIR}/test-requirements.txt
azvyagintsevfd555032017-12-18 15:50:25 +020050 fi
jan kaufmanfd7a9ec2016-01-25 23:41:58 +010051}
52
Dennis Dmitriev4f6d8db2017-07-06 21:07:28 +030053setup_mock_bin() {
54 # If some state requires a binary, a lightweight replacement for
55 # such binary can be put into MOCK_BIN_DIR for test purposes
56 if [ -d "${MOCK_BIN_DIR}" ]; then
57 PATH="${MOCK_BIN_DIR}:$PATH"
58 export PATH
59 fi
60}
61
jan kaufmanfd7a9ec2016-01-25 23:41:58 +010062setup_pillar() {
63 [ ! -d ${SALT_PILLAR_DIR} ] && mkdir -p ${SALT_PILLAR_DIR}
64 echo "base:" > ${SALT_PILLAR_DIR}/top.sls
65 for pillar in ${PILLARDIR}/*; do
Dennis Dmitriev4f6d8db2017-07-06 21:07:28 +030066 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
jan kaufmanfd7a9ec2016-01-25 23:41:58 +010067 state_name=$(basename ${pillar%.sls})
68 echo -e " ${state_name}:\n - ${state_name}" >> ${SALT_PILLAR_DIR}/top.sls
69 done
70}
71
72setup_salt() {
73 [ ! -d ${SALT_FILE_DIR} ] && mkdir -p ${SALT_FILE_DIR}
74 [ ! -d ${SALT_CONFIG_DIR} ] && mkdir -p ${SALT_CONFIG_DIR}
75 [ ! -d ${SALT_CACHE_DIR} ] && mkdir -p ${SALT_CACHE_DIR}
azvyagintsevfd555032017-12-18 15:50:25 +020076 [ ! -d ${SALT_CACHE_EXTMODS_DIR} ] && mkdir -p ${SALT_CACHE_EXTMODS_DIR}
jan kaufmanfd7a9ec2016-01-25 23:41:58 +010077
78 echo "base:" > ${SALT_FILE_DIR}/top.sls
79 for pillar in ${PILLARDIR}/*.sls; do
Dennis Dmitriev4f6d8db2017-07-06 21:07:28 +030080 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
jan kaufmanfd7a9ec2016-01-25 23:41:58 +010081 state_name=$(basename ${pillar%.sls})
82 echo -e " ${state_name}:\n - ${FORMULA_NAME}" >> ${SALT_FILE_DIR}/top.sls
83 done
84
85 cat << EOF > ${SALT_CONFIG_DIR}/minion
86file_client: local
87cachedir: ${SALT_CACHE_DIR}
azvyagintsevfd555032017-12-18 15:50:25 +020088extension_modules: ${SALT_CACHE_EXTMODS_DIR}
jan kaufmanfd7a9ec2016-01-25 23:41:58 +010089verify_env: False
Dennis Dmitriev4f6d8db2017-07-06 21:07:28 +030090minion_id_caching: False
jan kaufmanfd7a9ec2016-01-25 23:41:58 +010091
92file_roots:
93 base:
94 - ${SALT_FILE_DIR}
95 - ${CURDIR}/..
96
97pillar_roots:
98 base:
99 - ${SALT_PILLAR_DIR}
100 - ${PILLARDIR}
101EOF
102}
103
104fetch_dependency() {
azvyagintsevfd555032017-12-18 15:50:25 +0200105 # example: fetch_dependency "linux:https://github.com/salt-formulas/salt-formula-linux"
Filip Pytloun57077e62016-04-14 11:51:09 +0200106 dep_name="$(echo $1|cut -d : -f 1)"
Filip Pytlounf0734882016-04-14 16:39:31 +0200107 dep_source="$(echo $1|cut -d : -f 2-)"
Filip Pytloun57077e62016-04-14 11:51:09 +0200108 dep_root="${DEPSDIR}/$(basename $dep_source .git)"
jan kaufmanfd7a9ec2016-01-25 23:41:58 +0100109 dep_metadata="${dep_root}/metadata.yml"
110
azvyagintsevfd555032017-12-18 15:50:25 +0200111 [ -d $dep_root ] && { log_info "Dependency $dep_name already fetched"; return 0; }
jan kaufmanfd7a9ec2016-01-25 23:41:58 +0100112
Filip Pytloun57077e62016-04-14 11:51:09 +0200113 log_info "Fetching dependency $dep_name"
jan kaufmanfd7a9ec2016-01-25 23:41:58 +0100114 [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
Filip Pytloun13fe1582016-04-14 12:46:30 +0200115 git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git)
jan kaufmanfd7a9ec2016-01-25 23:41:58 +0100116 ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name}
117
118 METADATA="${dep_metadata}" install_dependencies
119}
120
azvyagintsevfd555032017-12-18 15:50:25 +0200121link_modules(){
122 # Link modules *.py files to temporary salt-root
123 local SALT_ROOT=${1:-$SALT_FILE_DIR}
124 local SALT_ENV=${2:-$DEPSDIR}
125
126 mkdir -p "${SALT_ROOT}/_modules/"
127 # from git, development versions
128 find ${SALT_ENV} -maxdepth 3 -mindepth 3 -path '*_modules*' -iname "*.py" -type f -print0 | while read -d $'\0' file; do
129 ln -fs $(readlink -e ${file}) "$SALT_ROOT"/_modules/$(basename ${file}) ;
130 done
131 salt_run saltutil.sync_all
132}
133
jan kaufmanfd7a9ec2016-01-25 23:41:58 +0100134install_dependencies() {
135 grep -E "^dependencies:" ${METADATA} >/dev/null || return 0
136 (python - | while read dep; do fetch_dependency "$dep"; done) << EOF
137import sys,yaml
138for dep in yaml.load(open('${METADATA}', 'ro'))['dependencies']:
Filip Pytloun57077e62016-04-14 11:51:09 +0200139 print '%s:%s' % (dep["name"], dep["source"])
jan kaufmanfd7a9ec2016-01-25 23:41:58 +0100140EOF
141}
142
143clean() {
144 log_info "Cleaning up ${BUILDDIR}"
145 [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0
146}
147
148salt_run() {
azvyagintsevfd555032017-12-18 15:50:25 +0200149 [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
Jakub Josef51020392018-02-08 13:22:19 +0100150 python $(which salt-call) ${SALT_OPTS} $*
jan kaufmanfd7a9ec2016-01-25 23:41:58 +0100151}
152
153prepare() {
154 [ -d ${BUILDDIR} ] && mkdir -p ${BUILDDIR}
155
azvyagintsevfd555032017-12-18 15:50:25 +0200156 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
Dennis Dmitriev4f6d8db2017-07-06 21:07:28 +0300157 setup_mock_bin
jan kaufmanfd7a9ec2016-01-25 23:41:58 +0100158 setup_pillar
159 setup_salt
160 install_dependencies
161}
162
azvyagintsevfd555032017-12-18 15:50:25 +0200163lint_releasenotes() {
164 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
165 source ${VENV_DIR}/bin/activate
166 reno lint ${CURDIR}/../
167}
168
169lint() {
170# lint_releasenotes
171 log_err "TODO: lint_releasenotes"
172}
173
jan kaufmanfd7a9ec2016-01-25 23:41:58 +0100174run() {
175 for pillar in ${PILLARDIR}/*.sls; do
Dennis Dmitriev4f6d8db2017-07-06 21:07:28 +0300176 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
jan kaufmanfd7a9ec2016-01-25 23:41:58 +0100177 state_name=$(basename ${pillar%.sls})
Dennis Dmitriev4f6d8db2017-07-06 21:07:28 +0300178 salt_run grains.set 'noservices' False force=True
179
180 echo "Checking state ${FORMULA_NAME}.${state_name} ..."
jan kaufmanfd7a9ec2016-01-25 23:41:58 +0100181 salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
Dennis Dmitriev4f6d8db2017-07-06 21:07:28 +0300182
183 # Check that all files in 'meta' folder can be rendered using any valid pillar
184 for meta in `find ${FORMULA_META_DIR} -type f`; do
185 meta_name=$(basename ${meta})
186 echo "Checking meta ${meta_name} ..."
187 salt_run --out=quiet --id=${state_name} cp.get_template ${meta} ${SALT_CACHE_DIR}/${meta_name} \
azvyagintsevfd555032017-12-18 15:50:25 +0200188 || { log_err "Failed to render meta ${meta} using pillar ${FORMULA_NAME}.${state_name}"; exit 1; }
Dennis Dmitriev4f6d8db2017-07-06 21:07:28 +0300189 cat ${SALT_CACHE_DIR}/${meta_name}
190 done
191 done
192}
193
194real_run() {
195 for pillar in ${PILLARDIR}/*.sls; do
196 state_name=$(basename ${pillar%.sls})
azvyagintsevfd555032017-12-18 15:50:25 +0200197 salt_run --id=${state_name} state.sls ${FORMULA_NAME} || { log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1; }
198 done
199}
200
201run_model_validate(){
202 [[ -d ${SCHEMARDIR} ]] || { log_err "${SCHEMARDIR} not found!"; return 1; }
203 # model validator require py modules
204 fetch_dependency "salt:https://github.com/salt-formulas/salt-formula-salt"
205 link_modules
206 # Sync, afer dep's update
207 salt_run saltutil.clear_cache; salt_run saltutil.refresh_pillar; salt_run saltutil.sync_all
208 # Rendered Example:
209 # salt-call --local -c /test1/maas/tests/build/salt --id=maas_cluster modelschema.model_validate maas cluster
210 for role in ${SCHEMARDIR}/*.yaml; do
211 state_name=$(basename "${role%*.yaml}")
212 minion_id="${state_name}"
213 log_info "Processing model_validate for:${FORMULA_NAME} ${state_name} ${minion_id}"
214 salt_run --id=${minion_id} modelschema.model_validate ${FORMULA_NAME} ${state_name} || { log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1 ; }
jan kaufmanfd7a9ec2016-01-25 23:41:58 +0100215 done
216}
217
218_atexit() {
219 RETVAL=$?
220 trap true INT TERM EXIT
221
222 if [ $RETVAL -ne 0 ]; then
223 log_err "Execution failed"
224 else
225 log_info "Execution successful"
226 fi
227 return $RETVAL
228}
229
230## Main
231trap _atexit INT TERM EXIT
232
233case $1 in
234 clean)
235 clean
236 ;;
237 prepare)
238 prepare
239 ;;
azvyagintsevfd555032017-12-18 15:50:25 +0200240 lint)
241 lint
242 ;;
jan kaufmanfd7a9ec2016-01-25 23:41:58 +0100243 run)
244 run
245 ;;
Dennis Dmitriev4f6d8db2017-07-06 21:07:28 +0300246 real-run)
247 real_run
248 ;;
azvyagintsevfd555032017-12-18 15:50:25 +0200249 model-validate)
250 prepare
251 run_model_validate
252 ;;
jan kaufmanfd7a9ec2016-01-25 23:41:58 +0100253 *)
254 prepare
azvyagintsevfd555032017-12-18 15:50:25 +0200255# lint
jan kaufmanfd7a9ec2016-01-25 23:41:58 +0100256 run
azvyagintsevfd555032017-12-18 15:50:25 +0200257 run_model_validate
jan kaufmanfd7a9ec2016-01-25 23:41:58 +0100258 ;;
259esac