Class: VagrantPlugins::ProviderVeertu::Driver::Version_5_0

Inherits:
Base
  • Object
show all
Defined in:
lib/vagrant-veertu/driver/version_5_0.rb

Overview

Driver for Veertu 1.2.0.x

Instance Method Summary collapse

Methods inherited from Base

#execute, #max_network_adapters, #raw, #share_folders

Constructor Details

#initialize(uuid) ⇒ Version_5_0

Returns a new instance of Version_5_0.



12
13
14
15
16
17
# File 'lib/vagrant-veertu/driver/version_5_0.rb', line 12

def initialize(uuid)
  super()

  @logger = Log4r::Logger.new("vagrant::provider::veertu_5_0")
  @uuid = uuid
end

Instance Method Details

#clear_forwarded_portsObject



23
24
25
26
27
# File 'lib/vagrant-veertu/driver/version_5_0.rb', line 23

def clear_forwarded_ports
  read_forwarded_ports(@uuid).each do |nic, name, _, _|
    execute("modify", @uuid, 'delete', 'port_forwarding', name)
  end
end

#deleteObject



29
30
31
# File 'lib/vagrant-veertu/driver/version_5_0.rb', line 29

def delete
  execute("delete",'--yes', @uuid)
end

#enable_adapters(adapters) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/vagrant-veertu/driver/version_5_0.rb', line 278

def enable_adapters(adapters)
  vm_describe = get_vm_describe(@uuid)
  puts vm_describe
  network_cards = vm_describe['hardware']["network cards"]
  if network_cards.is_a?(Hash)
    network_cards = [network_cards]
  end
  if network_cards.nil?
    network_cards = []
  end
  
  puts adapters
  network_cards.each do |net_card| # set our net cards to be desired type by config 
    adapter = adapters.pop()
    set_card(net_card['card index'], adapter[:type])
  end
  adapters.each do |adapter| # in case there are more adapters - add them
    add_card(adapter[:type])
  end
end

#execute_command(command) ⇒ Object



33
34
35
# File 'lib/vagrant-veertu/driver/version_5_0.rb', line 33

def execute_command(command)
  execute(*command)
end

#export(path) ⇒ Object



37
38
39
# File 'lib/vagrant-veertu/driver/version_5_0.rb', line 37

def export(path)
  execute("export", @uuid, path.to_s, '--fmt=box')
end

#forward_ports(ports) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/vagrant-veertu/driver/version_5_0.rb', line 41

def forward_ports(ports)
  ports.each do |options|
    name = options[:name]
    protocol = options[:protocol] || "tcp"
    host_ip = options[:hostip] || "127.0.0.1"
    host_port = options[:hostport]
    guest_ip = options[:guestip] || ""
    guest_port = options[:guestport]
    execute('--machine-readable', 'modify', @uuid, 'add', 'port_forwarding', name, '--host-ip',
            host_ip, '--host-port', host_port.to_s, '--protocol', protocol, '--guest-ip',
            guest_ip, '--guest-port', guest_port.to_s)

  end
end

#get_machine_id(machine_name) ⇒ Object



56
57
58
59
# File 'lib/vagrant-veertu/driver/version_5_0.rb', line 56

def get_machine_id(machine_name)
  vm = get_vm_info(machine_name)
  return vm['id'] 
end

#haltObject



65
66
67
# File 'lib/vagrant-veertu/driver/version_5_0.rb', line 65

def halt
  execute("shutdown", @uuid)
end

#import(box) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/vagrant-veertu/driver/version_5_0.rb', line 69

def import(box)
  box = Vagrant::Util::Platform.cygwin_windows_path(box)

  output = ""
  total = ""
  last  = 0

  # Dry-run the import to get the suggested name and path
  @logger.debug("Doing dry-run import to determine parallel-safe name...")
  output = execute("import", "-n", box)
  result = /Suggested VM name "(.+?)"/.match(output)
  puts result
  if !result
    raise Errors::VeertuManageNoNameError, output: output
  end
  suggested_name = result[1].to_s

  # Append millisecond plus a random to the path in case we're
  # importing the same box elsewhere.
  specified_name = "#{suggested_name}_#{(Time.now.to_f * 1000.0).to_i}_#{rand(100000)}"
  @logger.debug("-- Parallel safe name: #{specified_name}")

  # Build the specified name param list
  name_params = [
    "--os-family", "0",
    "--name", specified_name,
  ]

  # Extract the disks list and build the disk target params
  disk_params = []
  disks = output.scan(/(\d+): Hard disk image: source image=.+, target path=(.+),/)
  disks.each do |unit_num, path|
    disk_params << "--vsys"
    disk_params << "0"
    disk_params << "--unit"
    disk_params << unit_num
    disk_params << "--disk"
    disk_params << path.reverse.sub("/#{suggested_name}/".reverse, "/#{specified_name}/".reverse).reverse # Replace only last occurrence
  end
  success_json = execute("--machine-readable", "import", box , *name_params, *disk_params)
  begin
    success = JSON.parse(success_json)
    if success['status'] == 'ok'
      puts 'imported successfully'
    end
  rescue JSON::ParserError
    puts 'json parse error'
    return nil
  end
  puts specified_name
  machine_id = get_machine_id(specified_name)
  puts machine_id
  return machine_id 
end

#read_bridged_interfacesObject



19
20
21
# File 'lib/vagrant-veertu/driver/version_5_0.rb', line 19

def read_bridged_interfaces
  raise Errors::VeertuUnsupported, action: 'bridged interfaces'
end

