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.



21
22
23
24
# File 'lib/vgrnt/base.rb', line 21

def initialize(*args)
  super(*args)
  @logger = Vgrnt::Util::Logger
end

Instance Method Details

#np(*args) ⇒ Object



94
95
96
97
# File 'lib/vgrnt/base.rb', line 94

def np(*args)
  command = (["VAGRANT_NO_PLUGINS=1", "vagrant" ] + args).join(" ")
  Vgrnt::Util::Exec.popen(command)
end

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



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
57
58
59
60
61
# File 'lib/vgrnt/base.rb', line 27

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

  Vgrnt::Util::Exec.popen(ssh_command)
end

#ssh_config(target = "default") ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/vgrnt/base.rb', line 64

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/vgrnt/base.rb', line 76

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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
# File 'lib/vgrnt/base.rb', line 106

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")
  Vgrnt::Util::Exec.popen(command)
end