blob: ea1a3638e19f2ebe8162bb53083ab4bcacddf373 [file] [log] [blame]
marco836d9552016-01-25 22:22:46 +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
Simak, Jana0727892016-06-23 13:51:02 +020021SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null"
marco836d9552016-01-25 22:22:46 +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
40 pip install salt${PIP_SALT_VERSION}
41}
42
43setup_pillar() {
44 [ ! -d ${SALT_PILLAR_DIR} ] && mkdir -p ${SALT_PILLAR_DIR}
45 echo "base:" > ${SALT_PILLAR_DIR}/top.sls
46 for pillar in ${PILLARDIR}/*; do
47 state_name=$(basename ${pillar%.sls})
48 echo -e " ${state_name}:\n - ${state_name}" >> ${SALT_PILLAR_DIR}/top.sls
49 done
50}
51
52setup_salt() {
53 [ ! -d ${SALT_FILE_DIR} ] && mkdir -p ${SALT_FILE_DIR}
54 [ ! -d ${SALT_CONFIG_DIR} ] && mkdir -p ${SALT_CONFIG_DIR}
55 [ ! -d ${SALT_CACHE_DIR} ] && mkdir -p ${SALT_CACHE_DIR}
56
57 echo "base:" > ${SALT_FILE_DIR}/top.sls
58 for pillar in ${PILLARDIR}/*.sls; do
59 state_name=$(basename ${pillar%.sls})
60 echo -e " ${state_name}:\n - ${FORMULA_NAME}" >> ${SALT_FILE_DIR}/top.sls
61 done
62
63 cat << EOF > ${SALT_CONFIG_DIR}/minion
64file_client: local
65cachedir: ${SALT_CACHE_DIR}
66verify_env: False
Ondřej Novýdea6e972016-04-23 17:11:02 +020067minion_id_caching: False
marco836d9552016-01-25 22:22:46 +010068
69file_roots:
70 base:
71 - ${SALT_FILE_DIR}
72 - ${CURDIR}/..
Filip Pytloun70d116c2016-04-14 11:51:09 +020073 - /usr/share/salt-formulas/env
marco836d9552016-01-25 22:22:46 +010074
75pillar_roots:
76 base:
77 - ${SALT_PILLAR_DIR}
78 - ${PILLARDIR}
79EOF
80}
81
82fetch_dependency() {
Filip Pytloun70d116c2016-04-14 11:51:09 +020083 dep_name="$(echo $1|cut -d : -f 1)"
84 dep_source="$(echo $1|cut -d : -f 2-)"
85 dep_root="${DEPSDIR}/$(basename $dep_source .git)"
marco836d9552016-01-25 22:22:46 +010086 dep_metadata="${dep_root}/metadata.yml"
87
Filip Pytloun70d116c2016-04-14 11:51:09 +020088 [ -d /usr/share/salt-formulas/env/${dep_name} ] && log_info "Dependency $dep_name already present in system-wide salt env" && return 0
89 [ -d $dep_root ] && log_info "Dependency $dep_name already fetched" && return 0
marco836d9552016-01-25 22:22:46 +010090
Filip Pytloun70d116c2016-04-14 11:51:09 +020091 log_info "Fetching dependency $dep_name"
marco836d9552016-01-25 22:22:46 +010092 [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
Filip Pytloun70d116c2016-04-14 11:51:09 +020093 git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git)
marco836d9552016-01-25 22:22:46 +010094 ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name}
95
96 METADATA="${dep_metadata}" install_dependencies
97}
98
99install_dependencies() {
100 grep -E "^dependencies:" ${METADATA} >/dev/null || return 0
101 (python - | while read dep; do fetch_dependency "$dep"; done) << EOF
102import sys,yaml
103for dep in yaml.load(open('${METADATA}', 'ro'))['dependencies']:
Filip Pytloun70d116c2016-04-14 11:51:09 +0200104 print '%s:%s' % (dep["name"], dep["source"])
marco836d9552016-01-25 22:22:46 +0100105EOF
106}
107
108clean() {
109 log_info "Cleaning up ${BUILDDIR}"
110 [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0
111}
112
113salt_run() {
Jakub Josef97b4eeb2017-12-15 16:58:29 +0100114 [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
marco836d9552016-01-25 22:22:46 +0100115 salt-call ${SALT_OPTS} $*
116}
117
118prepare() {
119 [ -d ${BUILDDIR} ] && mkdir -p ${BUILDDIR}
120
Filip Pytloun70d116c2016-04-14 11:51:09 +0200121 which salt-call || setup_virtualenv
marco836d9552016-01-25 22:22:46 +0100122 setup_pillar
123 setup_salt
124 install_dependencies
125}
126
127run() {
128 for pillar in ${PILLARDIR}/*.sls; do
129 state_name=$(basename ${pillar%.sls})
130 salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
131 done
132}
133
134_atexit() {
135 RETVAL=$?
136 trap true INT TERM EXIT
137
138 if [ $RETVAL -ne 0 ]; then
139 log_err "Execution failed"
140 else
141 log_info "Execution successful"
142 fi
143 return $RETVAL
144}
145
146## Main
147trap _atexit INT TERM EXIT
148
149case $1 in
150 clean)
151 clean
152 ;;
153 prepare)
154 prepare
155 ;;
156 run)
157 run
158 ;;
159 *)
160 prepare
161 run
162 ;;
163esac