blob: 688643f7fc79f55b5d1a58c78f55c866bf082b74 [file] [log] [blame]
marcodaa52fa2016-01-25 23:49:50 +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
21SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR}"
22
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ýe751ca52016-04-23 17:10:58 +020067minion_id_caching: False
marcodaa52fa2016-01-25 23:49:50 +010068
69file_roots:
70 base:
71 - ${SALT_FILE_DIR}
72 - ${CURDIR}/..
Filip Pytloun358220d2016-04-14 11:48:41 +020073 - /usr/share/salt-formulas/env
marcodaa52fa2016-01-25 23:49:50 +010074
75pillar_roots:
76 base:
77 - ${SALT_PILLAR_DIR}
78 - ${PILLARDIR}
79EOF
80}
81
82fetch_dependency() {
Filip Pytloun358220d2016-04-14 11:48:41 +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)"
marcodaa52fa2016-01-25 23:49:50 +010086 dep_metadata="${dep_root}/metadata.yml"
87
Filip Pytloun358220d2016-04-14 11:48:41 +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
marcodaa52fa2016-01-25 23:49:50 +010090
Filip Pytloun358220d2016-04-14 11:48:41 +020091 log_info "Fetching dependency $dep_name"
marcodaa52fa2016-01-25 23:49:50 +010092 [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
Filip Pytloun358220d2016-04-14 11:48:41 +020093 git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git)
marcodaa52fa2016-01-25 23:49:50 +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 Pytloun358220d2016-04-14 11:48:41 +0200104 print '%s:%s' % (dep["name"], dep["source"])
marcodaa52fa2016-01-25 23:49:50 +0100105EOF
106}
107
108clean() {
109 log_info "Cleaning up ${BUILDDIR}"
110 [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0
111}
112
113salt_run() {
Filip Pytloun358220d2016-04-14 11:48:41 +0200114 [ -e ${VEN_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
marcodaa52fa2016-01-25 23:49:50 +0100115 salt-call ${SALT_OPTS} $*
116}
117
118prepare() {
119 [ -d ${BUILDDIR} ] && mkdir -p ${BUILDDIR}
120
Filip Pytloun358220d2016-04-14 11:48:41 +0200121 which salt-call || setup_virtualenv
marcodaa52fa2016-01-25 23:49:50 +0100122 setup_pillar
123 setup_salt
124 install_dependencies
125}
126
127run() {
128 for pillar in ${PILLARDIR}/*.sls; do
Mykyta Karpin1d45fc82017-09-04 10:19:39 +0300129 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
marcodaa52fa2016-01-25 23:49:50 +0100130 state_name=$(basename ${pillar%.sls})
131 salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
132 done
133}
134
135_atexit() {
136 RETVAL=$?
137 trap true INT TERM EXIT
138
139 if [ $RETVAL -ne 0 ]; then
140 log_err "Execution failed"
141 else
142 log_info "Execution successful"
143 fi
144 return $RETVAL
145}
146
147## Main
148trap _atexit INT TERM EXIT
149
150case $1 in
151 clean)
152 clean
153 ;;
154 prepare)
155 prepare
156 ;;
157 run)
158 run
159 ;;
160 *)
161 prepare
162 run
163 ;;
164esac