Filip Pytloun | 398aa69 | 2016-02-26 16:39:51 +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}/.. |
| 72 | |
| 73 | pillar_roots: |
| 74 | base: |
| 75 | - ${SALT_PILLAR_DIR} |
| 76 | - ${PILLARDIR} |
| 77 | EOF |
| 78 | } |
| 79 | |
| 80 | fetch_dependency() { |
| 81 | dep_root="${DEPSDIR}/$(basename $1 .git)" |
| 82 | dep_metadata="${dep_root}/metadata.yml" |
| 83 | |
| 84 | [ -d $dep_root ] && log_info "Dependency $1 already fetched" && return 0 |
| 85 | |
| 86 | log_info "Fetching dependency $1" |
| 87 | [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR} |
| 88 | git clone $1 ${DEPSDIR}/$(basename $1 .git) |
| 89 | |
| 90 | dep_name=$(cat $dep_metadata | python -c "import sys,yaml; print yaml.load(sys.stdin)['name']") |
| 91 | ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name} |
| 92 | |
| 93 | METADATA="${dep_metadata}" install_dependencies |
| 94 | } |
| 95 | |
| 96 | install_dependencies() { |
| 97 | grep -E "^dependencies:" ${METADATA} >/dev/null || return 0 |
| 98 | (python - | while read dep; do fetch_dependency "$dep"; done) << EOF |
| 99 | import sys,yaml |
| 100 | for dep in yaml.load(open('${METADATA}', 'ro'))['dependencies']: |
| 101 | print dep["source"] |
| 102 | EOF |
| 103 | } |
| 104 | |
| 105 | clean() { |
| 106 | log_info "Cleaning up ${BUILDDIR}" |
| 107 | [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0 |
| 108 | } |
| 109 | |
| 110 | salt_run() { |
| 111 | source ${VENV_DIR}/bin/activate |
| 112 | salt-call ${SALT_OPTS} $* |
| 113 | } |
| 114 | |
| 115 | prepare() { |
| 116 | [ -d ${BUILDDIR} ] && mkdir -p ${BUILDDIR} |
| 117 | |
| 118 | setup_virtualenv |
| 119 | setup_pillar |
| 120 | setup_salt |
| 121 | install_dependencies |
| 122 | } |
| 123 | |
| 124 | run() { |
| 125 | for pillar in ${PILLARDIR}/*.sls; do |
| 126 | state_name=$(basename ${pillar%.sls}) |
| 127 | salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1) |
| 128 | done |
| 129 | } |
| 130 | |
| 131 | _atexit() { |
| 132 | RETVAL=$? |
| 133 | trap true INT TERM EXIT |
| 134 | |
| 135 | if [ $RETVAL -ne 0 ]; then |
| 136 | log_err "Execution failed" |
| 137 | else |
| 138 | log_info "Execution successful" |
| 139 | fi |
| 140 | return $RETVAL |
| 141 | } |
| 142 | |
| 143 | ## Main |
| 144 | trap _atexit INT TERM EXIT |
| 145 | |
| 146 | case $1 in |
| 147 | clean) |
| 148 | clean |
| 149 | ;; |
| 150 | prepare) |
| 151 | prepare |
| 152 | ;; |
| 153 | run) |
| 154 | run |
| 155 | ;; |
| 156 | *) |
| 157 | prepare |
| 158 | run |
| 159 | ;; |
| 160 | esac |