class Bow::SshHelper

Constants

COPY_TOOLS

Attributes

conn[R]

Public Class Methods

method_missing(m, *args, &block) click to toggle source
# File lib/bow/ssh_helper.rb, line 8
def method_missing(m, *args, &block)
  new(args.shift).send m, *args, &block
end
new(conn, app) click to toggle source
# File lib/bow/ssh_helper.rb, line 17
def initialize(conn, app)
  @app = app
  @conn = conn
end

Public Instance Methods

copy(source, target) click to toggle source
# 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_script() click to toggle source
# 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_tasks() click to toggle source
# 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_tool() click to toggle source
# 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_dir() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_provision() click to toggle source
# 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) click to toggle source
# File lib/bow/ssh_helper.rb, line 87
def run(cmd)
  return cmd if @app.debug?
  Open3.capture3(cmd).first(2)
end