vagrant-vmware-esxi 1.5.0 → 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  #
2
- # This is a Vagrantfile multi machine example.
2
+ # Multi machine, essential options Vagrant file example. (documentation removed)
3
3
  #
4
4
 
5
5
  nodes = [
@@ -7,7 +7,7 @@ nodes = [
7
7
  { hostname: 'VM-multi2', box: 'hashicorp/precise64' }
8
8
  ]
9
9
 
10
- Vagrant.configure("2") do |config|
10
+ Vagrant.configure('2') do |config|
11
11
  nodes.each do |node|
12
12
  config.vm.define node[:hostname] do |node_config|
13
13
  node_config.vm.hostname = node[:hostname]
@@ -20,17 +20,14 @@ Vagrant.configure("2") do |config|
20
20
  #
21
21
  # Provider settings
22
22
  #
23
- esxi.esxi_hostname = "esxi"
24
- esxi.esxi_username = "root"
25
- esxi.esxi_password = "file:"
23
+ esxi.esxi_hostname = 'esxi'
24
+ esxi.esxi_username = 'root'
25
+ esxi.esxi_password = 'file:'
26
26
  #esxi.esxi_hostport = 22
27
- #esxi.virtual_network = "vmnet_example"
28
- #esxi.nic_type = 'e1000'
29
- #esxi.vm_disk_store = "DS_001"
30
- #esxi.vmname_prefix = "V-"
31
- #esxi.memsize = "2048"
32
- #esxi.numvcpus = "2"
33
- #esxi.resource_pool = "/Vagrant"
34
- #esxi.allow_overwrite = 'True'
27
+ #esxi.virtual_network = 'vmnet_example'
28
+ #esxi.vm_disk_store = 'DS_001'
29
+ #esxi.memsize = '2048'
30
+ #esxi.numvcpus = '2'
31
+ #esxi.resource_pool = '/Vagrant'
35
32
  end
36
33
  end
@@ -34,7 +34,7 @@ module VagrantPlugins
34
34
  env[:ui].info I18n.t('vagrant_vmware_esxi.vagrant_vmware_esxi_message',
35
35
  message: 'Cannot boot in this state')
36
36
  else
37
- Net::SSH.start( config.esxi_hostname, config.esxi_username,
37
+ Net::SSH.start(config.esxi_hostname, config.esxi_username,
38
38
  password: $esxi_password,
39
39
  port: config.esxi_hostport,
40
40
  keys: config.esxi_private_keys,
@@ -78,12 +78,13 @@ module VagrantPlugins
78
78
  #
79
79
  # Figure out DataStore
80
80
  r = ssh.exec!(
81
- 'df | grep "^[VMFS|NFS]" | sort -nk4 |'\
82
- 'sed "s|.*/vmfs/volumes/||g" | tail +2')
81
+ 'df 2>/dev/null| grep "^[VMFS|NFS].*/vmfs/volumes/" | '\
82
+ 'sort -nk4 | sed "s|.*/vmfs/volumes/||g"')
83
83
 
84
84
  availvolumes = r.split(/\n/)
85
- if (config.debug =~ %r{true}i)
86
- puts "Available DS Volumes: #{availvolumes}"
85
+ if config.debug =~ %r{true}i
86
+ env[:ui].info I18n.t('vagrant_vmware_esxi.vagrant_vmware_esxi_message',
87
+ message: "Avail DS vols : #{availvolumes}")
87
88
  end
88
89
  if (r == '') || (r.exitstatus != 0)
89
90
  raise Errors::ESXiError,
@@ -93,6 +94,10 @@ module VagrantPlugins
93
94
  # Use least-used if vm_disk_store is not set (or not found)
94
95
  if config.vm_disk_store.nil?
95
96
  desired_ds = '--- Least Used ---'
97
+ env[:ui].info I18n.t('vagrant_vmware_esxi.vagrant_vmware_esxi_message',
98
+ message: 'WARNING : '\
99
+ "vm_disk_store not set, using "\
100
+ "\"--- Least Used ---\"")
96
101
  else
97
102
  desired_ds = config.vm_disk_store.to_s
98
103
  end
@@ -122,8 +127,9 @@ module VagrantPlugins
122
127
  'grep Portgroups | sed "s/^ Portgroups: //g" |'\
123
128
  'sed "s/,./\n/g"')
124
129
  availnetworks = r.split(/\n/)
125
- if (config.debug =~ %r{true}i)
126
- puts "Available Networks: #{availnetworks}"
130
+ if config.debug =~ %r{true}i
131
+ env[:ui].info I18n.t('vagrant_vmware_esxi.vagrant_vmware_esxi_message',
132
+ message: "Avail Networks : #{availnetworks}")
127
133
  end
128
134
  if (availnetworks == '') || (r.exitstatus != 0)
129
135
  raise Errors::ESXiError,
@@ -131,30 +137,62 @@ module VagrantPlugins
131
137
  "#{r.stderr}"
132
138
  end
133
139
 
134
- @guestvm_network = []
135
- counter = 0
136
- if config.virtual_network.nil?
137
- @guestvm_network[0] = availnetworks.first
140
+ # How many vm.network are there?
141
+ vm_network_index = 0
142
+ env[:machine].config.vm.networks.each do |type, options|
143
+ # I only handle private and public networks
144
+ next if type != :private_network && type != :public_network
145
+ vm_network_index += 1
146
+ end
147
+
148
+ if config.debug =~ %r{true}i
138
149
  env[:ui].info I18n.t('vagrant_vmware_esxi.vagrant_vmware_esxi_message',
139
- message: 'WARNING : '\
140
- "config.virtual_network not "\
141
- "set, using #{availnetworks.first}")
142
- else
143
- networkID = 0
144
- for aVirtNet in Array(config.virtual_network) do
145
- if availnetworks.include? aVirtNet
146
- @guestvm_network << aVirtNet
150
+ message: "virtual_network : #{config.virtual_network}")
151
+ end
152
+
153
+ # If there is more vm.network than virtual_network's configured
154
+ # I need to add more virtual_networks. Setting each to ---NotSet---
155
+ # to give a warning below...
156
+ if vm_network_index >= config.virtual_network.count
157
+ config.virtual_network.count.upto(vm_network_index) do |index|
158
+ config.virtual_network << '--NotSet--'
159
+ end
160
+ end
161
+
162
+ # Go through each virtual_network and make sure it's good. If not
163
+ # display a WARNING that we are choosing the first found.
164
+ @guestvm_network = []
165
+ networkID = 0
166
+ for aVirtNet in Array(config.virtual_network) do
167
+ if config.virtual_network == [''] ||
168
+ config.virtual_network[0] == '--NotSet--'
169
+ # First (and only ) interface is not configure or not set
170
+ @guestvm_network = [availnetworks.first]
171
+ config.virtual_network = [availnetworks.first]
172
+ env[:ui].info I18n.t('vagrant_vmware_esxi.vagrant_vmware_esxi_message',
173
+ message: 'WARNING : '\
174
+ "virtual_network[#{networkID}] not "\
175
+ "set, using #{availnetworks.first}")
176
+ elsif availnetworks.include? aVirtNet
177
+ # Network interface is good
178
+ @guestvm_network << aVirtNet
179
+ else
180
+ # Network interface is NOT good.
181
+ @guestvm_network[networkID] = availnetworks.first
182
+ config.virtual_network[networkID] = availnetworks.first
183
+ if aVirtNet == '--NotSet--'
184
+ aVirtNet_msg = "virtual_network[#{networkID}]"
147
185
  else
