jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
azvyagintsev | fd55503 | 2017-12-18 15:50:25 +0200 | [diff] [blame] | 3 | ### |
| 4 | # Script requirments: |
| 5 | # apt-get install -y python-yaml virtualenv git |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 6 | set -e |
| 7 | [ -n "$DEBUG" ] && set -x |
| 8 | |
| 9 | CURDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
| 10 | METADATA=${CURDIR}/../metadata.yml |
| 11 | FORMULA_NAME=$(cat $METADATA | python -c "import sys,yaml; print yaml.load(sys.stdin)['name']") |
Dennis Dmitriev | 4f6d8db | 2017-07-06 21:07:28 +0300 | [diff] [blame] | 12 | FORMULA_META_DIR=${CURDIR}/../${FORMULA_NAME}/meta |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 13 | |
| 14 | ## Overrideable parameters |
| 15 | PILLARDIR=${PILLARDIR:-${CURDIR}/pillar} |
| 16 | BUILDDIR=${BUILDDIR:-${CURDIR}/build} |
| 17 | VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv} |
Dennis Dmitriev | 4f6d8db | 2017-07-06 21:07:28 +0300 | [diff] [blame] | 18 | MOCK_BIN_DIR=${MOCK_BIN_DIR:-${CURDIR}/mock_bin} |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 19 | DEPSDIR=${BUILDDIR}/deps |
azvyagintsev | fd55503 | 2017-12-18 15:50:25 +0200 | [diff] [blame] | 20 | SCHEMARDIR=${SCHEMARDIR:-"${CURDIR}/../${FORMULA_NAME}/schemas/"} |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 21 | |
| 22 | SALT_FILE_DIR=${SALT_FILE_DIR:-${BUILDDIR}/file_root} |
| 23 | SALT_PILLAR_DIR=${SALT_PILLAR_DIR:-${BUILDDIR}/pillar_root} |
| 24 | SALT_CONFIG_DIR=${SALT_CONFIG_DIR:-${BUILDDIR}/salt} |
| 25 | SALT_CACHE_DIR=${SALT_CACHE_DIR:-${SALT_CONFIG_DIR}/cache} |
azvyagintsev | fd55503 | 2017-12-18 15:50:25 +0200 | [diff] [blame] | 26 | SALT_CACHE_EXTMODS_DIR=${SALT_CACHE_EXTMODS_DIR:-${SALT_CONFIG_DIR}/cache_master_extmods} |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 27 | |
Dennis Dmitriev | 4f6d8db | 2017-07-06 21:07:28 +0300 | [diff] [blame] | 28 | SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null" |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 29 | |
| 30 | if [ "x${SALT_VERSION}" != "x" ]; then |
| 31 | PIP_SALT_VERSION="==${SALT_VERSION}" |
| 32 | fi |
| 33 | |
| 34 | ## Functions |
| 35 | log_info() { |
azvyagintsev | fd55503 | 2017-12-18 15:50:25 +0200 | [diff] [blame] | 36 | echo -e "[INFO] $*" |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | log_err() { |
azvyagintsev | fd55503 | 2017-12-18 15:50:25 +0200 | [diff] [blame] | 40 | echo -e "[ERROR] $*" >&2 |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | setup_virtualenv() { |
| 44 | log_info "Setting up Python virtualenv" |
| 45 | virtualenv $VENV_DIR |
| 46 | source ${VENV_DIR}/bin/activate |
Jakub Josef | 5102039 | 2018-02-08 13:22:19 +0100 | [diff] [blame] | 47 | python -m pip install salt${PIP_SALT_VERSION} |
azvyagintsev | fd55503 | 2017-12-18 15:50:25 +0200 | [diff] [blame] | 48 | if [[ -f ${CURDIR}/test-requirements.txt ]]; then |
Jakub Josef | 5102039 | 2018-02-08 13:22:19 +0100 | [diff] [blame] | 49 | python -m pip install -r ${CURDIR}/test-requirements.txt |
azvyagintsev | fd55503 | 2017-12-18 15:50:25 +0200 | [diff] [blame] | 50 | fi |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 51 | } |
| 52 | |
Dennis Dmitriev | 4f6d8db | 2017-07-06 21:07:28 +0300 | [diff] [blame] | 53 | setup_mock_bin() { |
| 54 | # If some state requires a binary, a lightweight replacement for |
| 55 | # such binary can be put into MOCK_BIN_DIR for test purposes |
| 56 | if [ -d "${MOCK_BIN_DIR}" ]; then |
| 57 | PATH="${MOCK_BIN_DIR}:$PATH" |
| 58 | export PATH |
| 59 | fi |
| 60 | } |
| 61 | |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 62 | setup_pillar() { |
| 63 | [ ! -d ${SALT_PILLAR_DIR} ] && mkdir -p ${SALT_PILLAR_DIR} |
| 64 | echo "base:" > ${SALT_PILLAR_DIR}/top.sls |
| 65 | for pillar in ${PILLARDIR}/*; do |
Dennis Dmitriev | 4f6d8db | 2017-07-06 21:07:28 +0300 | [diff] [blame] | 66 | grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 67 | state_name=$(basename ${pillar%.sls}) |
| 68 | echo -e " ${state_name}:\n - ${state_name}" >> ${SALT_PILLAR_DIR}/top.sls |
| 69 | done |
| 70 | } |
| 71 | |
| 72 | setup_salt() { |
| 73 | [ ! -d ${SALT_FILE_DIR} ] && mkdir -p ${SALT_FILE_DIR} |
| 74 | [ ! -d ${SALT_CONFIG_DIR} ] && mkdir -p ${SALT_CONFIG_DIR} |
| 75 | [ ! -d ${SALT_CACHE_DIR} ] && mkdir -p ${SALT_CACHE_DIR} |
azvyagintsev | fd55503 | 2017-12-18 15:50:25 +0200 | [diff] [blame] | 76 | [ ! -d ${SALT_CACHE_EXTMODS_DIR} ] && mkdir -p ${SALT_CACHE_EXTMODS_DIR} |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 77 | |
| 78 | echo "base:" > ${SALT_FILE_DIR}/top.sls |
| 79 | for pillar in ${PILLARDIR}/*.sls; do |
Dennis Dmitriev | 4f6d8db | 2017-07-06 21:07:28 +0300 | [diff] [blame] | 80 | grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 81 | state_name=$(basename ${pillar%.sls}) |
| 82 | echo -e " ${state_name}:\n - ${FORMULA_NAME}" >> ${SALT_FILE_DIR}/top.sls |
| 83 | done |
| 84 | |
| 85 | cat << EOF > ${SALT_CONFIG_DIR}/minion |
| 86 | file_client: local |
| 87 | cachedir: ${SALT_CACHE_DIR} |
azvyagintsev | fd55503 | 2017-12-18 15:50:25 +0200 | [diff] [blame] | 88 | extension_modules: ${SALT_CACHE_EXTMODS_DIR} |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 89 | verify_env: False |
Dennis Dmitriev | 4f6d8db | 2017-07-06 21:07:28 +0300 | [diff] [blame] | 90 | minion_id_caching: False |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 91 | |
| 92 | file_roots: |
| 93 | base: |
| 94 | - ${SALT_FILE_DIR} |
| 95 | - ${CURDIR}/.. |
| 96 | |
| 97 | pillar_roots: |
| 98 | base: |
| 99 | - ${SALT_PILLAR_DIR} |
| 100 | - ${PILLARDIR} |
| 101 | EOF |
| 102 | } |
| 103 | |
| 104 | fetch_dependency() { |
azvyagintsev | fd55503 | 2017-12-18 15:50:25 +0200 | [diff] [blame] | 105 | # example: fetch_dependency "linux:https://github.com/salt-formulas/salt-formula-linux" |
Filip Pytloun | 57077e6 | 2016-04-14 11:51:09 +0200 | [diff] [blame] | 106 | dep_name="$(echo $1|cut -d : -f 1)" |
Filip Pytloun | f073488 | 2016-04-14 16:39:31 +0200 | [diff] [blame] | 107 | dep_source="$(echo $1|cut -d : -f 2-)" |
Filip Pytloun | 57077e6 | 2016-04-14 11:51:09 +0200 | [diff] [blame] | 108 | dep_root="${DEPSDIR}/$(basename $dep_source .git)" |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 109 | dep_metadata="${dep_root}/metadata.yml" |
| 110 | |
azvyagintsev | fd55503 | 2017-12-18 15:50:25 +0200 | [diff] [blame] | 111 | [ -d $dep_root ] && { log_info "Dependency $dep_name already fetched"; return 0; } |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 112 | |
Filip Pytloun | 57077e6 | 2016-04-14 11:51:09 +0200 | [diff] [blame] | 113 | log_info "Fetching dependency $dep_name" |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 114 | [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR} |
Filip Pytloun | 13fe158 | 2016-04-14 12:46:30 +0200 | [diff] [blame] | 115 | git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git) |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 116 | ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name} |
| 117 | |
| 118 | METADATA="${dep_metadata}" install_dependencies |
| 119 | } |
| 120 | |
azvyagintsev | fd55503 | 2017-12-18 15:50:25 +0200 | [diff] [blame] | 121 | link_modules(){ |
| 122 | # Link modules *.py files to temporary salt-root |
| 123 | local SALT_ROOT=${1:-$SALT_FILE_DIR} |
| 124 | local SALT_ENV=${2:-$DEPSDIR} |
| 125 | |
| 126 | mkdir -p "${SALT_ROOT}/_modules/" |
| 127 | # from git, development versions |
| 128 | find ${SALT_ENV} -maxdepth 3 -mindepth 3 -path '*_modules*' -iname "*.py" -type f -print0 | while read -d $'\0' file; do |
| 129 | ln -fs $(readlink -e ${file}) "$SALT_ROOT"/_modules/$(basename ${file}) ; |
| 130 | done |
| 131 | salt_run saltutil.sync_all |
| 132 | } |
| 133 | |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 134 | install_dependencies() { |
| 135 | grep -E "^dependencies:" ${METADATA} >/dev/null || return 0 |
| 136 | (python - | while read dep; do fetch_dependency "$dep"; done) << EOF |
| 137 | import sys,yaml |
| 138 | for dep in yaml.load(open('${METADATA}', 'ro'))['dependencies']: |
Filip Pytloun | 57077e6 | 2016-04-14 11:51:09 +0200 | [diff] [blame] | 139 | print '%s:%s' % (dep["name"], dep["source"]) |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 140 | EOF |
| 141 | } |
| 142 | |
| 143 | clean() { |
| 144 | log_info "Cleaning up ${BUILDDIR}" |
| 145 | [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0 |
| 146 | } |
| 147 | |
| 148 | salt_run() { |
azvyagintsev | fd55503 | 2017-12-18 15:50:25 +0200 | [diff] [blame] | 149 | [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate |
Jakub Josef | 5102039 | 2018-02-08 13:22:19 +0100 | [diff] [blame] | 150 | python $(which salt-call) ${SALT_OPTS} $* |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | prepare() { |
| 154 | [ -d ${BUILDDIR} ] && mkdir -p ${BUILDDIR} |
| 155 | |
azvyagintsev | fd55503 | 2017-12-18 15:50:25 +0200 | [diff] [blame] | 156 | [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv |
Dennis Dmitriev | 4f6d8db | 2017-07-06 21:07:28 +0300 | [diff] [blame] | 157 | setup_mock_bin |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 158 | setup_pillar |
| 159 | setup_salt |
| 160 | install_dependencies |
| 161 | } |
| 162 | |
azvyagintsev | fd55503 | 2017-12-18 15:50:25 +0200 | [diff] [blame] | 163 | lint_releasenotes() { |
| 164 | [[ ! -f "${VENV_DIR}/bin/activate" ]] && setup_virtualenv |
| 165 | source ${VENV_DIR}/bin/activate |
| 166 | reno lint ${CURDIR}/../ |
| 167 | } |
| 168 | |
| 169 | lint() { |
| 170 | # lint_releasenotes |
| 171 | log_err "TODO: lint_releasenotes" |
| 172 | } |
| 173 | |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 174 | run() { |
| 175 | for pillar in ${PILLARDIR}/*.sls; do |
Dennis Dmitriev | 4f6d8db | 2017-07-06 21:07:28 +0300 | [diff] [blame] | 176 | grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 177 | state_name=$(basename ${pillar%.sls}) |
Dennis Dmitriev | 4f6d8db | 2017-07-06 21:07:28 +0300 | [diff] [blame] | 178 | salt_run grains.set 'noservices' False force=True |
| 179 | |
| 180 | echo "Checking state ${FORMULA_NAME}.${state_name} ..." |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 181 | salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1) |
Dennis Dmitriev | 4f6d8db | 2017-07-06 21:07:28 +0300 | [diff] [blame] | 182 | |
| 183 | # Check that all files in 'meta' folder can be rendered using any valid pillar |
| 184 | for meta in `find ${FORMULA_META_DIR} -type f`; do |
| 185 | meta_name=$(basename ${meta}) |
| 186 | echo "Checking meta ${meta_name} ..." |
| 187 | salt_run --out=quiet --id=${state_name} cp.get_template ${meta} ${SALT_CACHE_DIR}/${meta_name} \ |
azvyagintsev | fd55503 | 2017-12-18 15:50:25 +0200 | [diff] [blame] | 188 | || { log_err "Failed to render meta ${meta} using pillar ${FORMULA_NAME}.${state_name}"; exit 1; } |
Dennis Dmitriev | 4f6d8db | 2017-07-06 21:07:28 +0300 | [diff] [blame] | 189 | cat ${SALT_CACHE_DIR}/${meta_name} |
| 190 | done |
| 191 | done |
| 192 | } |
| 193 | |
| 194 | real_run() { |
| 195 | for pillar in ${PILLARDIR}/*.sls; do |
| 196 | state_name=$(basename ${pillar%.sls}) |
azvyagintsev | fd55503 | 2017-12-18 15:50:25 +0200 | [diff] [blame] | 197 | salt_run --id=${state_name} state.sls ${FORMULA_NAME} || { log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1; } |
| 198 | done |
| 199 | } |
| 200 | |
| 201 | run_model_validate(){ |
| 202 | [[ -d ${SCHEMARDIR} ]] || { log_err "${SCHEMARDIR} not found!"; return 1; } |
| 203 | # model validator require py modules |
| 204 | fetch_dependency "salt:https://github.com/salt-formulas/salt-formula-salt" |
| 205 | link_modules |
| 206 | # Sync, afer dep's update |
| 207 | salt_run saltutil.clear_cache; salt_run saltutil.refresh_pillar; salt_run saltutil.sync_all |
| 208 | # Rendered Example: |
| 209 | # salt-call --local -c /test1/maas/tests/build/salt --id=maas_cluster modelschema.model_validate maas cluster |
| 210 | for role in ${SCHEMARDIR}/*.yaml; do |
| 211 | state_name=$(basename "${role%*.yaml}") |
| 212 | minion_id="${state_name}" |
| 213 | log_info "Processing model_validate for:${FORMULA_NAME} ${state_name} ${minion_id}" |
| 214 | salt_run --id=${minion_id} modelschema.model_validate ${FORMULA_NAME} ${state_name} || { log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1 ; } |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 215 | done |
| 216 | } |
| 217 | |
| 218 | _atexit() { |
| 219 | RETVAL=$? |
| 220 | trap true INT TERM EXIT |
| 221 | |
| 222 | if [ $RETVAL -ne 0 ]; then |
| 223 | log_err "Execution failed" |
| 224 | else |
| 225 | log_info "Execution successful" |
| 226 | fi |
| 227 | return $RETVAL |
| 228 | } |
| 229 | |
| 230 | ## Main |
| 231 | trap _atexit INT TERM EXIT |
| 232 | |
| 233 | case $1 in |
| 234 | clean) |
| 235 | clean |
| 236 | ;; |
| 237 | prepare) |
| 238 | prepare |
| 239 | ;; |
azvyagintsev | fd55503 | 2017-12-18 15:50:25 +0200 | [diff] [blame] | 240 | lint) |
| 241 | lint |
| 242 | ;; |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 243 | run) |
| 244 | run |
| 245 | ;; |
Dennis Dmitriev | 4f6d8db | 2017-07-06 21:07:28 +0300 | [diff] [blame] | 246 | real-run) |
| 247 | real_run |
| 248 | ;; |
azvyagintsev | fd55503 | 2017-12-18 15:50:25 +0200 | [diff] [blame] | 249 | model-validate) |
| 250 | prepare |
| 251 | run_model_validate |
| 252 | ;; |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 253 | *) |
| 254 | prepare |
azvyagintsev | fd55503 | 2017-12-18 15:50:25 +0200 | [diff] [blame] | 255 | # lint |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 256 | run |
azvyagintsev | fd55503 | 2017-12-18 15:50:25 +0200 | [diff] [blame] | 257 | run_model_validate |
jan kaufman | fd7a9ec | 2016-01-25 23:41:58 +0100 | [diff] [blame] | 258 | ;; |
| 259 | esac |