blob: 96cb9ad332bf6f4d3899c01c3162fadbf46caf56 [file] [log] [blame]
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +03001#!/usr/bin/env bash
2
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +03003set -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']")
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +03009
10## Overrideable parameters
11PILLARDIR=${PILLARDIR:-${CURDIR}/pillar}
12BUILDDIR=${BUILDDIR:-${CURDIR}/build}
13VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv}
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +030014DEPSDIR=${BUILDDIR}/deps
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +030015
16SALT_FILE_DIR=${SALT_FILE_DIR:-${BUILDDIR}/file_root}
17SALT_PILLAR_DIR=${SALT_PILLAR_DIR:-${BUILDDIR}/pillar_root}
18SALT_CONFIG_DIR=${SALT_CONFIG_DIR:-${BUILDDIR}/salt}
19SALT_CACHE_DIR=${SALT_CACHE_DIR:-${SALT_CONFIG_DIR}/cache}
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +030020
21SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null"
22
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +030023if [ "x${SALT_VERSION}" != "x" ]; then
24 PIP_SALT_VERSION="==${SALT_VERSION}"
25fi
26
27## Functions
28log_info() {
29 echo -e "[INFO] $*"
30}
31
32log_err() {
33 echo -e "[ERROR] $*" >&2
34}
35
36setup_virtualenv() {
37 log_info "Setting up Python virtualenv"
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +030038 virtualenv $VENV_DIR
39 source ${VENV_DIR}/bin/activate
40 python -m pip install salt${PIP_SALT_VERSION}
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +030041}
42
Oleh Hryhorov1bc333c2018-05-19 12:55:07 +000043setup_test_state() {
44 local template_path=$1
45 local state_name=$2
46
47 [ ! -d ${BUILDDIR}/tstates/ ] && mkdir ${BUILDDIR}/tstates/
48 [ ! -d ${BUILDDIR}/rfiles/ ] && mkdir ${BUILDDIR}/rfiles/
49
50 cat << EOF > ${BUILDDIR}/tstates/${state_name}.sls
51
52test_${state_name}_rendering:
53 file.managed:
54 - name: ${BUILDDIR}/rfiles/${state_name}.conf
55 - template: jinja
56 - source: ${template_path}
57 - context:
58 _data: {{ pillar.get("_data", {}) }}
59EOF
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +030060}
61
62setup_pillar() {
63 [ ! -d ${SALT_PILLAR_DIR} ] && mkdir -p ${SALT_PILLAR_DIR}
64 echo "base:" > ${SALT_PILLAR_DIR}/top.sls
Oleh Hryhorov1bc333c2018-05-19 12:55:07 +000065 local sdir
66 local state_name
67 local template_name
68 local pillar_name
69
70 pushd ${PILLARDIR}/
71 for spath in $(find ./ -type f -name '*.sls'); do
72 pillar_name=$(basename $spath | sed -e 's/.sls$//')
73 sdir=$(dirname $spath | sed -e 's/^.\///g')
74 template_name=$(echo $pillar_name | cut -d '-' -f 1)
75 state_name=$(echo ${sdir}_${pillar_name} | sed -e 's/\//_/g')
76 if ! echo $pillar_name |grep '-'; then
77 setup_test_state "salt://oslo_templates/files/$sdir/$template_name.conf" "$state_name"
78 fi
79 echo -e " ${state_name}:\n - ${sdir}/${pillar_name}" >> ${SALT_PILLAR_DIR}/top.sls
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +030080 done
81}
82
83setup_salt() {
84 [ ! -d ${SALT_FILE_DIR} ] && mkdir -p ${SALT_FILE_DIR}
85 [ ! -d ${SALT_CONFIG_DIR} ] && mkdir -p ${SALT_CONFIG_DIR}
86 [ ! -d ${SALT_CACHE_DIR} ] && mkdir -p ${SALT_CACHE_DIR}
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +030087
88 echo "base:" > ${SALT_FILE_DIR}/top.sls
89 for pillar in ${PILLARDIR}/*.sls; do
90 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
91 state_name=$(basename ${pillar%.sls})
92 echo -e " ${state_name}:\n - ${FORMULA_NAME}" >> ${SALT_FILE_DIR}/top.sls
93 done
94
95 cat << EOF > ${SALT_CONFIG_DIR}/minion
96file_client: local
97cachedir: ${SALT_CACHE_DIR}
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +030098verify_env: False
99minion_id_caching: False
100
101file_roots:
102 base:
103 - ${SALT_FILE_DIR}
104 - ${CURDIR}/..
Oleh Hryhorov1bc333c2018-05-19 12:55:07 +0000105 - ${BUILDDIR}/tstates/
106 - /usr/share/salt-formulas/env
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +0300107
108pillar_roots:
109 base:
110 - ${SALT_PILLAR_DIR}
111 - ${PILLARDIR}
112EOF
113}
114
115fetch_dependency() {
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +0300116 dep_name="$(echo $1|cut -d : -f 1)"
117 dep_source="$(echo $1|cut -d : -f 2-)"
118 dep_root="${DEPSDIR}/$(basename $dep_source .git)"
119 dep_metadata="${dep_root}/metadata.yml"
120
Oleh Hryhorov1bc333c2018-05-19 12:55:07 +0000121 [ -d /usr/share/salt-formulas/env/${dep_name} ] && log_info "Dependency $dep_name already present in system-wide salt env" && return 0
122 [ -d $dep_root ] && log_info "Dependency $dep_name already fetched" && return 0
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +0300123
124 log_info "Fetching dependency $dep_name"
125 [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
126 git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git)
127 ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name}
128
129 METADATA="${dep_metadata}" install_dependencies
130}
131
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +0300132install_dependencies() {
133 grep -E "^dependencies:" ${METADATA} >/dev/null || return 0
134 (python - | while read dep; do fetch_dependency "$dep"; done) << EOF
135import sys,yaml
136for dep in yaml.load(open('${METADATA}', 'ro'))['dependencies']:
137 print '%s:%s' % (dep["name"], dep["source"])
138EOF
139}
140
141clean() {
142 log_info "Cleaning up ${BUILDDIR}"
143 [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0
144}
145
146salt_run() {
147 [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
Oleh Hryhorov1bc333c2018-05-19 12:55:07 +0000148 local cmd=''
149 cmd="python $(which salt-call) ${SALT_OPTS} $*"
150 log_info "$cmd"
151 $cmd
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +0300152}
153
154prepare() {
Oleh Hryhorov1bc333c2018-05-19 12:55:07 +0000155 [ -d ${BUILDDIR} ] && mkdir -p ${BUILDDIR}
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +0300156
Oleh Hryhorov1bc333c2018-05-19 12:55:07 +0000157 which salt-call || setup_virtualenv
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +0300158 setup_pillar
159 setup_salt
160 install_dependencies
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +0300161}
162
163run() {
Oleh Hryhorov1bc333c2018-05-19 12:55:07 +0000164 pushd ${PILLARDIR}/
165 local sdir
166 local sname
167 local state_name
168 local pillar_name
169 for spath in $(find ./ -type f -name '*.sls'); do
170 sname=$(basename $spath | basename $spath | sed -e 's/.sls$//')
171 sdir=$(dirname $spath | sed -e 's/^.\///')
172 state_name=$(echo ${sdir}_${sname} | sed -e 's/\//_/g' | cut -d '-' -f 1)
173 pillar_name=$(echo ${sdir}_${sname} | sed -e 's/\//_/g')
174 salt_run --id=${pillar_name} state.show_sls ${state_name} || (log_err "Show state ${state_name} with pillar ${pillar_name} failed"; exit 1)
175 salt_run --id=${pillar_name} state.sls ${state_name} || (log_err "Execution of ${state_name} with_pillar ${pillar_name} failed"; exit 1)
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +0300176 done
Oleh Hryhorov1bc333c2018-05-19 12:55:07 +0000177 popd
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +0300178}
179
180_atexit() {
181 RETVAL=$?
182 trap true INT TERM EXIT
183
184 if [ $RETVAL -ne 0 ]; then
185 log_err "Execution failed"
186 else
187 log_info "Execution successful"
188 fi
189 return $RETVAL
190}
191
192## Main
Oleh Hryhorov1bc333c2018-05-19 12:55:07 +0000193[[ "$0" != "$BASH_SOURCE" ]] || {
194 trap _atexit INT TERM EXIT
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +0300195
Oleh Hryhorov1bc333c2018-05-19 12:55:07 +0000196 case $1 in
197 clean)
198 clean
199 ;;
200 prepare)
201 prepare
202 ;;
203 run)
204 run
205 ;;
206 *)
207 prepare
208 run
209 ;;
210 esac
211}