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



34
35
36
37
# File 'lib/bow/ssh_helper.rb', line 34

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

#copy_preprovision_scriptObject



53
54
55
56
57
58
# File 'lib/bow/ssh_helper.rb', line 53

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

#copy_rake_tasksObject



60
61
62
# File 'lib/bow/ssh_helper.rb', line 60

def copy_rake_tasks
  copy("#{@app.inventory.location}/*", @app.config.guest_from_host[:rake_dir])
end

#copy_toolObject



64
65
66
67
68
69
70
71
72
# File 'lib/bow/ssh_helper.rb', line 64

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



49
50
51
# File 'lib/bow/ssh_helper.rb', line 49

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

#execute(cmd, timeout = 10, chdir = false) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bow/ssh_helper.rb', line 22

def execute(cmd, timeout = 10, chdir = false)
  cmd = if chdir
          format("'cd %s && %s'",
                 @app.config.guest_from_host[:rake_dir],
                 cmd)
        else
          format("'%s'", cmd)
        end
  cmd = "ssh -o ConnectTimeout=#{timeout} #{conn} #{cmd}"
  run(cmd)
end

#merge_results(result1, *results) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/bow/ssh_helper.rb', line 74

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



39
40
41
42
43
44
45
46
47
# File 'lib/bow/ssh_helper.rb', line 39

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

#run(cmd) ⇒ Object



87
88
89
90
# File 'lib/bow/ssh_helper.rb', line 87

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