Class: HashiCorp::VagrantVMwarefusion::Driver::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-mount/actions/providers/vmware-fusion/driver/base.rb

Instance Method Summary collapse

Instance Method Details

#find_free_port(vm_info) ⇒ Object

Raises:

  • (KeyError)


73
74
75
76
77
78
79
80
# File 'lib/vagrant-mount/actions/providers/vmware-fusion/driver/base.rb', line 73

def find_free_port(vm_info)
  data = vm_info.find {|k, v| v == 'cdrom-raw' }
  raise KeyError unless data
  @logger.debug "Found data: #{data.inspect}"
  raise KeyError unless data[0] =~ /([a-z]+)(\d+):(\d+)\.devicetype/
  @logger.debug "type: #{$1}, device: #{$2}, port: #{$3}"
  "#{$1}#{$2}:#{$3}"
end

#find_iso(vm_info, mount_point) ⇒ Object

Raises:

  • (KeyError)


63
64
65
66
67
68
69
70
71
# File 'lib/vagrant-mount/actions/providers/vmware-fusion/driver/base.rb', line 63

def find_iso(vm_info, mount_point)
  data = vm_info.find {|k, v| v =~ /.*\/#{mount_point}/ }
  raise KeyError unless data
  @logger.debug "Found data: #{data.inspect}"
  data[0] =~ /([a-z]+)(\d+):(\d+)\.filename/
  raise KeyError unless data[0] =~ /([a-z]+)(\d+):(\d+)\.filename/
  @logger.debug "type: #{$1}, device: #{$2}, port: #{$3}"
  "#{$1}#{$2}:#{$3}"
end

#mount(mount_point) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/vagrant-mount/actions/providers/vmware-fusion/driver/base.rb', line 7

def mount(mount_point)
  @logger.info "Mounting #{mount_point}"
  info = vminfo(vmx_path)
  begin
    controller_name = find_iso(info, mount_point)
    @logger.debug "Already mounted on #{controller_name}"
    return 1
  rescue KeyError
    @logger.debug "Not mounted, we are good to go"
  end
  begin
    controller_name = find_free_port(info)
    @logger.info "Mounting on: #{controller_name}"
    info["#{controller_name}.devicetype"]     = "cdrom-image"
    info["#{controller_name}.filename"]       = Pathname.new(mount_point).realpath.to_s
    info["#{controller_name}.present"]        = "TRUE"
    info["#{controller_name}.autodetect"]     = "TRUE"
    info["#{controller_name}.startconnected"] = "TRUE"
    vmsave(vmx_path, info)
    return 1
  rescue KeyError
    @logger.error "Could not find any free port"
    return 0
  end
end

#unmount(mount_point, remove_device = false) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/vagrant-mount/actions/providers/vmware-fusion/driver/base.rb', line 33

def unmount(mount_point, remove_device=false)
  @logger.info "Unmounting #{mount_point}"
  info = vminfo(vmx_path)
  begin
    controller_name = find_iso(info, mount_point)
    @logger.info "Mounted on #{controller_name}"
    info["#{controller_name}.devicetype"]     = "cdrom-raw"
    info["#{controller_name}.filename"]       = "auto detect"
    info["#{controller_name}.present"]        = "TRUE"
    info["#{controller_name}.autodetect"]     = "TRUE"
    info["#{controller_name}.startconnected"] = "FALSE"
    vmsave(vmx_path, info)
    return 1
  rescue KeyError
    @logger.debug "Not mounted, nothing to do"
    return 0
  end
end

#vminfo(path) ⇒ Object



52
53
54
# File 'lib/vagrant-mount/actions/providers/vmware-fusion/driver/base.rb', line 52

def vminfo(path)
  return Hash[*File.readlines(path).map {|line| line.scan(/^(.+?) = "(.*?)"$/m)}.flatten]
end

#vmsave(path, vm_info) ⇒ Object



56
57
58
59
60
61
# File 'lib/vagrant-mount/actions/providers/vmware-fusion/driver/base.rb', line 56

def vmsave(path, vm_info)
  temp_path="#{path}.new"
  @logger.debug "Saving in: #{temp_path}"
  File.open("#{temp_path}", "w") { |f| vm_info.each{|k,v| f.puts "#{k} = \"#{v}\""}}
  FileUtils.mv temp_path, path, force: true
end