Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wpcli_all_the_way #625

Merged
merged 3 commits into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
AllCops:
TargetRubyVersion: 2.7.1
TargetRubyVersion: 3.0.2
DisplayCopNames: true
DisplayStyleGuide: true
NewCops: enable
Expand Down
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.7.1
3.0.2
8 changes: 4 additions & 4 deletions 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,18 +34,16 @@
require 'wordmove/wordpress_directory'
require 'wordmove/version'
require 'wordmove/environments_list'
require 'wordmove/wpcli'

require 'wordmove/generators/movefile_adapter'
require 'wordmove/generators/movefile'

require 'wordmove/db_paths_config'

require 'wordmove/actions/helpers'
require 'wordmove/actions/ssh/helpers'
require 'wordmove/actions/ftp/helpers'
Dir[File.join(__dir__, 'wordmove/actions/**/*.rb')].sort.each { |file| require file }
Dir[File.join(__dir__, 'wordmove/organizers/**/*.rb')].sort.each { |file| require file }
Dir[File.join(__dir__, 'wordmove/actions/**/*.rb')].each { |file| require file }
Dir[File.join(__dir__, 'wordmove/organizers/**/*.rb')].each { |file| require file }

module Wordmove
# Interactors' namespce. Interactors are called "Actions", following the LightService convention.
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
Loading