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