jan kaufman | d6d5f7c | 2016-01-26 13:48:09 +0100 | [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 |
| 8 | FORMULA_NAME=$(cat $METADATA | python -c "import sys,yaml; print yaml.load(sys.stdin)['name']") |
| 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 | |
| 21 | SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR}" |
| 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 |
| 47 | state_name=$(basename ${pillar%.sls}) |
| 48 | echo -e " ${state_name}:\n - ${state_name}" >> ${SALT_PILLAR_DIR}/top.sls |
| 49 | done |
| 50 | } |
| 51 | |
| 52 | setup_salt() { |
| 53 | [ ! -d ${SALT_FILE_DIR} ] && mkdir -p ${SALT_FILE_DIR} |
| 54 | [ ! -d ${SALT_CONFIG_DIR} ] && mkdir -p ${SALT_CONFIG_DIR} |
| 55 | [ ! -d ${SALT_CACHE_DIR} ] && mkdir -p ${SALT_CACHE_DIR} |
| 56 | |
| 57 | echo "base:" > ${SALT_FILE_DIR}/top.sls |
| 58 | for pillar in ${PILLARDIR}/*.sls; do |
| 59 | state_name=$(basename ${pillar%.sls}) |
| 60 | echo -e " ${state_name}:\n - ${FORMULA_NAME}" >> ${SALT_FILE_DIR}/top.sls |
| 61 | done |
| 62 | |
| 63 | cat << EOF > ${SALT_CONFIG_DIR}/minion |
| 64 | file_client: local |
| 65 | cachedir: ${SALT_CACHE_DIR} |
| 66 | verify_env: False |
| 67 | |
| 68 | file_roots: |
| 69 | base: |
| 70 | - ${SALT_FILE_DIR} |
| 71 | - ${CURDIR}/.. |
Filip Pytloun | 0b0dbd8 | 2016-04-14 11:51:09 +0200 | [diff] [blame] | 72 | - /usr/share/salt-formulas/env |
jan kaufman | d6d5f7c | 2016-01-26 13:48:09 +0100 | [diff] [blame] | 73 | |
| 74 | pillar_roots: |
| 75 | base: |
| 76 | - ${SALT_PILLAR_DIR} |
| 77 | - ${PILLARDIR} |
| 78 | EOF |
| 79 | } |
| 80 | |
| 81 | fetch_dependency() { |
Filip Pytloun | 0b0dbd8 | 2016-04-14 11:51:09 +0200 | [diff] [blame] | 82 | dep_name="$(echo $1|cut -d : -f 1)" |
| 83 | dep_source="$(echo $1|cut -d : -f 2)" |
| 84 | dep_root="${DEPSDIR}/$(basename $dep_source .git)" |
jan kaufman | d6d5f7c | 2016-01-26 13:48:09 +0100 | [diff] [blame] | 85 | dep_metadata="${dep_root}/metadata.yml" |
| 86 | |
Filip Pytloun | 0b0dbd8 | 2016-04-14 11:51:09 +0200 | [diff] [blame] | 87 | [ -d /usr/share/salt-formulas/env/${dep_name} ] && log_info "Dependency $dep_name already present in system-wide salt env" && return 0 |
| 88 | [ -d $dep_root ] && log_info "Dependency $dep_name already fetched" && return 0 |
jan kaufman | d6d5f7c | 2016-01-26 13:48:09 +0100 | [diff] [blame] | 89 | |
Filip Pytloun | 0b0dbd8 | 2016-04-14 11:51:09 +0200 | [diff] [blame] | 90 | log_info "Fetching dependency $dep_name" |
jan kaufman | d6d5f7c | 2016-01-26 13:48:09 +0100 | [diff] [blame] | 91 | [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR} |
Filip Pytloun | 8279619 | 2016-04-14 12:46:30 +0200 | [diff] [blame^] | 92 | git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git) |
jan kaufman | d6d5f7c | 2016-01-26 13:48:09 +0100 | [diff] [blame] | 93 | ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name} |
| 94 | |
| 95 | METADATA="${dep_metadata}" install_dependencies |
| 96 | } |
| 97 | |
| 98 | install_dependencies() { |
| 99 | grep -E "^dependencies:" ${METADATA} >/dev/null || return 0 |
| 100 | (python - | while read dep; do fetch_dependency "$dep"; done) << EOF |
| 101 | import sys,yaml |
| 102 | for dep in yaml.load(open('${METADATA}', 'ro'))['dependencies']: |
Filip Pytloun | 0b0dbd8 | 2016-04-14 11:51:09 +0200 | [diff] [blame] | 103 | print '%s:%s' % (dep["name"], dep["source"]) |
jan kaufman | d6d5f7c | 2016-01-26 13:48:09 +0100 | [diff] [blame] | 104 | EOF |
| 105 | } |
| 106 | |
| 107 | clean() { |
| 108 | log_info "Cleaning up ${BUILDDIR}" |
| 109 | [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0 |
| 110 | } |
| 111 | |
| 112 | salt_run() { |
Filip Pytloun | 0b0dbd8 | 2016-04-14 11:51:09 +0200 | [diff] [blame] | 113 | [ -e ${VEN_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate |
jan kaufman | d6d5f7c | 2016-01-26 13:48:09 +0100 | [diff] [blame] | 114 | salt-call ${SALT_OPTS} $* |
| 115 | } |
| 116 | |
| 117 | prepare() { |
| 118 | [ -d ${BUILDDIR} ] && mkdir -p ${BUILDDIR} |
| 119 | |
Filip Pytloun | 0b0dbd8 | 2016-04-14 11:51:09 +0200 | [diff] [blame] | 120 | which salt-call || setup_virtualenv |
jan kaufman | d6d5f7c | 2016-01-26 13:48:09 +0100 | [diff] [blame] | 121 | setup_pillar |
| 122 | setup_salt |
| 123 | install_dependencies |
| 124 | } |
| 125 | |
| 126 | run() { |
| 127 | for pillar in ${PILLARDIR}/*.sls; do |
| 128 | state_name=$(basename ${pillar%.sls}) |
| 129 | salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1) |
| 130 | done |
| 131 | } |
| 132 | |
| 133 | _atexit() { |
| 134 | RETVAL=$? |
| 135 | trap true INT TERM EXIT |
| 136 | |
| 137 | if [ $RETVAL -ne 0 ]; then |
| 138 | log_err "Execution failed" |
| 139 | else |
| 140 | log_info "Execution successful" |
| 141 | fi |
| 142 | return $RETVAL |
| 143 | } |
| 144 | |
| 145 | ## Main |
| 146 | trap _atexit INT TERM EXIT |
| 147 | |
| 148 | case $1 in |
| 149 | clean) |
| 150 | clean |
| 151 | ;; |
| 152 | prepare) |
| 153 | prepare |
| 154 | ;; |
| 155 | run) |
| 156 | run |
| 157 | ;; |
| 158 | *) |
| 159 | prepare |
| 160 | run |
| 161 | ;; |
| 162 | esac |