blob: a4cac88132e1eba3bf1888e7dcbaf2da230ba069 [file] [log] [blame]
Ivan Suzdal67bcad52017-05-26 18:45:54 +03001#!/usr/bin/env bash
2
3set -e
4[ -n "$DEBUG" ] && set -x
5
6CURDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
7METADATA=${CURDIR}/../metadata.yml
Mykyta Karpin00bbbb42017-07-03 17:48:53 +03008FORMULA_NAME=$(cat $METADATA | python -c "import sys,yaml; print yaml.load(sys.stdin)['name']")
Ivan Suzdal67bcad52017-05-26 18:45:54 +03009
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
Mykyta Karpin00bbbb42017-07-03 17:48:53 +030021SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null"
Ivan Suzdal67bcad52017-05-26 18:45:54 +030022
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
Mykyta Karpin00bbbb42017-07-03 17:48:53 +030047 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Ivan Suzdal67bcad52017-05-26 18:45:54 +030048 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
Mykyta Karpin00bbbb42017-07-03 17:48:53 +030060 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Ivan Suzdal67bcad52017-05-26 18:45:54 +030061 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
Mykyta Karpin00bbbb42017-07-03 17:48:53 +030069minion_id_caching: False
Ivan Suzdal67bcad52017-05-26 18:45:54 +030070
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
Mykyta Karpin00bbbb42017-07-03 17:48:53 +030091 [ -d $dep_root ] && log_info "Dependency $dep_name already fetched" && return 0
Ivan Suzdal67bcad52017-05-26 18:45:54 +030092
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() {
116 [ -e ${VEN_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
117 salt-call ${SALT_OPTS} $*
118}
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
Mykyta Karpin00bbbb42017-07-03 17:48:53 +0300131 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
Ivan Suzdal67bcad52017-05-26 18:45:54 +0300132 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