Advertisement
Guest User

Untitled

a guest
Apr 7th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. [ "$DB_NAME" ] || DB_NAME='wordpress'
  4. [ "$DB_PASS" ] || DB_PASS='root'
  5. [ "$THEMES" ] || THEMES='twentysixteen'
  6. [ "$WP_DEBUG" ] || WP_DEBUG='false'
  7. [ "$ADMIN_EMAIL" ] || ADMIN_EMAIL="admin@${DB_NAME}.com"
  8. [ "$SEARCH_REPLACE" ] && \
  9. BEFORE_URL=$(echo "$SEARCH_REPLACE" | cut -d ',' -f 1) && \
  10. AFTER_URL=$(echo "$SEARCH_REPLACE" | cut -d ',' -f 2) || \
  11. SEARCH_REPLACE=false
  12.  
  13. ERROR () {
  14. echo -e "\n=> $(tput -T xterm setaf 1)$(tput -T xterm bold)ERROR$(tput -T xterm sgr 0) (Line $1): $2.";
  15. exit 1;
  16. }
  17.  
  18. # Configure wp-cli
  19. # ----------------
  20. cat > /app/wp-cli.yml <<EOF
  21. quiet: true
  22. apache_modules:
  23. - mod_rewrite
  24.  
  25. core config:
  26. dbuser: root
  27. dbpass: $DB_PASS
  28. dbname: $DB_NAME
  29. dbhost: db:3306
  30. extra-php: |
  31. define('WP_DEBUG', ${WP_DEBUG,,});
  32.  
  33. core install:
  34. url: $([ "$AFTER_URL" ] && echo "$AFTER_URL" || echo localhost:8080)
  35. title: $DB_NAME
  36. admin_user: root
  37. admin_password: $DB_PASS
  38. admin_email: $ADMIN_EMAIL
  39. skip-email: true
  40. EOF
  41.  
  42.  
  43. # Download WordPress
  44. # ------------------
  45.  
  46.  
  47.  
  48. # Wait for MySQL
  49. # --------------
  50. printf "=> Waiting for MySQL to initialize... \n"
  51. while ! mysqladmin ping --host=db --password=$DB_PASS --silent; do
  52. sleep 1
  53. done
  54.  
  55.  
  56. printf "\t%s\n" \
  57. "=======================================" \
  58. " Begin WordPress Configuration" \
  59. "======================================="
  60.  
  61.  
  62. # wp-config.php
  63. # -------------
  64. #printf "=> Generating wp.config.php file... "
  65. #rm -f /app/wp-config.php
  66. #sudo -u www-data wp core config >/dev/null 2>&1 || \
  67. # ERROR $LINENO "Could not generate wp-config.php file"
  68. #printf "Done!\n"
  69.  
  70. # Setup database
  71. # --------------
  72. printf "=> Create database '%s'... " "$DB_NAME"
  73. if [ ! "$(wp core is-installed --allow-root >/dev/null 2>&1 && echo $?)" ]; then
  74. sudo -u www-data wp db create >/dev/null 2>&1 || \
  75. ERROR $LINENO "Database creation failed"
  76. printf "Done!\n"
  77.  
  78. # If an SQL file exists in /data => load it
  79. if [ "$(stat -t /data/*.sql >/dev/null 2>&1 && echo $?)" ]; then
  80. DATA_PATH=$(find /data/*.sql | head -n 1)
  81. printf "=> Loading data backup from %s... " "$DATA_PATH"
  82. sudo -u www-data wp db import "$DATA_PATH" >/dev/null 2>&1 || \
  83. ERROR $LINENO "Could not import database"
  84. printf "Done!\n"
  85.  
  86. # If SEARCH_REPLACE is set => Replace URLs
  87. if [ "$SEARCH_REPLACE" != false ]; then
  88. printf "=> Replacing URLs... "
  89. REPLACEMENTS=$(sudo -u www-data wp search-replace "$BEFORE_URL" "$AFTER_URL" \
  90. --no-quiet --skip-columns=guid | grep replacement) || \
  91. ERROR $((LINENO-2)) "Could not execute SEARCH_REPLACE on database"
  92. echo -ne "$REPLACEMENTS\n"
  93. fi
  94. else
  95. printf "=> No database backup found. Initializing new database... "
  96. sudo -u www-data wp core install >/dev/null 2>&1 || \
  97. ERROR $LINENO "WordPress Install Failed"
  98. printf "Done!\n"
  99. fi
  100. else
  101. printf "Already exists!\n"
  102. fi
  103.  
  104.  
  105. # .htaccess
  106. # ---------
  107. if [ ! -f /app/.htaccess ]; then
  108. printf "=> Generating .htaccess file... "
  109. sudo -u www-data wp rewrite flush --hard >/dev/null 2>&1 || \
  110. ERROR $LINENO "Could not generate .htaccess file"
  111. printf "Done!\n"
  112. else
  113. printf "=> .htaccess exists. SKIPPING...\n"
  114. fi
  115.  
  116.  
  117. # Filesystem Permissions
  118. # ----------------------
  119. printf "=> Adjusting filesystem permissions... "
  120. groupadd -f docker && usermod -aG docker www-data
  121. find /app -type d -exec chmod 755 {} \;
  122. find /app -type f -exec chmod 644 {} \;
  123. mkdir -p /app/wp-content/uploads
  124. chmod -R 775 /app/wp-content/uploads && \
  125. chown -R :docker /app/wp-content/uploads
  126. printf "Done!\n"
  127.  
  128.  
  129. # Install Plugins
  130. # ---------------
  131. if [ "$PLUGINS" ]; then
  132. printf "=> Checking plugins...\n"
  133. while IFS=',' read -ra plugin; do
  134. for i in "${!plugin[@]}"; do
  135. plugin_name=$(echo "${plugin[$i]}" | xargs)
  136. sudo -u www-data wp plugin is-installed "${plugin_name}"
  137. if [ $? -eq 0 ]; then
  138. printf "=> ($((i+1))/${#plugin[@]}) Plugin '%s' found. SKIPPING...\n" "${plugin_name}"
  139. else
  140. printf "=> ($((i+1))/${#plugin[@]}) Plugin '%s' not found. Installing...\n" "${plugin_name}"
  141. sudo -u www-data wp plugin install "${plugin_name}"
  142. fi
  143. done
  144. done <<< "$PLUGINS"
  145. else
  146. printf "=> No plugin dependencies listed. SKIPPING...\n"
  147. fi
  148.  
  149.  
  150. # Operations to perform on first build
  151. # ------------------------------------
  152. if [ -d /app/wp-content/plugins/akismet ]; then
  153. printf "=> Removing default plugins... "
  154. sudo -u www-data wp plugin uninstall akismet hello --deactivate
  155. printf "Done!\n"
  156.  
  157. printf "=> Removing unneeded themes... "
  158. REMOVE_LIST=(twentyfourteen twentyfifteen twentysixteen)
  159. THEME_LIST=()
  160. while IFS=',' read -ra theme; do
  161. for i in "${!theme[@]}"; do
  162. REMOVE_LIST=( "${REMOVE_LIST[@]/${theme[$i]}}" )
  163. THEME_LIST+="${theme[$i]}"
  164. done
  165. sudo -u www-data wp theme delete "${REMOVE_LIST[@]}"
  166. done <<< $THEMES
  167. printf "Done!\n"
  168.  
  169. printf "=> Installing needed themes... "
  170. sudo -u www-data wp theme install "${THEME_LIST[@]}"
  171. printf "Done!\n"
  172. fi
  173.  
  174.  
  175. printf "\t%s\n" \
  176. "=======================================" \
  177. " WordPress Configuration Complete!" \
  178. "======================================="
  179.  
  180.  
  181. # Start apache
  182. # ------------
  183. source /etc/apache2/envvars
  184. exec apache2 -D FOREGROUND
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement