Ivan Suzdal | 67bcad5 | 2017-05-26 18:45:54 +0300 | [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 |
Mykyta Karpin | 00bbbb4 | 2017-07-03 17:48:53 +0300 | [diff] [blame^] | 8 | FORMULA_NAME=$(cat $METADATA | python -c "import sys,yaml; print yaml.load(sys.stdin)['name']") |
Ivan Suzdal | 67bcad5 | 2017-05-26 18:45:54 +0300 | [diff] [blame] | 9 | |
| 10 | ## Overrideable parameters |
| 11 | PILLARDIR=${PILLARDIR:-${CURDIR}/pillar} |
| 12 | BUILDDIR=${BUILDDIR:-${CURDIR}/build} |
| 13 | VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv} |
| 14 | DEPSDIR=${BUILDDIR}/deps |
| 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} |
| 20 | |
Mykyta Karpin | 00bbbb4 | 2017-07-03 17:48:53 +0300 | [diff] [blame^] | 21 | SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null" |
Ivan Suzdal | 67bcad5 | 2017-05-26 18:45:54 +0300 | [diff] [blame] | 22 | |
| 23 | if [ "x${SALT_VERSION}" != "x" ]; then |
| 24 | PIP_SALT_VERSION="==${SALT_VERSION}" |
| 25 | fi |
| 26 | |
| 27 | ## Functions |
| 28 | log_info() { |
| 29 | echo "[INFO] $*" |
| 30 | } |
| 31 | |
| 32 | log_err() { |
| 33 | echo "[ERROR] $*" >&2 |
| 34 | } |
| 35 | |
| 36 | setup_virtualenv() { |
| 37 | log_info "Setting up Python virtualenv" |
| 38 | virtualenv $VENV_DIR |
| 39 | source ${VENV_DIR}/bin/activate |
| 40 | pip install salt${PIP_SALT_VERSION} |
| 41 | } |
| 42 | |
| 43 | setup_pillar() { |
| 44 | [ ! -d ${SALT_PILLAR_DIR} ] && mkdir -p ${SALT_PILLAR_DIR} |
| 45 | echo "base:" > ${SALT_PILLAR_DIR}/top.sls |
| 46 | for pillar in ${PILLARDIR}/*; do |
Mykyta Karpin | 00bbbb4 | 2017-07-03 17:48:53 +0300 | [diff] [blame^] | 47 | grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue |
Ivan Suzdal | 67bcad5 | 2017-05-26 18:45:54 +0300 | [diff] [blame] | 48 | state_name=$(basename ${pillar%.sls}) |
| 49 | echo -e " ${state_name}:\n - ${state_name}" >> ${SALT_PILLAR_DIR}/top.sls |
| 50 | done |
| 51 | } |
| 52 | |
| 53 | setup_salt() { |
| 54 | [ ! -d ${SALT_FILE_DIR} ] && mkdir -p ${SALT_FILE_DIR} |
| 55 | [ ! -d ${SALT_CONFIG_DIR} ] && mkdir -p ${SALT_CONFIG_DIR} |
| 56 | [ ! -d ${SALT_CACHE_DIR} ] && mkdir -p ${SALT_CACHE_DIR} |
| 57 | |
| 58 | echo "base:" > ${SALT_FILE_DIR}/top.sls |
| 59 | for pillar in ${PILLARDIR}/*.sls; do |
Mykyta Karpin | 00bbbb4 | 2017-07-03 17:48:53 +0300 | [diff] [blame^] | 60 | grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue |
Ivan Suzdal | 67bcad5 | 2017-05-26 18:45:54 +0300 | [diff] [blame] | 61 | state_name=$(basename ${pillar%.sls}) |
| 62 | echo -e " ${state_name}:\n - ${FORMULA_NAME}" >> ${SALT_FILE_DIR}/top.sls |
| 63 | done |
| 64 | |
| 65 | cat << EOF > ${SALT_CONFIG_DIR}/minion |
| 66 | file_client: local |
| 67 | cachedir: ${SALT_CACHE_DIR} |
| 68 | verify_env: False |
Mykyta Karpin | 00bbbb4 | 2017-07-03 17:48:53 +0300 | [diff] [blame^] | 69 | minion_id_caching: False |
Ivan Suzdal | 67bcad5 | 2017-05-26 18:45:54 +0300 | [diff] [blame] | 70 | |
| 71 | file_roots: |
| 72 | base: |
| 73 | - ${SALT_FILE_DIR} |
| 74 | - ${CURDIR}/.. |
| 75 | - /usr/share/salt-formulas/env |
| 76 | |
| 77 | pillar_roots: |
| 78 | base: |
| 79 | - ${SALT_PILLAR_DIR} |
| 80 | - ${PILLARDIR} |
| 81 | EOF |
| 82 | } |
| 83 | |
| 84 | fetch_dependency() { |
| 85 | dep_name="$(echo $1|cut -d : -f 1)" |
| 86 | dep_source="$(echo $1|cut -d : -f 2-)" |
| 87 | dep_root="${DEPSDIR}/$(basename $dep_source .git)" |
| 88 | dep_metadata="${dep_root}/metadata.yml" |
| 89 | |
| 90 | [ -d /usr/share/salt-formulas/env/${dep_name} ] && log_info "Dependency $dep_name already present in system-wide salt env" && return 0 |
Mykyta Karpin | 00bbbb4 | 2017-07-03 17:48:53 +0300 | [diff] [blame^] | 91 | [ -d $dep_root ] && log_info "Dependency $dep_name already fetched" && return 0 |
Ivan Suzdal | 67bcad5 | 2017-05-26 18:45:54 +0300 | [diff] [blame] | 92 | |
| 93 | log_info "Fetching dependency $dep_name" |
| 94 | [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR} |
| 95 | git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git) |
| 96 | ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name} |
| 97 | |
| 98 | METADATA="${dep_metadata}" install_dependencies |
| 99 | } |
| 100 | |
| 101 | install_dependencies() { |
| 102 | grep -E "^dependencies:" ${METADATA} >/dev/null || return 0 |
| 103 | (python - | while read dep; do fetch_dependency "$dep"; done) << EOF |
| 104 | import sys,yaml |
| 105 | for dep in yaml.load(open('${METADATA}', 'ro'))['dependencies']: |
| 106 | print '%s:%s' % (dep["name"], dep["source"]) |
| 107 | EOF |
| 108 | } |
| 109 | |
| 110 | clean() { |
| 111 | log_info "Cleaning up ${BUILDDIR}" |
| 112 | [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0 |
| 113 | } |
| 114 | |
| 115 | salt_run() { |
| 116 | [ -e ${VEN_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate |
| 117 | salt-call ${SALT_OPTS} $* |
| 118 | } |
| 119 | |
| 120 | prepare() { |
| 121 | [ -d ${BUILDDIR} ] && mkdir -p ${BUILDDIR} |
| 122 | |
| 123 | which salt-call || setup_virtualenv |
| 124 | setup_pillar |
| 125 | setup_salt |
| 126 | install_dependencies |
| 127 | } |
| 128 | |
| 129 | run() { |
| 130 | for pillar in ${PILLARDIR}/*.sls; do |
Mykyta Karpin | 00bbbb4 | 2017-07-03 17:48:53 +0300 | [diff] [blame^] | 131 | grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue |
Ivan Suzdal | 67bcad5 | 2017-05-26 18:45:54 +0300 | [diff] [blame] | 132 | state_name=$(basename ${pillar%.sls}) |
| 133 | salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1) |
| 134 | done |
| 135 | } |
| 136 | |
| 137 | _atexit() { |
| 138 | RETVAL=$? |
| 139 | trap true INT TERM EXIT |
| 140 | |
| 141 | if [ $RETVAL -ne 0 ]; then |
| 142 | log_err "Execution failed" |
| 143 | else |
| 144 | log_info "Execution successful" |
| 145 | fi |
| 146 | return $RETVAL |
| 147 | } |
| 148 | |
| 149 | ## Main |
| 150 | trap _atexit INT TERM EXIT |
| 151 | |
| 152 | case $1 in |
| 153 | clean) |
| 154 | clean |
| 155 | ;; |
| 156 | prepare) |
| 157 | prepare |
| 158 | ;; |
| 159 | run) |
| 160 | run |
| 161 | ;; |
| 162 | *) |
| 163 | prepare |
| 164 | run |
| 165 | ;; |
| 166 | esac |