blob: dbc436e5f93c52ae146e040e1d6944bad8d0f0a3 [file] [log] [blame]
Elena Ezhovaa3a43232017-06-02 17:53:00 +04001#!/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}
Ann Taraday32e396f2020-03-17 16:59:33 +040041 pip install 'msgpack<1.0.0'
Elena Ezhovaa3a43232017-06-02 17:53:00 +040042}
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
48 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
49 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
61 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
62 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
70minion_id_caching: False
71
72file_roots:
73 base:
74 - ${SALT_FILE_DIR}
75 - ${CURDIR}/..
76 - /usr/share/salt-formulas/env
77
78pillar_roots:
79 base:
80 - ${SALT_PILLAR_DIR}
81 - ${PILLARDIR}
82EOF
83}
84
85fetch_dependency() {
86 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)"
89 dep_metadata="${dep_root}/metadata.yml"
90
91 [ -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
93
94 log_info "Fetching dependency $dep_name"
95 [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
96 git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git)
97 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']:
107 print '%s:%s' % (dep["name"], dep["source"])
108EOF
109}
110
111clean() {
112 log_info "Cleaning up ${BUILDDIR}"
113 [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0
114}
115
116salt_run() {
117 [ -e ${VEN_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
118 salt-call ${SALT_OPTS} $*
119}
120
121prepare() {
122 [ -d ${BUILDDIR} ] && mkdir -p ${BUILDDIR}
123
124 which salt-call || setup_virtualenv
125 setup_pillar
126 setup_salt
127 install_dependencies
128}
129
130run() {
131 for pillar in ${PILLARDIR}/*.sls; do
132 grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
133 state_name=$(basename ${pillar%.sls})
134 salt_run grains.set 'noservices' False force=True
135 salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
136 done
137}
138
139_atexit() {
140 RETVAL=$?
141 trap true INT TERM EXIT
142
143 if [ $RETVAL -ne 0 ]; then
144 log_err "Execution failed"
145 else
146 log_info "Execution successful"
147 fi
148 return $RETVAL
149}
150
151## Main
152trap _atexit INT TERM EXIT
153
154case $1 in
155 clean)
156 clean
157 ;;
158 prepare)
159 prepare
160 ;;
161 run)
162 run
163 ;;
164 *)
165 prepare
166 run
167 ;;
168esac