Class: VagrantPlugins::CommandSSHConfigTTL::Command

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Util::SafePuts
Defined in:
lib/vagrant-ssh-config-ttl/command.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.synopsisObject



10
11
12
# File 'lib/vagrant-ssh-config-ttl/command.rb', line 10

def self.synopsis
  "outputs OpenSSH valid configuration to connect to the machine"
end

Instance Method Details

#executeObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/vagrant-ssh-config-ttl/command.rb', line 14

def execute
  options = {}

  opts = OptionParser.new do |o|
    o.banner = "Usage: vagrant ssh-config-ttl [options] [name]"
    o.separator ""
    o.separator "Options:"
    o.separator ""

    o.on("--host NAME", "Name the host for the config") do |h|
      options[:host] = h
    end
  end

  argv = parse_options(opts)
  return if !argv

  with_target_vms(argv) do |machine|
    ssh_info = machine.ssh_info
    raise Vagrant::Errors::SSHNotReady if ssh_info.nil?

    variables = {
      template_root: File.join(VagrantPlugins::CommandSSHConfigTTL.root, "templates"),
      host_key: options[:host] || machine.name || "vagrant",
      ssh_host: ssh_info[:host],
      ssh_port: ssh_info[:port],
      ssh_user: ssh_info[:username],
      private_key_path: ssh_info[:private_key_path],
      forward_agent: ssh_info[:forward_agent],
      forward_x11:   ssh_info[:forward_x11],
      proxy_command: ssh_info[:proxy_command],
      ssh_command: ssh_info[:ssh_command]
    }

    # Render the template and output directly to STDOUT
    template = "tera_term_language_template"
    safe_puts(Vagrant::Util::TemplateRenderer.render(template, variables))
    safe_puts
  end

  # Success, exit status 0
  0
end