blob: 25e512507e0a6d4a6d958e885488951b081f9d79 [file] [log] [blame]
Bartosz Kupiduraaefd0392017-11-06 09:27:40 +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']")
9FORMULA_META_DIR=${CURDIR}/../${FORMULA_NAME}/meta
10
11## Overrideable parameters
12PILLARDIR=${PILLARDIR:-${CURDIR}/pillar}
13BUILDDIR=${BUILDDIR:-${CURDIR}/build}
14VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv}
15MOCK_BIN_DIR=${MOCK_BIN_DIR:-${CURDIR}/mock_bin}
16DEPSDIR=${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
23SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null"
24
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 Josef98d9bd82018-02-08 13:22:15 +010042 python -m pip install salt${PIP_SALT_VERSION}
43 python -m pip install reno
Bartosz Kupiduraaefd0392017-11-06 09:27:40 +010044}
45
46setup_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
55setup_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
59 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
60 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
72 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
73 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
81minion_id_caching: False
82
83file_roots:
84 base:
85 - ${SALT_FILE_DIR}
86 - ${CURDIR}/..
87 - /usr/share/salt-formulas/env
88
89pillar_roots:
90 base:
91 - ${SALT_PILLAR_DIR}
92 - ${PILLARDIR}
93EOF
94}
95
96fetch_dependency() {
97 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)"
100 dep_metadata="${dep_root}/metadata.yml"
101
102 [ -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
104
105 log_info "Fetching dependency $dep_name"
106 [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
107 git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git)
108 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']:
118 print '%s:%s' % (dep["name"], dep["source"])
119EOF
120}
121
122clean() {
123 log_info "Cleaning up ${BUILDDIR}"
124 [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0
125}
126
127salt_run() {
Jakub Josef45529082017-12-15 16:58:29 +0100128 [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
Jakub Josef98d9bd82018-02-08 13:22:15 +0100129 python $(which salt-call) ${SALT_OPTS} $*
Bartosz Kupiduraaefd0392017-11-06 09:27:40 +0100130}
131
132prepare() {
133 [ -d ${BUILDDIR} ] && mkdir -p ${BUILDDIR}
134
135 which salt-call || setup_virtualenv
136 setup_mock_bin
137 setup_pillar
138 setup_salt
139 install_dependencies
140}
141
142lint_releasenotes() {
143 echo "placeholder"
144 #reno lint ${CURDIR}/../
145}
146
147lint() {
148 lint_releasenotes
149}
150
151run() {
152 for pillar in ${PILLARDIR}/*.sls; do
153 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
154 state_name=$(basename ${pillar%.sls})
155 salt_run grains.set 'noservices' False force=True
156
157 echo "Checking state ${FORMULA_NAME}.${state_name} ..."
158 salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
159
160 # Check that all files in 'meta' folder can be rendered using any valid pillar
161 for meta in `find ${FORMULA_META_DIR} -type f`; do
162 meta_name=$(basename ${meta})
163 echo "Checking meta ${meta_name} ..."
164 salt_run --out=quiet --id=${state_name} cp.get_template ${meta} ${SALT_CACHE_DIR}/${meta_name} \
165 || (log_err "Failed to render meta ${meta} using pillar ${FORMULA_NAME}.${state_name}"; exit 1)
166 cat ${SALT_CACHE_DIR}/${meta_name}
167 done
168 done
169}
170
171real_run() {
172 for pillar in ${PILLARDIR}/*.sls; do
173 state_name=$(basename ${pillar%.sls})
174 salt_run --id=${state_name} state.sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
175 done
176}
177
178_atexit() {
179 RETVAL=$?
180 trap true INT TERM EXIT
181
182 if [ $RETVAL -ne 0 ]; then
183 log_err "Execution failed"
184 else
185 log_info "Execution successful"
186 fi
187 return $RETVAL
188}
189
190## Main
191trap _atexit INT TERM EXIT
192
193case $1 in
194 clean)
195 clean
196 ;;
197 prepare)
198 prepare
199 ;;
200 lint)
201 lint
202 ;;
203 run)
204 run
205 ;;
206 real-run)
207 real_run
208 ;;
209 *)
210 prepare
211 lint
212 run
213 ;;
214esac