class SSHBro::Retriever

Public Class Methods

new(token) click to toggle source
# File lib/ssh_bro.rb, line 29
def initialize(token)
  @seeder = GoogleDocSeed.new(token)
end

Public Instance Methods

apply_ansible_template(h) click to toggle source
# File lib/ssh_bro.rb, line 83
def apply_ansible_template(h)
  words = [ h[:aliases].split(' ').first,
            "ansible_ssh_host=#{h[:hostname]}",
            "ansible_ssh_user=#{h[:user]}" ]
  words.join(" ")
end
apply_ssh_template(h) click to toggle source
# File lib/ssh_bro.rb, line 59
def apply_ssh_template(h)
  lines = [ "Host #{h[:aliases]}",
            "Hostname #{h[:hostname]}",
            "User #{h[:user]}",
            "RemoteForward 52698 localhost:52698" ].join("\n  ")


  lines.prepend("# #{h[:comment]}\n\n") if h[:comment]

  lines
end
retrieve(doc_id) click to toggle source
# File lib/ssh_bro.rb, line 33
def retrieve(doc_id)
  @csv = CSV.parse(@seeder.to_csv_string(doc_id), {
    headers: true,
    header_converters: :symbol,
    converters: [
      -> (f) { f.respond_to?(:empty?) && f.empty? ? nil : f },
      -> (f) { f.respond_to?(:gsub) ? f.gsub(/\s+$/, '') : f },
      -> (f) { f.respond_to?(:to_yaml_boolean) ? f.to_yaml_boolean : f },
      :all
    ]
  }).map(&:to_hash)
    .reject { |h| h[:aliases].nil? || h[:aliases].empty? || h[:use] != true }
end
to_ansible_hosts() click to toggle source
# File lib/ssh_bro.rb, line 71
def to_ansible_hosts
  hosts = @csv.map { |h| h.merge(text: apply_ansible_template(h)) }
  groups = hosts.group_by do |h| 
    "; " + [h[:provider], h[:owner]].join(" - ") + "\n[#{h[:group]}]\n"
  end

  groups.map do |group_name, group|
    body = group.map { |h| h[:text] }.join("\n")
    group_name + body
  end.join("\n\n")
end
to_ssh_hosts() click to toggle source
# File lib/ssh_bro.rb, line 47
def to_ssh_hosts
  # TODO: validate
  hosts = @csv.map { |h| h.merge(text: apply_ssh_template(h)) }
  groups = hosts.group_by { |h| [h[:provider], h[:owner]].join(" - ") }

  groups.map do |group_name, group|
    header = ('#' * 45) + "\n# #{group_name}\n" + ('#' * 45) + "\n\n"
    body = group.map { |h| h[:text] }.join("\n\n")
    header + body
  end.join("\n\n")
end
to_yaml() click to toggle source
# File lib/ssh_bro.rb, line 90
def to_yaml
  @csv.map { |h| h.reduce({}) { |memo,(k,v)| memo.merge(k.to_s => v) } }.to_yaml
end