Skip to content

Commit

Permalink
Wordmove does not require anymore various local config
Browse files Browse the repository at this point in the history
The only needed one is `local.wordpress_path` now.

Other config are taken through - the now mandatory -
wp-cli.
  • Loading branch information
alessandro-fazzi committed Jan 3, 2022
1 parent 0ba1f38 commit 11120be
Show file tree
Hide file tree
Showing 21 changed files with 403 additions and 226 deletions.
3 changes: 2 additions & 1 deletion lib/wordmove.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

require 'photocopier'

require 'wordmove/wpcli'

require 'wordmove/cli'
require 'wordmove/doctor'
require 'wordmove/doctor/movefile'
Expand All @@ -32,7 +34,6 @@
require 'wordmove/wordpress_directory'
require 'wordmove/version'
require 'wordmove/environments_list'
require 'wordmove/wpcli'

require 'wordmove/generators/movefile_adapter'
require 'wordmove/generators/movefile'
Expand Down
137 changes: 89 additions & 48 deletions lib/wordmove/actions/adapt_local_db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Actions
class AdaptLocalDb
extend ::LightService::Action
include Wordmove::Actions::Helpers
include Wordmove::Wpcli
include Wordmove::WpcliHelpers

expects :local_options,
:remote_options,
Expand Down Expand Up @@ -38,63 +38,104 @@ class AdaptLocalDb

next context if simulate?(cli_options: context.cli_options)

result = Wordmove::Actions::RunLocalCommand.execute(
cli_options: context.cli_options,
logger: context.logger,
command: mysql_dump_command(
env_db_options: context.local_options[:database],
save_to_path: context.db_paths.local.path
)
)
context.fail_and_return!(result.message) if result.failure?
context.logger.task_step true, dump_command(context)
begin
system(dump_command(context), exception: true)
rescue RuntimeError, SystemExit => e
context.fail_and_return!("Local command status reports an error: #{e.message}")
end

if context.cli_options[:no_adapt]
context.logger.warn 'Skipping DB adapt'
else
result = Wordmove::Actions::RunLocalCommand.execute(
cli_options: context.cli_options,
logger: context.logger,
command: wpcli_search_replace_command(context, :vhost)
)
context.fail_and_return!(result.message) if result.failure?

result = Wordmove::Actions::RunLocalCommand.execute(
cli_options: context.cli_options,
logger: context.logger,
command: wpcli_search_replace_command(context, :wordpress_path)
)
context.fail_and_return!(result.message) if result.failure?
end
%i[vhost wordpress_path].each do |key|
command = search_replace_command(context, key)
context.logger.task_step true, command

result = Wordmove::Actions::RunLocalCommand.execute(
cli_options: context.cli_options,
begin
system(command, exception: true)
rescue RuntimeError, SystemExit => e
context.fail_and_return!("Local command status reports an error: #{e.message}")
end
end
end

logger: context.logger,
command: mysql_dump_command(
env_db_options: context.local_options[:database],
save_to_path: context.db_paths.local.adapted_path
)
)
context.fail_and_return!(result.message) if result.failure?
context.logger.task_step true, dump_adapted_command(context)
begin
system(dump_adapted_command(context), exception: true)
rescue RuntimeError, SystemExit => e
context.fail_and_return!("Local command status reports an error: #{e.message}")
end

if context.photocopier.is_a? Photocopier::SSH
result = Wordmove::Actions::RunLocalCommand.execute(
cli_options: context.cli_options,
logger: context.logger,
command: compress_command(file_path: context.db_paths.local.adapted_path)
)
context.fail_and_return!(result.message) if result.failure?
context.logger.task_step true, compress_command(context)
begin
system(compress_command(context), exception: true)
rescue RuntimeError, SystemExit => e
context.fail_and_return!("Local command status reports an error: #{e.message}")
end
end

context.logger.task_step true, import_original_db_command(context)
begin
system(import_original_db_command(context), exception: true)
rescue RuntimeError, SystemExit => e
context.fail_and_return!("Local command status reports an error: #{e.message}")
end
end

