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