blob: 8e70bedeb9548911a0e91a697dd3e6f3060ce361 [file] [log] [blame]
Jakub Pavlikc89ba902016-01-25 22:48:37 +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']")
9
10## Overrideable parameters
11PILLARDIR=${PILLARDIR:-${CURDIR}/pillar}
12BUILDDIR=${BUILDDIR:-${CURDIR}/build}
13VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv}
14DEPSDIR=${BUILDDIR}/deps
15
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}
20
Lukas Zmrzlik935e9052016-06-22 14:57:52 +020021SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null"
Jakub Pavlikc89ba902016-01-25 22:48:37 +010022
23if [ "x${SALT_VERSION}" != "x" ]; then
24 PIP_SALT_VERSION="==${SALT_VERSION}"
25fi
26
27## Functions
28log_info() {
29 echo "[INFO] $*"
30}
31
32log_err() {
33 echo "[ERROR] $*" >&2
34}
35
36setup_virtualenv() {
37 log_info "Setting up Python virtualenv"
38 virtualenv $VENV_DIR
39 source ${VENV_DIR}/bin/activate
Jakub Josef8632e3a2018-02-08 13:22:19 +010040 python -m pip install salt${PIP_SALT_VERSION}
Ksawery Dziekoński6d36f462020-04-22 13:20:16 +020041 python -m pip install 'msgpack<1.0.0'
Jakub Pavlikc89ba902016-01-25 22:48:37 +010042}
43
44setup_pillar() {
45 [ ! -d ${SALT_PILLAR_DIR} ] && mkdir -p ${SALT_PILLAR_DIR}
46 echo "base:" > ${SALT_PILLAR_DIR}/top.sls
47 for pillar in ${PILLARDIR}/*; do
Petr Michalec579e64d2017-03-24 12:54:29 +010048 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Jakub Pavlikc89ba902016-01-25 22:48:37 +010049 state_name=$(basename ${pillar%.sls})
50 echo -e " ${state_name}:\n - ${state_name}" >> ${SALT_PILLAR_DIR}/top.sls
51 done
52}
53
54setup_salt() {
55 [ ! -d ${SALT_FILE_DIR} ] && mkdir -p ${SALT_FILE_DIR}
56 [ ! -d ${SALT_CONFIG_DIR} ] && mkdir -p ${SALT_CONFIG_DIR}
57 [ ! -d ${SALT_CACHE_DIR} ] && mkdir -p ${SALT_CACHE_DIR}
58
59 echo "base:" > ${SALT_FILE_DIR}/top.sls
60 for pillar in ${PILLARDIR}/*.sls; do
Petr Michalec579e64d2017-03-24 12:54:29 +010061 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Jakub Pavlikc89ba902016-01-25 22:48:37 +010062 state_name=$(basename ${pillar%.sls})
63 echo -e " ${state_name}:\n - ${FORMULA_NAME}" >> ${SALT_FILE_DIR}/top.sls
64 done
65
66 cat << EOF > ${SALT_CONFIG_DIR}/minion
67file_client: local
68cachedir: ${SALT_CACHE_DIR}
69verify_env: False
Ondřej Novýc91488b2016-04-23 17:11:08 +020070minion_id_caching: False
Jakub Pavlikc89ba902016-01-25 22:48:37 +010071
72file_roots:
73 base:
74 - ${SALT_FILE_DIR}
75 - ${CURDIR}/..
Filip Pytlound63c5622016-04-14 11:51:09 +020076 - /usr/share/salt-formulas/env
Jakub Pavlikc89ba902016-01-25 22:48:37 +010077
78pillar_roots:
79 base:
80 - ${SALT_PILLAR_DIR}
81 - ${PILLARDIR}
82EOF
83}
84
85fetch_dependency() {
Filip Pytlound63c5622016-04-14 11:51:09 +020086 dep_name="$(echo $1|cut -d : -f 1)"
87 dep_source="$(echo $1|cut -d : -f 2-)"
88 dep_root="${DEPSDIR}/$(basename $dep_source .git)"
Jakub Pavlikc89ba902016-01-25 22:48:37 +010089 dep_metadata="${dep_root}/metadata.yml"
90
Filip Pytlound63c5622016-04-14 11:51:09 +020091 [ -d /usr/share/salt-formulas/env/${dep_name} ] && log_info "Dependency $dep_name already present in system-wide salt env" && return 0
92 [ -d $dep_root ] && log_info "Dependency $dep_name already fetched" && return 0
Jakub Pavlikc89ba902016-01-25 22:48:37 +010093
Filip Pytlound63c5622016-04-14 11:51:09 +020094 log_info "Fetching dependency $dep_name"
Jakub Pavlikc89ba902016-01-25 22:48:37 +010095 [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
Filip Pytlound63c5622016-04-14 11:51:09 +020096 git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git)
Jakub Pavlikc89ba902016-01-25 22:48:37 +010097 ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name}
98
99 METADATA="${dep_metadata}" install_dependencies
100}
101
102install_dependencies() {
103 grep -E "^dependencies:" ${METADATA} >/dev/null || return 0
104 (python - | while read dep; do fetch_dependency "$dep"; done) << EOF
105import sys,yaml
106for dep in yaml.load(open('${METADATA}', 'ro'))['dependencies']:
Filip Pytlound63c5622016-04-14 11:51:09 +0200107 print '%s:%s' % (dep["name"], dep["source"])
Jakub Pavlikc89ba902016-01-25 22:48:37 +0100108EOF
109}
110
111clean() {
112 log_info "Cleaning up ${BUILDDIR}"
113 [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0
114}
115
116salt_run() {
Jakub Josef168d8de2017-12-15 16:58:30 +0100117 [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
Jakub Josef8632e3a2018-02-08 13:22:19 +0100118 python $(which salt-call) ${SALT_OPTS} $*
Jakub Pavlikc89ba902016-01-25 22:48:37 +0100119}
120
121prepare() {
122 [ -d ${BUILDDIR} ] && mkdir -p ${BUILDDIR}
123
Filip Pytlound63c5622016-04-14 11:51:09 +0200124 which salt-call || setup_virtualenv
Jakub Pavlikc89ba902016-01-25 22:48:37 +0100125 setup_pillar
126 setup_salt
127 install_dependencies
128}
129
130run() {
131 for pillar in ${PILLARDIR}/*.sls; do
Petr Michalec579e64d2017-03-24 12:54:29 +0100132 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Jakub Pavlikc89ba902016-01-25 22:48:37 +0100133 state_name=$(basename ${pillar%.sls})
134 salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
135 done
136}
137
138_atexit() {
139 RETVAL=$?
140 trap true INT TERM EXIT
141
142 if [ $RETVAL -ne 0 ]; then
143 log_err "Execution failed"
144 else
145 log_info "Execution successful"
146 fi
147 return $RETVAL
148}
149
150## Main
151trap _atexit INT TERM EXIT
152
153case $1 in
154 clean)
155 clean
156 ;;
157 prepare)
158 prepare
159 ;;
160 run)
161 run
162 ;;
163 *)
164 prepare
165 run
166 ;;
167esac