#read_forwarded_ports(uuid = nil, active_only = false) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/vagrant-veertu/driver/version_5_0.rb', line 124

def read_forwarded_ports(uuid=nil, active_only=false)
  uuid ||= @uuid

  @logger.debug("read_forward_ports: uuid=#{uuid} active_only=#{active_only}")

  results = []
  current_nic = 'nat'
  vm_info = get_vm_info(uuid)
  if active_only && vm_info['status'] != "running"
        return []
  end
  port_forwarding_rules = vm_info['port_forwarding']
  
  port_forwarding_rules.each do |rule|
    result = [current_nic, rule['name'], rule['host_port'], rule['guest_port']]
    results << result
    @logger.debug("  - #{result.inspect}")
  end

  results
end

#read_guest_ip(adapter_number) ⇒ Object



184
185
186
187
# File 'lib/vagrant-veertu/driver/version_5_0.rb', line 184

def read_guest_ip(adapter_number)
  vm_info = get_vm_info(@uuid)
  return vm_info['ip']
end

#read_host_only_interfacesObject



146
147
148
149
150
151
152
153
154
# File 'lib/vagrant-veertu/driver/version_5_0.rb', line 146

def read_host_only_interfaces
  vms_array = get_vm_list()
  info = {}
  vms_array.each do |vm|
    info['name'] = vm['name']
    info['uuid'] = vm['id']
  end
  info
end

#read_mac_addressObject



263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/vagrant-veertu/driver/version_5_0.rb', line 263

def read_mac_address
  vm_describe = get_vm_describe(@uuid)
  network_cards = vm_describe['hardware']["network cards"]
  if not network_cards
    return nil
  end
  if network_cards.is_a?(Hash)
    network_card = network_cards
  else
    network_card = network_cards.pop(0)
  end
  puts network_card
  return network_card['mac address']
end

#read_network_interfacesObject



156
157
158
# File 'lib/vagrant-veertu/driver/version_5_0.rb', line 156

def read_network_interfaces
  return { '0' => {:type => :nat}, '1' => {:type => :bridged}}
end

#read_stateObject



160
161
162
163
164
165
166
# File 'lib/vagrant-veertu/driver/version_5_0.rb', line 160

def read_state
  vm = get_vm_info(@uuid)
  status =  vm['status']
  @logger.debug('getting machine status')
  @logger.debug(status)
  return status.to_sym
end

#read_used_portsObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/vagrant-veertu/driver/version_5_0.rb', line 168

def read_used_ports
  ports = []
  vms_array = get_vm_list()
  vms_array.each do |vm|
      # Ignore our own used ports
      uuid = vm['uuid']
      next if uuid  == @uuid

      read_forwarded_ports(uuid, true).each do |_, _, hostport, _|
        ports << hostport
      end
  end

  ports
end

#read_vmsObject



189
190
191
192
193
194
195
196
# File 'lib/vagrant-veertu/driver/version_5_0.rb', line 189

def read_vms
  results = {}
  vms = get_vm_list()
  vms.each do |vm|
    results[vm[:id]] = vm[:name]
  end
  results
end

#resumeObject



213
214
215
# File 'lib/vagrant-veertu/driver/version_5_0.rb', line 213

def resume
  start(nil)
end

#set_name(name) ⇒ Object



61
62
63
# File 'lib/vagrant-veertu/driver/version_5_0.rb', line 61

def set_name(name)
  execute('modify', @uuid, 'set', '--name', name)  
end

#ssh_port(expected_port) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/vagrant-veertu/driver/version_5_0.rb', line 199

def ssh_port(expected_port)
  expected_port = expected_port.to_s
  @logger.debug("Searching for SSH port: #{expected_port.inspect}")

  # Look for the forwarded port only by comparing the guest port
  read_forwarded_ports.each do |_, _, hostport, guestport|
    if guestport == expected_port
      return hostport
    end
  end

  nil
end

#start(mode) ⇒ Object

Raises:

  • (Vagrant::Errors::VeertuManageError)


217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/vagrant-veertu/driver/version_5_0.rb', line 217

def start(mode)
  if mode == 'headless'
    execute('--machine-readable', 'modify', @uuid, 'set', '--headless', '1')
  elsif mode == 'gui'
    execute('--machine-readable', 'modify', @uuid, 'set', '--headless', '0')
  end
  json_success  = execute('--machine-readable', 'start', @uuid)
  result = JSON.parse(json_success)
  if result['status'] == 'OK'
    return true
  end

  # If we reached this point then it didn't work out.
  raise Vagrant::Errors::VeertuManageError,
    command: command.inspect,
    stderr: r.stderr
end

#suspendObject



235
236
237
# File 'lib/vagrant-veertu/driver/version_5_0.rb', line 235

def suspend
  execute("pause", @uuid)
end

#verify!Object



239
240
241
242
243
# File 'lib/vagrant-veertu/driver/version_5_0.rb', line 239

def verify!
  # This command sometimes fails if kernel drivers aren't properly loaded
  # so we just run the command and verify that it succeeded.
  execute("list", retryable: true)
end

#vm_exists?(uuid) ⇒ Boolean

Returns:

  • (Boolean)


245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/vagrant-veertu/driver/version_5_0.rb', line 245

def vm_exists?(uuid)
  5.times do |i|
    info = get_vm_info(uuid)
    if info
      return true
    else
      return false
    end
    sleep 2
  end

  # If we reach this point, it means that we consistently got the
  # failure, do a standard veertumanage now. This will raise an
  # exception if it fails again.
  execute("show", uuid)
  return true
end