provisioning tool for building opinionated architecture
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.

176 lines
4.8 KiB

9 months ago
  1. #!/bin/bash
  2. ### error_handling
  3. function trap_error() {
  4. error_code=$1
  5. error_line=$2
  6. if [[ ${error_code} -lt 100 ]]; then
  7. printf "\nEXIT #${error_code} due to error at line ${error_line} : \n-----------------------------------------\n"
  8. sed "${error_line}q;d" $0
  9. echo
  10. fi
  11. exit $error_code
  12. }
  13. set -e
  14. trap 'trap_error $? $LINENO' ERR
  15. ### ------------------
  16. function detectWordpress() {
  17. local result=$(pwd)
  18. while [[ ! ("$result" == / || -f "$result/wp-config.php") ]]; do
  19. result=$(dirname "$result")
  20. done
  21. if [[ "$result" == / ]]; then
  22. echo >&2 "no WORDPRESS detected!"
  23. exit 100
  24. fi
  25. echo "$result"
  26. }
  27. function getConfigComment() {
  28. local result=$(grep -e "^#" $WP_CONFIG | grep "$1" | head -n1 | cut -d ',' -f2 | cut -d \' -f2)
  29. if [[ -z "$result" ]]; then
  30. echo "config comment: $1 not found!"
  31. exit 2
  32. fi
  33. echo "$result"
  34. }
  35. function getConfigEntry() {
  36. local result=$(grep "$1" $WP_CONFIG | head -n1 | cut -d ',' -f2 | cut -d \' -f2)
  37. if [[ -z "$result" ]]; then
  38. echo "config entry: $1 not found!"
  39. exit 2
  40. fi
  41. echo "$result"
  42. }
  43. function sql() {
  44. local result=$(echo "$1" | mysql -srN -u $DB_USER -h $DB_HOST $DB_NAME -p$DB_PASS 2>&1)
  45. if [[ $result =~ ^ERROR ]]; then
  46. echo >&2 "sql failure: $result"
  47. exit 3
  48. else
  49. echo "$result"
  50. fi
  51. }
  52. function sqlFile() {
  53. local result=$(cat "$1" | mysql -srN -u $DB_USER -h $DB_HOST $DB_NAME -p$DB_PASS 2>&1)
  54. if [[ $result =~ ^ERROR ]]; then
  55. echo >&2 "sql failure: $result"
  56. exit 3
  57. else
  58. echo "$result"
  59. fi
  60. }
  61. function changeHome() {
  62. local FROM=$1
  63. local TO=$2
  64. sql "UPDATE wp_options SET option_value = replace(option_value, '$FROM', '$TO') WHERE option_name = 'home' OR option_name = 'siteurl'"
  65. sql "UPDATE wp_posts SET guid = replace(guid, '$FROM','$TO')"
  66. sql "UPDATE wp_posts SET post_content = replace(post_content, '$FROM', '$TO')"
  67. sql "UPDATE wp_postmeta SET meta_value = replace(meta_value,'$FROM','$TO')"
  68. }
  69. function lastMigration() {
  70. sql "SELECT migration_file FROM migrations ORDER BY last_run DESC LIMIT 1"
  71. }
  72. function upgradeMigration() {
  73. local LAST_MIGRATION=$1
  74. local UPGRADE=false
  75. if [[ "$LAST_MIGRATION" == '' ]]; then
  76. UPGRADE=true
  77. fi
  78. local MIG_BASE="$WP_BASE/wp-content/migrations"
  79. local MIGRATIONS=$(ls -p1 $MIG_BASE | grep -v /)
  80. local MIG_FILE
  81. for mig in $MIGRATIONS; do
  82. if [[ "$UPGRADE" == true ]]; then
  83. printf "applying %50s ... " $mig
  84. printf "%d %d" $(sqlFile $MIG_BASE/$mig)
  85. echo " DONE"
  86. MIG_FILE=$mig
  87. else
  88. printf "useless %50s \n" $mig
  89. if [[ "$LAST_MIGRATION" == "$mig" ]]; then
  90. UPGRADE=true
  91. fi
  92. fi
  93. done
  94. if [[ $UPGRADE == true && $MIG_FILE != '' ]]; then
  95. local done=$(sql "INSERT INTO migrations(migration_file, last_run) VALUES ('$mig', NOW())")
  96. echo "all migrations succeeded, wrote: $mig"
  97. else
  98. echo "already up-to-date"
  99. fi
  100. }
  101. function buildMigrations() {
  102. if [[ ! -d "$WP_BASE"/wp-content/migrations ]]; then
  103. mkdir -p "$WP_BASE"/wp-content/migrations
  104. echo "migrations folder created!"
  105. fi
  106. sql "CREATE TABLE IF NOT EXISTS migrations (id int(11) NOT NULL AUTO_INCREMENT, migration_file varchar(255) COLLATE utf8_unicode_ci NOT NULL, last_run varchar(45) COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (id) )"
  107. }
  108. function playEnvironment() {
  109. buildMigrations
  110. local PLATFORM=$1
  111. local PLATFORM_BASE="$WP_BASE/wp-content/migrations/$PLATFORM"
  112. if [[ -d "$PLATFORM_BASE" ]]; then
  113. echo play platform $PLATFORM
  114. local MIGRATIONS=$(ls -p1 $PLATFORM_BASE | grep -v /)
  115. for mig in $MIGRATIONS; do
  116. printf "applying %50s ... " $mig
  117. printf "%d %d" $(sqlFile $PLATFORM_BASE/$mig)
  118. echo " DONE"
  119. done
  120. fi
  121. }
  122. ## MAIN
  123. ## ----
  124. WP_BASE=$(detectWordpress)
  125. WP_CONFIG="$WP_BASE/wp-config.php"
  126. echo "WP_BASE = $WP_BASE"
  127. WP_HOME=$(getConfigComment WP_HOME)
  128. echo "WP_HOME = $WP_HOME"
  129. DB_HOST=$(getConfigEntry DB_HOST)
  130. DB_NAME=$(getConfigEntry DB_NAME)
  131. DB_USER=$(getConfigEntry DB_USER)
  132. DB_PASS=$(getConfigEntry DB_PASSWORD)
  133. CURRENT_HOME=$(sql "SELECT option_value FROM wp_options WHERE option_name = 'home'")
  134. if [[ "$CURRENT_HOME" != "$WP_HOME" ]]; then
  135. echo "HOME detected = $CURRENT_HOME , needs to apply changes"
  136. $(changeHome "$CURRENT_HOME" "$WP_HOME")
  137. fi
  138. if [[ "$WP_HOME" =~ https?:\/\/beta[0-9]*\..*|https?:\/\/.*\.beta[0-9]*\..* ]]; then
  139. playEnvironment BETA
  140. else
  141. if [[ "$WP_HOME" =~ https?:\/\/dev[0-9]*\..*|https?:\/\/.*\.dev[0-9]*\..* ]]; then
  142. playEnvironment DEV
  143. else
  144. playEnvironment PROD
  145. fi
  146. fi
  147. CURRENT_MIGRATION=$(lastMigration)
  148. upgradeMigration "$CURRENT_MIGRATION"