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