2016-03-01 10:51:47 -05:00
|
|
|
require "English"
|
2016-02-28 18:16:11 -05:00
|
|
|
require "fileutils"
|
|
|
|
require "pathname"
|
2014-04-22 11:36:44 -04:00
|
|
|
|
2013-06-14 12:09:32 -04:00
|
|
|
module TestApp
|
2013-09-27 06:45:55 -04:00
|
|
|
extend self
|
|
|
|
|
|
|
|
def install
|
|
|
|
install_test_app_with(default_config)
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_config
|
2016-03-01 23:13:36 -05:00
|
|
|
<<-CONFIG
|
2013-09-27 06:45:55 -04:00
|
|
|
set :deploy_to, '#{deploy_to}'
|
|
|
|
set :repo_url, 'git://github.com/capistrano/capistrano.git'
|
2013-11-02 07:08:10 -04:00
|
|
|
set :branch, 'master'
|
2015-02-12 20:34:09 -05:00
|
|
|
set :ssh_options, { keys: "\#{ENV['HOME']}/.vagrant.d/insecure_private_key", auth_methods: ['publickey'] }
|
2013-09-27 06:45:55 -04:00
|
|
|
server 'vagrant@localhost:2220', roles: %w{web app}
|
|
|
|
set :linked_files, #{linked_files}
|
|
|
|
set :linked_dirs, #{linked_dirs}
|
Make Airbrussh the default format; add format opts
To accommodate Airbrussh, this commit makes the following changes:
1. Adds the airbrussh gem as a dependency
2. Bumps the sshkit dependency to 1.8.0 (for the new `use_format` method)
3. Introduces a `:format_options` setting
Out of the box, Capistrano now defaults to the following:
set :format, :airbussh
It also requires the airbrussh gem at the appropriate point in the Capistrano
boot process, which allows Airbrussh to register its `deploy:failed` hook.
We also change the default Airbrussh settings slightly, to acknowledge the fact
that Airbrussh will now be the first impression new Capistrano users have of the
tool.
Specifically:
* Enable command output (hiding the command output is tidy but probably will
confuse new users who will not know what their deploy script is doing)
* Disable the "using airbrussh format" banner at startup
Finally, this commit adds an official mechanism for sending a hash of options to
the formatter initializer. This lets users easily change Airbrussh's settings.
set :format_options, color: false, truncate: false, ...
If `:format_options` are set, Capistrano will send them to the formatter object
using SSHKit's `use_format` factory method. As of the latest SSHKit, all
formatters inheriting from `Formatter::Abstract` now accept an options hash
(although currently only Airbrussh does anything with them).
2015-12-25 21:40:34 -05:00
|
|
|
set :format_options, log_file: nil
|
2017-02-09 23:35:50 -05:00
|
|
|
set :local_user, #{current_user.inspect}
|
2016-03-01 23:13:36 -05:00
|
|
|
CONFIG
|
2013-09-27 06:45:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def linked_files
|
|
|
|
%w{config/database.yml}
|
|
|
|
end
|
|
|
|
|
|
|
|
def linked_file
|
|
|
|
shared_path.join(linked_files.first)
|
|
|
|
end
|
|
|
|
|
|
|
|
def linked_dirs
|
2015-06-03 12:50:49 -04:00
|
|
|
%w{bin log public/system}
|
2013-09-27 06:45:55 -04:00
|
|
|
end
|
|
|
|
|
2013-06-14 12:09:32 -04:00
|
|
|
def create_test_app
|
2013-09-27 06:45:55 -04:00
|
|
|
FileUtils.rm_rf(test_app_path)
|
|
|
|
FileUtils.mkdir(test_app_path)
|
2013-06-14 12:09:32 -04:00
|
|
|
|
2016-02-28 18:16:11 -05:00
|
|
|
File.open(gemfile, "w+") do |file|
|
2013-06-14 12:09:32 -04:00
|
|
|
file.write "gem 'capistrano', path: '#{path_to_cap}'"
|
|
|
|
end
|
|
|
|
|
|
|
|
Dir.chdir(test_app_path) do
|
2016-03-01 10:46:36 -05:00
|
|
|
`bundle`
|
2013-06-14 12:09:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-16 08:08:15 -04:00
|
|
|
def install_test_app_with(config)
|
|
|
|
create_test_app
|
|
|
|
Dir.chdir(test_app_path) do
|
2016-03-01 10:46:36 -05:00
|
|
|
`bundle exec cap install STAGES=#{stage}`
|
2013-06-16 08:08:15 -04:00
|
|
|
end
|
|
|
|
write_local_deploy_file(config)
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_local_deploy_file(config)
|
2016-02-28 18:16:11 -05:00
|
|
|
File.open(test_stage_path, "w") do |file|
|
2013-06-16 08:08:15 -04:00
|
|
|
file.write config
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-21 14:55:57 -04:00
|
|
|
def write_local_stage_file(filename, config=nil)
|
|
|
|
File.open(test_app_path.join("config/deploy/#{filename}"), "w") do |file|
|
|
|
|
file.write(config) if config
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-18 16:18:55 -05:00
|
|
|
def append_to_deploy_file(config)
|
2016-02-28 18:16:11 -05:00
|
|
|
File.open(test_stage_path, "a") do |file|
|
2014-01-18 16:18:55 -05:00
|
|
|
file.write config + "\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-11 12:24:37 -04:00
|
|
|
def prepend_to_capfile(config)
|
|
|
|
current_capfile = File.read(capfile)
|
2016-02-28 18:16:11 -05:00
|
|
|
File.open(capfile, "w") do |file|
|
2013-10-11 12:24:37 -04:00
|
|
|
file.write config
|
|
|
|
file.write current_capfile
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-16 08:08:15 -04:00
|
|
|
def create_shared_directory(path)
|
|
|
|
FileUtils.mkdir_p(shared_path.join(path))
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_shared_file(path)
|
2016-02-28 18:16:11 -05:00
|
|
|
File.open(shared_path.join(path), "w")
|
2013-06-16 08:08:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def cap(task)
|
2013-10-11 12:24:37 -04:00
|
|
|
run "bundle exec cap #{stage} #{task}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def run(command)
|
2014-01-18 16:18:55 -05:00
|
|
|
output = nil
|
2013-06-16 08:08:15 -04:00
|
|
|
Dir.chdir(test_app_path) do
|
2016-03-01 10:46:36 -05:00
|
|
|
output = `#{command}`
|
2013-06-16 08:08:15 -04:00
|
|
|
end
|
2016-03-01 10:36:05 -05:00
|
|
|
[$CHILD_STATUS.success?, output]
|
2013-06-16 08:08:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def stage
|
2016-02-28 18:16:11 -05:00
|
|
|
"test"
|
2013-06-16 08:08:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_stage_path
|
2016-02-28 18:16:11 -05:00
|
|
|
test_app_path.join("config/deploy/test.rb")
|
2013-06-16 08:08:15 -04:00
|
|
|
end
|
2013-06-14 12:09:32 -04:00
|
|
|
|
|
|
|
def test_app_path
|
2016-02-28 18:16:11 -05:00
|
|
|
Pathname.new("/tmp/test_app")
|
2013-06-14 12:09:32 -04:00
|
|
|
end
|
|
|
|
|
2013-06-16 08:08:15 -04:00
|
|
|
def deploy_to
|
2016-02-28 18:16:11 -05:00
|
|
|
Pathname.new("/home/vagrant/var/www/deploy")
|
2013-06-16 08:08:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def shared_path
|
2016-02-28 18:16:11 -05:00
|
|
|
deploy_to.join("shared")
|
2013-06-16 08:08:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def current_path
|
2016-02-28 18:16:11 -05:00
|
|
|
deploy_to.join("current")
|
2013-06-16 08:08:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def releases_path
|
2016-02-28 18:16:11 -05:00
|
|
|
deploy_to.join("releases")
|
2013-06-16 08:08:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def release_path
|
2013-09-27 06:45:55 -04:00
|
|
|
releases_path.join(timestamp)
|
|
|
|
end
|
|
|
|
|
|
|
|
def timestamp
|
|
|
|
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
|
|
|
end
|
|
|
|
|
|
|
|
def repo_path
|
2016-02-28 18:16:11 -05:00
|
|
|
deploy_to.join("repo")
|
2013-06-16 08:08:15 -04:00
|
|
|
end
|
|
|
|
|
2013-06-14 12:09:32 -04:00
|
|
|
def path_to_cap
|
2016-02-28 18:16:11 -05:00
|
|
|
File.expand_path(".")
|
2013-06-14 12:09:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def gemfile
|
2016-02-28 18:16:11 -05:00
|
|
|
test_app_path.join("Gemfile")
|
2013-06-14 12:09:32 -04:00
|
|
|
end
|
2013-06-16 08:08:15 -04:00
|
|
|
|
2013-08-16 11:59:36 -04:00
|
|
|
def capfile
|
2016-02-28 18:16:11 -05:00
|
|
|
test_app_path.join("Capfile")
|
2013-08-16 11:59:36 -04:00
|
|
|
end
|
|
|
|
|
2013-06-16 08:08:15 -04:00
|
|
|
def current_user
|
2017-02-09 23:35:50 -05:00
|
|
|
"(GitHub Web Flow) via ShipIt"
|
2013-06-16 08:08:15 -04:00
|
|
|
end
|
2013-08-16 11:59:36 -04:00
|
|
|
|
|
|
|
def task_dir
|
2016-02-28 18:16:11 -05:00
|
|
|
test_app_path.join("lib/capistrano/tasks")
|
2013-08-16 11:59:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def copy_task_to_test_app(source)
|
|
|
|
FileUtils.cp(source, task_dir)
|
|
|
|
end
|
2013-10-11 12:24:37 -04:00
|
|
|
|
|
|
|
def config_path
|
2016-02-28 18:16:11 -05:00
|
|
|
test_app_path.join("config")
|
2013-10-11 12:24:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def move_configuration_to_custom_location(location)
|
|
|
|
prepend_to_capfile(
|
2016-03-01 23:13:36 -05:00
|
|
|
<<-CONFIG
|
2013-10-11 12:24:37 -04:00
|
|
|
set :stage_config_path, "app/config/deploy"
|
|
|
|
set :deploy_config_path, "app/config/deploy.rb"
|
2016-03-01 23:13:36 -05:00
|
|
|
CONFIG
|
2013-10-11 12:24:37 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
location = test_app_path.join(location)
|
|
|
|
FileUtils.mkdir_p(location)
|
|
|
|
FileUtils.mv(config_path, location)
|
|
|
|
end
|
2016-05-17 10:25:38 -04:00
|
|
|
|
|
|
|
def git_wrapper_path
|
|
|
|
"/tmp/git-ssh-my_app_name-#{stage}-#{current_user}.sh"
|
|
|
|
end
|
2013-06-14 12:09:32 -04:00
|
|
|
end
|