Class: Hocho::Drivers::Mitamae

Inherits:
SshBase show all
Defined in:
lib/hocho/drivers/mitamae.rb

Instance Attribute Summary

Attributes inherited from Base

#base_dir, #host, #initializers

Instance Method Summary collapse

Methods inherited from SshBase

#deploy, #finalize, #ssh

Methods inherited from Base

#finalize, #run_list

Constructor Details

#initialize(host, base_dir: '.', mitamae_path: 'mitamae', mitamae_prepare_script: [], mitamae_outdate_check_script: nil, initializers: [], mitamae_options: [], deploy_options: {}) ⇒ Mitamae

Returns a new instance of Mitamae.



6
7
8
9
10
11
12
13
14
# File 'lib/hocho/drivers/mitamae.rb', line 6

def initialize(host, base_dir: '.', mitamae_path: 'mitamae', mitamae_prepare_script: [], mitamae_outdate_check_script: nil, initializers: [], mitamae_options: [], deploy_options: {})
  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
end

Instance Method Details

#mitamae_available?Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
# File 'lib/hocho/drivers/mitamae.rb', line 24

def mitamae_available?
  exitstatus, _ = if @mitamae_path.start_with?('/')
    ssh_run("test -x #{@mitamae_path.shellescape}", error: false)
  else
    ssh_run("#{@mitamae_path.shellescape} version 2>/dev/null >/dev/null", error: false)
  end
  exitstatus == 0
end

#mitamae_outdated?Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
# File 'lib/hocho/drivers/mitamae.rb', line 33

def mitamae_outdated?
  if @mitamae_outdate_check_script
    exitstatus, _ = ssh_run("export HOCHO_MITAMAE_PATH=#{@mitamae_path.shellescape}; #{@mitamae_outdate_check_script}", error: false)
    exitstatus == 0
  else
    false
  end
end

#prepare_mitamaeObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/hocho/drivers/mitamae.rb', line 42

def prepare_mitamae
  return if mitamae_available? && !mitamae_outdated?
  script = [*@mitamae_prepare_script].join("\n\n")
  if script.empty?
    raise "We have to prepare MItamae, but not mitamae_prepare_script is specified"
  end
  prepare_sudo do |sh, sudovars, sudocmd|
    log_prefix = "=> #{host.name} # "
    log_prefix_white = ' ' * log_prefix.size
    puts "#{log_prefix}#{script.each_line.map{ |_| "#{log_prefix_white}#{_.chomp}" }.join("\n")}"

    ssh_run("bash") do |c|
      set_ssh_output_hook(c)

      c.send_data("cd #{host_basedir.shellescape}\n#{sudovars}\n#{sudocmd} bash <<-'HOCHOEOS'\n#{script}HOCHOEOS\n")
      c.eof!
    end
  end
  availability, outdated = mitamae_available?, mitamae_outdated?
  if !availability || outdated
    status = [availability ? nil : 'unavailable', outdated ? 'outdated' : nil].compact.join(' and ')
    raise "prepared MItamae, but it's still #{status}"
  end
end

#run(dry_run: false) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/hocho/drivers/mitamae.rb', line 16

def run(dry_run: false)
  ssh # Test connection
  deploy(**@deploy_options) do
    prepare_mitamae
    run_mitamae(dry_run: dry_run)
  end
end

#run_mitamae(dry_run: false) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/hocho/drivers/mitamae.rb', line 67

def run_mitamae(dry_run: false)
  with_host_node_json_file do
    itamae_cmd = [@mitamae_path, 'local', '-j', host_node_json_path, *@mitamae_options]
    itamae_cmd.push('--dry-run') if dry_run
    # itamae_cmd.push('--color') if $stdout.tty?
    itamae_cmd.push(*run_list)

    prepare_sudo do |sh, sudovars, sudocmd|
      puts "=> #{host.name} # #{itamae_cmd.shelljoin}"
      ssh_run("bash") do |c|
        set_ssh_output_hook(c)

        c.send_data("cd #{host_basedir.shellescape}\n#{sudovars}\n#{sudocmd} #{itamae_cmd.shelljoin}\n")
        c.eof!
      end
    end
  end
end