Saltstack Official MongoDB Formula
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

173 lines
4.3KB

  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. 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 ${VEN_DIR}/bin/activate ] && source ${VENV_DIR}/bin/activate
  92. 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. real_run() {
  108. for pillar in ${PILLARDIR}/*.sls; do
  109. state_name=$(basename ${pillar%.sls})
  110. salt_run --id=${state_name} state.sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
  111. done
  112. }
  113. _atexit() {
  114. RETVAL=$?
  115. trap true INT TERM EXIT
  116. if [ $RETVAL -ne 0 ]; then
  117. log_err "Execution failed"
  118. else
  119. log_info "Execution successful"
  120. fi
  121. return $RETVAL
  122. }
  123. ## Main
  124. trap _atexit INT TERM EXIT
  125. case $1 in
  126. clean)
  127. clean
  128. ;;
  129. prepare)
  130. prepare
  131. ;;
  132. run)
  133. run
  134. ;;
  135. real-run)
  136. real_run
  137. ;;
  138. *)
  139. prepare
  140. run
  141. ;;
  142. esac