blob: 9993b8d29a1b710ff585e371edc07e83bbc4c6ee [file] [log] [blame]
Filip Pytloun889c5792016-01-25 21:15:08 +01001#!/usr/bin/env bash
2
Filip Pytloun72cd3972018-01-18 13:40:40 +01003###
4# Script requirments:
5# apt-get install -y python-yaml virtualenv git
Filip Pytloun889c5792016-01-25 21:15:08 +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']")
Filip Pytloun72cd3972018-01-18 13:40:40 +010012FORMULA_META_DIR=${CURDIR}/../${FORMULA_NAME}/meta
Filip Pytloun889c5792016-01-25 21:15:08 +010013
14## Overrideable parameters
15PILLARDIR=${PILLARDIR:-${CURDIR}/pillar}
16BUILDDIR=${BUILDDIR:-${CURDIR}/build}
17VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv}
Filip Pytloun72cd3972018-01-18 13:40:40 +010018MOCK_BIN_DIR=${MOCK_BIN_DIR:-${CURDIR}/mock_bin}
Filip Pytloun889c5792016-01-25 21:15:08 +010019DEPSDIR=${BUILDDIR}/deps
Filip Pytloun72cd3972018-01-18 13:40:40 +010020SCHEMARDIR=${SCHEMARDIR:-"${CURDIR}/../${FORMULA_NAME}/schemas/"}
Filip Pytloun889c5792016-01-25 21:15:08 +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}
Filip Pytloun72cd3972018-01-18 13:40:40 +010026SALT_CACHE_EXTMODS_DIR=${SALT_CACHE_EXTMODS_DIR:-${SALT_CONFIG_DIR}/cache_master_extmods}
Filip Pytloun889c5792016-01-25 21:15:08 +010027
Filip Pytloun72cd3972018-01-18 13:40:40 +010028SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null"
Filip Pytloun889c5792016-01-25 21:15:08 +010029
30if [ "x${SALT_VERSION}" != "x" ]; then
31 PIP_SALT_VERSION="==${SALT_VERSION}"
32fi
33
34## Functions
35log_info() {
Filip Pytloun72cd3972018-01-18 13:40:40 +010036 echo -e "[INFO] $*"
Filip Pytloun889c5792016-01-25 21:15:08 +010037}
38
39log_err() {
Filip Pytloun72cd3972018-01-18 13:40:40 +010040 echo -e "[ERROR] $*" >&2
Filip Pytloun889c5792016-01-25 21:15:08 +010041}
42
43setup_virtualenv() {
44 log_info "Setting up Python virtualenv"
45 virtualenv $VENV_DIR
46 source ${VENV_DIR}/bin/activate
47 pip install salt${PIP_SALT_VERSION}
Filip Pytloun72cd3972018-01-18 13:40:40 +010048 if [[ -f ${CURDIR}/pip_requirements.txt ]]; then
49 pip install -r ${CURDIR}/pip_requirements.txt
50 fi
51}
52
53setup_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
Filip Pytloun889c5792016-01-25 21:15:08 +010060}
61
62setup_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
Filip Pytloun72cd3972018-01-18 13:40:40 +010066 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Filip Pytloun889c5792016-01-25 21:15:08 +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}
Filip Pytloun72cd3972018-01-18 13:40:40 +010076 [ ! -d ${SALT_CACHE_EXTMODS_DIR} ] && mkdir -p ${SALT_CACHE_EXTMODS_DIR}
Filip Pytloun889c5792016-01-25 21:15:08 +010077
78 echo "base:" > ${SALT_FILE_DIR}/top.sls
79 for pillar in ${PILLARDIR}/*.sls; do
Filip Pytloun72cd3972018-01-18 13:40:40 +010080 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Filip Pytloun889c5792016-01-25 21:15:08 +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}
Filip Pytloun72cd3972018-01-18 13:40:40 +010088extension_modules: ${SALT_CACHE_EXTMODS_DIR}
Filip Pytloun889c5792016-01-25 21:15:08 +010089verify_env: False
Filip Pytloun72cd3972018-01-18 13:40:40 +010090minion_id_caching: False
Filip Pytloun889c5792016-01-25 21:15:08 +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() {
Filip Pytloun72cd3972018-01-18 13:40:40 +0100105 # example: fetch_dependency "linux:https://github.com/salt-formulas/salt-formula-linux"
Filip Pytlounc22b2db2016-04-14 14:27:55 +0200106 dep_name="$(echo $1|cut -d : -f 1)"
Filip Pytloun3b9e5cb2016-04-14 20:44:12 +0200107 dep_source="$(echo $1|cut -d : -f 2-)"
Filip Pytlounc22b2db2016-04-14 14:27:55 +0200108 dep_root="${DEPSDIR}/$(basename $dep_source .git)"
Filip Pytloun889c5792016-01-25 21:15:08 +0100109 dep_metadata="${dep_root}/metadata.yml"
110
Filip Pytloun72cd3972018-01-18 13:40:40 +0100111 [ -d $dep_root ] && { log_info "Dependency $dep_name already fetched"; return 0; }
Filip Pytloun889c5792016-01-25 21:15:08 +0100112
Filip Pytlounc22b2db2016-04-14 14:27:55 +0200113 log_info "Fetching dependency $dep_name"
Filip Pytloun889c5792016-01-25 21:15:08 +0100114 [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
Filip Pytlounc22b2db2016-04-14 14:27:55 +0200115 git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git)
Filip Pytloun889c5792016-01-25 21:15:08 +0100116 ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name}
117
118 METADATA="${dep_metadata}" install_dependencies
119}
120
Filip Pytloun72cd3972018-01-18 13:40:40 +0100121link_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
Filip Pytloun889c5792016-01-25 21:15:08 +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 Pytlounc22b2db2016-04-14 14:27:55 +0200139 print '%s:%s' % (dep["name"], dep["source"])
Filip Pytloun889c5792016-01-25 21:15:08 +0100140EOF
141}
142
143clean() {
144 log_info "Cleaning up ${BUILDDIR}"
145 [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0
146}
147
148salt_run() {
Filip Pytloun72cd3972018-01-18 13:40:40 +0100149 [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
Filip Pytloun889c5792016-01-25 21:15:08 +0100150 salt-call ${SALT_OPTS} $*
151}
152
153prepare() {
154 [ -d ${BUILDDIR} ] && mkdir -p ${BUILDDIR}
155
Filip Pytloun72cd3972018-01-18 13:40:40 +0100156 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
157 setup_mock_bin
Filip Pytloun889c5792016-01-25 21:15:08 +0100158 setup_pillar
159 setup_salt
160 install_dependencies
161}
162
Filip Pytloun72cd3972018-01-18 13:40:40 +0100163lint_releasenotes() {
164 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
165 source ${VENV_DIR}/bin/activate
166 pip install reno
167 reno lint ${CURDIR}/../
168}
169
170lint() {
171# lint_releasenotes
172 log_err "TODO: lint_releasenotes"
173}
174
Filip Pytloun889c5792016-01-25 21:15:08 +0100175run() {
176 for pillar in ${PILLARDIR}/*.sls; do
Filip Pytloun72cd3972018-01-18 13:40:40 +0100177 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Filip Pytloun889c5792016-01-25 21:15:08 +0100178 state_name=$(basename ${pillar%.sls})
Filip Pytloun72cd3972018-01-18 13:40:40 +0100179 salt_run grains.set 'noservices' False force=True
180
181 echo "Checking state ${FORMULA_NAME}.${state_name} ..."
Filip Pytloun889c5792016-01-25 21:15:08 +0100182 salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
Filip Pytloun72cd3972018-01-18 13:40:40 +0100183
184 # Check that all files in 'meta' folder can be rendered using any valid pillar
185 for meta in `find ${FORMULA_META_DIR} -type f`; do
186 meta_name=$(basename ${meta})
187 echo "Checking meta ${meta_name} ..."
188 salt_run --out=quiet --id=${state_name} cp.get_template ${meta} ${SALT_CACHE_DIR}/${meta_name} \
189 || { log_err "Failed to render meta ${meta} using pillar ${FORMULA_NAME}.${state_name}"; exit 1; }
190 cat ${SALT_CACHE_DIR}/${meta_name}
191 done
192 done
193}
194
195real_run() {
196 for pillar in ${PILLARDIR}/*.sls; do
197 state_name=$(basename ${pillar%.sls})
198 salt_run --id=${state_name} state.sls ${FORMULA_NAME} || { log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1; }
199 done
200}
201
202run_model_validate(){
203 [[ -d ${SCHEMARDIR} ]] || { log_err "${SCHEMARDIR} not found!"; return 1; }
204 # model validator require py modules
205 fetch_dependency "salt:https://github.com/salt-formulas/salt-formula-salt"
206 link_modules
207 # Rendered Example:
208 # salt-call --local -c /test1/maas/tests/build/salt --id=maas_cluster modelschema.model_validate maas cluster
209 for role in ${SCHEMARDIR}/*.yaml; do
210 state_name=$(basename "${role%*.yaml}")
211 minion_id="${state_name}"
212 # in case debug-reruns, usefull to make cleanup
213 [ -n "$DEBUG" ] && { salt_run saltutil.clear_cache; salt_run saltutil.refresh_pillar; salt_run saltutil.sync_all; }
214 salt_run --id=${minion_id} modelschema.model_validate ${FORMULA_NAME} ${state_name} || { log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1 ; }
Filip Pytloun889c5792016-01-25 21:15:08 +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 ;;
Filip Pytloun72cd3972018-01-18 13:40:40 +0100240 lint)
241 lint
242 ;;
Filip Pytloun889c5792016-01-25 21:15:08 +0100243 run)
244 run
245 ;;
Filip Pytloun72cd3972018-01-18 13:40:40 +0100246 real-run)
247 real_run
248 ;;
249 model-validate)
250 prepare
251 run_model_validate
252 ;;
Filip Pytloun889c5792016-01-25 21:15:08 +0100253 *)
254 prepare
Filip Pytloun72cd3972018-01-18 13:40:40 +0100255# lint
Filip Pytloun889c5792016-01-25 21:15:08 +0100256 run
Filip Pytloun72cd3972018-01-18 13:40:40 +0100257 run_model_validate
Filip Pytloun889c5792016-01-25 21:15:08 +0100258 ;;
259esac