def self.dump_command(context)
"wp db export #{context.db_paths.local.path} --allow-root --quiet " \
"--path=#{wpcli_config_path(context)}"
end

def self.dump_adapted_command(context)
"wp db export #{context.db_paths.local.adapted_path} --allow-root --quiet " \
"--path=#{wpcli_config_path(context)}"
end

def self.import_original_db_command(context)
"wp db import #{context.db_paths.local.path} --allow-root --quiet " \
"--path=#{wpcli_config_path(context)}"
end

def self.compress_command(context)
command = ['nice']
command << '-n'
command << '0'
command << 'gzip'
command << '-9'
command << '-f'
command << "\"#{context.db_paths.local.adapted_path}\""
command.join(' ')
end

# Compose and returns the search-replace command. It's intended to be
# used from a +LightService::Action+
#
# @param context [LightService::Context] The context of an action
# @param config_key [:vhost, :wordpress_path] Determines what will be replaced in DB
# @return [String]
# @!scope class
def self.search_replace_command(context, config_key)
unless %i[vhost wordpress_path].include?(config_key)
raise ArgumentError, "Unexpected `config_key` #{config_key}.:vhost" \
'or :wordpress_path expected'
end

result = Wordmove::Actions::RunLocalCommand.execute(
cli_options: context.cli_options,
logger: context.logger,
command: mysql_import_command(
dump_path: context.db_paths.local.path,
env_db_options: context.local_options[:database]
)
)
context.fail_and_return!(result.message) if result.failure?
[
'wp search-replace',
"--path=#{wpcli_config_path(context)}",
'"\A' + context.dig(:local_options, config_key) + '\Z"', # rubocop:disable Style/StringConcatenation
'"' + context.dig(:remote_options, config_key) + '"', # rubocop:disable Style/StringConcatenation
'--regex-delimiter="|"',
'--regex',
'--precise',
'--quiet',
'--skip-columns=guid',
'--all-tables',
'--allow-root'
].join(' ')
end
end
end
Expand Down
96 changes: 68 additions & 28 deletions lib/wordmove/actions/adapt_remote_db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Actions
class AdaptRemoteDb
extend ::LightService::Action
include Wordmove::Actions::Helpers
include Wordmove::Wpcli
include Wordmove::WpcliHelpers

expects :local_options,
:cli_options,
Expand All @@ -39,11 +39,12 @@ class AdaptRemoteDb
next context if simulate?(cli_options: context.cli_options)

if File.exist?(context.db_paths.local.gzipped_path)
Wordmove::Actions::RunLocalCommand.execute(
cli_options: context.cli_options,
logger: context.logger,
command: uncompress_command(file_path: context.db_paths.local.gzipped_path)
)
context.logger.task_step true, uncompress_command(context)
begin
system(uncompress_command(context), exception: true)
rescue RuntimeError, SystemExit => e
context.fail_and_return!("Local command status reports an error: #{e.message}")
end
end

unless File.exist?(context.db_paths.local.path)
Expand All @@ -52,37 +53,76 @@ class AdaptRemoteDb
)
end

result = Wordmove::Actions::RunLocalCommand.execute(
cli_options: context.cli_options,
logger: context.logger,
command: mysql_import_command(
dump_path: context.db_paths.local.path,
env_db_options: context.local_options[:database]
)
)
context.fail_and_return!(result.message) if result.failure?
context.logger.task_step true, import_db_command(context)
begin
system(import_db_command(context), exception: true)
rescue RuntimeError, SystemExit => e
context.fail_and_return!("Local command status reports an error: #{e.message}")
end

if context.cli_options[:no_adapt]
context.logger.warn 'Skipping DB adapt'
next context
end

result = Wordmove::Actions::RunLocalCommand.execute(
cli_options: context.cli_options,
logger: context.logger,
command: wpcli_search_replace_command(context, :vhost)
)
context.fail_and_return!(result.message) if result.failure?