148
- @guestvm_network << availnetworks.first
149
- env[:ui].info I18n.t('vagrant_vmware_esxi.vagrant_vmware_esxi_message',
150
- message: 'WARNING : '\
151
- "#{aVirtNet} not "\
152
- "found, using #{availnetworks.first}")
153
- end
154
- networkID += 1
155
- if networkID >= 4
156
- break
186
+ aVirtNet_msg = aVirtNet
157
187
  end
188
+ env[:ui].info I18n.t('vagrant_vmware_esxi.vagrant_vmware_esxi_message',
189
+ message: 'WARNING : '\
190
+ "#{aVirtNet_msg} not "\
191
+ "found, using #{availnetworks.first}")
192
+ end
193
+ networkID += 1
194
+ if networkID >= 4
195
+ break
158
196
  end
159
197
  end
160
198
  end
@@ -231,8 +269,9 @@ module VagrantPlugins
231
269
  netOpts = ""
232
270
  networkID = 0
233
271
  for element in @guestvm_network do
234
- if (config.debug =~ %r{true}i)
235
- puts "guestvm_network[#{networkID}]: #{element}"
272
+ if config.debug =~ %r{true}i
273
+ env[:ui].info I18n.t('vagrant_vmware_esxi.vagrant_vmware_esxi_message',
274
+ message: "Network[#{networkID}] : #{element}")
236
275
  end
