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