Saltstack Official Galera 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.

пре 9 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. pillar_roots:
  59. base:
  60. - ${SALT_PILLAR_DIR}
  61. - ${PILLARDIR}
  62. EOF
  63. }
  64. fetch_dependency() {
  65. dep_root="${DEPSDIR}/$(basename $1 .git)"
  66. dep_metadata="${dep_root}/metadata.yml"
  67. [ -d $dep_root ] && log_info "Dependency $1 already fetched" && return 0
  68. log_info "Fetching dependency $1"
  69. [ ! -d ${DEPSDIR} ] && mkdir -p ${DEPSDIR}
  70. git clone $1 ${DEPSDIR}/$(basename $1 .git)
  71. dep_name=$(cat $dep_metadata | python -c "import sys,yaml; print yaml.load(sys.stdin)['name']")
  72. ln -s ${dep_root}/${dep_name} ${SALT_FILE_DIR}/${dep_name}
  73. METADATA="${dep_metadata}" install_dependencies
  74. }
  75. install_dependencies() {
  76. grep -E "^dependencies:" ${METADATA} >/dev/null || return 0
  77. (python - | while read dep; do fetch_dependency "$dep"; done) << EOF
  78. import sys,yaml
  79. for dep in yaml.load(open('${METADATA}', 'ro'))['dependencies']:
  80. print dep["source"]
  81. EOF
  82. }
  83. clean() {
  84. log_info "Cleaning up ${BUILDDIR}"
  85. [ -d ${BUILDDIR} ] && rm -rf ${BUILDDIR} || exit 0
  86. }
  87. salt_run() {
  88. source ${VENV_DIR}/bin/activate
  89. salt-call ${SALT_OPTS} $*
  90. }
  91. prepare() {
  92. [ -d ${BUILDDIR} ] && mkdir -p ${BUILDDIR}
  93. setup_virtualenv
  94. setup_pillar
  95. setup_salt
  96. install_dependencies
  97. }
  98. run() {
  99. for pillar in ${PILLARDIR}/*.sls; do
  100. state_name=$(basename ${pillar%.sls})
  101. salt_run --id=${state_name} state.show_sls ${FORMULA_NAME} || (log_err "Execution of ${FORMULA_NAME}.${state_name} failed"; exit 1)
  102. done
  103. }
  104. _atexit() {
  105. RETVAL=$?
  106. trap true INT TERM EXIT
  107. if [ $RETVAL -ne 0 ]; then
  108. log_err "Execution failed"
  109. else
  110. log_info "Execution successful"
  111. fi
  112. return $RETVAL
  113. }
  114. ## Main
  115. trap _atexit INT TERM EXIT
  116. case $1 in
  117. clean)
  118. clean
  119. ;;
  120. prepare)
  121. prepare
  122. ;;
  123. run)
  124. run
  125. ;;
  126. *)
  127. prepare
  128. run
  129. ;;
  130. esac