vagrant-vmware-dhcp 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8d5f260c0846fb882557b8a0126d4bc905f93af9
4
- data.tar.gz: a76636beea09d3be97f32cf5f23d1df7b520985d
3
+ metadata.gz: e0bc39c6cd747318ab85baf9c099e0744ef8eebc
4
+ data.tar.gz: b236907c228d090f4d935525b99a5f9f83b03a91
5
5
  SHA512:
6
- metadata.gz: a0303e52af1f2727f7c050e84dad78c0360308ada672d15a99ea8b839540af466a52b39173c44324903860762f51b7f54129d369a4619a7484e7e5168cf2ffe8
7
- data.tar.gz: 5153762c66271ffd56cdfa0ce238f688d8c4c2949a619883573283a33e7e5ced5e2c6d75eb03dbc3052d2bd6c1005563af74aee06103974996f985069c8bf382
6
+ metadata.gz: 90aa5e525f2b6ac08fe534db152f41c576878a10d5a65e0a1df0f979997fba67f3dd775bc5801205aeb627d46fbdd6f94a2efb736578b72808c44aebdfe6abc1
7
+ data.tar.gz: 2738e4446d5034abe7175718f604965638eb3963f62ae9954b82af9ab205c360ddea5abbb37c38678e1b906ff00db34f4ab818c85b55a1a4145f0af3abf8786c
@@ -17,7 +17,7 @@ module VagrantPlugins
17
17
 
18
18
  if @env[:machine]
19
19
  if @env[:machine].provider_name == :vmware_fusion or @env[:machine].provider_name == :vmware_workstation
20
- configure_dhcp
20
+ configure_dhcp(@env[:machine])
21
21
  end
22
22
  end
23
23
 
@@ -26,130 +26,29 @@ module VagrantPlugins
26
26
 
27
27
  private
28
28
 
29
- def configure_dhcp
30
- machine = @env[:machine]
29
+ def configure_dhcp(machine)
30
+ # dhcp_manager = VagrantPlugins::VagrantVmwareDhcp::DhcpManager.new(@env[:ui], @logger, machine)
31
+ dhcp_manager = get_dhcp_manager(machine)
31
32
 
32
- vmx_networks = retrieve_vmx_network_settings(machine)
33
+ dhcp_manager.prune()
33
34
 
34
- @logger.debug("After retrieval vmx_networks are #{vmx_networks}")
35
-
36
- network_map = make_network_map(machine, vmx_networks)
37
-
38
- @logger.debug("After retrieval network_map is #{network_map}")
39
-
40
- apply_static_dhcp_config(machine, network_map)
41
- end
42
-
43
- def retrieve_vmx_network_settings(machine)
44
- vmx_networks = {}
45
-
46
- File.open(machine.provider.driver.vmx_path).each_line {
47
- |line|
48
-
49
- matches = /^(ethernet\d+\.)(.+?)\s*=\s*"?(.*?)"?\s*$/.match(line)
50
-
51
- if not matches
52
- next
53
- end
54
-
55
- adapter_name = matches[1]
56
- adapter_property = matches[2]
57
- property_value = matches[3]
58
-
59
- if not vmx_networks.has_key?(adapter_name)
60
- vmx_networks[adapter_name] = {}
61
- end
62
-
63
- vmx_networks[adapter_name][adapter_property] = property_value
64
- }
65
-
66
- vmx_networks_by_mac = {}
67
-
68
- vmx_networks.each {
69
- |adapter_name, properties|
70
-
71
- if not properties.has_key?("address") or not properties.has_key?("vnet")
72
- next
73
- end
74
-
75
- vmx_networks_by_mac[ properties['address'] ] = properties
76
- }
77
-
78
- vmx_networks_by_mac
79
- end
80
-
81
- def make_network_map(machine, vmx_networks)
82
- vm_networks = machine.config.vm.networks.select { |vm_network| vm_network[0] == :private_network and vm_network[1][:ip] and vm_network[1][:mac] }
83
-
84
- network_map = {}
85
-
86
- vm_networks.each { |vm_network|
87
- mac = vm_network[1][ :mac ]
88
- mac = mac.scan(/.{2}/).join(":")
89
-
90
- if not vmx_networks.has_key?(mac)
91
- @logger.error("Missing VMX network configuration for vm_network #{vm_network}")
92
- next
93
- end
94
-
95
- network_map[mac] = { :ip => vm_network[1][:ip], :vnet => vmx_networks[mac]["vnet"], :mac => mac }
96
- }
97
-
98
- network_map
99
- end
100
-
101
- def apply_static_dhcp_config(machine, network_map)
102
-
103
- network_map.each {
104
- |mac, network|
105
-
106
- @logger.info("Pruning DHCP configuration for #{network[:ip]}")
107
- prune_dhcpd_conf(network)
108
-
109
- @env[:ui].error("CONTROL_DHCP is #{machine.config.control_dhcp.enable}")
110
-
111
- if machine.config.control_dhcp.enable
112
- @env[:ui].info("Configuring DHCP for #{network[:ip]} on #{network[:vnet]}")
113
-
114
- write_dhcpd_conf(network)
115
- end
116
- }
117
-
118
- trigger_dhcpd_update
119
-
120
- @env[:ui].info("DHCP Configured")
35
+ if machine.config.control_dhcp.enable
36
+ dhcp_manager.configure()
37
+ end
121
38
 
