blob: f62611256b0abd196867d0a6a1583c51158d13ae [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", {}) }}
sgarbuz2d8f8052018-07-13 13:04:54 +030059 {%- if pillar.get('service_name') %}
60 service_name: {{ pillar.service_name }}
61 {%- endif %}
Oleh Hryhorov1bc333c2018-05-19 12:55:07 +000062EOF
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +030063}
64
65setup_pillar() {
66 [ ! -d ${SALT_PILLAR_DIR} ] && mkdir -p ${SALT_PILLAR_DIR}
67 echo "base:" > ${SALT_PILLAR_DIR}/top.sls
Oleh Hryhorov1bc333c2018-05-19 12:55:07 +000068 local sdir
69 local state_name
70 local template_name
71 local pillar_name
72
73 pushd ${PILLARDIR}/
74 for spath in $(find ./ -type f -name '*.sls'); do
75 pillar_name=$(basename $spath | sed -e 's/.sls$//')
76 sdir=$(dirname $spath | sed -e 's/^.\///g')
77 template_name=$(echo $pillar_name | cut -d '-' -f 1)
78 state_name=$(echo ${sdir}_${pillar_name} | sed -e 's/\//_/g')
79 if ! echo $pillar_name |grep '-'; then
80 setup_test_state "salt://oslo_templates/files/$sdir/$template_name.conf" "$state_name"
81 fi
82 echo -e " ${state_name}:\n - ${sdir}/${pillar_name}" >> ${SALT_PILLAR_DIR}/top.sls
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +030083 done
84}
85
86setup_salt() {
87 [ ! -d ${SALT_FILE_DIR} ] && mkdir -p ${SALT_FILE_DIR}
88 [ ! -d ${SALT_CONFIG_DIR} ] && mkdir -p ${SALT_CONFIG_DIR}
89 [ ! -d ${SALT_CACHE_DIR} ] && mkdir -p ${SALT_CACHE_DIR}
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +030090
91 echo "base:" > ${SALT_FILE_DIR}/top.sls
92 for pillar in ${PILLARDIR}/*.sls; do
93 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
94 state_name=$(basename ${pillar%.sls})
95 echo -e " ${state_name}:\n - ${FORMULA_NAME}" >> ${SALT_FILE_DIR}/top.sls
96 done
97
98 cat << EOF > ${SALT_CONFIG_DIR}/minion
99file_client: local
100cachedir: ${SALT_CACHE_DIR}
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +0300101verify_env: False
102minion_id_caching: False
103
104file_roots:
105 base:
106 - ${SALT_FILE_DIR}
107 - ${CURDIR}/..
Oleh Hryhorov1bc333c2018-05-19 12:55:07 +0000108 - ${BUILDDIR}/tstates/
109 - /usr/share/salt-formulas/env
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +0300110
111pillar_roots:
112 base:
113 - ${SALT_PILLAR_DIR}
114 - ${PILLARDIR}
115EOF
116}
117
118fetch_dependency() {
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +0300119 dep_name="$(echo $1|cut -d : -f 1)"
120 dep_source="$(echo $1|cut -d : -f 2-)"
121 dep_root="${DEPSDIR}/$(basename $dep_source .git)"
122 dep_metadata="${dep_root}/metadata.yml"
123
Oleh Hryhorov1bc333c2018-05-19 12:55:07 +0000124 [ -d /usr/share/salt-formulas/env/${dep_name} ] && log_info "Dependency $dep_name already present in system-wide salt env" && return 0
125 [ -d $dep_root ] && log_info "Dependency $dep_name already fetched" && return 0
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +0300126
127 log_info "Fetching dependency $dep_name"
128 [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
129 git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git)
130 ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name}
131
132 METADATA="${dep_metadata}" install_dependencies
133}
134
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +0300135install_dependencies() {
136 grep -E "^dependencies:" ${METADATA} >/dev/null || return 0
137 (python - | while read dep; do fetch_dependency "$dep"; done) << EOF
138import sys,yaml
139for dep in yaml.load(open('${METADATA}', 'ro'))['dependencies']:
140 print '%s:%s' % (dep["name"], dep["source"])
141EOF
142}
143
144clean() {
145 log_info "Cleaning up ${BUILDDIR}"
146 [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0
147}
148
149salt_run() {
150 [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
Oleh Hryhorov1bc333c2018-05-19 12:55:07 +0000151 local cmd=''
152 cmd="python $(which salt-call) ${SALT_OPTS} $*"
153 log_info "$cmd"
154 $cmd
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +0300155}
156
157prepare() {
Oleh Hryhorov1bc333c2018-05-19 12:55:07 +0000158 [ -d ${BUILDDIR} ] && mkdir -p ${BUILDDIR}
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +0300159
Oleh Hryhorov1bc333c2018-05-19 12:55:07 +0000160 which salt-call || setup_virtualenv
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +0300161 setup_pillar
162 setup_salt
163 install_dependencies
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +0300164}
165
166run() {
Oleh Hryhorov1bc333c2018-05-19 12:55:07 +0000167 pushd ${PILLARDIR}/
168 local sdir
169 local sname
170 local state_name
171 local pillar_name
172 for spath in $(find ./ -type f -name '*.sls'); do
173 sname=$(basename $spath | basename $spath | sed -e 's/.sls$//')
174 sdir=$(dirname $spath | sed -e 's/^.\///')
175 state_name=$(echo ${sdir}_${sname} | sed -e 's/\//_/g' | cut -d '-' -f 1)
176 pillar_name=$(echo ${sdir}_${sname} | sed -e 's/\//_/g')
177 salt_run --id=${pillar_name} state.show_sls ${state_name} || (log_err "Show state ${state_name} with pillar ${pillar_name} failed"; exit 1)
178 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 +0300179 done
Oleh Hryhorov1bc333c2018-05-19 12:55:07 +0000180 popd
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +0300181}
182
183_atexit() {
184 RETVAL=$?
185 trap true INT TERM EXIT
186
187 if [ $RETVAL -ne 0 ]; then
188 log_err "Execution failed"
189 else
190 log_info "Execution successful"
191 fi
192 return $RETVAL
193}
194
195## Main
Oleh Hryhorov1bc333c2018-05-19 12:55:07 +0000196[[ "$0" != "$BASH_SOURCE" ]] || {
197 trap _atexit INT TERM EXIT
Oleh Hryhorov3b72bf52018-05-18 16:23:04 +0300198
Oleh Hryhorov1bc333c2018-05-19 12:55:07 +0000199 case $1 in
200 clean)
201 clean
202 ;;
203 prepare)
204 prepare
205 ;;
206 run)
207 run
208 ;;
209 *)
210 prepare
211 run
212 ;;
213 esac
214}