blob: e4034f8212b3b6c3894b2e7f84ca7c2b039b5971 [file] [log] [blame]
marcodc62db92016-01-25 22:43:45 +01001#!/usr/bin/env bash
2
Mykyta Karpin72d0b992018-02-16 12:01:11 +02003###
4# Script requirments:
5#apt-get install -y python-yaml virtualenv git
6
marcodc62db92016-01-25 22:43:45 +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']")
Mykyta Karpin72d0b992018-02-16 12:01:11 +020013FORMULA_META_DIR=${CURDIR}/../${FORMULA_NAME}/meta
marcodc62db92016-01-25 22:43:45 +010014
15## Overrideable parameters
16PILLARDIR=${PILLARDIR:-${CURDIR}/pillar}
17BUILDDIR=${BUILDDIR:-${CURDIR}/build}
18VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv}
Mykyta Karpin72d0b992018-02-16 12:01:11 +020019MOCK_BIN_DIR=${MOCK_BIN_DIR:-${CURDIR}/mock_bin}
marcodc62db92016-01-25 22:43:45 +010020DEPSDIR=${BUILDDIR}/deps
Mykyta Karpin72d0b992018-02-16 12:01:11 +020021SCHEMARDIR=${SCHEMARDIR:-"${CURDIR}/../${FORMULA_NAME}/schemas/"}
marcodc62db92016-01-25 22:43:45 +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}
Mykyta Karpin72d0b992018-02-16 12:01:11 +020027SALT_CACHE_EXTMODS_DIR=${SALT_CACHE_EXTMODS_DIR:-${SALT_CONFIG_DIR}/cache_master_extmods}
marcodc62db92016-01-25 22:43:45 +010028
Simak, Jan91060192016-06-23 13:49:53 +020029SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null"
marcodc62db92016-01-25 22:43:45 +010030
31if [ "x${SALT_VERSION}" != "x" ]; then
32 PIP_SALT_VERSION="==${SALT_VERSION}"
33fi
34
35## Functions
36log_info() {
Mykyta Karpin72d0b992018-02-16 12:01:11 +020037 echo -e "[INFO] $*"
marcodc62db92016-01-25 22:43:45 +010038}
39
40log_err() {
Mykyta Karpin72d0b992018-02-16 12:01:11 +020041 echo -e "[ERROR] $*" >&2
marcodc62db92016-01-25 22:43:45 +010042}
43
44setup_virtualenv() {
45 log_info "Setting up Python virtualenv"
Mykyta Karpin72d0b992018-02-16 12:01:11 +020046 dependency_check virtualenv
marcodc62db92016-01-25 22:43:45 +010047 virtualenv $VENV_DIR
48 source ${VENV_DIR}/bin/activate
Jakub Josef4a7754d2018-02-08 13:22:14 +010049 python -m pip install salt${PIP_SALT_VERSION}
Mykyta Karpin72d0b992018-02-16 12:01:11 +020050 python -m pip install jsonschema
51 if [[ -f ${CURDIR}/pip_requirements.txt ]]; then
52 python -m pip install -r ${CURDIR}/pip_requirements.txt
53 fi
54}
55
56setup_mock_bin() {
57 # If some state requires a binary, a lightweight replacement for
58 # such binary can be put into MOCK_BIN_DIR for test purposes
59 if [ -d "${MOCK_BIN_DIR}" ]; then
60 PATH="${MOCK_BIN_DIR}:$PATH"
61 export PATH
62 fi
marcodc62db92016-01-25 22:43:45 +010063}
64
65setup_pillar() {
66 [ ! -d ${SALT_PILLAR_DIR} ] && mkdir -p ${SALT_PILLAR_DIR}
67 echo "base:" > ${SALT_PILLAR_DIR}/top.sls
68 for pillar in ${PILLARDIR}/*; do
Mykyta Karpin72d0b992018-02-16 12:01:11 +020069 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
marcodc62db92016-01-25 22:43:45 +010070 state_name=$(basename ${pillar%.sls})
71 echo -e " ${state_name}:\n - ${state_name}" >> ${SALT_PILLAR_DIR}/top.sls
72 done
73}
74
75setup_salt() {
76 [ ! -d ${SALT_FILE_DIR} ] && mkdir -p ${SALT_FILE_DIR}
77 [ ! -d ${SALT_CONFIG_DIR} ] && mkdir -p ${SALT_CONFIG_DIR}
78 [ ! -d ${SALT_CACHE_DIR} ] && mkdir -p ${SALT_CACHE_DIR}
Mykyta Karpin72d0b992018-02-16 12:01:11 +020079 [ ! -d ${SALT_CACHE_EXTMODS_DIR} ] && mkdir -p ${SALT_CACHE_EXTMODS_DIR}
marcodc62db92016-01-25 22:43:45 +010080
81 echo "base:" > ${SALT_FILE_DIR}/top.sls
82 for pillar in ${PILLARDIR}/*.sls; do
Mykyta Karpin72d0b992018-02-16 12:01:11 +020083 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
marcodc62db92016-01-25 22:43:45 +010084 state_name=$(basename ${pillar%.sls})
85 echo -e " ${state_name}:\n - ${FORMULA_NAME}" >> ${SALT_FILE_DIR}/top.sls
86 done
87
88 cat << EOF > ${SALT_CONFIG_DIR}/minion
89file_client: local
90cachedir: ${SALT_CACHE_DIR}
Mykyta Karpin72d0b992018-02-16 12:01:11 +020091extension_modules: ${SALT_CACHE_EXTMODS_DIR}
marcodc62db92016-01-25 22:43:45 +010092verify_env: False
Ondřej Novýa16c79e2016-04-23 17:10:06 +020093minion_id_caching: False
marcodc62db92016-01-25 22:43:45 +010094
95file_roots:
96 base:
97 - ${SALT_FILE_DIR}
98 - ${CURDIR}/..
99
100pillar_roots:
101 base:
102 - ${SALT_PILLAR_DIR}
103 - ${PILLARDIR}
104EOF
105}
106
107fetch_dependency() {
Mykyta Karpin72d0b992018-02-16 12:01:11 +0200108 # example: fetch_dependency "linux:https://github.com/salt-formulas/salt-formula-linux"
Filip Pytloun1eedfdf2016-04-14 11:48:37 +0200109 dep_name="$(echo $1|cut -d : -f 1)"
110 dep_source="$(echo $1|cut -d : -f 2-)"
111 dep_root="${DEPSDIR}/$(basename $dep_source .git)"
marcodc62db92016-01-25 22:43:45 +0100112 dep_metadata="${dep_root}/metadata.yml"
113
Mykyta Karpin72d0b992018-02-16 12:01:11 +0200114 dependency_check git
115 [ -d $dep_root ] && { log_info "Dependency $dep_name already fetched"; return 0; }
marcodc62db92016-01-25 22:43:45 +0100116
Filip Pytloun1eedfdf2016-04-14 11:48:37 +0200117 log_info "Fetching dependency $dep_name"
marcodc62db92016-01-25 22:43:45 +0100118 [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
Filip Pytloun1eedfdf2016-04-14 11:48:37 +0200119 git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git)
marcodc62db92016-01-25 22:43:45 +0100120 ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name}
121
122 METADATA="${dep_metadata}" install_dependencies
123}
124
Mykyta Karpin72d0b992018-02-16 12:01:11 +0200125link_modules(){
126 # Link modules *.py files to temporary salt-root
127 local SALT_ROOT=${1:-$SALT_FILE_DIR}
128 local SALT_ENV=${2:-$DEPSDIR}
129
130 mkdir -p "${SALT_ROOT}/_modules/"
131 # from git, development versions
132 find ${SALT_ENV} -maxdepth 3 -mindepth 3 -path '*_modules*' -iname "*.py" -type f -print0 | while read -d $'\0' file; do
133 ln -fs $(readlink -e ${file}) "$SALT_ROOT"/_modules/$(basename ${file}) ;
134 done
135 salt_run saltutil.sync_all
136}
137
marcodc62db92016-01-25 22:43:45 +0100138install_dependencies() {
139 grep -E "^dependencies:" ${METADATA} >/dev/null || return 0
140 (python - | while read dep; do fetch_dependency "$dep"; done) << EOF
141import sys,yaml
142for dep in yaml.load(open('${METADATA}', 'ro'))['dependencies']:
Filip Pytloun1eedfdf2016-04-14 11:48:37 +0200143 print '%s:%s' % (dep["name"], dep["source"])
marcodc62db92016-01-25 22:43:45 +0100144EOF
145}
146
147clean() {
148 log_info "Cleaning up ${BUILDDIR}"
149 [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0
150}
151
152salt_run() {
Jakub Josefc22dff22017-12-15 16:58:29 +0100153 [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
Jakub Josef4a7754d2018-02-08 13:22:14 +0100154 python $(which salt-call) ${SALT_OPTS} $*
marcodc62db92016-01-25 22:43:45 +0100155}
156
157prepare() {
158 [ -d ${BUILDDIR} ] && mkdir -p ${BUILDDIR}
159
Mykyta Karpinee4e9cf2018-04-06 11:33:05 +0300160 # On ci salt is preinstalled from packages, so use it if present
161 which salt-call || setup_virtualenv
Mykyta Karpin72d0b992018-02-16 12:01:11 +0200162 setup_mock_bin
marcodc62db92016-01-25 22:43:45 +0100163 setup_pillar
164 setup_salt
165 install_dependencies
166}
167
Mykyta Karpin72d0b992018-02-16 12:01:11 +0200168lint_releasenotes() {
169 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
170 source ${VENV_DIR}/bin/activate
171 python -m pip install reno
172 reno lint ${CURDIR}/../
173}
174
175lint() {
176# lint_releasenotes
177 log_err "TODO: lint_releasenotes"
178}
179
marcodc62db92016-01-25 22:43:45 +0100180run() {
181 for pillar in ${PILLARDIR}/*.sls; do
Mykyta Karpin72d0b992018-02-16 12:01:11 +0200182 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
marcodc62db92016-01-25 22:43:45 +0100183 state_name=$(basename ${pillar%.sls})
Mykyta Karpin72d0b992018-02-16 12:01:11 +0200184 salt_run grains.set 'noservices' False force=True
185
186 echo "Checking state ${FORMULA_NAME}.${state_name} ..."
marcodc62db92016-01-25 22:43:45 +0100187 salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
Mykyta Karpin72d0b992018-02-16 12:01:11 +0200188
189 # Check that all files in 'meta' folder can be rendered using any valid pillar
190 for meta in `find ${FORMULA_META_DIR} -type f`; do
191 meta_name=$(basename ${meta})
192 echo "Checking meta ${meta_name} ..."
193 salt_run --out=quiet --id=${state_name} cp.get_template ${meta} ${SALT_CACHE_DIR}/${meta_name} \
194 || { log_err "Failed to render meta ${meta} using pillar ${FORMULA_NAME}.${state_name}"; exit 1; }
195 cat ${SALT_CACHE_DIR}/${meta_name}
196 done
marcodc62db92016-01-25 22:43:45 +0100197 done
198}
199
Mykyta Karpin72d0b992018-02-16 12:01:11 +0200200real_run() {
201 for pillar in ${PILLARDIR}/*.sls; do
202 state_name=$(basename ${pillar%.sls})
203 salt_run --id=${state_name} state.sls ${FORMULA_NAME} || { log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1; }
204 done
205}
206
207run_model_validate(){
208 if [ -d ${SCHEMARDIR} ]; then
209 # model validator require py modules
210 fetch_dependency "salt:https://github.com/salt-formulas/salt-formula-salt"
211 link_modules
212 # Rendered Example:
213 # python $(which salt-call) --local -c /test1/maas/tests/build/salt --id=maas_cluster modelschema.model_validate maas cluster
214 for role in ${SCHEMARDIR}/*.yaml; do
215 state_name=$(basename "${role%*.yaml}")
216 minion_id="${state_name}"
217 # in case debug-reruns, usefull to make cleanup
218 [ -n "$DEBUG" ] && { salt_run saltutil.clear_cache; salt_run saltutil.refresh_pillar; salt_run saltutil.sync_all; }
219 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 ; }
220 done
221 else
222 log_info "${SCHEMARDIR} not found!";
223 fi
224}
225
226dependency_check() {
227 local DEPENDENCY_COMMANDS=$*
228
229 for DEPENDENCY_COMMAND in $DEPENDENCY_COMMANDS; do
230 which $DEPENDENCY_COMMAND > /dev/null || ( log_err "Command \"$DEPENDENCY_COMMAND\" can not be found in default path."; exit 1; )
231 done
232}
233
marcodc62db92016-01-25 22:43:45 +0100234_atexit() {
235 RETVAL=$?
236 trap true INT TERM EXIT
237
238 if [ $RETVAL -ne 0 ]; then
239 log_err "Execution failed"
240 else
241 log_info "Execution successful"
242 fi
243 return $RETVAL
244}
245
246## Main
247trap _atexit INT TERM EXIT
248
249case $1 in
250 clean)
251 clean
252 ;;
253 prepare)
254 prepare
255 ;;
Mykyta Karpin72d0b992018-02-16 12:01:11 +0200256 lint)
257 lint
258 ;;
marcodc62db92016-01-25 22:43:45 +0100259 run)
260 run
261 ;;
Mykyta Karpin72d0b992018-02-16 12:01:11 +0200262 real-run)
263 real_run
264 ;;
265 model-validate)
266 prepare
267 run_model_validate
268 ;;
marcodc62db92016-01-25 22:43:45 +0100269 *)
270 prepare
Mykyta Karpin72d0b992018-02-16 12:01:11 +0200271# lint
marcodc62db92016-01-25 22:43:45 +0100272 run
Mykyta Karpin72d0b992018-02-16 12:01:11 +0200273# run_model_validate
marcodc62db92016-01-25 22:43:45 +0100274 ;;
Mykyta Karpinee4e9cf2018-04-06 11:33:05 +0300275esac