New version of salt-formula from Saltstack
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
4.0KB

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