Skip to content

Instantly share code, notes, and snippets.

@tyaakow

tyaakow/deploy.rb

Last active Oct 17, 2018
Embed
What would you like to do?
# This file and guide does not consist of the perfect practices, or most complete
# set of procedures, but it showcases things we can do with mina, and introduces
# the audience to the subject. Starting from here, readers will build much more complete
# and polished scripts. Sometimes we need reusable, quick shortcuts that will make
# developer's lives easier - hopefully this is one of those tips.
require 'mina/rails'
require 'mina/git'
set :term_mode, nil
set :application_name, 'mina 2'
set :user, 'root'
set :from_dir, '/my/origin/dir/'
set :from_dbname, "origin_db"
set :from_dbuser, "origin_db_user"
set :from_dbpass, "origin_db_pass"
set :to_dir, '/my/target/dir'
set :to_dbname, "target_db"
set :to_dbuser, "target_db_user"
set :to_dbpass, "target_db_pass"
set :domain1, 'origin-domain.com'
set :domain2, 'target-domain.com'
# with these two functions we assign a remote domain for every task
# by invoking :server0 or :server1
# in this way we solve one mina limitation - of using only one remote destination.
# by assigning a destination on every task, we are able to coordinate actions on more than one server,
# - but the limitation is still of one remote server per task -
# because one task equals one bash script executed on a remote server
task :server0 do
set :domain, 'origin-domain.com'
end
task :server1 do
set :domain, 'target-domain.com'
end
# here we prepare database dump on origin server
# - this is not the perfect practice, but if someone can access the root command history, then
# the intruder access to database would be the least of our problems.
# database is deleted in cleanup task
task :db_prepare do
invoke :server0
command %{ cd #{fetch(:from_dir)}}
comment "dumping db on origin"
command %{mysqldump -u #{fetch(:from_dbuser)} -p#{fetch(:from_dbpass)} #{fetch(:from_dbname)} > .tmp_db_00.sql 2> /dev/null }
comment "...dumped."
command %{hostname}
end
# installing wp cli on the destination helps us execute search remote for the domain
# wp cli could do a lot more for us on both the origin (db dump) and destination server,
# this merely showcases various possibilities
task :cli_install do
invoke :server1
comment "installing wp cli on destination..."
command %{curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar && chmod +x wp-cli.phar && mv wp-cli.phar /usr/local/bin/wp}
end
# migration, using rsync to fetch the origin with database,
# then importing the database, deleting dump, and configuring wp-config.php
task :migrate do
invoke :server1
in_path(fetch(:to_dir)) do
comment "fetching remote website..."
command %{rsync -azh root@#{fetch(:domain1)}:#{fetch(:from_dir)}/ . -e "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"}
comment "mysql import..."
command %{mysql -u #{fetch(:to_dbuser)} -p#{fetch(:to_dbpass)} #{fetch(:to_dbname)} < .tmp_db_00.sql 2> /dev/null }
command %{rm .tmp_db_00.sql }
comment "db imported"
comment "adjusting database access"
command %{sed -i 's/database_name_here/#{fetch(:to_dbname)}/g' wp-config-sample.php}
command %{sed -i 's/username_here/#{fetch(:to_dbuser)}/g' wp-config-sample.php}
command %{sed -i 's/password_here/#{fetch(:to_dbpass)}/g' wp-config-sample.php}
command %{mv wp-config-sample.php wp-config.php}
end
end
# we use wp cli to change old domain in wp database into new one
task :change_domain do
invoke :server1
in_path(fetch(:to_dir)) do
comment "changing the domain..."
command %{wp search-replace //#{fetch(:domain1)} //#{fetch(:domain2)} --allow-root }
command %{wp search-replace www.#{fetch(:domain1)} www.#{fetch(:domain2)} --allow-root }
comment "changed."
end
end
# after the migration, we delete the database dump on the origin server
task :cleanup do
invoke :server0
in_path(fetch(:from_dir)) do
comment "deleting the dump..."
command %{ rm .tmp_db_00.sql }
command %{hostname}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.