class Pennyworth::Vagrant

Public Class Methods

new(vagrant_dir) click to toggle source
# File lib/pennyworth/vagrant.rb, line 20
def initialize(vagrant_dir)
  @vagrant_dir = vagrant_dir
end

Public Instance Methods

run(*args) click to toggle source
# File lib/pennyworth/vagrant.rb, line 24
def run(*args)
  # Many vagrant commands don't have a --provider option, but they fail
  # because they can't find VirtualBox. The following code is a crude
  # workaround.
  Dir.chdir(@vagrant_dir) do
    with_env "VAGRANT_DEFAULT_PROVIDER" => "libvirt" do
      Cheetah.run "vagrant", *args
    end
  end
end
ssh_config(vm_name) click to toggle source
# File lib/pennyworth/vagrant.rb, line 35
def ssh_config(vm_name)
  output = Dir.chdir(@vagrant_dir) do
    if vm_name
      run "ssh-config", vm_name, :stdout => :capture
    else
      config = ""
      status = run "status", :stdout => :capture
      status.scan(/\n(.*?)\s*running/).each do |vm_name|
        config += run "ssh-config", vm_name.first, :stdout => :capture
      end
      config
    end
  end

  parse_ssh_config_output(output)
end

Private Instance Methods

parse_ssh_config_output(output) click to toggle source
# File lib/pennyworth/vagrant.rb, line 54
def parse_ssh_config_output(output)
  # See http://linux.die.net/man/5/ssh_config for description of the format.

  config = {}
  host = nil

  output.each_line do |line|
    line.chomp!("\n")

    next if line.empty? || line.start_with?("#")

    m = /^\s*(?<key>\w+)(\s*=\s*|\s+)(?<value>.*)$/.match(line)
    raise "Invalid line in SSH config: #{line.inspect}." unless m

    if m[:key] == "Host"
      host = m[:value]
      config[host] = {}
    else
      raise "Missing Host keyword before #{m[:key]}." unless host

      config[host][m[:key]] = m[:value]
    end
  end

  config
end