39
+ dhcp_manager.reload()
122
40
  end
123
41
 
124
- def get_dhcpd_section(network)
125
- netname = [network[:vnet], network[:ip].gsub(/\./, '_')].join('_')
126
-
127
- output = Vagrant::Util::TemplateRenderer.render('dhcpd_static',
128
- mac: network[:mac],
129
- ip: network[:ip],
130
- vnet: network[:vnet],
131
- name: netname,
132
- template_root: VagrantPlugins::VagrantVmwareDhcp.source_root().join("templates")
133
- )
134
- return output
42
+ def get_dhcp_manager(machine)
43
+ if Vagrant::Util::Platform.windows?
44
+ return VagrantPlugins::VagrantVmwareDhcp::DhcpManagerWindows.new(@env[:ui], @logger, machine)
45
+ elsif Vagrant::Util::Platform.linux?
46
+ return VagrantPlugins::VagrantVmwareDhcp::DhcpManagerLinux.new(@env[:ui], @logger, machine)
47
+ elsif Vagrant::Util::Platform.darwin?
48
+ return VagrantPlugins::VagrantVmwareDhcp::DhcpManagerDarwin.new(@env[:ui], @logger, machine)
49
+ end
135
50
  end
136
51
 
137
- # def prune_dhcpd_conf(network)
138
- # raise "This should never happen!"
139
- # end
140
- #
141
- # def dhcpd_conf_location(network)
142
- # raise "This should never happen!"
143
- # end
144
- #
145
- # def write_dhcpd_conf(network)
146
- # raise "This should never happen!"
147
- # end
148
- #
149
- # def trigger_dhcpd_update(network)
150
- # raise "This should never happen!"
151
- # end
152
-
153
52
  end
154
53
  end
155
54
  end
@@ -0,0 +1,52 @@
1
+ require "log4r"
2
+ require "digest"
3
+
4
+ # Someone who's had some sleep recently is welcome to come fill in comments here...
5
+ module VagrantPlugins
6
+ module VagrantVmwareDhcp
7
+ module Action
8
+ class PruneDhcp
9
+ def initialize(app, env)
10
+ @app = app
11
+ @env = env
12
+ @logger = Log4r::Logger.new("vagrant::plugins::vagrant-vmware-dhcp::prune_dhcp")
13
+ end
14
+
15
+ def call(env)
16
+ @env = env
17
+
18
+ if @env[:machine]
19
+ if @env[:machine].provider_name == :vmware_fusion or @env[:machine].provider_name == :vmware_workstation
20
+ @env[:ui].info("Pruning altered DHCP configuration")
21
+ prune_dhcp(@env[:machine])
22
+ end
23
+ end
24
+
25
+ @app.call(@env)
26
+ end
27
+
28
+ private
29
+
30
+ def prune_dhcp(machine)
31
+ # dhcp_manager = VagrantPlugins::VagrantVmwareDhcp::DhcpManager.new(@env[:ui], @logger, machine)
32
+ dhcp_manager = get_dhcp_manager(machine)
33
+
34
+ dhcp_manager.prune()
35
+
36
+ dhcp_manager.reload()
37
+ end
38
+
39
+ def get_dhcp_manager(machine)
40
+ if Vagrant::Util::Platform.windows?
41
+ return VagrantPlugins::VagrantVmwareDhcp::DhcpManagerWindows.new(@env[:ui], @logger, machine)
42
+ elsif Vagrant::Util::Platform.linux?
43
+ return VagrantPlugins::VagrantVmwareDhcp::DhcpManagerLinux.new(@env[:ui], @logger, machine)
44
+ elsif Vagrant::Util::Platform.darwin?
45
+ return VagrantPlugins::VagrantVmwareDhcp::DhcpManagerDarwin.new(@env[:ui], @logger, machine)
46
+ end
47
+ end
48
+
49
+ end
50
+ end
51
+ end
52
+ end
@@ -1,9 +1,7 @@
1
- module AutoNetwork
2
- module Action
3
- require 'vagrant-vmware-dhcp/action/set_mac'
4
- require 'vagrant-vmware-dhcp/action/config_dhcp'
5
- require 'vagrant-vmware-dhcp/action/config_dhcp_darwin'
6
- require 'vagrant-vmware-dhcp/action/config_dhcp_linux'
7
- require 'vagrant-vmware-dhcp/action/config_dhcp_windows'
1
+ module VagrantPlugins
2
+ module VagrantVmwareDhcp
3
+ require_relative 'action/set_mac'
4
+ require_relative 'action/config_dhcp'
5
+ require_relative 'action/prune_dhcp'
8
6
  end
9
7
  end
