blob: a348912d9069d7fbb31e51dd704626a5c1729a62 [file] [log] [blame]
Mikhail Ivanov79133102018-05-29 13:27:45 +04001#!/usr/bin/env bash
2
3###
4# Script requirments:
5#apt-get install -y python-yaml virtualenv git
6
7set -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']")
13FORMULA_META_DIR=${CURDIR}/../${FORMULA_NAME}/meta
14
15## Overrideable parameters
16PILLARDIR=${PILLARDIR:-${CURDIR}/pillar}
17BUILDDIR=${BUILDDIR:-${CURDIR}/build}
18VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv}
19MOCK_BIN_DIR=${MOCK_BIN_DIR:-${CURDIR}/mock_bin}
20DEPSDIR=${BUILDDIR}/deps
21SCHEMARDIR=${SCHEMARDIR:-"${CURDIR}/../${FORMULA_NAME}/schemas/"}
22
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}
27SALT_CACHE_EXTMODS_DIR=${SALT_CACHE_EXTMODS_DIR:-${SALT_CONFIG_DIR}/cache_master_extmods}
28
29SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null"
30
31IGNORE_MODELVALIDATE_MASK=${IGNORE_MODELVALIDATE_MASK:-"novalidate"}
32
33if [ "x${SALT_VERSION}" != "x" ]; then
34 PIP_SALT_VERSION="==${SALT_VERSION}"
35fi
36
37## Functions
38log_info() {
39 echo -e "[INFO] $*"
40}
41
42log_err() {
43 echo -e "[ERROR] $*" >&2
44}
45
46setup_virtualenv() {
47 log_info "Setting up Python virtualenv"
48 dependency_check virtualenv
49 virtualenv $VENV_DIR
50 source ${VENV_DIR}/bin/activate
51 python -m pip install salt${PIP_SALT_VERSION}
52 if [[ -f ${CURDIR}/test-requirements.txt ]]; then
53 python -m pip install -r ${CURDIR}/test-requirements.txt
54 fi
55}
56
57setup_mock_bin() {
58 # If some state requires a binary, a lightweight replacement for
59 # such binary can be put into MOCK_BIN_DIR for test purposes
60 if [ -d "${MOCK_BIN_DIR}" ]; then
61 PATH="${MOCK_BIN_DIR}:$PATH"
62 export PATH
63 fi
64}
65
66setup_pillar() {
67 [ ! -d ${SALT_PILLAR_DIR} ] && mkdir -p ${SALT_PILLAR_DIR}
68 echo "base:" > ${SALT_PILLAR_DIR}/top.sls
69 for pillar in ${PILLARDIR}/*; do
70 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
71 state_name=$(basename ${pillar%.sls})
72 echo -e " ${state_name}:\n - ${state_name}" >> ${SALT_PILLAR_DIR}/top.sls
73 done
74}
75
76setup_salt() {
77 [ ! -d ${SALT_FILE_DIR} ] && mkdir -p ${SALT_FILE_DIR}
78 [ ! -d ${SALT_CONFIG_DIR} ] && mkdir -p ${SALT_CONFIG_DIR}
79 [ ! -d ${SALT_CACHE_DIR} ] && mkdir -p ${SALT_CACHE_DIR}
80 [ ! -d ${SALT_CACHE_EXTMODS_DIR} ] && mkdir -p ${SALT_CACHE_EXTMODS_DIR}
81
82 echo "base:" > ${SALT_FILE_DIR}/top.sls
83 for pillar in ${PILLARDIR}/*.sls; do
84 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
85 state_name=$(basename ${pillar%.sls})
86 echo -e " ${state_name}:\n - ${FORMULA_NAME}" >> ${SALT_FILE_DIR}/top.sls
87 done
88
89 cat << EOF > ${SALT_CONFIG_DIR}/minion
90file_client: local
91cachedir: ${SALT_CACHE_DIR}
92extension_modules: ${SALT_CACHE_EXTMODS_DIR}
93verify_env: False
94minion_id_caching: False
95
96file_roots:
97 base:
98 - ${SALT_FILE_DIR}
99 - ${CURDIR}/..
100
101pillar_roots:
102 base:
103 - ${SALT_PILLAR_DIR}
104 - ${PILLARDIR}
105EOF
106}
107
108fetch_dependency() {
109 # example: fetch_dependency "linux:https://github.com/salt-formulas/salt-formula-linux"
110 dep_name="$(echo $1|cut -d : -f 1)"
111 dep_source="$(echo $1|cut -d : -f 2-)"
112 dep_root="${DEPSDIR}/$(basename $dep_source .git)"
113 dep_metadata="${dep_root}/metadata.yml"
114
115 dependency_check git
116 [ -d $dep_root ] && { log_info "Dependency $dep_name already fetched"; return 0; }
117
118 log_info "Fetching dependency $dep_name"
119 [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
120 git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git)
121 ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name}
122
123 METADATA="${dep_metadata}" install_dependencies
124}
125
126link_modules(){
127 # Link modules *.py files to temporary salt-root
128 local SALT_ROOT=${1:-$SALT_FILE_DIR}
129 local SALT_ENV=${2:-$DEPSDIR}
130
131 mkdir -p "${SALT_ROOT}/_modules/"
132 # from git, development versions
133 find ${SALT_ENV} -maxdepth 3 -mindepth 3 -path '*_modules*' -iname "*.py" -type f -print0 | while read -d $'\0' file; do
134 ln -fs $(readlink -e ${file}) "$SALT_ROOT"/_modules/$(basename ${file}) ;
135 done
136 salt_run saltutil.sync_all
137}
138
139install_dependencies() {
140 grep -E "^dependencies:" ${METADATA} >/dev/null || return 0
141 (python - | while read dep; do fetch_dependency "$dep"; done) << EOF
142import sys,yaml
143for dep in yaml.load(open('${METADATA}', 'ro'))['dependencies']:
144 print '%s:%s' % (dep["name"], dep["source"])
145EOF
146}
147
148clean() {
149 log_info "Cleaning up ${BUILDDIR}"
150 [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0
151}
152
153salt_run() {
154 [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
155 python $(which salt-call) ${SALT_OPTS} $*
156}
157
158prepare() {
159 if [[ -f ${BUILDDIR}/.prepare_done ]]; then
160 log_info "${BUILDDIR}/.prepare_done exist, not rebuilding BUILDDIR"
161 return
162 fi
163 [[ -d ${BUILDDIR} ]] && mkdir -p ${BUILDDIR}
164
165 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
166 setup_mock_bin
167 setup_pillar
168 setup_salt
169 install_dependencies
170 link_modules
171 touch ${BUILDDIR}/.prepare_done
172}
173
174lint_releasenotes() {
175 [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv
176 source ${VENV_DIR}/bin/activate
177 reno lint ${CURDIR}/../
178}
179
180lint() {
181# lint_releasenotes
182 log_err "TODO: lint_releasenotes"
183}
184
185run() {
186 for pillar in ${PILLARDIR}/*.sls; do
187 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
188 state_name=$(basename ${pillar%.sls})
189 salt_run grains.set 'noservices' False force=True
190
191 echo "Checking state ${FORMULA_NAME}.${state_name} ..."
192 salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
193
194 # Check that all files in 'meta' folder can be rendered using any valid pillar
195 for meta in `find ${FORMULA_META_DIR} -type f`; do
196 meta_name=$(basename ${meta})
197 echo "Checking meta ${meta_name} ..."
198 salt_run --out=quiet --id=${state_name} cp.get_template ${meta} ${SALT_CACHE_DIR}/${meta_name} \
199 || { log_err "Failed to render meta ${meta} using pillar ${FORMULA_NAME}.${state_name}"; exit 1; }
200 cat ${SALT_CACHE_DIR}/${meta_name}
201 done
202 done
203}
204
205real_run() {
206 for pillar in ${PILLARDIR}/*.sls; do
207 state_name=$(basename ${pillar%.sls})
208 salt_run --id=${state_name} state.sls ${FORMULA_NAME} || { log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1; }
209 done
210}
211
212run_model_validate(){
213 # Run modelschema.model_validate validation.
214 # TEST iterateble, run for `each formula ROLE against each ROLE_PILLARNAME`
215 # Pillars should be named in conviend ROLE_XXX.sls or ROLE.sls
216 # Example:
217 # client.sls client_auth.sls server.sls server_auth.sls
218 if [ -d ${SCHEMARDIR} ]; then
219 # model validator require py modules
220 fetch_dependency "salt:https://github.com/salt-formulas/salt-formula-salt"
221 link_modules
222 salt_run saltutil.clear_cache; salt_run saltutil.refresh_pillar; salt_run saltutil.sync_all;
223 for role in ${SCHEMARDIR}/*.yaml; do
224 role_name=$(basename "${role%*.yaml}")
225 for pillar in $(ls pillar/${role_name}*.sls | grep -v ${IGNORE_MODELVALIDATE_MASK} ); do
226 pillar_name=$(basename "${pillar%*.sls}")
227 local _message="FORMULA:${FORMULA_NAME} ROLE:${role_name} against PILLAR:${pillar_name}"
228 log_info "model_validate ${_message}"
229 # Rendered Example:
230 # python $(which salt-call) --local -c /test1/maas/tests/build/salt --id=maas_cluster modelschema.model_validate maas cluster
231 salt_run -m ${DEPSDIR}/salt-formula-salt --id=${pillar_name} modelschema.model_validate ${FORMULA_NAME} ${role_name} || { log_err "Execution of model_validate ${_message} failed"; exit 1 ; }
232 done
233 done
234 else
235 log_info "${SCHEMARDIR} not found!";
236 fi
237}
238
239dependency_check() {
240 local DEPENDENCY_COMMANDS=$*
241
242 for DEPENDENCY_COMMAND in $DEPENDENCY_COMMANDS; do
243 which $DEPENDENCY_COMMAND > /dev/null || ( log_err "Command \"$DEPENDENCY_COMMAND\" can not be found in default path."; exit 1; )
244 done
245}
246
247_atexit() {
248 RETVAL=$?
249 trap true INT TERM EXIT
250
251 if [ $RETVAL -ne 0 ]; then
252 log_err "Execution failed"
253 else
254 log_info "Execution successful"
255 fi
256 return $RETVAL
257}
258
259## Main
260trap _atexit INT TERM EXIT
261
262case $1 in
263 clean)
264 clean
265 ;;
266 prepare)
267 prepare
268 ;;
269 lint)
270 lint
271 ;;
272 run)
273 run
274 ;;
275 real-run)
276 real_run
277 ;;
278 model-validate)
279 prepare
280 run_model_validate
281 ;;
282 *)
283 prepare
284# lint
285 run
286 run_model_validate
287 ;;
288esac