Skip to content

Commit

Permalink
This, if it will born, will be version 6
Browse files Browse the repository at this point in the history
Fix bug in AdaptRemoteDb

Undefined method `waring` was called on logger

Fix wrong signature on method call in AdaptRemoteDb

Warn when db adapt is skipper

SetupContextForDb won't run unless necessary

Update some log messages for better clarity

Fix mu-plugins key in mu_plugins. Fixes #619

Please the rubocop

Remove FTP support and cleanup as consequently

Re-implement #597 in this branch

Improve wpcli_search_replace_command with argument check

Add yard documentation

Fix bug in FilterAndSetupTasksToRun action

Was getting key by string on a symbolized hash

Migreate from Thor do dry-cli

Improve UX a bit w/ better log messages

Remove unused WordpressDirectory::HelperMethods module

Re-enable --debug flag

Update version: will stick to a pre-release

Update scaffold and namespace for organizers

WIP: re-implement FTP support

WIP: re-implement FTP support

Fix typo and reword a variable

WIP: re-implement FTP support

WIP: re-implement FTP support

WIP: re-implement FTP support

WIP: re-implement FTP support

WIP: re-implement FTP support

WIP: re-implement FTP support

WIP: re-implement FTP support

WIP: re-implement FTP support

Update get_file_spec

Update hook_spec

Update rspec config

Add adapt_local_db_spec

Various cleanups, typos, rubocop disables

Fix Wordmove::Movefile by removing - never triggered - wrong logic

Update deps and please the (new) rubocop
  • Loading branch information
alessandro-fazzi committed Dec 26, 2021
1 parent 0fc74a1 commit 94cba67
Show file tree
Hide file tree
Showing 88 changed files with 1,900 additions and 834 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ AllCops:
TargetRubyVersion: 2.7.1
DisplayCopNames: true
DisplayStyleGuide: true
NewCops: enable

Exclude:
- 'bin/*'
Expand Down
3 changes: 2 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ require 'rspec/core/rake_task'
require 'rubocop/rake_task'

RSpec::Core::RakeTask.new(:spec)
task default: :spec
RuboCop::RakeTask.new(:rubocop)

task default: :rubocop
task default: :spec
3 changes: 2 additions & 1 deletion exe/wordmove
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
$LOAD_PATH.unshift File.expand_path('../lib', __dir__)

require 'wordmove'
Wordmove::CLI.start

Dry::CLI.new(Wordmove::CLI::Commands).call
15 changes: 13 additions & 2 deletions lib/wordmove.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
require 'active_support/core_ext'
require 'colorize'
require 'dotenv'
require 'dry/cli'
require 'dry-configurable'
require 'dry/files'
require 'erb'
require 'kwalify'
require 'light-service'
require 'logger'
require 'open-uri'
require 'ostruct'
require 'thor'
require 'thor/group'
require 'yaml'

require 'photocopier'
Expand All @@ -32,6 +32,7 @@
require 'wordmove/wordpress_directory'
require 'wordmove/version'
require 'wordmove/environments_list'
require 'wordmove/wpcli'

require 'wordmove/generators/movefile_adapter'
require 'wordmove/generators/movefile'
Expand All @@ -40,7 +41,9 @@

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 }

module Wordmove
# Interactors' namespce. Interactors are called "Actions", following the LightService convention.
Expand All @@ -54,4 +57,12 @@ module Actions
module Ssh
end
end

# Organizers are responsible of running organizer procedures putting together Actions
# following business logic requirements.
module Organizers
# Organizers for the Ssh protocol
module Ssh
end
end
end
95 changes: 95 additions & 0 deletions lib/wordmove/actions/adapt_local_db.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
module Wordmove
module Actions
#
# Adapt the local DB for the remote destination.
# "To adapt" in Wordmove jargon means to transform URLs strings into the database. This action
# will substitute local URLs with remote ones, in order to make the DB to work correctly once
# pushed to the remote wordpress installation.
#
class AdaptLocalDb
extend ::LightService::Action
include Wordmove::Actions::Helpers
include Wordmove::Wpcli

expects :local_options,
:remote_options,
:cli_options,
:logger,
:photocopier,
:db_paths

# @!method execute
# @param local_options [Hash] Local host options fetched from
# movefile (with symbolized keys)
# @param remote_options [Hash] Remote host options fetched from
# movefile (with symbolized keys)
# @param cli_options [Hash] Command line options
# @param logger [Wordmove::Logger]
# @param photocopier [Photocopier::SSH|Photocopier::FTP]
# @param db_paths [BbPathsConfig] Configuration object for database
# @!scope class
# @return [LightService::Context] Action's context
executed do |context| # rubocop:disable Metrics/BlockLength
context.logger.task 'Adapt local DB'

unless wp_in_path?
raise UnmetPeerDependencyError, 'WP-CLI is not installed or not in your $PATH'
end

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

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
)
)

if context.cli_options[:no_adapt]
context.logger.warn 'Skipping DB adapt'
else
Wordmove::Actions::RunLocalCommand.execute(
cli_options: context.cli_options,
logger: context.logger,
command: wpcli_search_replace_command(context, :vhost)
)

Wordmove::Actions::RunLocalCommand.execute(
cli_options: context.cli_options,
logger: context.logger,
command: wpcli_search_replace_command(context, :wordpress_path)
)
end

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.adapted_path
)
)

