Class: Bow::SshHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/bow/ssh_helper.rb

Constant Summary collapse

COPY_TOOLS =
{ rsync: Ssh::Rsync, scp: Ssh::Scp }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conn, app) ⇒ SshHelper

Returns a new instance of SshHelper.



17
18
19
20
# File 'lib/bow/ssh_helper.rb', line 17

def initialize(conn, app)
  @app = app
  @conn = conn
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



15
16
17
# File 'lib/bow/ssh_helper.rb', line 15

def conn
  @conn
end

Class Method Details

.method_missing(m, *args, &block) ⇒ Object



8
9
10
# File 'lib/bow/ssh_helper.rb', line 8

def method_missing(m, *args, &block)
  new(args.shift).send m, *args, &block
end

Instance Method Details

#copy(source, target) ⇒ Object



27
28
29
30
# File 'lib/bow/ssh_helper.rb', line 27

def copy(source, target)
  source = source.match?(%r{^\/}) ? source : File.join(Dir.pwd, source)
  copy_tool.call(source, target)
end

#copy_preprovision_scriptObject



45
46
47
48
49
50
# File 'lib/bow/ssh_helper.rb', line 45

def copy_preprovision_script
  copy(
    @app.config.host[:pre_script],
    @app.config.guest_from_host[:pre_script]
  )
end

#copy_rake_tasksObject



52
53
54
# File 'lib/bow/ssh_helper.rb', line 52

def copy_rake_tasks
  copy(@app.inventory.location, @app.config.guest_from_host[:rake_dir])
end

#copy_toolObject



56
57
58
59
60
61
62
63
64
# File 'lib/bow/ssh_helper.rb', line 56

def copy_tool
  return @copy_tool if @copy_tool
  unless (key = @app.options[:copy_tool]&.to_sym)
    key = COPY_TOOLS.keys.detect { |t| system("which #{t} &>/dev/null") }
    error = "Either #{COPY_TOOLS.keys.join(' or ')} should be installed!"
    raise error unless key
  end
  @copy_tool = COPY_TOOLS[key].new(self)
end

#ensure_base_dirObject



41
42
43
# File 'lib/bow/ssh_helper.rb', line 41

def ensure_base_dir
  execute("mkdir -p #{@app.config.guest_from_host[:base_dir]}")
end

#execute(cmd, timeout = 10) ⇒ Object



22
23
24
25
# File 'lib/bow/ssh_helper.rb', line 22

def execute(cmd, timeout = 10)
  cmd = "ssh -o ConnectTimeout=#{timeout} #{conn} #{cmd}"
  run(cmd)
end

#merge_results(result1, *results) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/bow/ssh_helper.rb', line 66

def merge_results(result1, *results)
  merged = result1.map { |v| [v] }
  results.each_with_object(merged) do |result, acc|
    result.each_with_index do |val, i|
      if val.is_a? Array
        acc[i] += val
      else
        acc[i] << val
      end
    end
  end
end

#prepare_provisionObject



32
33
34
35
36
37
38
39
# File 'lib/bow/ssh_helper.rb', line 32

def prepare_provision
  @app.inventory.ensure!
  results = []
  results << ensure_base_dir
  results << copy_preprovision_script
  results << copy_rake_tasks
  merge_results(*results)
end

#run(cmd) ⇒ Object



79
80
81
82
# File 'lib/bow/ssh_helper.rb', line 79

def run(cmd)
  return cmd if @app.debug?
  Open3.capture3(cmd).first(2)
end