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

Add --keep-synced-files to the 'apply' command #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions lib/hocho/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ def show(name)
method_option :dry_run, type: :boolean, default: false, aliases: %w(-n)
method_option :exclude, type: :string, default: '', aliases: %w(-e)
method_option :driver, type: :string
method_option :keep_synced_files, type: :boolean, default: false,
desc: "Keep the recipes on a server after run (for bundler and mitamae drivers)"
def apply(name)
hosts = inventory.filter({name: name}, exclude_filters: {name: options[:exclude]})
if hosts.empty?
Expand Down Expand Up @@ -84,8 +86,11 @@ def apply(name)
driver_options: config[:driver_options] || {},
).run(
dry_run: options[:dry_run],
keep_synced_files: options[:keep_synced_files]
)
end
rescue Hocho::Utils::Finder::NotFound => e
abort e.message
end

private
Expand Down
4 changes: 3 additions & 1 deletion lib/hocho/drivers/bundler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
module Hocho
module Drivers
class Bundler < SshBase
def initialize(host, base_dir: '.', initializers: [], itamae_options: [], bundle_without: [], bundle_path: nil, deploy_options: {})
def initialize(host, base_dir: '.', initializers: [], itamae_options: [], bundle_without: [], bundle_path: nil,
deploy_options: {}, keep_synced_files: false, **)
super host, base_dir: base_dir, initializers: initializers

@itamae_options = itamae_options
@bundle_without = bundle_without
@bundle_path = bundle_path
@deploy_options = deploy_options
@keep_synced_files = keep_synced_files
end

def run(dry_run: false)
Expand Down
2 changes: 1 addition & 1 deletion lib/hocho/drivers/itamae_ssh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
module Hocho
module Drivers
class ItamaeSsh < Base
def initialize(host, base_dir: '.', initializers: [], itamae_options: [])
def initialize(host, base_dir: '.', initializers: [], itamae_options: [], **)
super host, base_dir: base_dir, initializers: initializers
@itamae_options = itamae_options
end
Expand Down
5 changes: 4 additions & 1 deletion lib/hocho/drivers/mitamae.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
module Hocho
module Drivers
class Mitamae < SshBase
def initialize(host, base_dir: '.', mitamae_path: 'mitamae', mitamae_prepare_script: [], mitamae_outdate_check_script: nil, initializers: [], mitamae_options: [], deploy_options: {})
def initialize(host, base_dir: '.', mitamae_path: 'mitamae', mitamae_prepare_script: [],
mitamae_outdate_check_script: nil, initializers: [], mitamae_options: [],
deploy_options: {}, keep_synced_files: false, **)
super host, base_dir: base_dir, initializers: initializers

@mitamae_path = mitamae_path
@mitamae_prepare_script = mitamae_prepare_script
@mitamae_outdate_check_script = mitamae_outdate_check_script
@mitamae_options = mitamae_options
@deploy_options = deploy_options
@keep_synced_files = keep_synced_files
end

def run(dry_run: false)
Expand Down
10 changes: 4 additions & 6 deletions lib/hocho/drivers/ssh_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ def ssh
end

def finalize
return if @keep_synced_files

remove_host_tmpdir!
remove_host_shmdir!
end

# @param deploy_dir [String] path on the server to copy the files to. If not specified, an automatic dir in /tmp is used
# @param shm_prefix [Array<String>] additional directories that will be copied to /dev/shm on the server
def deploy(deploy_dir: nil, shm_prefix: [])
@host_basedir = deploy_dir if deploy_dir

Expand Down Expand Up @@ -44,12 +48,6 @@ def deploy(deploy_dir: nil, shm_prefix: [])
end

yield
ensure
if !deploy_dir || !keep_synced_files
cmd = "rm -rf #{host_basedir.shellescape}"
puts "=> #{host.name} $ #{cmd}"
ssh_run(cmd, error: false)
end
end

private
Expand Down
5 changes: 4 additions & 1 deletion lib/hocho/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ def initialize(host, driver: nil, base_dir: '.', initializers: [], driver_option

attr_reader :host, :driver, :base_dir, :initializers

def run(dry_run: false)
def run(dry_run: false, keep_synced_files: false)
puts "=> Running on #{host.name} using #{best_driver_name}"
driver_options = @driver_options[best_driver_name] || {}

driver_options[:keep_synced_files] = keep_synced_files

driver = best_driver.new(host, base_dir: base_dir, initializers: initializers, **driver_options)
driver.run(dry_run: dry_run)
ensure
Expand Down
8 changes: 8 additions & 0 deletions spec/drivers/bundler_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'spec_helper'
require 'hocho/drivers/bundler'

RSpec.describe Hocho::Drivers::Bundler do
it "ignores unknown initialize arguments" do
expect { described_class.new(double, foo: :bar) }.not_to raise_error
end
end
8 changes: 8 additions & 0 deletions spec/drivers/itamae_ssh_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'spec_helper'
require 'hocho/drivers/itamae_ssh'

RSpec.describe Hocho::Drivers::ItamaeSsh do
it "ignores unknown initialize arguments" do
expect { described_class.new(double, foo: :bar) }.not_to raise_error
end
end
8 changes: 8 additions & 0 deletions spec/drivers/mitamae_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'spec_helper'
require 'hocho/drivers/mitamae'

RSpec.describe Hocho::Drivers::Mitamae do
it "ignores unknown initialize arguments" do
expect { described_class.new(double, foo: :bar) }.not_to raise_error
end
end
38 changes: 38 additions & 0 deletions spec/runner_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'spec_helper'
require 'hocho/runner'
require 'hocho/drivers/bundler'
require 'hocho/drivers/itamae_ssh'
require 'hocho/drivers/mitamae'

RSpec.describe Hocho::Runner do
let(:host) { double(:host, name: "example.com") }

it "runs bundler" do
instance = described_class.new(host, driver: :bundler)
expect_any_instance_of(Hocho::Drivers::Bundler).to receive(:run)

expect { instance.run }.to output("=> Running on example.com using bundler\n").to_stdout
end

it "runs itamae via ssh" do
instance = described_class.new(host, driver: :itamae_ssh)
expect_any_instance_of(Hocho::Drivers::ItamaeSsh).to receive(:run)

expect { instance.run }.to output("=> Running on example.com using itamae_ssh\n").to_stdout
end

it "runs mitamae" do
instance = described_class.new(host, driver: :mitamae)
expect_any_instance_of(Hocho::Drivers::Mitamae).to receive(:run)

expect { instance.run }.to output("=> Running on example.com using mitamae\n").to_stdout
end

it "complains about unknown driver" do
instance = described_class.new(host, driver: :foo)

expect { instance.run }
.to raise_error(Hocho::Utils::Finder::NotFound)
.and output("=> Running on example.com using foo\n").to_stdout
end
end