237
276
  new_vmx_contents << "ethernet#{networkID}.networkName = \"net#{networkID}\"\n"
238
277
  new_vmx_contents << "ethernet#{networkID}.present = \"TRUE\"\n"
@@ -419,12 +458,13 @@ module VagrantPlugins
419
458
  "#{$encoded_esxi_password}@#{config.esxi_hostname}"\
420
459
  "#{resource_pool}"
421
460
 
422
- # Security bug if unremarked! Password will be exposed in log file.
423
- if (config.debug =~ %r{password}i)
461
+ #
462
+ # Security alert! If password debugging is enabled, Password will
463
+ # be exposed in log file.
464
+ if config.debug =~ %r{password}i
424
465
  @logger.info("vagrant-vmware-esxi, createvm: ovf_cmd #{ovf_cmd}")
425
466
  puts "ovftool command: #{ovf_cmd}"
426
- end
427
- if (config.debug =~ %r{true}i)
467
+ elsif config.debug =~ %r{true}i
428
468
  ovf_cmd_nopw = ovf_cmd.gsub(/#{$encoded_esxi_password}/, '******')
429
469
  puts "ovftool command: #{ovf_cmd_nopw}"
430
470
  end
@@ -433,8 +473,9 @@ module VagrantPlugins
433
473
  end
434
474
 
435
475
  # VMX file is not needed any longer. Delete it
436
- if (config.debug =~ %r{true}i)
437
- puts "Keeping file: #{new_vmx_file}"
476
+ if config.debug =~ %r{true}i
477
+ env[:ui].info I18n.t('vagrant_vmware_esxi.vagrant_vmware_esxi_message',
478
+ message: "Keeping file : #{new_vmx_file}")
438
479
  else
439
480
  File.delete(new_vmx_file)
440
481
  end
@@ -442,7 +483,7 @@ module VagrantPlugins
442
483
  #
443
484
  # Re-open the network connection to get VMID
444
485
  #
445
- Net::SSH.start( config.esxi_hostname, config.esxi_username,
486
+ Net::SSH.start(config.esxi_hostname, config.esxi_username,
446
487
  password: $esxi_password,
447
488
  port: config.esxi_hostport,
448
489
  keys: config.esxi_private_keys,
@@ -461,7 +502,7 @@ module VagrantPlugins
461
502
 
462
503
  env[:machine].id = vmid.to_i
463
504
  env[:ui].info I18n.t('vagrant_vmware_esxi.vagrant_vmware_esxi_message',
464
- message: "VMID: #{env[:machine].id}")
505
+ message: "VMID : #{env[:machine].id}")
465
506
 
466
507
  #
467
508
  # -=-=-=-=-=-=-
@@ -478,7 +519,7 @@ module VagrantPlugins
478
519
 
479
520
  # Get vmx file in memory
480
521
  esxi_orig_vmx_file = ssh.exec!("cat #{dst_vmx_file} 2>/dev/null")
481
- if (config.debug =~ %r{vmx}i)
522
+ if config.debug =~ %r{vmx}i
482
523
  puts "orig vmx: #{esxi_orig_vmx_file}"
483
524
  puts "\n\n"
484
525
  end
@@ -597,7 +638,7 @@ module VagrantPlugins
597
638
 
598
639
  # If there was changes, update esxi
599
640
  if vmx_need_change_flag == true
600
- if (config.debug =~ %r{vmx}i)
641
+ if config.debug =~ %r{vmx}i
601
642
  puts "new vmx: #{new_vmx_contents}"
602
643
  puts "\n\n"
603
644
  end
@@ -614,8 +655,9 @@ module VagrantPlugins
614
655
  end
615
656
  ssh.loop
616
657
  else
617
- if (config.debug =~ %r{vmx}i)
618
- puts "no changes requried to vmx file"
658
+ if config.debug =~ %r{vmx}i
659
+ env[:ui].info I18n.t('vagrant_vmware_esxi.vagrant_vmware_esxi_message',
660
+ message: 'ESXi vmx file : Unmodified')
619
661
  end
620
662
  end
621
663
  end
@@ -33,7 +33,7 @@ module VagrantPlugins
33
33
  raise Errors::ESXiError,
34
34
  message: 'Guest VM should have been powered off...'
35
35
  else
36
- Net::SSH.start( config.esxi_hostname, config.esxi_username,
36
+ Net::SSH.start(config.esxi_hostname, config.esxi_username,
37
37
  password: $esxi_password,
38
38
  port: config.esxi_hostport,
39
39
  keys: config.esxi_private_keys,
@@ -8,7 +8,7 @@ module VagrantPlugins
8
8
  # This action set the global variable esxi_password and attempt to
9
9
  # login to the esxi server to verify connectivity.
10
10
  class SetESXiPassword
11
- def initialize(app, env)
11
+ def initialize(app, _env)
12
12
  @app = app
13
13
  @logger = Log4r::Logger.new('vagrant_vmware_esxi::action::set_esxi_password')
14
14
  end
@@ -22,26 +22,25 @@ module VagrantPlugins
22
22
  @logger.info('vagrant-vmware-esxi, set_esxi_password: start...')
23
23
 
24
24
  # Get config.
25
- machine = env[:machine]
26
25
  config = env[:machine].provider_config
27
26
 
28
27
  #
29
28
  # Set global variable $esxi_password
30
29
  #
31
30
  if $esxi_password.nil?
32
- if (config.esxi_password =~ %r{^prompt:}i)
31
+ if config.esxi_password =~ %r{^prompt:}i
33
32
  #
34
33
  # Prompt for password
35
34
  #
36
35
  begin
37
36
  print "#{config.esxi_hostname} password:"
38
37
  $esxi_password = STDIN.noecho(&:gets).chomp
39
- puts ""
38
+ puts ''
40
39
  rescue
41
40
  raise Errors::ESXiError,
42
- message: "Prompt for password error???"
41
+ message: 'Prompt for password error???'
43
42
  end
44
- elsif (config.esxi_password =~ %r{^env:}i)
43
+ elsif config.esxi_password =~ %r{^env:}i
45
44
  #
46
45
  # Get pw from environment variable
47
46
  #
@@ -56,19 +55,19 @@ module VagrantPlugins
56
55
  raise Errors::ESXiError,
57
56
  message: "Unable to read environment variable: #{esxi_password_env}"
58
57
  end
59
- elsif (config.esxi_password =~ %r{^file:}i)
58
+ elsif config.esxi_password =~ %r{^file:}i
60
59
  #
61
60
  # Get password from file
62
61
  #
63
62
  esxi_password_file = config.esxi_password.gsub(/file:/i, '').chomp
64
- if esxi_password_file.length < 1
63
+ if esxi_password_file.empty?
65
64
  esxi_password_file = '~/.esxi_password'
66
65
  end
67
66
  esxi_password_file = File.expand_path(esxi_password_file)
68
67
  # Get password from file
69
68
  begin
70
69
  if File.file?(esxi_password_file)
71
- file_pw=""
70
+ file_pw = ''
72
71
  fh = File.open(File.expand_path(esxi_password_file))
73
72
  file_pw = fh.readline
74
73
  fh.close
@@ -79,13 +78,13 @@ module VagrantPlugins
79
78
  rescue
80
79
  raise Errors::ESXiError, message: "Unable to open #{esxi_password_file}"
81
80
  end
82
- elsif (config.esxi_password =~ %r{^key:}i)
81
+ elsif config.esxi_password =~ %r{^key:}i
83
82
  #
84
83
  # use ssh keys
85
84
  #
86
- $esxi_password = ""
85
+ $esxi_password = ''
87
86
  esxi_password_key = config.esxi_password.gsub(/key:/i, '').chomp
88
- if esxi_password_key.length < 1
87
+ if esxi_password_key.empty?
89
88
  config.esxi_private_keys = config.system_private_keys_path
90
89
  else
91
90
  config.esxi_private_keys = esxi_password_key
@@ -99,11 +98,11 @@ module VagrantPlugins
99
98
  #
100
99
  # Encode special characters in PW
101
100
  #
102
- $encoded_esxi_password = $esxi_password.gsub('@', '%40').gsub(\
101
+ $encoded_esxi_password = $esxi_password.gsub('%', '%25').gsub(\
103
102
  '<', '%3c').gsub('>', '%3e').gsub(\
104
103
  '[', '%5b').gsub(']', '%5d').gsub(\
105
104
  '(', '%28').gsub(')', '%29').gsub(\
106
- '%', '%25').gsub('#', '%23').gsub(\
105
+ '@', '%40').gsub('#', '%23').gsub(\
107
106
  '&', '%26').gsub(':', '%3a').gsub(\
108
107
  '/', '%2f').gsub('\\','%5c').gsub(\
109
108
  '"', '%22').gsub('\'','%27').gsub(\
@@ -117,7 +116,7 @@ module VagrantPlugins
117
116
  # Test ESXi host connectivity
118
117
  #
119
118
  begin
120
- Net::SSH.start( config.esxi_hostname, config.esxi_username,
119
+ Net::SSH.start(config.esxi_hostname, config.esxi_username,
121
120
  password: $esxi_password,
122
121
  port: config.esxi_hostport,
123
122
  keys: config.esxi_private_keys,
@@ -126,31 +125,33 @@ module VagrantPlugins
126
125
  non_interactive: true
127
126
  ) do |ssh|
128
127
 
129
- esxi_version = ssh.exec!("vmware -v")
128
+ esxi_version = ssh.exec!('vmware -v')
130
129
  ssh.close
131
130
 
132
131
  @logger = Log4r::Logger.new('vagrant_vmware_esxi::action::set_esxi_password')
133
- if esxi_version =~ %r{^vmware esxi}i
134
- @logger.info('vagrant-vmware-esxi, set_esxi_password: '\
135
- "ESXi version: #{esxi_version}")
136
- else
132
+ if (config.debug =~ %r{true}i) && $showVersionFlag.nil?
133
+ $showVersionFlag = true
134
+ env[:ui].info I18n.t('vagrant_vmware_esxi.vagrant_vmware_esxi_message',
135
+ message: "ESXi version : #{esxi_version}")
136
+ end
137
+ if esxi_version !~ %r{^vmware esxi}i
137
138
  @logger.info('vagrant-vmware-esxi, set_esxi_password: '\
138
139
  "ESXi version: #{esxi_version}")
139
140
  raise Errors::ESXiError,
140
- message: "Unable to connect to ESXi host!"
141
+ message: 'Unable to connect to ESXi host!'
141
142
  end
142
143
  end
143
144
  rescue
144
- if (config.esxi_password =~ %r{^prompt:}i)
145
- access_error_message = "Prompt for password"
146
- elsif (config.esxi_password =~ %r{^env:}i)
145
+ if config.esxi_password =~ %r{^prompt:}i
146
+ access_error_message = 'Prompt for password'
147
+ elsif config.esxi_password =~ %r{^env:}i
147
148
  access_error_message = "env:#{esxi_password_env}"
148
- elsif (config.esxi_password =~ %r{^file:}i)
149
+ elsif config.esxi_password =~ %r{^file:}i
149
150
  access_error_message = "file:#{esxi_password_file}"
150
- elsif (config.esxi_password =~ %r{^key:}i)
151
+ elsif config.esxi_password =~ %r{^key:}i
151
152
  access_error_message = "key:#{config.esxi_private_keys}"
152
153
  else
153
- access_error_message = "password in Vagrantfile"
154
+ access_error_message = 'password in Vagrantfile'
154
155
  end
155
156
 
156
157
  env[:ui].info I18n.t('vagrant_vmware_esxi.vagrant_vmware_esxi_message',
@@ -160,7 +161,7 @@ module VagrantPlugins
160
161
  "ESXi host access : #{access_error_message}")
161
162
 
162
163
  raise Errors::ESXiError,
163
- message: "Unable to connect to ESXi host!"
164
+ message: 'Unable to connect to ESXi host!'
164
165
  end
165
166
  end
166
167
  end
@@ -6,7 +6,7 @@ module VagrantPlugins
6
6
  module Action
7
7
  # This action will halt (power off) the VM
8
8
  class Halt
9
- def initialize(app, env)
9
+ def initialize(app, _env)
10
10
  @app = app
11
11
  @logger = Log4r::Logger.new('vagrant_vmware_esxi::action::halt')
12
12
  end
@@ -28,7 +28,7 @@ module VagrantPlugins
28
28
  elsif env[:machine_state].to_s == 'not_created'
29
29
  env[:ui].info I18n.t('vagrant_vmware_esxi.already_destroyed')
30
30
  else
31
- Net::SSH.start( config.esxi_hostname, config.esxi_username,
31
+ Net::SSH.start(config.esxi_hostname, config.esxi_username,
32
32
  password: $esxi_password,
33
33
  port: config.esxi_hostport,
34
34
  keys: config.esxi_private_keys,
@@ -41,8 +41,7 @@ module VagrantPlugins
41
41
 
42
42
  if r.exitstatus != 0
43
43
  raise Errors::ESXiError,
44
- message: "Unable to power off the VM:\n"
45
- " #{r}"
44
+ message: "Unable to power off the VM:\n #{r}"
46
45
  end
47
46
  env[:ui].info I18n.t('vagrant_vmware_esxi.states.powered_off.short')
48
47
  end