Oleh Hryhorov | 3b72bf5 | 2018-05-18 16:23:04 +0300 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
Oleh Hryhorov | 3b72bf5 | 2018-05-18 16:23:04 +0300 | [diff] [blame] | 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']") |
Oleh Hryhorov | 3b72bf5 | 2018-05-18 16:23:04 +0300 | [diff] [blame] | 9 | |
| 10 | ## Overrideable parameters |
| 11 | PILLARDIR=${PILLARDIR:-${CURDIR}/pillar} |
| 12 | BUILDDIR=${BUILDDIR:-${CURDIR}/build} |
| 13 | VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv} |
Oleh Hryhorov | 3b72bf5 | 2018-05-18 16:23:04 +0300 | [diff] [blame] | 14 | DEPSDIR=${BUILDDIR}/deps |
Oleh Hryhorov | 3b72bf5 | 2018-05-18 16:23:04 +0300 | [diff] [blame] | 15 | |
| 16 | SALT_FILE_DIR=${SALT_FILE_DIR:-${BUILDDIR}/file_root} |
| 17 | SALT_PILLAR_DIR=${SALT_PILLAR_DIR:-${BUILDDIR}/pillar_root} |
| 18 | SALT_CONFIG_DIR=${SALT_CONFIG_DIR:-${BUILDDIR}/salt} |
| 19 | SALT_CACHE_DIR=${SALT_CACHE_DIR:-${SALT_CONFIG_DIR}/cache} |
Oleh Hryhorov | 3b72bf5 | 2018-05-18 16:23:04 +0300 | [diff] [blame] | 20 | |
| 21 | SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null" |
| 22 | |
Oleh Hryhorov | 3b72bf5 | 2018-05-18 16:23:04 +0300 | [diff] [blame] | 23 | if [ "x${SALT_VERSION}" != "x" ]; then |
| 24 | PIP_SALT_VERSION="==${SALT_VERSION}" |
| 25 | fi |
| 26 | |
| 27 | ## Functions |
| 28 | log_info() { |
| 29 | echo -e "[INFO] $*" |
| 30 | } |
| 31 | |
| 32 | log_err() { |
| 33 | echo -e "[ERROR] $*" >&2 |
| 34 | } |
| 35 | |
| 36 | setup_virtualenv() { |
| 37 | log_info "Setting up Python virtualenv" |
Oleh Hryhorov | 3b72bf5 | 2018-05-18 16:23:04 +0300 | [diff] [blame] | 38 | virtualenv $VENV_DIR |
| 39 | source ${VENV_DIR}/bin/activate |
| 40 | python -m pip install salt${PIP_SALT_VERSION} |
Oleh Hryhorov | 3b72bf5 | 2018-05-18 16:23:04 +0300 | [diff] [blame] | 41 | } |
| 42 | |
Oleh Hryhorov | 1bc333c | 2018-05-19 12:55:07 +0000 | [diff] [blame] | 43 | setup_test_state() { |
| 44 | local template_path=$1 |
| 45 | local state_name=$2 |
| 46 | |
| 47 | [ ! -d ${BUILDDIR}/tstates/ ] && mkdir ${BUILDDIR}/tstates/ |
| 48 | [ ! -d ${BUILDDIR}/rfiles/ ] && mkdir ${BUILDDIR}/rfiles/ |
| 49 | |
| 50 | cat << EOF > ${BUILDDIR}/tstates/${state_name}.sls |
| 51 | |
| 52 | test_${state_name}_rendering: |
| 53 | file.managed: |
| 54 | - name: ${BUILDDIR}/rfiles/${state_name}.conf |
| 55 | - template: jinja |
| 56 | - source: ${template_path} |
| 57 | - context: |
| 58 | _data: {{ pillar.get("_data", {}) }} |
sgarbuz | 2d8f805 | 2018-07-13 13:04:54 +0300 | [diff] [blame] | 59 | {%- if pillar.get('service_name') %} |
| 60 | service_name: {{ pillar.service_name }} |
| 61 | {%- endif %} |
Oleh Hryhorov | 1bc333c | 2018-05-19 12:55:07 +0000 | [diff] [blame] | 62 | EOF |
Oleh Hryhorov | 3b72bf5 | 2018-05-18 16:23:04 +0300 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | setup_pillar() { |
| 66 | [ ! -d ${SALT_PILLAR_DIR} ] && mkdir -p ${SALT_PILLAR_DIR} |
| 67 | echo "base:" > ${SALT_PILLAR_DIR}/top.sls |
Oleh Hryhorov | 1bc333c | 2018-05-19 12:55:07 +0000 | [diff] [blame] | 68 | local sdir |
| 69 | local state_name |
| 70 | local template_name |
| 71 | local pillar_name |
| 72 | |
| 73 | pushd ${PILLARDIR}/ |
| 74 | for spath in $(find ./ -type f -name '*.sls'); do |
| 75 | pillar_name=$(basename $spath | sed -e 's/.sls$//') |
| 76 | sdir=$(dirname $spath | sed -e 's/^.\///g') |
| 77 | template_name=$(echo $pillar_name | cut -d '-' -f 1) |
| 78 | state_name=$(echo ${sdir}_${pillar_name} | sed -e 's/\//_/g') |
| 79 | if ! echo $pillar_name |grep '-'; then |
| 80 | setup_test_state "salt://oslo_templates/files/$sdir/$template_name.conf" "$state_name" |
| 81 | fi |
| 82 | echo -e " ${state_name}:\n - ${sdir}/${pillar_name}" >> ${SALT_PILLAR_DIR}/top.sls |
Oleh Hryhorov | 3b72bf5 | 2018-05-18 16:23:04 +0300 | [diff] [blame] | 83 | done |
| 84 | } |
| 85 | |
| 86 | setup_salt() { |
| 87 | [ ! -d ${SALT_FILE_DIR} ] && mkdir -p ${SALT_FILE_DIR} |
| 88 | [ ! -d ${SALT_CONFIG_DIR} ] && mkdir -p ${SALT_CONFIG_DIR} |
| 89 | [ ! -d ${SALT_CACHE_DIR} ] && mkdir -p ${SALT_CACHE_DIR} |
Oleh Hryhorov | 3b72bf5 | 2018-05-18 16:23:04 +0300 | [diff] [blame] | 90 | |
| 91 | echo "base:" > ${SALT_FILE_DIR}/top.sls |
| 92 | for pillar in ${PILLARDIR}/*.sls; do |
| 93 | grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue |
| 94 | state_name=$(basename ${pillar%.sls}) |
| 95 | echo -e " ${state_name}:\n - ${FORMULA_NAME}" >> ${SALT_FILE_DIR}/top.sls |
| 96 | done |
| 97 | |
| 98 | cat << EOF > ${SALT_CONFIG_DIR}/minion |
| 99 | file_client: local |
| 100 | cachedir: ${SALT_CACHE_DIR} |
Oleh Hryhorov | 3b72bf5 | 2018-05-18 16:23:04 +0300 | [diff] [blame] | 101 | verify_env: False |
| 102 | minion_id_caching: False |
| 103 | |
| 104 | file_roots: |
| 105 | base: |
| 106 | - ${SALT_FILE_DIR} |
| 107 | - ${CURDIR}/.. |
Oleh Hryhorov | 1bc333c | 2018-05-19 12:55:07 +0000 | [diff] [blame] | 108 | - ${BUILDDIR}/tstates/ |
| 109 | - /usr/share/salt-formulas/env |
Oleh Hryhorov | 3b72bf5 | 2018-05-18 16:23:04 +0300 | [diff] [blame] | 110 | |
| 111 | pillar_roots: |
| 112 | base: |
| 113 | - ${SALT_PILLAR_DIR} |
| 114 | - ${PILLARDIR} |
| 115 | EOF |
| 116 | } |
| 117 | |
| 118 | fetch_dependency() { |
Oleh Hryhorov | 3b72bf5 | 2018-05-18 16:23:04 +0300 | [diff] [blame] | 119 | dep_name="$(echo $1|cut -d : -f 1)" |
| 120 | dep_source="$(echo $1|cut -d : -f 2-)" |
| 121 | dep_root="${DEPSDIR}/$(basename $dep_source .git)" |
| 122 | dep_metadata="${dep_root}/metadata.yml" |
| 123 | |
Oleh Hryhorov | 1bc333c | 2018-05-19 12:55:07 +0000 | [diff] [blame] | 124 | [ -d /usr/share/salt-formulas/env/${dep_name} ] && log_info "Dependency $dep_name already present in system-wide salt env" && return 0 |
| 125 | [ -d $dep_root ] && log_info "Dependency $dep_name already fetched" && return 0 |
Oleh Hryhorov | 3b72bf5 | 2018-05-18 16:23:04 +0300 | [diff] [blame] | 126 | |
| 127 | log_info "Fetching dependency $dep_name" |
| 128 | [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR} |
| 129 | git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git) |
| 130 | ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name} |
| 131 | |
| 132 | METADATA="${dep_metadata}" install_dependencies |
| 133 | } |
| 134 | |
Oleh Hryhorov | 3b72bf5 | 2018-05-18 16:23:04 +0300 | [diff] [blame] | 135 | install_dependencies() { |
| 136 | grep -E "^dependencies:" ${METADATA} >/dev/null || return 0 |
| 137 | (python - | while read dep; do fetch_dependency "$dep"; done) << EOF |
| 138 | import sys,yaml |
| 139 | for dep in yaml.load(open('${METADATA}', 'ro'))['dependencies']: |
| 140 | print '%s:%s' % (dep["name"], dep["source"]) |
| 141 | EOF |
| 142 | } |
| 143 | |
| 144 | clean() { |
| 145 | log_info "Cleaning up ${BUILDDIR}" |
| 146 | [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0 |
| 147 | } |
| 148 | |
| 149 | salt_run() { |
| 150 | [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate |
Oleh Hryhorov | 1bc333c | 2018-05-19 12:55:07 +0000 | [diff] [blame] | 151 | local cmd='' |
| 152 | cmd="python $(which salt-call) ${SALT_OPTS} $*" |
| 153 | log_info "$cmd" |
| 154 | $cmd |
Oleh Hryhorov | 3b72bf5 | 2018-05-18 16:23:04 +0300 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | prepare() { |
Oleh Hryhorov | 1bc333c | 2018-05-19 12:55:07 +0000 | [diff] [blame] | 158 | [ -d ${BUILDDIR} ] && mkdir -p ${BUILDDIR} |
Oleh Hryhorov | 3b72bf5 | 2018-05-18 16:23:04 +0300 | [diff] [blame] | 159 | |
Oleh Hryhorov | 1bc333c | 2018-05-19 12:55:07 +0000 | [diff] [blame] | 160 | which salt-call || setup_virtualenv |
Oleh Hryhorov | 3b72bf5 | 2018-05-18 16:23:04 +0300 | [diff] [blame] | 161 | setup_pillar |
| 162 | setup_salt |
| 163 | install_dependencies |
Oleh Hryhorov | 3b72bf5 | 2018-05-18 16:23:04 +0300 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | run() { |
Oleh Hryhorov | 1bc333c | 2018-05-19 12:55:07 +0000 | [diff] [blame] | 167 | pushd ${PILLARDIR}/ |
| 168 | local sdir |
| 169 | local sname |
| 170 | local state_name |
| 171 | local pillar_name |
| 172 | for spath in $(find ./ -type f -name '*.sls'); do |
| 173 | sname=$(basename $spath | basename $spath | sed -e 's/.sls$//') |
| 174 | sdir=$(dirname $spath | sed -e 's/^.\///') |
| 175 | state_name=$(echo ${sdir}_${sname} | sed -e 's/\//_/g' | cut -d '-' -f 1) |
| 176 | pillar_name=$(echo ${sdir}_${sname} | sed -e 's/\//_/g') |
| 177 | salt_run --id=${pillar_name} state.show_sls ${state_name} || (log_err "Show state ${state_name} with pillar ${pillar_name} failed"; exit 1) |
| 178 | salt_run --id=${pillar_name} state.sls ${state_name} || (log_err "Execution of ${state_name} with_pillar ${pillar_name} failed"; exit 1) |
Oleh Hryhorov | 3b72bf5 | 2018-05-18 16:23:04 +0300 | [diff] [blame] | 179 | done |
Oleh Hryhorov | 1bc333c | 2018-05-19 12:55:07 +0000 | [diff] [blame] | 180 | popd |
Oleh Hryhorov | 3b72bf5 | 2018-05-18 16:23:04 +0300 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | _atexit() { |
| 184 | RETVAL=$? |
| 185 | trap true INT TERM EXIT |
| 186 | |
| 187 | if [ $RETVAL -ne 0 ]; then |
| 188 | log_err "Execution failed" |
| 189 | else |
| 190 | log_info "Execution successful" |
| 191 | fi |
| 192 | return $RETVAL |
| 193 | } |
| 194 | |
| 195 | ## Main |
Oleh Hryhorov | 1bc333c | 2018-05-19 12:55:07 +0000 | [diff] [blame] | 196 | [[ "$0" != "$BASH_SOURCE" ]] || { |
| 197 | trap _atexit INT TERM EXIT |
Oleh Hryhorov | 3b72bf5 | 2018-05-18 16:23:04 +0300 | [diff] [blame] | 198 | |
Oleh Hryhorov | 1bc333c | 2018-05-19 12:55:07 +0000 | [diff] [blame] | 199 | case $1 in |
| 200 | clean) |
| 201 | clean |
| 202 | ;; |
| 203 | prepare) |
| 204 | prepare |
| 205 | ;; |
| 206 | run) |
| 207 | run |
| 208 | ;; |
| 209 | *) |
| 210 | prepare |
| 211 | run |
| 212 | ;; |
| 213 | esac |
| 214 | } |