blob: f4d5df02fca58f3bc5832cf6b1a40aea841e62ac [file] [log] [blame]
marco836d9552016-01-25 22:22:46 +01001#!/usr/bin/env bash
2
3set -e
4[ -n "$DEBUG" ] && set -x
5
6CURDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
7METADATA=${CURDIR}/../metadata.yml
8FORMULA_NAME=$(cat $METADATA | python -c "import sys,yaml; print yaml.load(sys.stdin)['name']")
Oleksandr Shyshko26a92062018-08-01 14:18:14 +03009FORMULA_META_DIR=${CURDIR}/../${FORMULA_NAME}/meta
marco836d9552016-01-25 22:22:46 +010010
11## Overrideable parameters
12PILLARDIR=${PILLARDIR:-${CURDIR}/pillar}
13BUILDDIR=${BUILDDIR:-${CURDIR}/build}
14VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv}
Oleksandr Shyshko26a92062018-08-01 14:18:14 +030015MOCK_BIN_DIR=${MOCK_BIN_DIR:-${CURDIR}/mock_bin}
marco836d9552016-01-25 22:22:46 +010016DEPSDIR=${BUILDDIR}/deps
17
18SALT_FILE_DIR=${SALT_FILE_DIR:-${BUILDDIR}/file_root}
19SALT_PILLAR_DIR=${SALT_PILLAR_DIR:-${BUILDDIR}/pillar_root}
20SALT_CONFIG_DIR=${SALT_CONFIG_DIR:-${BUILDDIR}/salt}
21SALT_CACHE_DIR=${SALT_CACHE_DIR:-${SALT_CONFIG_DIR}/cache}
22
Simak, Jana0727892016-06-23 13:51:02 +020023SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null"
marco836d9552016-01-25 22:22:46 +010024
25if [ "x${SALT_VERSION}" != "x" ]; then
26 PIP_SALT_VERSION="==${SALT_VERSION}"
27fi
28
29## Functions
30log_info() {
31 echo "[INFO] $*"
32}
33
34log_err() {
35 echo "[ERROR] $*" >&2
36}
37
38setup_virtualenv() {
39 log_info "Setting up Python virtualenv"
40 virtualenv $VENV_DIR
41 source ${VENV_DIR}/bin/activate
Jakub Josef285612d2018-02-08 13:22:16 +010042 python -m pip install salt${PIP_SALT_VERSION}
Ksawery Dziekoński435edce2020-02-28 15:51:43 +010043 python -m pip install 'msgpack<1.0.0'
marco836d9552016-01-25 22:22:46 +010044}
45
Oleksandr Shyshko26a92062018-08-01 14:18:14 +030046setup_mock_bin() {
47 # If some state requires a binary, a lightweight replacement for
48 # such binary can be put into MOCK_BIN_DIR for test purposes
49 if [ -d "${MOCK_BIN_DIR}" ]; then
50 PATH="${MOCK_BIN_DIR}:$PATH"
51 export PATH
52 fi
53}
54
marco836d9552016-01-25 22:22:46 +010055setup_pillar() {
56 [ ! -d ${SALT_PILLAR_DIR} ] && mkdir -p ${SALT_PILLAR_DIR}
57 echo "base:" > ${SALT_PILLAR_DIR}/top.sls
58 for pillar in ${PILLARDIR}/*; do
Oleksandr Shyshko26a92062018-08-01 14:18:14 +030059 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
marco836d9552016-01-25 22:22:46 +010060 state_name=$(basename ${pillar%.sls})
61 echo -e " ${state_name}:\n - ${state_name}" >> ${SALT_PILLAR_DIR}/top.sls
62 done
63}
64
65setup_salt() {
66 [ ! -d ${SALT_FILE_DIR} ] && mkdir -p ${SALT_FILE_DIR}
67 [ ! -d ${SALT_CONFIG_DIR} ] && mkdir -p ${SALT_CONFIG_DIR}
68 [ ! -d ${SALT_CACHE_DIR} ] && mkdir -p ${SALT_CACHE_DIR}
69
70 echo "base:" > ${SALT_FILE_DIR}/top.sls
71 for pillar in ${PILLARDIR}/*.sls; do
Oleksandr Shyshko26a92062018-08-01 14:18:14 +030072 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
marco836d9552016-01-25 22:22:46 +010073 state_name=$(basename ${pillar%.sls})
74 echo -e " ${state_name}:\n - ${FORMULA_NAME}" >> ${SALT_FILE_DIR}/top.sls
75 done
76
77 cat << EOF > ${SALT_CONFIG_DIR}/minion
78file_client: local
79cachedir: ${SALT_CACHE_DIR}
80verify_env: False
Ondřej Novýdea6e972016-04-23 17:11:02 +020081minion_id_caching: False
marco836d9552016-01-25 22:22:46 +010082
83file_roots:
84 base:
85 - ${SALT_FILE_DIR}
86 - ${CURDIR}/..
Filip Pytloun70d116c2016-04-14 11:51:09 +020087 - /usr/share/salt-formulas/env
marco836d9552016-01-25 22:22:46 +010088
89pillar_roots:
90 base:
91 - ${SALT_PILLAR_DIR}
92 - ${PILLARDIR}
93EOF
94}
95
96fetch_dependency() {
Filip Pytloun70d116c2016-04-14 11:51:09 +020097 dep_name="$(echo $1|cut -d : -f 1)"
98 dep_source="$(echo $1|cut -d : -f 2-)"
99 dep_root="${DEPSDIR}/$(basename $dep_source .git)"
marco836d9552016-01-25 22:22:46 +0100100 dep_metadata="${dep_root}/metadata.yml"
101
Filip Pytloun70d116c2016-04-14 11:51:09 +0200102 [ -d /usr/share/salt-formulas/env/${dep_name} ] && log_info "Dependency $dep_name already present in system-wide salt env" && return 0
103 [ -d $dep_root ] && log_info "Dependency $dep_name already fetched" && return 0
marco836d9552016-01-25 22:22:46 +0100104
Filip Pytloun70d116c2016-04-14 11:51:09 +0200105 log_info "Fetching dependency $dep_name"
marco836d9552016-01-25 22:22:46 +0100106 [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
Filip Pytloun70d116c2016-04-14 11:51:09 +0200107 git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git)
marco836d9552016-01-25 22:22:46 +0100108 ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name}
109
110 METADATA="${dep_metadata}" install_dependencies
111}
112
113install_dependencies() {
114 grep -E "^dependencies:" ${METADATA} >/dev/null || return 0
115 (python - | while read dep; do fetch_dependency "$dep"; done) << EOF
116import sys,yaml
117for dep in yaml.load(open('${METADATA}', 'ro'))['dependencies']:
Filip Pytloun70d116c2016-04-14 11:51:09 +0200118 print '%s:%s' % (dep["name"], dep["source"])
marco836d9552016-01-25 22:22:46 +0100119EOF
120}
121
122clean() {
123 log_info "Cleaning up ${BUILDDIR}"
124 [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0
125}
126
127salt_run() {
Jakub Josef97b4eeb2017-12-15 16:58:29 +0100128 [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
Jakub Josef285612d2018-02-08 13:22:16 +0100129 python $(which salt-call) ${SALT_OPTS} $*
marco836d9552016-01-25 22:22:46 +0100130}
131
132prepare() {
133 [ -d ${BUILDDIR} ] && mkdir -p ${BUILDDIR}
134
Filip Pytloun70d116c2016-04-14 11:51:09 +0200135 which salt-call || setup_virtualenv
Oleksandr Shyshko26a92062018-08-01 14:18:14 +0300136 setup_mock_bin
marco836d9552016-01-25 22:22:46 +0100137 setup_pillar
138 setup_salt
139 install_dependencies
140}
141
142run() {
143 for pillar in ${PILLARDIR}/*.sls; do
Oleksandr Shyshko26a92062018-08-01 14:18:14 +0300144 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
marco836d9552016-01-25 22:22:46 +0100145 state_name=$(basename ${pillar%.sls})
Oleksandr Shyshko26a92062018-08-01 14:18:14 +0300146 salt_run grains.set 'noservices' False force=True
147
148 echo "Checking state ${FORMULA_NAME}.${state_name} ..."
marco836d9552016-01-25 22:22:46 +0100149 salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
Oleksandr Shyshko26a92062018-08-01 14:18:14 +0300150
151 # Check that all files in 'meta' folder can be rendered using any valid pillar
152 for meta in `find ${FORMULA_META_DIR} -type f`; do
153 meta_name=$(basename ${meta})
154 echo "Checking meta ${meta_name} ..."
155 salt_run --out=quiet --id=${state_name} cp.get_template ${meta} ${SALT_CACHE_DIR}/${meta_name} \
156 || (log_err "Failed to render meta ${meta} using pillar ${FORMULA_NAME}.${state_name}"; exit 1)
157 cat ${SALT_CACHE_DIR}/${meta_name}
158 done
159 done
160}
161
162real_run() {
163 for pillar in ${PILLARDIR}/*.sls; do
164 state_name=$(basename ${pillar%.sls})
165 salt_run --id=${state_name} state.sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
marco836d9552016-01-25 22:22:46 +0100166 done
167}
168
169_atexit() {
170 RETVAL=$?
171 trap true INT TERM EXIT
172
173 if [ $RETVAL -ne 0 ]; then
174 log_err "Execution failed"
175 else
176 log_info "Execution successful"
177 fi
178 return $RETVAL
179}
180
181## Main
182trap _atexit INT TERM EXIT
183
184case $1 in
185 clean)
186 clean
187 ;;
188 prepare)
189 prepare
190 ;;
191 run)
192 run
193 ;;
Oleksandr Shyshko26a92062018-08-01 14:18:14 +0300194 real-run)
195 real_run
196 ;;
marco836d9552016-01-25 22:22:46 +0100197 *)
198 prepare
199 run
200 ;;
201esac