Class: Vgrnt::App

Inherits:
Thor
  • Object
show all
Defined in:
lib/vgrnt/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ App

Returns a new instance of App.



38
39
40
41
# File 'lib/vgrnt/base.rb', line 38

def initialize(*args)
  super(*args)
  @logger = Logger.new
end

Instance Method Details

#ssh(target_vm = 'default', *args) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/vgrnt/base.rb', line 44

def ssh(target_vm = 'default', *args)

  # removes --, since it's just a separator
  target_vm = 'default' if (target_vm == '--')
  args.shift if args.first == '--'

  # @logger.debug [target_vm, args].inspect ; exit 0

  if File.exists?(".vgrnt-sshconfig")
    @logger.debug "Using .vgrnt-sshconf"
    ssh_command = "ssh -F .vgrnt-sshconfig #{target_vm} #{args.join(' ')}"
  else
    @logger.debug ".vgrnt-sshconfig file not found; using VBoxManage to get connection info."
    machine = Util::VirtualBox::runningMachines()[target_vm]
    ssh_info = Util::VirtualBox::machineSSH(target_vm)

    if machine && machine[:state] == 'running'
      # found by running "VAGRANT_LOG=debug vagrant ssh"
      default_ssh_args = [
        "vagrant@#{ssh_info[:ssh_ip]}",
        "-p", ssh_info[:ssh_port],
        "-o", "DSAAuthentication=yes", "-o", "LogLevel=FATAL", "-o", "StrictHostKeyChecking=no",
        "-o", "UserKnownHostsFile=/dev/null", "-o", "IdentitiesOnly=yes",
        "-i", "~/.vagrant.d/insecure_private_key"
      ]

      ssh_command = "ssh #{default_ssh_args.join(" ")} #{args.join(' ')}"
    else
      @logger.error "Specified target vm (#{target_vm}) not running."
      exit 1
    end
  end

  # Using IO.popen is important:
  # - POpen3 ignores STDIN. Backticks buffer stdout. Kernel::exec breaks rspec.
  # - getc instead of gets fixes the 1 line lag.
  IO.popen(ssh_command) do |io|
    while c = io.getc do 
      putc c
    end
  end
end

#ssh_config(target = "default") ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/vgrnt/base.rb', line 88

def ssh_config(target="default")
  output = `VAGRANT_NO_PLUGINS=1 vagrant ssh-config #{target}`
  if $? && !output.empty?
    IO.write('.vgrnt-sshconfig', output)
    @logger.notice "Created ./.vgrnt-sshconfig with the following: " + output
  else
    @logger.error "Call to 'vagrant ssh-config' failed."
    exit 1
  end
end

#status(target_vm = 'default') ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/vgrnt/base.rb', line 100

def status(target_vm = 'default')
  machines = Util::VirtualBox::runningMachines()

  puts "Current machine states (as detected by vgrnt):\n\n"
  machines.each do |machine_name, machine|
    puts "#{machine_name.ljust(25)} #{machine[:state]} (virtualbox)"
  end
  defined_vms = Util::Vagrantfile::defined_vms()

  defined_vms.each do |machine_name|
    machine_name = machine_name.to_s   # they're usually symbols
    if !machines[machine_name]
      puts "#{machine_name.ljust(25)} not created (virtualbox)"
    end
  end
end

#vboxmanage(target_vm = 'default', *args) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/vgrnt/base.rb', line 123

def vboxmanage(target_vm = 'default', *args)

  # removes --, since it's just a separator
  target_vm = 'default' if (target_vm == '--')
  args.shift if args.first == '--'

  # derived as follows: `VBoxManage | sed '1,/Commands/d' | grep '^  [a-z]' | awk '{ print $1; }' | sort -u`
  vboxmanage_commands_all  = %w{
      adoptstate bandwidthctl clonehd clonevm closemedium controlvm
      convertfromraw createhd createvm debugvm dhcpserver discardstate
      export extpack getextradata guestcontrol guestproperty hostonlyif
      import list metrics modifyhd modifyvm registervm setextradata
      setproperty sharedfolder showhdinfo showvminfo snapshot startvm
      storageattach storagectl unregistervm usbfilter}

  # not of form `VBoxManage showvminfo uuid|name ...`
  vboxmanage_commands_special = %w{
      list registervm createvm import export closemedium createhd clonehd
      convertfromraw getextradata setextradata setproperty usbfilter
      sharedfolder guestproperty metrics hostonlyif dhcpserver extpack}

  # of form `VBoxManage showvminfo uuid|name ...`
  vboxmanage_commands_standard = vboxmanage_commands_all - vboxmanage_commands_special
  machine = Util::VirtualBox::runningMachines()[target_vm]
  if !machine
    @logger.error "The specified target vm (#{target_vm}) has not been started."
    exit 1
  end

  vboxmanage_subcommand = args.shift

  if vboxmanage_commands_standard.include?(vboxmanage_subcommand)
    @logger.debug("Performing UUID insertion for standard vboxmanage command: #{vboxmanage_subcommand}")
    command = (["VBoxManage", vboxmanage_subcommand, machine[:id]] + args).join(" ")
  else
    # TODO: handle substitution for commands like `usbfilter add 0 --target <uuid|name>`

    @logger.debug "Non-standard vboxmanage command detected (#{vboxmanage_subcommand}). Substituting 'VM_ID' for VM id."

    # [VM_ID] is an optional literal token which will be replaced by the UUID of the VM referenced by Vagrant
    args.map! { |a| a == 'VM_UUID' ? machine[:id] : a }
    command = (["VBoxManage", vboxmanage_subcommand] + args).join(" ")
  end

  @logger.debug "Executing: #{command}"
  #TODO: windows support (path to VBoxManage.exe")
  Open3.popen3(command) do |stdin, stdout, stderr|
    @logger.stdout stdout.read
    @logger.error stderr.read
  end
end