@@ -0,0 +1,8 @@
1
+ module VagrantPlugins
2
+ module VagrantVmwareDhcp
3
+ require_relative 'dhcp_manager/dhcp_manager.rb'
4
+ require_relative 'dhcp_manager/dhcp_manager_darwin.rb'
5
+ require_relative 'dhcp_manager/dhcp_manager_linux.rb'
6
+ require_relative 'dhcp_manager/dhcp_manager_windows.rb'
7
+ end
8
+ end
@@ -0,0 +1,129 @@
1
+ require "log4r"
2
+ require "digest"
3
+
4
+ # Someone who's had some sleep recently is welcome to come fill in comments here...
5
+ module VagrantPlugins
6
+ module VagrantVmwareDhcp
7
+ class DhcpManager
8
+
9
+ def initialize(ui, logger, machine)
10
+ @ui = ui
11
+ @logger = logger
12
+ @machine = machine
13
+ @network_map = make_network_map(machine)
14
+ end
15
+
16
+ def prune()
17
+ @network_map.each {
18
+ |mac, network|
19
+
20
+ prune_configuration(network)
21
+ }
22
+
23
+ reload_configuration
24
+ end
25
+
26
+ def configure()
27
+ @network_map.each {
28
+ |mac, network|
29
+
30
+ @ui.info("Configuring DHCP for #{network[:ip]} on #{network[:vnet]}")
31
+
32
+ write_configuration(network)
33
+ }
34
+
35
+ @ui.info("DHCP Configured")
36
+ end
37
+
38
+ def reload()
39
+ @ui.info("Reloading DHCP Configuration")
40
+ reload_configuration
41
+ end
42
+
43
+ protected
44
+
45
+ def template_machine_definition(network)
46
+ netname = [network[:vnet], network[:ip].gsub(/\./, '_')].join('_')
47
+
48
+ output = Vagrant::Util::TemplateRenderer.render('dhcpd_static',
49
+ mac: network[:mac],
50
+ ip: network[:ip],
51
+ vnet: network[:vnet],
52
+ name: netname,
53
+ template_root: VagrantPlugins::VagrantVmwareDhcp.source_root().join("templates")
54
+ )
55
+
56
+ @logger.debug("DHCPD template for interface #{network[:vnet]} is #{output}")
57
+
58
+ return output
59
+ end
60
+
61
+ private
62
+
63
+ def make_network_map(machine)
64
+ vmx_networks = retrieve_vmx_network_settings(machine)
65
+
66
+ vm_networks = machine.config.vm.networks.select { |vm_network| vm_network[0] == :private_network and vm_network[1][:ip] and vm_network[1][:mac] }
67
+
68
+ network_map = {}
69
+
70
+ vm_networks.each { |vm_network|
71
+ mac = vm_network[1][ :mac ]
72
+ mac = mac.scan(/.{2}/).join(":")
73
+
74
+ if not vmx_networks.has_key?(mac)
75
+ @logger.error("Missing VMX network configuration for vm_network #{vm_network}")
76
+ next
77
+ end
78
+
79
+ network_map[mac] = { :ip => vm_network[1][:ip], :vnet => vmx_networks[mac]["vnet"], :mac => mac }
80
+ }
81
+
82
+ @logger.debug("After mutating, network_map is #{network_map}")
83
+
84
+ network_map
85
+ end
86
+
87
+ def retrieve_vmx_network_settings(machine)
88
+ vmx_networks = {}
89
+
90
+ File.open(machine.provider.driver.vmx_path).each_line {
91
+ |line|
92
+
93
+ matches = /^(ethernet\d+\.)(.+?)\s*=\s*"?(.*?)"?\s*$/.match(line)
94
+
95
+ if not matches
96
+ next
97
+ end
98
+
99
+ adapter_name = matches[1]
100
+ adapter_property = matches[2]
101
+ property_value = matches[3]
102
+
103
+ if not vmx_networks.has_key?(adapter_name)
104
+ vmx_networks[adapter_name] = {}
105
+ end
106
+
107
+ vmx_networks[adapter_name][adapter_property] = property_value
108
+ }
109
+
110
+ vmx_networks_by_mac = {}
111
+
112
+ vmx_networks.each {
113
+ |adapter_name, properties|
114
+
115
+ if not properties.has_key?("address") or not properties.has_key?("vnet")
116
+ next
117
+ end
118
+
119
+ vmx_networks_by_mac[ properties['address'] ] = properties
120
+ }
121
+
122
+ @logger.debug("After retrieval vmx_networks_by_mac is #{vmx_networks_by_mac}")
123
+
124
+ vmx_networks_by_mac
125
+ end
126
+
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,106 @@
1
+ require "log4r"
2
+ require "digest"
3
+ require "vagrant/util/shell_quote"
4
+ require "vagrant/util/subprocess"
5
+
6
+ # Someone who's had some sleep recently is welcome to come fill in comments here...
7
+ module VagrantPlugins
8
+ module VagrantVmwareDhcp
9
+ class DhcpManagerDarwin < DhcpManager
10
+
11
+ protected
12
+
13
+ def dhcpd_conf_location(network)
14
+ # Locations from https://pubs.vmware.com/workstation-9/index.jsp?topic=%2Fcom.vmware.ws.using.doc%2FGUID-04D783E1-3AB9-4D98-9891-2C58215905CC.html
15
+
16
+ location = "/Library/Preferences/VMware Fusion/#{network[:vnet]}/dhcpd.conf"
17
+
18
+ @logger.debug("Using dhcpd.conf at #{location}")
19
+
20
+ return location
21
+ end
22
+
23
+ def prune_configuration(network)
24
+ @logger.info("Pruning DHCP configuration for #{network[:ip]}")
25
+
26
+ conf_location = dhcpd_conf_location(network)
27
+ escaped_conf_location = Vagrant::Util::ShellQuote.escape(conf_location, "'")
28
+
29
+ mac = network[:mac]
30
+ ip = network[:ip]
31
+
32
+ before = File.open(conf_location).read
33
+ @logger.debug("Before altering, dhcpd.conf content is #{before}")
34
+
35
+ command = []
36
+ command << "sudo" if !File.writable?(conf_location)
37
+ command += [
38
+ "sed", "-E", "-e",
39
+ "/^# VAGRANT-BEGIN: #{mac}/," +
40
+ "/^# VAGRANT-END: #{mac}\s*/ d",
41
+ "-ibak",
42
+ conf_location
43
+ ]
44
+
45
+ system(*command)
46
+
47
+ command = []
48
+ command << "sudo" if !File.writable?(conf_location)
49
+ command += [
50
+ "sed", "-E", "-e",
51
+ "/^# VAGRANT-BEGIN: #{ip}/," +
52
+ "/^# VAGRANT-END: #{ip}\s*/ d",
53
+ "-ibak",
54
+ conf_location
55
+ ]
56
+
57
+ system(*command)
58
+
59
+ after = File.open(conf_location).read
60
+ @logger.debug("After, dhcpd.conf content is #{after}")
61
+ end
62
+
63
+ def write_configuration(network)
64
+ conf_location = dhcpd_conf_location(network)
65
+ escaped_conf_location = Vagrant::Util::ShellQuote.escape(conf_location, "'")
66
+
67
+ sudo_command = ""
68
+ sudo_command = "sudo " if !File.writable?(conf_location)
69
+
70
+ output = template_machine_definition(network)
71
+
72
+ before = File.open(conf_location).read
73
+ @logger.debug("Before altering, dhcpd.conf content is #{before}")
74
+
75
+ output.split("\n").each do |line|
76
+ line = Vagrant::Util::ShellQuote.escape(line, "'")
77
+ system(%Q[echo '#{line}' | #{sudo_command}tee -a '#{escaped_conf_location}' >/dev/null])
78
+ end
79
+
80
+ after = File.open(conf_location).read
81
+ @logger.debug("After, dhcpd.conf content is #{after}")
82
+ end
83
+
84
+ def reload_configuration
85
+ # Per http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1026510
86
+
87
+ vmnet_cli = "/Applications/VMware Fusion.app/Contents/Library/vmnet-cli"
88
+
89
+ configureCommand = [ "sudo", vmnet_cli, "--configure" ].flatten
90
+ stopCommand = [ "sudo", vmnet_cli, "--stop" ].flatten
91
+ startCommand = [ "sudo", vmnet_cli, "--start" ].flatten
92
+ statusCommand = [ "sudo", vmnet_cli, "--status" ].flatten
93
+
94
+ Vagrant::Util::Subprocess.execute(*configureCommand)
95
+ Vagrant::Util::Subprocess.execute(*stopCommand)
96
+ Vagrant::Util::Subprocess.execute(*startCommand)
97
+ r = Vagrant::Util::Subprocess.execute(*statusCommand)
98
+
99
+ if r.exit_code != 0
100
+ @ui.error("VMNet status exited with code #{r.exit_code} and output:\n#{r.stdout.chomp}")
101
+ end
102
+
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,101 @@
1
+ require "log4r"
2
+ require "digest"
3
+ require "vagrant/util/shell_quote"
4
+ require "vagrant/util/subprocess"
5
+
6
+ # Someone who's had some sleep recently is welcome to come fill in comments here...
7
+ module VagrantPlugins
8
+ module VagrantVmwareDhcp
9
+ class DhcpManagerLinux < DhcpManager
10
+
11
+ protected
12
+
13
+ def dhcpd_conf_location(network)
14
+ # Locations from https://pubs.vmware.com/workstation-9/index.jsp?topic=%2Fcom.vmware.ws.using.doc%2FGUID-04D783E1-3AB9-4D98-9891-2C58215905CC.html
15
+
16
+ location = "/etc/vmware/#{network[:vnet]}/dhcpd/dhcpd.conf"
17
+
18
+ @logger.debug("Using dhcpd.conf at #{location}")
19
+
20
+ return location
21
+ end
22
+
23
+ def prune_configuration(network)
24
+ @logger.info("Pruning DHCP configuration for #{network[:ip]}")
25
+
26
+ conf_location = dhcpd_conf_location(network)
27
+
28
+ mac = network[:mac]
29
+ ip = network[:ip]
30
+
31
+ before = File.open(conf_location).read
32
+ @logger.debug("Before altering, dhcpd.conf content is #{before}")
33
+
34
+ command = []
35
+ command << "sudo" if !File.writable?(conf_location)
36
+ command += [
37
+ "sed", "-E", "-e",
38
+ "/^# VAGRANT-BEGIN: #{mac}/," +
39
+ "/^# VAGRANT-END: #{mac}\s*/ d",
40
+ "-ibak",
41
+ conf_location
42
+ ]
43
+
44
+ system(*command)
45
+
46
+ command = []
47
+ command << "sudo" if !File.writable?(conf_location)
48
+ command += [
49
+ "sed", "-E", "-e",
50
+ "/^# VAGRANT-BEGIN: #{ip}/," +
51
+ "/^# VAGRANT-END: #{ip}\s*/ d",
52
+ "-ibak",
53
+ conf_location
54
+ ]
55
+
56
+ system(*command)
57
+
58
+ after = File.open(conf_location).read
59
+ @logger.debug("After, dhcpd.conf content is #{after}")
60
+ end
61
+
62
+ def write_configuration(network)
63
+ conf_location = dhcpd_conf_location(network)
64
+ escaped_conf_location = Vagrant::Util::ShellQuote.escape(conf_location, "'")
65
+
66
+ sudo_command = ""
67
+ sudo_command = "sudo " if !File.writable?(conf_location)
68
+
69
+ output = template_machine_definition(network)
70
+
71
+ before = File.open(conf_location).read
72
+ @logger.debug("Before altering, dhcpd.conf content is #{before}")
73
+
74
+ output.split("\n").each do |line|
75
+ line = Vagrant::Util::ShellQuote.escape(line, "'")
76
+ system(%Q[echo '#{line}' | #{sudo_command}tee -a '#{escaped_conf_location}' >/dev/null])
77
+ end
78
+
79
+ after = File.open(conf_location).read
80
+ @logger.debug("After, dhcpd.conf content is #{after}")
81
+ end
82
+
83
+ def reload_configuration
84
+ # Per http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1026510
85
+
86
+ service_location = "/etc/rc.d/init.d/vmware"
87
+
88
+ restartCommand = [ "sudo", service_location, "restart" ].flatten
89
+ statusCommand = [ "sudo", service_location, "status" ].flatten
90
+
91
+ Vagrant::Util::Subprocess.execute(*restartCommand)
92
+ r = Vagrant::Util::Subprocess.execute(*statusCommand)
93
+
94
+ if r.exit_code != 0
95
+ @ui.error("VMNet status exited with code #{r.exit_code} and output:\n#{r.stdout.chomp}")
96
+ end
97
+
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,78 @@
1
+ require "log4r"
2
+ require "digest"
3
+ require "vagrant/util/shell_quote"
4
+ require "vagrant/util/subprocess"
5
+
6
+ # Someone who's had some sleep recently is welcome to come fill in comments here...
7
+ module VagrantPlugins
8
+ module VagrantVmwareDhcp
9
+ class DhcpManagerWindows < DhcpManager
10
+
11
+ protected
12
+
13
+ def dhcpd_conf_location(network)
14
+ # Locations from https://pubs.vmware.com/workstation-9/index.jsp?topic=%2Fcom.vmware.ws.using.doc%2FGUID-04D783E1-3AB9-4D98-9891-2C58215905CC.html
15
+
16
+ if File.exist?('c:\ProgramData\VMware\vmnetdhcp.conf')
17
+ location = 'C:\ProgramData\VMware\vmnetdhcp.conf'
18
+ elsif File.exist?('C:\Documents and Settings\All Users\Application Data\VMware\vmnetdhcp.conf')
19
+ location = 'C:\Documents and Settings\All Users\Application Data\VMware\vmnetdhcp.conf'
20
+ end
21
+
22
+ @logger.debug("Using dhcpd.conf at #{location}")
23
+
24
+ return location
25
+ end
26
+
27
+ def prune_configuration(network)
28
+ conf_location = dhcpd_conf_location(network)
29
+
30
+ mac = network[:mac]
31
+ ip = network[:ip]
32
+
33
+ before = File.open(conf_location).read
34
+ @logger.debug("Before altering, dhcpd.conf content is #{before}")
35
+
36
+ intermediate = before.gsub(/^# VAGRANT-BEGIN: #{mac}.*^# VAGRANT-END: #{mac}\s+/m, '')
37
+ after = intermediate.gsub(/^# VAGRANT-BEGIN: #{ip}.*^# VAGRANT-END: #{ip}\s+/m, '')
38
+
39
+ File.open(conf_location, "w") { |fd| fd.write(after) }
40
+
41
+ after = File.open(conf_location).read
42
+ @logger.debug("After altering, dhcpd.conf content is #{after}")
43
+ end
44
+
45
+ def write_configuration(network)
46
+ conf_location = dhcpd_conf_location(network)
47
+
48
+ section = get_dhcpd_section(network)
49
+
50
+ @logger.debug("DHCPD template for interface #{network[:vnet]} is #{section}")
51
+
52
+ before = File.open(conf_location).read
53
+ @logger.debug("Before altering, dhcpd.conf content is #{before}")
54
+
55
+ after = "#{before}\n\n#{section}\n"
56
+
57
+ File.open(conf_location, "w") { |fd| fd.write(after) }
58
+
59
+ after = File.open(conf_location).read
60
+ @logger.debug("After, dhcpd.conf content is #{after}")
61
+ end
62
+
63
+ def reload_configuration
64
+ # This is non-authoritative, but is the obvious solution and seems to work.
65
+
66
+ stopCommand = [ "NET", "STOP", "VMware DHCP Service" ]
67
+ startCommand = [ "NET", "START", "VMware DHCP Service" ]
68
+
69
+ Vagrant::Util::Subprocess.execute(*stopCommand)
70
+ r = Vagrant::Util::Subprocess.execute(*startCommand)
71
+
72
+ if r.exit_code != 0
73
+ @ui.error("VMNet dhcp start exited with code #{r.exit_code} and output:\n#{r.stdout.chomp}")
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -1,4 +1,5 @@
1
1
  require_relative "action"
2
+ require_relative "dhcp"
2
3
 
3
4
  module VagrantPlugins
4
5
  module VagrantVmwareDhcp
@@ -13,26 +14,33 @@ module VagrantPlugins
13
14
  Config
14
15
  end
15
16
 
16
- action_hook('DA VMWare Network: Configure MAC addresses') do |hook|
17
+ action_hook('DA VMware Network: Configure MAC addresses') do |hook|
17
18
  action = Vagrant::Action::Builtin::ConfigValidate
18
19
  hook.before(action, VagrantVmwareDhcp::Action::SetMac)
19
20
  end
20
21
 
21
- action_hook('DA VMWare Network: Configure dhcp.conf') do |hook|
22
- if defined?(ActionClass) and defined?(ConfigDhcpClass)
22
+ action_hook('DA VMware Network: Configure dhcp.conf') do |hook|
23
+ if defined?(ActionConfigure)
23
24
  # no-op
24
- elsif Vagrant::Util::Platform.windows?
25
- ConfigDhcpClass = VagrantVmwareDhcp::Action::ConfigDhcpWindows
26
- ActionClass = HashiCorp::VagrantVMwareworkstation::Action::Network
27
- elsif Vagrant::Util::Platform.linux?
28
- ConfigDhcpClass = VagrantVmwareDhcp::Action::ConfigDhcpLinux
29
- ActionClass = HashiCorp::VagrantVMwareworkstation::Action::Network
25
+ elsif Vagrant::Util::Platform.windows? or Vagrant::Util::Platform.linux?
26
+ ActionConfigure = HashiCorp::VagrantVMwareworkstation::Action::Network
30
27
  elsif Vagrant::Util::Platform.darwin?
31
- ConfigDhcpClass = VagrantVmwareDhcp::Action::ConfigDhcpDarwin
32
- ActionClass = HashiCorp::VagrantVMwarefusion::Action::Network
28
+ ActionConfigure = HashiCorp::VagrantVMwarefusion::Action::Network
33
29
  end
34
30
 
35
- hook.after(ActionClass, ConfigDhcpClass)
31
+ hook.after(ActionConfigure, VagrantVmwareDhcp::Action::ConfigDhcp)
32
+ end
33
+
34
+ action_hook('DA VMware Network: Prune dhcp.conf') do |hook|
35
+ if defined?(ActionPrune)
36
+ # no-op
37
+ elsif Vagrant::Util::Platform.windows? or Vagrant::Util::Platform.linux?
38
+ ActionPrune = HashiCorp::VagrantVMwareworkstation::Action::Destroy
39
+ elsif Vagrant::Util::Platform.darwin?
40
+ ActionPrune = HashiCorp::VagrantVMwarefusion::Action::Destroy
41
+ end
42
+
43
+ hook.before(ActionPrune, VagrantVmwareDhcp::Action::PruneDhcp)
36
44
  end
37
45
 
38
46
  # action_hook(:init_i18n, :environment_load) { init_i18n }
@@ -1,5 +1,5 @@
1
1
  module VagrantPlugins
2
2
  module VagrantVmwareDhcp
3
- VERSION = '0.0.8'
3
+ VERSION = '0.0.9'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-vmware-dhcp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Israel Shirk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-19 00:00:00.000000000 Z
11
+ date: 2015-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -54,11 +54,14 @@ files:
54
54
  - lib/vagrant-vmware-dhcp.rb
55
55
  - lib/vagrant-vmware-dhcp/action.rb
56
56
  - lib/vagrant-vmware-dhcp/action/config_dhcp.rb
57
- - lib/vagrant-vmware-dhcp/action/config_dhcp_darwin.rb
58
- - lib/vagrant-vmware-dhcp/action/config_dhcp_linux.rb
59
- - lib/vagrant-vmware-dhcp/action/config_dhcp_windows.rb
57
+ - lib/vagrant-vmware-dhcp/action/prune_dhcp.rb
60
58
  - lib/vagrant-vmware-dhcp/action/set_mac.rb
61
59
  - lib/vagrant-vmware-dhcp/config.rb
60
+ - lib/vagrant-vmware-dhcp/dhcp.rb
61
+ - lib/vagrant-vmware-dhcp/dhcp_manager/dhcp_manager.rb
62
+ - lib/vagrant-vmware-dhcp/dhcp_manager/dhcp_manager_darwin.rb
63
+ - lib/vagrant-vmware-dhcp/dhcp_manager/dhcp_manager_linux.rb
64
+ - lib/vagrant-vmware-dhcp/dhcp_manager/dhcp_manager_windows.rb
62
65
  - lib/vagrant-vmware-dhcp/plugin.rb
63
66
  - lib/vagrant-vmware-dhcp/version.rb
64
67
  - locales/en.yml
@@ -1,106 +0,0 @@
1
- require "log4r"
2
- require "digest"
3
-
4
- # Someone who's had some sleep recently is welcome to come fill in comments here...
5
- module VagrantPlugins
6
- module VagrantVmwareDhcp
7
- module Action
8
- class ConfigDhcpDarwin < ConfigDhcp
9
-
10
- protected
11
-
12
- def dhcpd_conf_location(network)
13
- # Locations from https://pubs.vmware.com/workstation-9/index.jsp?topic=%2Fcom.vmware.ws.using.doc%2FGUID-04D783E1-3AB9-4D98-9891-2C58215905CC.html
14
-
15
- location = "/Library/Preferences/VMware Fusion/#{network[:vnet]}/dhcpd.conf"
16
-
17
- @logger.debug("Using dhcpd.conf at #{location}")
18
-
19
- return location
20
- end
21
-
22
- def prune_dhcpd_conf(network)
23
- conf_location = dhcpd_conf_location(network)
24
- escaped_conf_location = Vagrant::Util::ShellQuote.escape(conf_location, "'")
25
-
26
- mac = network[:mac]
27
- ip = network[:ip]
28
-
29
- before = File.open(conf_location).read
30
- @logger.debug("Before altering, dhcpd.conf content is #{before}")
31
-
32
- command = []
33
- command << "sudo" if !File.writable?(conf_location)
34
- command += [
35
- "sed", "-E", "-e",
36
- "/^# VAGRANT-BEGIN: #{mac}/," +
37
- "/^# VAGRANT-END: #{mac}/ d",
38
- "-ibak",
39
- conf_location
40
- ]
41
-
42
- system(*command)
43
-
44
- command = []
45
- command << "sudo" if !File.writable?(conf_location)
46
- command += [
47
- "sed", "-E", "-e",
48
- "/^# VAGRANT-BEGIN: #{ip}/," +
49
- "/^# VAGRANT-END: #{ip}/ d",
50
- "-ibak",
51
- conf_location
52
- ]
53
-
54
- system(*command)
55
-
56
- after = File.open(conf_location).read
57
- @logger.debug("After, dhcpd.conf content is #{after}")
58
- end
59
-
60
- def write_dhcpd_conf(network)
61
- conf_location = dhcpd_conf_location(network)
62
- escaped_conf_location = Vagrant::Util::ShellQuote.escape(conf_location, "'")
63
-
64
- sudo_command = ""
65
- sudo_command = "sudo " if !File.writable?(conf_location)
66
-
67
- output = get_dhcpd_section(network)
68
-
69
- @logger.debug("DHCPD template for interface #{network[:vnet]} is #{output}")
70
-
71
- before = File.open(conf_location).read
72
- @logger.debug("Before altering, dhcpd.conf content is #{before}")
73
-
74
- output.split("\n").each do |line|
75
- line = Vagrant::Util::ShellQuote.escape(line, "'")
76
- system(%Q[echo '#{line}' | #{sudo_command}tee -a '#{escaped_conf_location}' >/dev/null])
77
- end
78
-
79
- after = File.open(conf_location).read
80
- @logger.debug("After, dhcpd.conf content is #{after}")
81
- end
82
-
83
- def trigger_dhcpd_update
84
- # Per http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1026510
85
-
86
- vmnet_cli = "/Applications/VMware Fusion.app/Contents/Library/vmnet-cli"
87
-
88
- configureCommand = [ "sudo", vmnet_cli, "--configure" ].flatten
89
- stopCommand = [ "sudo", vmnet_cli, "--stop" ].flatten
90
- startCommand = [ "sudo", vmnet_cli, "--start" ].flatten
91
- statusCommand = [ "sudo", vmnet_cli, "--status" ].flatten
92
-
93
- Vagrant::Util::Subprocess.execute(*configureCommand)
94
- Vagrant::Util::Subprocess.execute(*stopCommand)
95
- Vagrant::Util::Subprocess.execute(*startCommand)
96
- r = Vagrant::Util::Subprocess.execute(*statusCommand)
97
-
98
- if r.exit_code != 0
99
- @env[:ui].error("VMNet status exited with code #{r.exit_code} and output:\n#{r.stdout.chomp}")
100
- end
101
-
102
- end
103
- end
104
- end
105
- end
106
- end
@@ -1,106 +0,0 @@
1
- require "log4r"
2
- require "digest"
3
-
4
- # Someone who's had some sleep recently is welcome to come fill in comments here...
5
- module VagrantPlugins
6
- module VagrantVmwareDhcp
7
- module Action
8
- class ConfigDhcpLinux < ConfigDhcp
9
-
10
- protected
11
-
12
- def dhcpd_conf_location(network)
13
- # Locations from https://pubs.vmware.com/workstation-9/index.jsp?topic=%2Fcom.vmware.ws.using.doc%2FGUID-04D783E1-3AB9-4D98-9891-2C58215905CC.html
14
-
15
- location = "/etc/vmware/#{network[:vnet]}/dhcp/dhcp.conf"
16
-
17
- @logger.debug("Using dhcpd.conf at #{location}")
18
-
19
- return location
20
- end
21
-
22
- def prune_dhcpd_conf(network)
23
- conf_location = dhcpd_conf_location(network)
24
- escaped_conf_location = Vagrant::Util::ShellQuote.escape(conf_location, "'")
25
-
26
- mac = network[:mac]
27
- ip = network[:ip]
28
-
29
- before = File.open(conf_location).read
30
- @logger.debug("Before altering, dhcpd.conf content is #{before}")
31
-
32
- command = []
33
- command << "sudo" if !File.writable?(conf_location)
34
- command += [
35
- "sed", "-E", "-e",
36
- "/^# VAGRANT-BEGIN: #{mac}/," +
37
- "/^# VAGRANT-END: #{mac}/ d",
38
- "-ibak",
39
- conf_location
40
- ]
41
-
42
- system(*command)
43
-
44
- command = []
45
- command << "sudo" if !File.writable?(conf_location)
46
- command += [
47
- "sed", "-E", "-e",
48
- "/^# VAGRANT-BEGIN: #{ip}/," +
49
- "/^# VAGRANT-END: #{ip}/ d",
50
- "-ibak",
51
- conf_location
52
- ]
53
-
54
- system(*command)
55
-
56
- after = File.open(conf_location).read
57
- @logger.debug("After, dhcpd.conf content is #{after}")
58
- end
59
-
60
- def write_dhcpd_conf(network)
61
- conf_location = dhcpd_conf_location(network)
62
- escaped_conf_location = Vagrant::Util::ShellQuote.escape(conf_location, "'")
63
-
64
- sudo_command = ""
65
- sudo_command = "sudo " if !File.writable?(conf_location)
66
-
67
- output = get_dhcpd_section(network)
68
-
69
- @logger.debug("DHCPD template for interface #{network[:vnet]} is #{output}")
70
-
71
- before = File.open(conf_location).read
72
- @logger.debug("Before altering, dhcpd.conf content is #{before}")
73
-
74
- output.split("\n").each do |line|
75
- line = Vagrant::Util::ShellQuote.escape(line, "'")
76
- system(%Q[echo '#{line}' | #{sudo_command}tee -a '#{escaped_conf_location}' >/dev/null])
77
- end
78
-
79
- after = File.open(conf_location).read
80
- @logger.debug("After, dhcpd.conf content is #{after}")
81
- end
82
-
83
- def trigger_dhcpd_update
84
- # Per http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1026510
85
-
86
- vmnet_cli = "/Applications/VMware Fusion.app/Contents/Library/vmnet-cli"
87
-
88
- configureCommand = [ "sudo", vmnet_cli, "--configure" ].flatten
89
- stopCommand = [ "sudo", vmnet_cli, "--stop" ].flatten
90
- startCommand = [ "sudo", vmnet_cli, "--start" ].flatten
91
- statusCommand = [ "sudo", vmnet_cli, "--status" ].flatten
92
-
93
- Vagrant::Util::Subprocess.execute(*configureCommand)
94
- Vagrant::Util::Subprocess.execute(*stopCommand)
95
- Vagrant::Util::Subprocess.execute(*startCommand)
96
- r = Vagrant::Util::Subprocess.execute(*statusCommand)
97
-
98
- if r.exit_code != 0
99
- @env[:ui].error("VMNet status exited with code #{r.exit_code} and output:\n#{r.stdout.chomp}")
100
- end
101
-
102
- end
103
- end
104
- end
105
- end
106
- end
@@ -1,79 +0,0 @@
1
- require "log4r"
2
- require "digest"
3
-
4
- # Someone who's had some sleep recently is welcome to come fill in comments here...
5
- module VagrantPlugins
6
- module VagrantVmwareDhcp
7
- module Action
8
- class ConfigDhcpWindows < ConfigDhcp
9
-
10
- protected
11
-
12
- def dhcpd_conf_location(network)
13
- # Locations from https://pubs.vmware.com/workstation-9/index.jsp?topic=%2Fcom.vmware.ws.using.doc%2FGUID-04D783E1-3AB9-4D98-9891-2C58215905CC.html
14
-
15
- if File.exist?('c:\ProgramData\VMware\vmnetdhcp.conf')
16
- location = 'C:\ProgramData\VMware\vmnetdhcp.conf'
17
- elsif File.exist?('C:\Documents and Settings\All Users\Application Data\VMware\vmnetdhcp.conf')
18
- location = 'C:\Documents and Settings\All Users\Application Data\VMware\vmnetdhcp.conf'
19
- end
20
-
21
- @logger.debug("Using dhcpd.conf at #{location}")
22
-
23
- return location
24
- end
25
-
26
- def prune_dhcpd_conf(network)
27
- conf_location = dhcpd_conf_location(network)
28
-
29
- mac = network[:mac]
30
- ip = network[:ip]
31
-
32
- before = File.open(conf_location).read
33
- @logger.debug("Before altering, dhcpd.conf content is #{before}")
34
-
35
- intermediate = before.gsub(/^# VAGRANT-BEGIN: #{mac}.*^# VAGRANT-END: #{mac}\s+/m, '')
36
- after = intermediate.gsub(/^# VAGRANT-BEGIN: #{ip}.*^# VAGRANT-END: #{ip}\s+/m, '')
37
-
38
- File.open(conf_location, "w") { |fd| fd.write(after) }
39
-
40
- after = File.open(conf_location).read
41
- @logger.debug("After altering, dhcpd.conf content is #{after}")
42
- end
43
-
44
- def write_dhcpd_conf(network)
45
- conf_location = dhcpd_conf_location(network)
46
-
47
- section = get_dhcpd_section(network)
48
-
49
- @logger.debug("DHCPD template for interface #{network[:vnet]} is #{section}")
50
-
51
- before = File.open(conf_location).read
52
- @logger.debug("Before altering, dhcpd.conf content is #{before}")
53
-
54
- after = "#{before}\n\n#{section}\n"
55
-
56
- File.open(conf_location, "w") { |fd| fd.write(after) }
57
-
58
- after = File.open(conf_location).read
59
- @logger.debug("After, dhcpd.conf content is #{after}")
60
- end
61
-
62
- def trigger_dhcpd_update
63
- # This is non-authoritative, but is the obvious solution and seems to work.
64
-
65
- stopCommand = [ "NET", "STOP", "VMware DHCP Service" ]
66
- startCommand = [ "NET", "START", "VMware DHCP Service" ]
67
-
68
- Vagrant::Util::Subprocess.execute(*stopCommand)
69
- r = Vagrant::Util::Subprocess.execute(*startCommand)
70
-
71
- if r.exit_code != 0
72
- @env[:ui].error("VMNet dhcp start exited with code #{r.exit_code} and output:\n#{r.stdout.chomp}")
73
- end
74
-
75
- end
76
- end
77
- end
78
- end
79
- end