Saltstack Official MongoDB Formula
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.

201 lines
5.6KB

  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. FORMULA_META_DIR=${CURDIR}/../${FORMULA_NAME}/meta
  8. ## Overrideable parameters
  9. PILLARDIR=${PILLARDIR:-${CURDIR}/pillar}
  10. BUILDDIR=${BUILDDIR:-${CURDIR}/build}
  11. VENV_DIR=${VENV_DIR:-${BUILDDIR}/virtualenv}
  12. MOCK_BIN_DIR=${MOCK_BIN_DIR:-${CURDIR}/mock_bin}
  13. DEPSDIR=${BUILDDIR}/deps
  14. SALT_FILE_DIR=${SALT_FILE_DIR:-${BUILDDIR}/file_root}
  15. SALT_PILLAR_DIR=${SALT_PILLAR_DIR:-${BUILDDIR}/pillar_root}
  16. SALT_CONFIG_DIR=${SALT_CONFIG_DIR:-${BUILDDIR}/salt}
  17. SALT_CACHE_DIR=${SALT_CACHE_DIR:-${SALT_CONFIG_DIR}/cache}
  18. SALT_OPTS="${SALT_OPTS} --retcode-passthrough --local -c ${SALT_CONFIG_DIR} --log-file=/dev/null"
  19. if [ "x${SALT_VERSION}" != "x" ]; then
  20. PIP_SALT_VERSION="==${SALT_VERSION}"
  21. fi
  22. ## Functions
  23. log_info() {
  24. echo "[INFO] $*"
  25. }
  26. log_err() {
  27. echo "[ERROR] $*" >&2
  28. }
  29. setup_virtualenv() {
  30. log_info "Setting up Python virtualenv"
  31. virtualenv $VENV_DIR
  32. source ${VENV_DIR}/bin/activate
  33. python -m pip install salt${PIP_SALT_VERSION}
  34. }
  35. setup_mock_bin() {
  36. # If some state requires a binary, a lightweight replacement for
  37. # such binary can be put into MOCK_BIN_DIR for test purposes
  38. if [ -d "${MOCK_BIN_DIR}" ]; then
  39. PATH="${MOCK_BIN_DIR}:$PATH"
  40. export PATH
  41. fi
  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. grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
  48. state_name=$(basename ${pillar%.sls})
  49. echo -e " ${state_name}:\n - ${state_name}" >> ${SALT_PILLAR_DIR}/top.sls
  50. done
  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. echo "base:" > ${SALT_FILE_DIR}/top.sls
  57. for pillar in ${PILLARDIR}/*.sls; do
  58. grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
  59. state_name=$(basename ${pillar%.sls})
  60. echo -e " ${state_name}:\n - ${FORMULA_NAME}" >> ${SALT_FILE_DIR}/top.sls
  61. done
  62. cat << EOF > ${SALT_CONFIG_DIR}/minion
  63. file_client: local
  64. cachedir: ${SALT_CACHE_DIR}
  65. verify_env: False
  66. minion_id_caching: False
  67. file_roots:
  68. base:
  69. - ${SALT_FILE_DIR}
  70. - ${CURDIR}/..
  71. - /usr/share/salt-formulas/env
  72. pillar_roots:
  73. base:
  74. - ${SALT_PILLAR_DIR}
  75. - ${PILLARDIR}
  76. EOF
  77. }
  78. fetch_dependency() {
  79. dep_name="$(echo $1|cut -d : -f 1)"
  80. dep_source="$(echo $1|cut -d : -f 2-)"
  81. dep_root="${DEPSDIR}/$(basename $dep_source .git)"
  82. dep_metadata="${dep_root}/metadata.yml"
  83. [ -d /usr/share/salt-formulas/env/${dep_name} ] && log_info "Dependency $dep_name already present in system-wide salt env" && return 0
  84. [ -d $dep_root ] && log_info "Dependency $dep_name already fetched" && return 0
  85. log_info "Fetching dependency $dep_name"
  86. [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
  87. git clone $dep_source ${DEPSDIR}/$(basename $dep_source .git)
  88. ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name}
  89. METADATA="${dep_metadata}" install_dependencies
  90. }
  91. install_dependencies() {
  92. grep -E "^dependencies:" ${METADATA} >/dev/null || return 0
  93. (python - | while read dep; do fetch_dependency "$dep"; done) << EOF
  94. import sys,yaml
  95. for dep in yaml.load(open('${METADATA}', 'ro'))['dependencies']:
  96. print '%s:%s' % (dep["name"], dep["source"])
  97. EOF
  98. }
  99. clean() {
  100. log_info "Cleaning up ${BUILDDIR}"
  101. [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0
  102. }
  103. salt_run() {
  104. [ -e ${VENV_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
  105. python $(which salt-call) ${SALT_OPTS} $*
  106. }
  107. prepare() {
  108. [ -d ${BUILDDIR} ] && mkdir -p ${BUILDDIR}
  109. which salt-call || setup_virtualenv
  110. setup_mock_bin
  111. setup_pillar
  112. setup_salt
  113. install_dependencies
  114. }
  115. run() {
  116. for pillar in ${PILLARDIR}/*.sls; do
  117. grep ${FORMULA_NAME}: ${pillar} &>/dev/null || continue
  118. state_name=$(basename ${pillar%.sls})
  119. salt_run grains.set 'noservices' False force=True
  120. echo "Checking state ${FORMULA_NAME}.${state_name} ..."
  121. salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
  122. # Check that all files in 'meta' folder can be rendered using any valid pillar
  123. for meta in `find ${FORMULA_META_DIR} -type f`; do
  124. meta_name=$(basename ${meta})
  125. echo "Checking meta ${meta_name} ..."
  126. salt_run --out=quiet --id=${state_name} cp.get_template ${meta} ${SALT_CACHE_DIR}/${meta_name} \
  127. || (log_err "Failed to render meta ${meta} using pillar ${FORMULA_NAME}.${state_name}"; exit 1)
  128. cat ${SALT_CACHE_DIR}/${meta_name}
  129. done
  130. done
  131. }
  132. real_run() {
  133. for pillar in ${PILLARDIR}/*.sls; do
  134. state_name=$(basename ${pillar%.sls})
  135. salt_run --id=${state_name} state.sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
  136. done
  137. }
  138. _atexit() {
  139. RETVAL=$?
  140. trap true INT TERM EXIT
  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. ## Main
  149. trap _atexit INT TERM EXIT
  150. case $1 in
  151. clean)
  152. clean
  153. ;;
  154. prepare)
  155. prepare
  156. ;;
  157. run)
  158. run
  159. ;;
  160. real-run)
  161. real_run
  162. ;;
  163. *)
  164. prepare
  165. run
  166. ;;
  167. esac