if context.photocopier.is_a? Photocopier::SSH
Wordmove::Actions::RunLocalCommand.execute(
cli_options: context.cli_options,
logger: context.logger,
command: compress_command(file_path: context.db_paths.local.adapted_path)
)
end

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]
)
)
end
end
end
end
87 changes: 87 additions & 0 deletions lib/wordmove/actions/adapt_remote_db.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
module Wordmove
module Actions
#
# Adapt the remote DB for the local destination.
# "To adapt" in Wordmove jargon means to transform URLs strings into the database. This action
# will substitute remote URLs with local ones, in order to make the DB to work correctly once
# pulled to the local wordpress installation.
#
# @note This action is not responsible to download the remote DB nor to backup any DB at all.
# It expects to find a dump of the remote DB into +context.db_paths.local.gzipped_path+
# (SSH) or +context.db_paths.local.path+ (FTP), otherwise it will fail and stop the
# procedure.
#
class AdaptRemoteDb
extend ::LightService::Action
include Wordmove::Actions::Helpers
include Wordmove::Wpcli

expects :local_options,
:cli_options,
:logger,
:db_paths

# @!method execute
# @param local_options [Hash] Local host options fetched from
# movefile (with symbolized keys)
# @param cli_options [Hash] Command line options
# @param logger [Wordmove::Logger]
# @param db_paths [BbPathsConfig] Configuration object for database
# @!scope class
# @return [LightService::Context] Action's context
executed do |context| # rubocop:disable Metrics/BlockLength
context.logger.task 'Adapt remote DB'

unless wp_in_path?
raise UnmetPeerDependencyError, 'WP-CLI is not installed or not in your $PATH'
end

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)
)
end

unless File.exist?(context.db_paths.local.path)
context.fail_and_return!(
"Cannot find the dump file to adapt in #{context.db_paths.local.path}"
)
end

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]
)
)

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?

context.logger.success 'Local DB adapted'
end
end
end
end
54 changes: 54 additions & 0 deletions lib/wordmove/actions/backup_local_db.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module Wordmove
module Actions
#
# Take a backup of the local database and save it in +wp-content/+ folder.
#
class BackupLocalDb
extend ::LightService::Action
include Wordmove::Actions::Helpers

expects :local_options
expects :cli_options
expects :db_paths
expects :logger

# @!method execute
# @param local_options [Hash] Local host options fetched from
# movefile (with symbolized keys)
# @param cli_options [Hash] Command line options
# @param db_paths [BbPathsConfig] Configuration object for database
# @param logger [Wordmove::Logger]
# @!scope class
# @return [LightService::Context] Action's context
executed do |context|
context.logger.task 'Backup local DB'

if simulate?(cli_options: context.cli_options)
context.logger.info 'A backup of the local DB would have been saved into ' \
"#{context.db_paths.backup.local.gzipped_path}, " \
'but you\'re simulating'
next context
end

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
)
)

Wordmove::Actions::RunLocalCommand.execute(
cli_options: context.cli_options,
logger: context.logger,
command: compress_command(file_path: context.db_paths.backup.local.path)
)

context.logger.success(
"Backup saved at #{context.db_paths.backup.local.gzipped_path}"
)
end
end
end
end
11 changes: 6 additions & 5 deletions lib/wordmove/actions/delete_local_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ class DeleteLocalFile
:cli_options

# @!method execute
# @param file_path [String]
# @param logger [Wordmove::Logger]
# @param cli_options [Hash] Command line options (with symbolized keys)
# @return [LightService::Context] Action's context
# @param file_path [String]
# @param logger [Wordmove::Logger]
# @param cli_options [Hash] Command line options (with symbolized keys)
# @!scope class
# @return [LightService::Context] Action's context
executed do |context|
context.logger.task_step true, "delete: '#{context.file_path}'"

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

unless File.exist?(context.file_path)
context.logger.warning 'File does not exist'
context.logger.info "File #{context.file_path} does not exist. Nothing done."
next context
end

Expand Down
33 changes: 23 additions & 10 deletions lib/wordmove/actions/delete_remote_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,39 @@ module Actions
# @note This action is *not* meant to be organized, but as a standalone one.
class DeleteRemoteFile
extend LightService::Action
include Wordmove::Actions::Helpers

expects :photocopier,
:logger,
:command_args
:cli_options,
:remote_file

# @!method execute
# @param photocopier [Photocopier]
# @param logger [Wordmove::Logger]
# @param command_args ((String) remote file path, (String) local file path)
# @return [LightService::Context] Action's context
# @param photocopier [Photocopier]
# @param logger [Wordmove::Logger]
# @param cli_options [Hash] Command line options (with symbolized keys)
# @param remote_file ((String) remote file path)
# @!scope class
# @return [LightService::Context] Action's context
executed do |context|
command = 'delete'

context.logger.task_step false, "#{command}: #{context.command_args.join(' ')}"
_stdout, stderr, exit_code = context.photocopier.send(command, *context.command_args)
context.logger.task_step false, "#{command}: #{context.remote_file}"

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

context.fail! "Error code #{exit_code} returned while deleting file "\
"#{context.command_args.join}: #{stderr}"
_stdout, stderr, exit_code = context.photocopier.send(command, context.remote_file)

next context if exit_code&.zero?

# When +context.photocopier+ is a +Photocopier::FTP+ instance, +delte+ will always
# return +nil+; so it's impossible to correctly fail the context when using
# FTP protocol. The problem is how +Net::FTP+ ruby class behaves.
# IMO this is an acceptable tradeoff.
unless exit_code.nil?
context.fail! "Error code #{exit_code} returned while deleting file "\
"#{context.remote_file}: #{stderr}"
end
end
end
end
Expand Down
Loading

0 comments on commit 94cba67

Please sign in to comment.