blob: 3f42101e04e95d35c8f4a2108de0fac5ce1090d5 [file] [log] [blame]
Jakub Pavlik4208c3f2016-06-14 11:21:14 +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}"
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
67
68file_roots:
69 base:
70 - ${SALT_FILE_DIR}
71 - ${CURDIR}/..
72 - /usr/share/salt-formulas/env
73
74pillar_roots:
75 base:
76 - ${SALT_PILLAR_DIR}
77 - ${PILLARDIR}
78EOF
79}
80
81fetch_dependency() {
82 dep_name="$(echo $1|cut -d : -f 1)"
83 dep_source="$(echo $1|cut -d : -f 2-)"
84 dep_root="${DEPSDIR}/$(basename $dep_source .git)"
85 dep_metadata="${dep_root}/metadata.yml"
86
87 [ -d /usr/share/salt-formulas/env/${dep_name} ] && log_info "Dependency $dep_name already present in system-wide salt env" && return 0
88 [ -d $dep_root ] && log_info "Dependency $dep_name already fetched" && return 0
89
90 log_info "Fetching dependency $dep_name"
91 [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
92 git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git)
93 ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name}
94
95 METADATA="${dep_metadata}" install_dependencies
96}
97
98install_dependencies() {
99 grep -E "^dependencies:" ${METADATA} >/dev/null || return 0
100 (python - | while read dep; do fetch_dependency "$dep"; done) << EOF
101import sys,yaml
102for dep in yaml.load(open('${METADATA}', 'ro'))['dependencies']:
103 print '%s:%s' % (dep["name"], dep["source"])
104EOF
105}
106
107clean() {
108 log_info "Cleaning up ${BUILDDIR}"
109 [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0
110}
111
112salt_run() {
113 [ -e ${VEN_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
114 salt-call ${SALT_OPTS} $*
115}
116
117prepare() {
118 [ -d ${BUILDDIR} ] && mkdir -p ${BUILDDIR}
119
120 which salt-call || setup_virtualenv
121 setup_pillar
122 setup_salt
123 install_dependencies
124}
125
126run() {
127 for pillar in ${PILLARDIR}/*.sls; do
128 state_name=$(basename ${pillar%.sls})
129 salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
130 done
131}
132
133_atexit() {
134 RETVAL=$?
135 trap true INT TERM EXIT
136
137 if [ $RETVAL -ne 0 ]; then
138 log_err "Execution failed"
139 else
140 log_info "Execution successful"
141 fi
142 return $RETVAL
143}
144
145## Main
146trap _atexit INT TERM EXIT
147
148case $1 in
149 clean)
150 clean
151 ;;
152 prepare)
153 prepare
154 ;;
155 run)
156 run
157 ;;
158 *)
159 prepare
160 run
161 ;;
162esac