Class: VagrantPlugins::VMM::Driver

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-vmm/driver.rb

Constant Summary collapse

ERROR_REGEXP =
/===Begin-Error===(.+?)===End-Error===/m
OUTPUT_REGEXP =
/===Begin-Output===(.+?)===End-Output===/m

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(machine) ⇒ Driver

Returns a new instance of Driver.



16
17
18
19
20
# File 'lib/vagrant-vmm/driver.rb', line 16

def initialize(machine)
  @machine = machine
  @vm_id = machine.id
  @logger = Log4r::Logger.new("vagrant::provider::vmm")
end

Instance Attribute Details

#machineObject (readonly)

Returns the value of attribute machine.



14
15
16
# File 'lib/vagrant-vmm/driver.rb', line 14

def machine
  @machine
end

#vm_idObject (readonly)

Returns the value of attribute vm_id.



13
14
15
# File 'lib/vagrant-vmm/driver.rb', line 13

def vm_id
  @vm_id
end

Instance Method Details

#delete_vm(options) ⇒ Object



67
68
69
70
# File 'lib/vagrant-vmm/driver.rb', line 67

def delete_vm(options)
  options['vm_id'] = vm_id
  execute('delete_vm.ps1', options)
end

#execute(path, options, print_output = false) ⇒ Object



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
57
58
59
60
# File 'lib/vagrant-vmm/driver.rb', line 22

def execute(path, options, print_output = false)
  r = execute_powershell(path, options) { |io_name, data|
    if print_output && ( io_name == :stderr || io_name == :stdout )
      if !OUTPUT_REGEXP.match(data) && !ERROR_REGEXP.match(data)
        if io_name == :stdout
          machine.ui.output(data)
        end
        if io_name == :stderr
          machine.ui.error(data)
        end
      end
    end
  }
  if r.exit_code != 0
    raise Errors::PowerShellError,
      script: path,
      stderr: r.stderr
  end

  # We only want unix-style line endings within Vagrant
  r.stdout.gsub!("\r\n", "\n")
  r.stderr.gsub!("\r\n", "\n")

  error_match  = ERROR_REGEXP.match(r.stdout)
  output_match = OUTPUT_REGEXP.match(r.stdout)

  if error_match
    data = JSON.parse(error_match[1])

    # We have some error data.
    raise Errors::PowerShellError,
      script: path,
      stderr: data["error"]
  end

  # Nothing
  return nil if !output_match
  return JSON.parse(output_match[1])
end

#get_current_state(options) ⇒ Object



62
63
64
65
# File 'lib/vagrant-vmm/driver.rb', line 62

def get_current_state(options)
  options['vm_id'] = vm_id
  execute('get_vm_status.ps1', options)
end

#import(options) ⇒ Object



97
98
99
# File 'lib/vagrant-vmm/driver.rb', line 97

def import(options)
  execute('import_vm.ps1', options)
end

#read_guest_ip(options) ⇒ Object



72
73
74
75
# File 'lib/vagrant-vmm/driver.rb', line 72

def read_guest_ip(options)
  options['vm_id'] = vm_id
  execute('get_network_config.ps1', options, true)
end

#resume(options) ⇒ Object



77
78
79
80
# File 'lib/vagrant-vmm/driver.rb', line 77

def resume(options)
  options['vm_id'] = vm_id
  execute('resume_vm.ps1', options)
end

#start(options) ⇒ Object



82
83
84
85
# File 'lib/vagrant-vmm/driver.rb', line 82

def start(options)
  options['vm_id'] = vm_id
  execute('start_vm.ps1', options)
end

#stop(options) ⇒ Object



87
88
89
90
# File 'lib/vagrant-vmm/driver.rb', line 87

def stop(options)
  options['vm_id'] = vm_id
  execute('stop_vm.ps1', options)
end

#suspend(options) ⇒ Object



92
93
94
95
# File 'lib/vagrant-vmm/driver.rb', line 92

def suspend(options)
  options['vm_id'] = vm_id
  execute("suspend_vm.ps1", options)
end

#sync_folders(options) ⇒ Object



101
102
103
# File 'lib/vagrant-vmm/driver.rb', line 101

def sync_folders(options)
  execute('sync_folders.ps1', options, true)
end