lib/capistrano/tasks/wp.cap in capistrano-cul-0.0.12 vs lib/capistrano/tasks/wp.cap in capistrano-cul-0.0.13

- old
+ new

@@ -1,9 +1,54 @@ +CUL_ALLOWED_UPLOAD_TYPES_PLUGIN_NAME = 'cul-allowed-upload-types' +CUL_ALLOWED_UPLOAD_TYPES_REPO_URL = "https://github.com/cul/#{CUL_ALLOWED_UPLOAD_TYPES_PLUGIN_NAME}" + +# Set cul_allowed_uplaod_types_version here so it can be overridden by env config +set :cul_allowed_uplaod_types_version, 'v0.2.0' + namespace :cul do namespace :wp do + before 'deploy:starting', 'cul:wp:display_maintenance_mode_warning' + after 'deploy:starting', 'cul:wp:enable_maintenance_mode' + after 'deploy:starting', 'cul:wp:update_cul_allowed_upload_types_plugin' after :deploy, 'cul:wp:symlink_custom_plugins_and_themes' + after :deploy, 'cul:wp:disable_maintenance_mode' + desc "Displays a message to the deploying user about how to disable maintenance mode" + task :display_maintenance_mode_warning do + puts color_text("WARNING: Starting a deployment will set WordPress to maintenance mode. If you cancel deployment mid-way through, you'll need to manually disable maintenance mode by running: cap [env] cul:wp:disable_maintenance_mode") + end + + desc "Enables maintenance mode for the WordPress site in the deploy environment" + task :enable_maintenance_mode do + path_to_maintenance_file = maintenance_file_path # save path to local variable because we can't call method inside `on roles(:web)` + on roles(:web) do + within fetch(:wp_docroot) do + # Set maintenance $upgrading value to current time. + # Note that WordPress will ignore maintenance mode file + # after 10 minutes have passed after the maintenance time + # we set in the file. + execute :echo, "'<?php $upgrading = #{Time.now.to_i};'", '>', path_to_maintenance_file + end + end + puts color_text("Maintenance mode enabled!") + end + + desc "Disable maintenance mode for the WordPress site in the deploy environment" + task :disable_maintenance_mode do + path_to_maintenance_file = maintenance_file_path # save path to local variable because we can't call method inside `on roles(:web)` + on roles(:web) do + within fetch(:wp_docroot) do + if test("[ -f #{path_to_maintenance_file} ]") + execute :rm, path_to_maintenance_file + else + puts "No maintenance file found, so there's nothing to delete." + end + end + end + puts color_text("Maintenance mode disabled!") + end + desc "Creates symlinks for custom plugins and themes as part of a WordPress deployment. Generally run as an `after :deploy` hook." task :symlink_custom_plugins_and_themes do symlink_custom_plugins_and_themes end @@ -75,11 +120,10 @@ # Delete original wp-content directory execute :rm, '-rf', wp_docroot_wp_content_path # Create symlink for wp_document_root wp-content to wp_content_path execute :ln, '-sf', wp_content_path, wp_docroot_wp_content_path end - symlink_custom_plugins_and_themes end desc "Runs a WordPress installation for a newly set up instance and creates a new admin user" task :install do @@ -117,21 +161,21 @@ wp_content_mu_plugin_path = File.join(wp_content_path, 'mu-plugins') wp_content_themes_path = File.join(wp_content_path, 'themes') if test("[ -d #{wp_content_path} ]") - # Create necessary directories + ### Create necessary directories execute :mkdir, '-p', wp_content_plugin_path execute :mkdir, '-p', wp_content_mu_plugin_path execute :mkdir, '-p', wp_content_themes_path - # Remove old symlinks + ### Remove old symlinks [wp_content_plugin_path, wp_content_mu_plugin_path, wp_content_themes_path].each do |dir| execute :find, dir, '-maxdepth 1', '-type l', '-exec rm {} \;' end - # Add latest symlinks + ### Add latest symlinks fetch(:wp_custom_plugins, {}).each do |plugin, repo_relative_path| execute :ln, '-sf', File.join(current_path, repo_relative_path), File.join(wp_content_plugin_path, plugin) end fetch(:wp_custom_mu_plugins, {}).each do |mu_plugin, repo_relative_path| @@ -139,14 +183,51 @@ end fetch(:wp_custom_themes, {}).each do |theme, repo_relative_path| execute :ln, '-sf', File.join(current_path, repo_relative_path), File.join(wp_content_themes_path, theme) end + + ### Also symlink to cul-allowed-upload-types plugin files + within deploy_path do + cul_allowed_upload_types_plugin_top_level_files_and_dirs = capture(:find, CUL_ALLOWED_UPLOAD_TYPES_PLUGIN_NAME, '-mindepth', '2', '-maxdepth', '2').split("\n") + # Symlink all plugin files and directories + cul_allowed_upload_types_plugin_top_level_files_and_dirs.each do |plugin_file_or_directory_path| + #puts 'Run: ' + [:ln, '-sf', File.join(deploy_path, plugin_file_or_directory_path), File.join(wp_content_mu_plugin_path, File.basename(plugin_file_or_directory_path))].join(' ') + execute :ln, '-sf', File.join(deploy_path, plugin_file_or_directory_path), File.join(wp_content_mu_plugin_path, File.basename(plugin_file_or_directory_path)) + end + end + end end end + desc "Downloads the latest version of the cul-allowed-upload-types plugin" + task :update_cul_allowed_upload_types_plugin do + # Download plugin to deploy_path + on roles(:web) do + within deploy_path do + # Clear out old plugin directory if it exists + execute :rm, '-rf', CUL_ALLOWED_UPLOAD_TYPES_PLUGIN_NAME + # Re-create plugin directory and temp dir inside of it + allowed_upload_types_tempdir = File.join(CUL_ALLOWED_UPLOAD_TYPES_PLUGIN_NAME, 'tmp') + execute :mkdir, '-p', allowed_upload_types_tempdir + + # Download and unzip plugin + allowed_upload_types_temp_zipfile = File.join(allowed_upload_types_tempdir, 'plugin.zip') + zip_file_name = "#{fetch(:cul_allowed_uplaod_types_version)}.zip" + execute :curl, '-L', '--silent', '-o', allowed_upload_types_temp_zipfile, "#{CUL_ALLOWED_UPLOAD_TYPES_REPO_URL}/archive/#{zip_file_name}" + execute :unzip, allowed_upload_types_temp_zipfile, '-d', CUL_ALLOWED_UPLOAD_TYPES_PLUGIN_NAME + + # Delete temp dir after unzip + execute :rm, '-rf', allowed_upload_types_tempdir + + # Remove .gitignore file from plugin directory so we don't symlink to it later + execute :find, CUL_ALLOWED_UPLOAD_TYPES_PLUGIN_NAME, '-name', '.gitignore', '-delete' + end + end + end + desc "Runs a search and replace operation on the tables in a WordPress installation" task :searchreplace do on roles(:web) do within fetch(:wp_docroot) do set :search_string, ask("search string") @@ -172,9 +253,23 @@ end puts "Search and replace complete (took #{(Time.now - start_time).to_s} seconds)" end end + end + + def self.cul_allowed_upload_types_plugin_path + File.join('mu-plugins', CUL_ALLOWED_UPLOAD_TYPES_PLUGIN_NAME) + end + + def self.maintenance_file_path + File.join(fetch(:wp_docroot), '.maintenance') + end + + def self.color_text(message, color_number=35) + text_color = "\e[#{color_number}m" + default_color = "\e[0m" + text_color + message + default_color end end end