marco | 836d955 | 2016-01-25 22:22:46 +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']") |
Oleksandr Shyshko | 26a9206 | 2018-08-01 14:18:14 +0300 | [diff] [blame] | 9 | FORMULA_META_DIR=${CURDIR}/../${FORMULA_NAME}/meta |
marco | 836d955 | 2016-01-25 22:22:46 +0100 | [diff] [blame] | 10 | |
| 11 | ## Overrideable parameters |
| 12 | PILLARDIR=${PILLARDIR:-${CURDIR}/pillar} |
| 13 | BUILDDIR=${BUILDDIR:-${CURDIR}/build} |
| 14 | VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv} |
Oleksandr Shyshko | 26a9206 | 2018-08-01 14:18:14 +0300 | [diff] [blame] | 15 | MOCK_BIN_DIR=${MOCK_BIN_DIR:-${CURDIR}/mock_bin} |
marco | 836d955 | 2016-01-25 22:22:46 +0100 | [diff] [blame] | 16 | DEPSDIR=${BUILDDIR}/deps |
| 17 | |
| 18 | SALT_FILE_DIR=${SALT_FILE_DIR:-${BUILDDIR}/file_root} |
| 19 | SALT_PILLAR_DIR=${SALT_PILLAR_DIR:-${BUILDDIR}/pillar_root} |
| 20 | SALT_CONFIG_DIR=${SALT_CONFIG_DIR:-${BUILDDIR}/salt} |
| 21 | SALT_CACHE_DIR=${SALT_CACHE_DIR:-${SALT_CONFIG_DIR}/cache} |
| 22 | |
Simak, Jan | a072789 | 2016-06-23 13:51:02 +0200 | [diff] [blame] | 23 | SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null" |
marco | 836d955 | 2016-01-25 22:22:46 +0100 | [diff] [blame] | 24 | |
| 25 | if [ "x${SALT_VERSION}" != "x" ]; then |
| 26 | PIP_SALT_VERSION="==${SALT_VERSION}" |
| 27 | fi |
| 28 | |
| 29 | ## Functions |
| 30 | log_info() { |
| 31 | echo "[INFO] $*" |
| 32 | } |
| 33 | |
| 34 | log_err() { |
| 35 | echo "[ERROR] $*" >&2 |
| 36 | } |
| 37 | |
| 38 | setup_virtualenv() { |
| 39 | log_info "Setting up Python virtualenv" |
| 40 | virtualenv $VENV_DIR |
| 41 | source ${VENV_DIR}/bin/activate |
Jakub Josef | 285612d | 2018-02-08 13:22:16 +0100 | [diff] [blame] | 42 | python -m pip install salt${PIP_SALT_VERSION} |
Ksawery Dziekoński | 435edce | 2020-02-28 15:51:43 +0100 | [diff] [blame^] | 43 | python -m pip install 'msgpack<1.0.0' |
marco | 836d955 | 2016-01-25 22:22:46 +0100 | [diff] [blame] | 44 | } |
| 45 | |
Oleksandr Shyshko | 26a9206 | 2018-08-01 14:18:14 +0300 | [diff] [blame] | 46 | setup_mock_bin() { |
| 47 | # If some state requires a binary, a lightweight replacement for |
| 48 | # such binary can be put into MOCK_BIN_DIR for test purposes |
| 49 | if [ -d "${MOCK_BIN_DIR}" ]; then |
| 50 | PATH="${MOCK_BIN_DIR}:$PATH" |
| 51 | export PATH |
| 52 | fi |
| 53 | } |
| 54 | |
marco | 836d955 | 2016-01-25 22:22:46 +0100 | [diff] [blame] | 55 | setup_pillar() { |
| 56 | [ ! -d ${SALT_PILLAR_DIR} ] && mkdir -p ${SALT_PILLAR_DIR} |
| 57 | echo "base:" > ${SALT_PILLAR_DIR}/top.sls |
| 58 | for pillar in ${PILLARDIR}/*; do |
Oleksandr Shyshko | 26a9206 | 2018-08-01 14:18:14 +0300 | [diff] [blame] | 59 | grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue |
marco | 836d955 | 2016-01-25 22:22:46 +0100 | [diff] [blame] | 60 | state_name=$(basename ${pillar%.sls}) |
| 61 | echo -e " ${state_name}:\n - ${state_name}" >> ${SALT_PILLAR_DIR}/top.sls |
| 62 | done |
| 63 | } |
| 64 | |
| 65 | setup_salt() { |
| 66 | [ ! -d ${SALT_FILE_DIR} ] && mkdir -p ${SALT_FILE_DIR} |
| 67 | [ ! -d ${SALT_CONFIG_DIR} ] && mkdir -p ${SALT_CONFIG_DIR} |
| 68 | [ ! -d ${SALT_CACHE_DIR} ] && mkdir -p ${SALT_CACHE_DIR} |
| 69 | |
| 70 | echo "base:" > ${SALT_FILE_DIR}/top.sls |
| 71 | for pillar in ${PILLARDIR}/*.sls; do |
Oleksandr Shyshko | 26a9206 | 2018-08-01 14:18:14 +0300 | [diff] [blame] | 72 | grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue |
marco | 836d955 | 2016-01-25 22:22:46 +0100 | [diff] [blame] | 73 | state_name=$(basename ${pillar%.sls}) |
| 74 | echo -e " ${state_name}:\n - ${FORMULA_NAME}" >> ${SALT_FILE_DIR}/top.sls |
| 75 | done |
| 76 | |
| 77 | cat << EOF > ${SALT_CONFIG_DIR}/minion |
| 78 | file_client: local |
| 79 | cachedir: ${SALT_CACHE_DIR} |
| 80 | verify_env: False |
Ondřej Nový | dea6e97 | 2016-04-23 17:11:02 +0200 | [diff] [blame] | 81 | minion_id_caching: False |
marco | 836d955 | 2016-01-25 22:22:46 +0100 | [diff] [blame] | 82 | |
| 83 | file_roots: |
| 84 | base: |
| 85 | - ${SALT_FILE_DIR} |
| 86 | - ${CURDIR}/.. |
Filip Pytloun | 70d116c | 2016-04-14 11:51:09 +0200 | [diff] [blame] | 87 | - /usr/share/salt-formulas/env |
marco | 836d955 | 2016-01-25 22:22:46 +0100 | [diff] [blame] | 88 | |
| 89 | pillar_roots: |
| 90 | base: |
| 91 | - ${SALT_PILLAR_DIR} |
| 92 | - ${PILLARDIR} |
| 93 | EOF |
| 94 | } |
| 95 | |
| 96 | fetch_dependency() { |
Filip Pytloun | 70d116c | 2016-04-14 11:51:09 +0200 | [diff] [blame] | 97 | dep_name="$(echo $1|cut -d : -f 1)" |
| 98 | dep_source="$(echo $1|cut -d : -f 2-)" |
| 99 | dep_root="${DEPSDIR}/$(basename $dep_source .git)" |
marco | 836d955 | 2016-01-25 22:22:46 +0100 | [diff] [blame] | 100 | dep_metadata="${dep_root}/metadata.yml" |
| 101 | |
Filip Pytloun | 70d116c | 2016-04-14 11:51:09 +0200 | [diff] [blame] | 102 | [ -d /usr/share/salt-formulas/env/${dep_name} ] && log_info "Dependency $dep_name already present in system-wide salt env" && return 0 |
| 103 | [ -d $dep_root ] && log_info "Dependency $dep_name already fetched" && return 0 |
marco | 836d955 | 2016-01-25 22:22:46 +0100 | [diff] [blame] | 104 | |
Filip Pytloun | 70d116c | 2016-04-14 11:51:09 +0200 | [diff] [blame] | 105 | log_info "Fetching dependency $dep_name" |
marco | 836d955 | 2016-01-25 22:22:46 +0100 | [diff] [blame] | 106 | [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR} |
Filip Pytloun | 70d116c | 2016-04-14 11:51:09 +0200 | [diff] [blame] | 107 | git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git) |
marco | 836d955 | 2016-01-25 22:22:46 +0100 | [diff] [blame] | 108 | ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name} |
| 109 | |
| 110 | METADATA="${dep_metadata}" install_dependencies |
| 111 | } |
| 112 | |
| 113 | install_dependencies() { |
| 114 | grep -E "^dependencies:" ${METADATA} >/dev/null || return 0 |
| 115 | (python - | while read dep; do fetch_dependency "$dep"; done) << EOF |
| 116 | import sys,yaml |
| 117 | for dep in yaml.load(open('${METADATA}', 'ro'))['dependencies']: |
Filip Pytloun | 70d116c | 2016-04-14 11:51:09 +0200 | [diff] [blame] | 118 | print '%s:%s' % (dep["name"], dep["source"]) |
marco | 836d955 | 2016-01-25 22:22:46 +0100 | [diff] [blame] | 119 | EOF |
| 120 | } |
| 121 | |
| 122 | clean() { |
| 123 | log_info "Cleaning up ${BUILDDIR}" |
| 124 | [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0 |
| 125 | } |
| 126 | |
| 127 | salt_run() { |
Jakub Josef | 97b4eeb | 2017-12-15 16:58:29 +0100 | [diff] [blame] | 128 | [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate |
Jakub Josef | 285612d | 2018-02-08 13:22:16 +0100 | [diff] [blame] | 129 | python $(which salt-call) ${SALT_OPTS} $* |
marco | 836d955 | 2016-01-25 22:22:46 +0100 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | prepare() { |
| 133 | [ -d ${BUILDDIR} ] && mkdir -p ${BUILDDIR} |
| 134 | |
Filip Pytloun | 70d116c | 2016-04-14 11:51:09 +0200 | [diff] [blame] | 135 | which salt-call || setup_virtualenv |
Oleksandr Shyshko | 26a9206 | 2018-08-01 14:18:14 +0300 | [diff] [blame] | 136 | setup_mock_bin |
marco | 836d955 | 2016-01-25 22:22:46 +0100 | [diff] [blame] | 137 | setup_pillar |
| 138 | setup_salt |
| 139 | install_dependencies |
| 140 | } |
| 141 | |
| 142 | run() { |
| 143 | for pillar in ${PILLARDIR}/*.sls; do |
Oleksandr Shyshko | 26a9206 | 2018-08-01 14:18:14 +0300 | [diff] [blame] | 144 | grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue |
marco | 836d955 | 2016-01-25 22:22:46 +0100 | [diff] [blame] | 145 | state_name=$(basename ${pillar%.sls}) |
Oleksandr Shyshko | 26a9206 | 2018-08-01 14:18:14 +0300 | [diff] [blame] | 146 | salt_run grains.set 'noservices' False force=True |
| 147 | |
| 148 | echo "Checking state ${FORMULA_NAME}.${state_name} ..." |
marco | 836d955 | 2016-01-25 22:22:46 +0100 | [diff] [blame] | 149 | salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1) |
Oleksandr Shyshko | 26a9206 | 2018-08-01 14:18:14 +0300 | [diff] [blame] | 150 | |
| 151 | # Check that all files in 'meta' folder can be rendered using any valid pillar |
| 152 | for meta in `find ${FORMULA_META_DIR} -type f`; do |
| 153 | meta_name=$(basename ${meta}) |
| 154 | echo "Checking meta ${meta_name} ..." |
| 155 | salt_run --out=quiet --id=${state_name} cp.get_template ${meta} ${SALT_CACHE_DIR}/${meta_name} \ |
| 156 | || (log_err "Failed to render meta ${meta} using pillar ${FORMULA_NAME}.${state_name}"; exit 1) |
| 157 | cat ${SALT_CACHE_DIR}/${meta_name} |
| 158 | done |
| 159 | done |
| 160 | } |
| 161 | |
| 162 | real_run() { |
| 163 | for pillar in ${PILLARDIR}/*.sls; do |
| 164 | state_name=$(basename ${pillar%.sls}) |
| 165 | salt_run --id=${state_name} state.sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1) |
marco | 836d955 | 2016-01-25 22:22:46 +0100 | [diff] [blame] | 166 | done |
| 167 | } |
| 168 | |
| 169 | _atexit() { |
| 170 | RETVAL=$? |
| 171 | trap true INT TERM EXIT |
| 172 | |
| 173 | if [ $RETVAL -ne 0 ]; then |
| 174 | log_err "Execution failed" |
| 175 | else |
| 176 | log_info "Execution successful" |
| 177 | fi |
| 178 | return $RETVAL |
| 179 | } |
| 180 | |
| 181 | ## Main |
| 182 | trap _atexit INT TERM EXIT |
| 183 | |
| 184 | case $1 in |
| 185 | clean) |
| 186 | clean |
| 187 | ;; |
| 188 | prepare) |
| 189 | prepare |
| 190 | ;; |
| 191 | run) |
| 192 | run |
| 193 | ;; |
Oleksandr Shyshko | 26a9206 | 2018-08-01 14:18:14 +0300 | [diff] [blame] | 194 | real-run) |
| 195 | real_run |
| 196 | ;; |
marco | 836d955 | 2016-01-25 22:22:46 +0100 | [diff] [blame] | 197 | *) |
| 198 | prepare |
| 199 | run |
| 200 | ;; |
| 201 | esac |