blob: a600206698768a59825f3952cd708684abbe8c5d [file] [log] [blame]
Martin Polreich21f23142017-05-16 11:03:46 +02001#!/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} --log-file=/dev/null"
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
Jakub Josef40845022018-02-08 13:22:13 +010040 python -m pip install salt${PIP_SALT_VERSION}
Martin Polreich21f23142017-05-16 11:03:46 +020041}
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 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
48 state_name=$(basename ${pillar%.sls})
49 echo -e " ${state_name}:\n - ${state_name}" >> ${SALT_PILLAR_DIR}/top.sls
50 done
51}
52
53setup_salt() {
54 [ ! -d ${SALT_FILE_DIR} ] && mkdir -p ${SALT_FILE_DIR}
55 [ ! -d ${SALT_CONFIG_DIR} ] && mkdir -p ${SALT_CONFIG_DIR}
56 [ ! -d ${SALT_CACHE_DIR} ] && mkdir -p ${SALT_CACHE_DIR}
57
58 echo "base:" > ${SALT_FILE_DIR}/top.sls
59 for pillar in ${PILLARDIR}/*.sls; do
60 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
61 state_name=$(basename ${pillar%.sls})
62 echo -e " ${state_name}:\n - ${FORMULA_NAME}" >> ${SALT_FILE_DIR}/top.sls
63 done
64
65 cat << EOF > ${SALT_CONFIG_DIR}/minion
66file_client: local
67cachedir: ${SALT_CACHE_DIR}
68verify_env: False
69minion_id_caching: False
70
71file_roots:
72 base:
73 - ${SALT_FILE_DIR}
74 - ${CURDIR}/..
75 - /usr/share/salt-formulas/env
76
77pillar_roots:
78 base:
79 - ${SALT_PILLAR_DIR}
80 - ${PILLARDIR}
81EOF
82}
83
84fetch_dependency() {
85 dep_name="$(echo $1|cut -d : -f 1)"
86 dep_source="$(echo $1|cut -d : -f 2-)"
87 dep_root="${DEPSDIR}/$(basename $dep_source .git)"
88 dep_metadata="${dep_root}/metadata.yml"
89
90 [ -d /usr/share/salt-formulas/env/${dep_name} ] && log_info "Dependency $dep_name already present in system-wide salt env" && return 0
91 [ -d $dep_root ] && log_info "Dependency $dep_name already fetched" && return 0
92
93 log_info "Fetching dependency $dep_name"
94 [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
95 git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git)
96 ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name}
97
98 METADATA="${dep_metadata}" install_dependencies
99}
100
101install_dependencies() {
102 grep -E "^dependencies:" ${METADATA} >/dev/null || return 0
103 (python - | while read dep; do fetch_dependency "$dep"; done) << EOF
104import sys,yaml
105for dep in yaml.load(open('${METADATA}', 'ro'))['dependencies']:
106 print '%s:%s' % (dep["name"], dep["source"])
107EOF
108}
109
110clean() {
111 log_info "Cleaning up ${BUILDDIR}"
112 [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0
113}
114
115salt_run() {
Jakub Josefdae63e72017-12-15 16:58:28 +0100116 [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
Jakub Josef40845022018-02-08 13:22:13 +0100117 python $(which salt-call) ${SALT_OPTS} $*
Martin Polreich21f23142017-05-16 11:03:46 +0200118}
119
120prepare() {
121 [ -d ${BUILDDIR} ] && mkdir -p ${BUILDDIR}
122
123 which salt-call || setup_virtualenv
124 setup_pillar
125 setup_salt
126 install_dependencies
127}
128
129run() {
130 for pillar in ${PILLARDIR}/*.sls; do
131 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
132 state_name=$(basename ${pillar%.sls})
133 salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
134 done
135}
136
137_atexit() {
138 RETVAL=$?
139 trap true INT TERM EXIT
140
141 if [ $RETVAL -ne 0 ]; then
142 log_err "Execution failed"
143 else
144 log_info "Execution successful"
145 fi
146 return $RETVAL
147}
148
149## Main
150trap _atexit INT TERM EXIT
151
152case $1 in
153 clean)
154 clean
155 ;;
156 prepare)
157 prepare
158 ;;
159 run)
160 run
161 ;;
162 *)
163 prepare
164 run
165 ;;
166esac