result = Wordmove::Actions::RunLocalCommand.execute(
cli_options: context.cli_options,
logger: context.logger,
command: wpcli_search_replace_command(context, :wordpress_path)
)
context.fail_and_return!(result.message) if result.failure?
%i[vhost wordpress_path].each do |key|
command = search_replace_command(context, key)
context.logger.task_step true, command
begin
system(command, exception: true)
rescue RuntimeError, SystemExit => e
context.fail_and_return!("Local command status reports an error: #{e.message}")
end
end

context.logger.success 'Local DB adapted'
end

# Construct the command to deflate a compressed file as a string.
#
# @param file_path [String] The path where the file to be deflated is located
# @return [String] the command
# @!scope class
def self.uncompress_command(context)
command = ['gzip']
command << '-d'
command << '-f'
command << "\"#{context.db_paths.local.gzipped_path}\""
command.join(' ')
end

def self.import_db_command(context)
"wp db import #{context.db_paths.local.path} --allow-root --quiet " \
"--path=#{wpcli_config_path(context)}"
end

# Compose and returns the search-replace command. It's intended to be
# used from a +LightService::Action+
#
# @param context [LightService::Context] The context of an action
# @param config_key [:vhost, :wordpress_path] Determines what will be replaced in DB
# @return [String]
# @!scope class
def self.search_replace_command(context, config_key)
unless %i[vhost wordpress_path].include?(config_key)
raise ArgumentError, "Unexpected `config_key` #{config_key}.:vhost" \
'or :wordpress_path expected'
end

[
'wp search-replace',
"--path=#{wpcli_config_path(context)}",
'"\A' + context.dig(:remote_options, config_key) + '\Z"', # rubocop:disable Style/StringConcatenation
'"' + context.dig(:local_options, config_key) + '"', # rubocop:disable Style/StringConcatenation
'--regex-delimiter="|"',
'--regex',
'--precise',
'--quiet',
'--skip-columns=guid',
'--all-tables',
'--allow-root'
].join(' ')
end
end
end
end
44 changes: 29 additions & 15 deletions lib/wordmove/actions/backup_local_db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,41 @@ class BackupLocalDb
next context
end

result = Wordmove::Actions::RunLocalCommand.execute(
cli_options: context.cli_options,
logger: context.logger,
command: mysql_dump_command(
env_db_options: context.local_options[:database],
save_to_path: context.db_paths.backup.local.path
)
)
context.fail_and_return!(result.message) if result.failure?
context.logger.task_step true, dump_command(context)

result = Wordmove::Actions::RunLocalCommand.execute(
cli_options: context.cli_options,
logger: context.logger,
command: compress_command(file_path: context.db_paths.backup.local.path)
)
context.fail_and_return!(result.message) if result.failure?
begin
system(dump_command(context), exception: true)
rescue RuntimeError, SystemExit => e
context.fail_and_return!("Local command status reports an error: #{e.message}")
end

context.logger.task_step true, compress_command(context)

begin
system(compress_command(context), exception: true)
rescue RuntimeError, SystemExit => e
context.fail_and_return!("Local command status reports an error: #{e.message}")
end

context.logger.success(
"Backup saved at #{context.db_paths.backup.local.gzipped_path}"
)
end

def self.dump_command(context)
"wp db export #{context.db_paths.backup.local.path} --allow-root --quiet"
end

def self.compress_command(context)
command = ['nice']
command << '-n'
command << '0'
command << 'gzip'
command << '-9'
command << '-f'
command << "\"#{context.db_paths.backup.local.path}\""
command.join(' ')
end
end
end
end
4 changes: 1 addition & 3 deletions lib/wordmove/assets/wordmove_schema_local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mapping:
vhost:
pattern: /^https?:\/\//
wordpress_path:
required: true
database:
type: map
required: true
Expand All @@ -15,9 +16,6 @@ mapping:
required: true
host:
required: true
mysqldump_options:
port:
charset:
paths:
type: map
mapping:
Expand Down
Loading

0 comments on commit 11120be

Please sign in to comment.