Jakub Pavlik | c89ba90 | 2016-01-25 22:48:37 +0100 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | set -e |
| 4 | [ -n "$DEBUG" ] && set -x |
| 5 | |
| 6 | CURDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
| 7 | METADATA=${CURDIR}/../metadata.yml |
| 8 | FORMULA_NAME=$(cat $METADATA | python -c "import sys,yaml; print yaml.load(sys.stdin)['name']") |
| 9 | |
| 10 | ## Overrideable parameters |
| 11 | PILLARDIR=${PILLARDIR:-${CURDIR}/pillar} |
| 12 | BUILDDIR=${BUILDDIR:-${CURDIR}/build} |
| 13 | VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv} |
| 14 | DEPSDIR=${BUILDDIR}/deps |
| 15 | |
| 16 | SALT_FILE_DIR=${SALT_FILE_DIR:-${BUILDDIR}/file_root} |
| 17 | SALT_PILLAR_DIR=${SALT_PILLAR_DIR:-${BUILDDIR}/pillar_root} |
| 18 | SALT_CONFIG_DIR=${SALT_CONFIG_DIR:-${BUILDDIR}/salt} |
| 19 | SALT_CACHE_DIR=${SALT_CACHE_DIR:-${SALT_CONFIG_DIR}/cache} |
| 20 | |
Lukas Zmrzlik | 935e905 | 2016-06-22 14:57:52 +0200 | [diff] [blame] | 21 | SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null" |
Jakub Pavlik | c89ba90 | 2016-01-25 22:48:37 +0100 | [diff] [blame] | 22 | |
| 23 | if [ "x${SALT_VERSION}" != "x" ]; then |
| 24 | PIP_SALT_VERSION="==${SALT_VERSION}" |
| 25 | fi |
| 26 | |
| 27 | ## Functions |
| 28 | log_info() { |
| 29 | echo "[INFO] $*" |
| 30 | } |
| 31 | |
| 32 | log_err() { |
| 33 | echo "[ERROR] $*" >&2 |
| 34 | } |
| 35 | |
| 36 | setup_virtualenv() { |
| 37 | log_info "Setting up Python virtualenv" |
| 38 | virtualenv $VENV_DIR |
| 39 | source ${VENV_DIR}/bin/activate |
Jakub Josef | 8632e3a | 2018-02-08 13:22:19 +0100 | [diff] [blame] | 40 | python -m pip install salt${PIP_SALT_VERSION} |
Ksawery Dziekoński | 6d36f46 | 2020-04-22 13:20:16 +0200 | [diff] [blame] | 41 | python -m pip install 'msgpack<1.0.0' |
Jakub Pavlik | c89ba90 | 2016-01-25 22:48:37 +0100 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | setup_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 |
Petr Michalec | 579e64d | 2017-03-24 12:54:29 +0100 | [diff] [blame] | 48 | grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue |
Jakub Pavlik | c89ba90 | 2016-01-25 22:48:37 +0100 | [diff] [blame] | 49 | state_name=$(basename ${pillar%.sls}) |
| 50 | echo -e " ${state_name}:\n - ${state_name}" >> ${SALT_PILLAR_DIR}/top.sls |
| 51 | done |
| 52 | } |
| 53 | |
| 54 | setup_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 |
Petr Michalec | 579e64d | 2017-03-24 12:54:29 +0100 | [diff] [blame] | 61 | grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue |
Jakub Pavlik | c89ba90 | 2016-01-25 22:48:37 +0100 | [diff] [blame] | 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 |
| 67 | file_client: local |
| 68 | cachedir: ${SALT_CACHE_DIR} |
| 69 | verify_env: False |
Ondřej Nový | c91488b | 2016-04-23 17:11:08 +0200 | [diff] [blame] | 70 | minion_id_caching: False |
Jakub Pavlik | c89ba90 | 2016-01-25 22:48:37 +0100 | [diff] [blame] | 71 | |
| 72 | file_roots: |
| 73 | base: |
| 74 | - ${SALT_FILE_DIR} |
| 75 | - ${CURDIR}/.. |
Filip Pytloun | d63c562 | 2016-04-14 11:51:09 +0200 | [diff] [blame] | 76 | - /usr/share/salt-formulas/env |
Jakub Pavlik | c89ba90 | 2016-01-25 22:48:37 +0100 | [diff] [blame] | 77 | |
| 78 | pillar_roots: |
| 79 | base: |
| 80 | - ${SALT_PILLAR_DIR} |
| 81 | - ${PILLARDIR} |
| 82 | EOF |
| 83 | } |
| 84 | |
| 85 | fetch_dependency() { |
Filip Pytloun | d63c562 | 2016-04-14 11:51:09 +0200 | [diff] [blame] | 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)" |
Jakub Pavlik | c89ba90 | 2016-01-25 22:48:37 +0100 | [diff] [blame] | 89 | dep_metadata="${dep_root}/metadata.yml" |
| 90 | |
Filip Pytloun | d63c562 | 2016-04-14 11:51:09 +0200 | [diff] [blame] | 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 |
Jakub Pavlik | c89ba90 | 2016-01-25 22:48:37 +0100 | [diff] [blame] | 93 | |
Filip Pytloun | d63c562 | 2016-04-14 11:51:09 +0200 | [diff] [blame] | 94 | log_info "Fetching dependency $dep_name" |
Jakub Pavlik | c89ba90 | 2016-01-25 22:48:37 +0100 | [diff] [blame] | 95 | [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR} |
Filip Pytloun | d63c562 | 2016-04-14 11:51:09 +0200 | [diff] [blame] | 96 | git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git) |
Jakub Pavlik | c89ba90 | 2016-01-25 22:48:37 +0100 | [diff] [blame] | 97 | ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name} |
| 98 | |
| 99 | METADATA="${dep_metadata}" install_dependencies |
| 100 | } |
| 101 | |
| 102 | install_dependencies() { |
| 103 | grep -E "^dependencies:" ${METADATA} >/dev/null || return 0 |
| 104 | (python - | while read dep; do fetch_dependency "$dep"; done) << EOF |
| 105 | import sys,yaml |
| 106 | for dep in yaml.load(open('${METADATA}', 'ro'))['dependencies']: |
Filip Pytloun | d63c562 | 2016-04-14 11:51:09 +0200 | [diff] [blame] | 107 | print '%s:%s' % (dep["name"], dep["source"]) |
Jakub Pavlik | c89ba90 | 2016-01-25 22:48:37 +0100 | [diff] [blame] | 108 | EOF |
| 109 | } |
| 110 | |
| 111 | clean() { |
| 112 | log_info "Cleaning up ${BUILDDIR}" |
| 113 | [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0 |
| 114 | } |
| 115 | |
| 116 | salt_run() { |
Jakub Josef | 168d8de | 2017-12-15 16:58:30 +0100 | [diff] [blame] | 117 | [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate |
Jakub Josef | 8632e3a | 2018-02-08 13:22:19 +0100 | [diff] [blame] | 118 | python $(which salt-call) ${SALT_OPTS} $* |
Jakub Pavlik | c89ba90 | 2016-01-25 22:48:37 +0100 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | prepare() { |
| 122 | [ -d ${BUILDDIR} ] && mkdir -p ${BUILDDIR} |
| 123 | |
Filip Pytloun | d63c562 | 2016-04-14 11:51:09 +0200 | [diff] [blame] | 124 | which salt-call || setup_virtualenv |
Jakub Pavlik | c89ba90 | 2016-01-25 22:48:37 +0100 | [diff] [blame] | 125 | setup_pillar |
| 126 | setup_salt |
| 127 | install_dependencies |
| 128 | } |
| 129 | |
| 130 | run() { |
| 131 | for pillar in ${PILLARDIR}/*.sls; do |
Petr Michalec | 579e64d | 2017-03-24 12:54:29 +0100 | [diff] [blame] | 132 | grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue |
Jakub Pavlik | c89ba90 | 2016-01-25 22:48:37 +0100 | [diff] [blame] | 133 | state_name=$(basename ${pillar%.sls}) |
| 134 | salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1) |
| 135 | done |
| 136 | } |
| 137 | |
| 138 | _atexit() { |
| 139 | RETVAL=$? |
| 140 | trap true INT TERM EXIT |
| 141 | |
| 142 | if [ $RETVAL -ne 0 ]; then |
| 143 | log_err "Execution failed" |
| 144 | else |
| 145 | log_info "Execution successful" |
| 146 | fi |
| 147 | return $RETVAL |
| 148 | } |
| 149 | |
| 150 | ## Main |
| 151 | trap _atexit INT TERM EXIT |
| 152 | |
| 153 | case $1 in |
| 154 | clean) |
| 155 | clean |
| 156 | ;; |
| 157 | prepare) |
| 158 | prepare |
| 159 | ;; |
| 160 | run) |
| 161 | run |
| 162 | ;; |
| 163 | *) |
| 164 | prepare |
| 165 | run |
| 166 | ;; |
| 167 | esac |