vagrant-vmware-esxi 1.2.1 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  require 'log4r'
2
- require 'net/ssh/simple'
2
+ require 'net/ssh'
3
3
 
4
4
  module VagrantPlugins
5
5
  module ESXi
@@ -33,19 +33,20 @@ module VagrantPlugins
33
33
  raise Errors::ESXiError,
34
34
  message: 'Guest VM should have been powered off...'
35
35
  else
36
- Net::SSH::Simple.sync(
37
- user: config.esxi_username,
38
- password: config.esxi_password,
39
- port: config.esxi_hostport,
40
- keys: config.esxi_private_keys
41
- ) do
36
+ Net::SSH.start( config.esxi_hostname, config.esxi_username,
37
+ password: $esxi_password,
38
+ port: config.esxi_hostport,
39
+ keys: config.esxi_private_keys,
40
+ timeout: 10,
41
+ number_of_password_prompts: 0,
42
+ non_interactive: true
43
+ ) do |ssh|
42
44
 
43
- r = ssh config.esxi_hostname,
44
- "vim-cmd vmsvc/destroy #{machine.id}"
45
- if r.exit_code != 0
45
+ r = ssh.exec!("vim-cmd vmsvc/destroy #{machine.id}")
46
+ if r.exitstatus != 0
46
47
  raise Errors::ESXiError,
47
48
  message => "Unable to destroy the VM:\n"\
48
- " #{r.stdout}\n#{r.stderr}"
49
+ " #{r}"
49
50
  end
50
51
  env[:ui].info I18n.t('vagrant_vmware_esxi.vagrant_vmware_esxi_message',
51
52
  message: 'VM has been destroyed...')
@@ -0,0 +1,176 @@
1
+ require 'log4r'
2
+ require 'net/ssh'
3
+ require 'io/console'
4
+
5
+ module VagrantPlugins
6
+ module ESXi
7
+ module Action
8
+ # This action set the global variable esxi_password and attempt to
9
+ # login to the esxi server to verify connectivity.
10
+ class SetESXiPassword
11
+ def initialize(app, env)
12
+ @app = app
13
+ @logger = Log4r::Logger.new('vagrant_vmware_esxi::action::set_esxi_password')
14
+ end
15
+
16
+ def call(env)
17
+ set_esxi_password(env)
18
+ @app.call(env)
19
+ end
20
+
21
+ def set_esxi_password(env)
22
+ @logger.info('vagrant-vmware-esxi, set_esxi_password: start...')
23
+
24
+ # Get config.
25
+ machine = env[:machine]
26
+ config = env[:machine].provider_config
27
+
28
+ #
29
+ # Set global variable $esxi_password
30
+ #
31
+ if $esxi_password.nil?
32
+ if (config.debug =~ %r{password}i)
33
+ puts "$esxi_password is unset..."
34
+ end
35
+ if (config.esxi_password =~ %r{^prompt:}i)
36
+ #
37
+ # Prompt for password
38
+ #
39
+ begin
40
+ print "#{config.esxi_hostname} password:"
41
+ $esxi_password = STDIN.noecho(&:gets).chomp
42
+ puts ""
43
+ rescue
44
+ raise Errors::ESXiError,
45
+ message: "Prompt for password error???"
46
+ end
47
+ elsif (config.esxi_password =~ %r{^env:}i)
48
+ #
49
+ # Get pw from environment variable
50
+ #
51
+ esxi_password_env = config.esxi_password.gsub(/env:/i, '').chomp
52
+ if esxi_password_env.length < 1
53
+ esxi_password_env = 'esxi_password'
54
+ end
55
+ begin
56
+ stdin_pw = ENV[esxi_password_env]
57
+ $esxi_password = stdin_pw.chomp
58
+ rescue
59
+ raise Errors::ESXiError,
60
+ message: "Unable to read environment variable: #{esxi_password_env}"
61
+ end
62
+ elsif (config.esxi_password =~ %r{^file:}i)
63
+ #
64
+ # Get password from file
65
+ #
66
+ esxi_password_file = config.esxi_password.gsub(/file:/i, '').chomp
67
+ if esxi_password_file.length < 1
68
+ esxi_password_file = '~/.esxi_password'
69
+ end
70
+ esxi_password_file = File.expand_path(esxi_password_file)
71
+ # Get password from file
72
+ begin
73
+ if File.file?(esxi_password_file)
74
+ file_pw=""
75
+ fh = File.open(File.expand_path(esxi_password_file))
76
+ file_pw = fh.readline
77
+ fh.close
78
+ $esxi_password = file_pw.chomp
79
+ else
80
+ raise Errors::ESXiError, message: "Unable to open #{esxi_password_file}"
81
+ end
82
+ rescue
83
+ raise Errors::ESXiError, message: "Unable to open #{esxi_password_file}"
84
+ end
85
+ elsif (config.esxi_password =~ %r{^key:}i)
86
+ #
87
+ # use ssh keys
88
+ #
89
+ $esxi_password = ""
90
+ esxi_password_key = config.esxi_password.gsub(/key:/i, '').chomp
91
+ if esxi_password_key.length < 1
92
+ config.esxi_private_keys = config.system_private_keys_path
93
+ else
94
+ config.esxi_private_keys = esxi_password_key
95
+ end
96
+ else
97
+ # Use plain text password from config
98
+ $esxi_password = config.esxi_password
99
+ end
100
+ end
101
+ if (config.debug =~ %r{password}i)
102
+ puts "pw (set_esxi_password): #{$esxi_password}"
103
+ puts "esxi_private_keys: #{config.esxi_private_keys}"
104
+ end
105
+
106
+
107
+ #
108
+ # Encode special characters in PW
109
+ #
110
+ $encoded_esxi_password = $esxi_password.gsub('@', '%40').gsub(\
111
+ '<', '%3c').gsub('>', '%3e').gsub(\
112
+ '[', '%5b').gsub(']', '%5d').gsub(\
113
+ '(', '%28').gsub(')', '%29').gsub(\
114
+ '%', '%25').gsub('#', '%23').gsub(\
115
+ '&', '%26').gsub(':', '%3a').gsub(\
116
+ '/', '%2f').gsub('\\','%5c').gsub(\
117
+ '"', '%22').gsub('\'','%27').gsub(\
118
+ '*', '%2a').gsub('?', '%3f')
119
+
120
+ @logger.info('vagrant-vmware-esxi, connect_esxi: esxi_private_keys: '\
121
+ "#{config.esxi_private_keys}")
122
+
123
+ #
124
+ # Test ESXi host connectivity
125
+ #
126
+ begin
127
+ Net::SSH.start( config.esxi_hostname, config.esxi_username,
128
+ password: $esxi_password,
129
+ port: config.esxi_hostport,
130
+ keys: config.esxi_private_keys,
131
+ timeout: 10,
132
+ number_of_password_prompts: 0,
133
+ non_interactive: true
134
+ ) do |ssh|
135
+
136
+ esxi_version = ssh.exec!("vmware -v")
137
+ ssh.close
138
+
139
+ @logger = Log4r::Logger.new('vagrant_vmware_esxi::action::set_esxi_password')
140
+ if esxi_version =~ %r{^vmware esxi}i
141
+ @logger.info('vagrant-vmware-esxi, set_esxi_password: '\
142
+ "ESXi version: #{esxi_version}")
143
+ else
144
+ @logger.info('vagrant-vmware-esxi, set_esxi_password: '\
145
+ "ESXi version: #{esxi_version}")
146
+ raise Errors::ESXiError,
147
+ message: "Unable to connect to ESXi host!"
148
+ end
149
+ end
150
+ rescue
151
+ if (config.esxi_password =~ %r{^prompt:}i)
152
+ access_error_message = "Prompt for password"
153
+ elsif (config.esxi_password =~ %r{^env:}i)
154
+ access_error_message = "env:#{esxi_password_env}"
155
+ elsif (config.esxi_password =~ %r{^file:}i)
156
+ access_error_message = "file:#{esxi_password_file}"
157
+ elsif (config.esxi_password =~ %r{^key:}i)
158
+ access_error_message = "key:#{config.esxi_private_keys}"
159
+ else
160
+ access_error_message = "password in Vagrantfile"
161
+ end
162
+
163
+ env[:ui].info I18n.t('vagrant_vmware_esxi.vagrant_vmware_esxi_message',
164
+ message: "ESXi host access : #{access_error_message}")
165
+
166
+ @logger.info('vagrant-vmware-esxi, set_esxi_password: '\
167
+ "ESXi host access : #{access_error_message}")
168
+
169
+ raise Errors::ESXiError,
170
+ message: "Unable to connect to ESXi host!"
171
+ end
172
+ end
173
+ end
174
+ end
175
+ end
176
+ end
@@ -1,5 +1,5 @@
1
1
  require 'log4r'
2
- require 'net/ssh/simple'
2
+ require 'net/ssh'
3
3
 
4
4
  module VagrantPlugins
5
5
  module ESXi
@@ -28,19 +28,21 @@ 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::Simple.sync(
32
- user: config.esxi_username,
33
- password: config.esxi_password,
34
- port: config.esxi_hostport,
35
- keys: config.esxi_private_keys
36
- ) do
31
+ Net::SSH.start( config.esxi_hostname, config.esxi_username,
32
+ password: $esxi_password,
33
+ port: config.esxi_hostport,
34
+ keys: config.esxi_private_keys,
35
+ timeout: 10,
36
+ number_of_password_prompts: 0,
37
+ non_interactive: true
38
+ ) do |ssh|
37
39
 
38
- r = ssh config.esxi_hostname,
39
- "vim-cmd vmsvc/power.off #{machine.id}"
40
- if r.exit_code != 0
40
+ r = ssh.exec!("vim-cmd vmsvc/power.off #{machine.id}")
41
+
42
+ if r.exitstatus != 0
41
43
  raise Errors::ESXiError,
42
44
  message: "Unable to power off the VM:\n"
43
- " #{r.stdout}\n#{r.stderr}"
45
+ " #{r}"
44
46
  end
45
47
  env[:ui].info I18n.t('vagrant_vmware_esxi.states.powered_off.short')
46
48
  end
@@ -1,11 +1,10 @@
1
1
  require 'log4r'
2
- require 'net/ssh/simple'
3
2
  require 'fileutils'
4
3
 
5
4
  module VagrantPlugins
6
5
  module ESXi
7
6
  module Action
8
- # This action will save (create) a new snapshot
7
+ # This action will package a VM.
9
8
  class Package
10
9
  def initialize(app, env)
11
10
  @app = app
@@ -95,7 +94,7 @@ module VagrantPlugins
95
94
 
96
95
  ovf_cmd = "ovftool --noSSLVerify -tt=VMX --name=\"#{boxname}\" "\
97
96
  "#{overwrite_opts} vi://#{config.esxi_username}:"\
98
- "#{config.esxi_password}@#{config.esxi_hostname}"\
97
+ "#{$encoded_esxi_password}@#{config.esxi_hostname}"\
99
98
  "?moref=vim.VirtualMachine:#{machine.id} #{tmpdir}"
100
99
 
101
100
  unless system "#{ovf_cmd}"
@@ -1,5 +1,5 @@
1
1
  require 'log4r'
2
- require 'net/ssh/simple'
2
+ require 'net/ssh'
3
3
 
4
4
  module VagrantPlugins
5
5
  module ESXi
@@ -23,19 +23,21 @@ module VagrantPlugins
23
23
  machine = env[:machine]
24
24
  config = env[:machine].provider_config
25
25
 
26
- return :not_created if machine.id.nil?
26
+ return nil if machine.id.nil?
27
27
 
28
28
  @logger.info("vagrant-vmware-esxi, read_ssh_info: machine id: #{machine.id}")
29
29
  @logger.info('vagrant-vmware-esxi, read_ssh_info: current state:'\
30
30
  " #{env[:machine_state]}")
31
31
 
32
32
  # Figure out vm_ipaddress
33
- Net::SSH::Simple.sync(
34
- user: config.esxi_username,
35
- password: config.esxi_password,
36
- port: config.esxi_hostport,
37
- keys: config.esxi_private_keys
38
- ) do
33
+ Net::SSH.start( config.esxi_hostname, config.esxi_username,
34
+ password: $esxi_password,
35
+ port: config.esxi_hostport,
36
+ keys: config.esxi_private_keys,
37
+ timeout: 10,
38
+ number_of_password_prompts: 0,
39
+ non_interactive: true
40
+ ) do |ssh|
39
41
 
40
42
  @logger = Log4r::Logger.new('vagrant_vmware_esxi::action::'\
41
43
  'read_ssh_info-net_ssh')
@@ -44,13 +46,13 @@ module VagrantPlugins
44
46
  ssh_execute_cmd = "vim-cmd vmsvc/get.guest #{machine.id} |"
45
47
  ssh_execute_cmd << 'grep -i "^ ipAddress"|'
46
48
  ssh_execute_cmd << 'grep -oE "((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])"'
47
- r = ssh config.esxi_hostname, ssh_execute_cmd
49
+ r = ssh.exec!(ssh_execute_cmd)
48
50
 
49
- ipaddress = r.stdout.strip
51
+ ipaddress = r.strip
50
52
  @logger.info('vagrant-vmware-esxi, read_ssh_info: ipaddress: '\
51
53
  "#{ipaddress}")
52
54
 
53
- return nil if (ipaddress == '') || (r.exit_code != 0)
55
+ return nil if (ipaddress == '') || (r.exitstatus != 0)
54
56
 
55
57
  return {
56
58
  host: ipaddress,
@@ -1,5 +1,5 @@
1
1
  require 'log4r'
2
- require 'net/ssh/simple'
2
+ require 'net/ssh'
3
3
  require 'socket'
4
4
 
5
5
  module VagrantPlugins
@@ -36,26 +36,28 @@ module VagrantPlugins
36
36
  machine = env[:machine]
37
37
  config = env[:machine].provider_config
38
38
 
39
-
40
39
  return :not_created if machine.id.to_i < 1
41
40
 
42
41
  @logger.info("vagrant-vmware-esxi, read_state: machine id: #{machine.id}")
43
42
  @logger.info("vagrant-vmware-esxi, read_state: current state: #{env[:machine_state]}")
44
43
 
45
- Net::SSH::Simple.sync(
46
- user: config.esxi_username,
47
- password: config.esxi_password,
48
- port: config.esxi_hostport,
49
- keys: config.esxi_private_keys
50
- ) do
44
+ Net::SSH.start( config.esxi_hostname, config.esxi_username,
45
+ password: $esxi_password,
46
+ port: config.esxi_hostport,
47
+ keys: config.esxi_private_keys,
48
+ timeout: 10,
49
+ number_of_password_prompts: 0,
50
+ non_interactive: true
51
+ ) do |ssh|
51
52
 
52
- r = ssh config.esxi_hostname,
53
+ r = ssh.exec!(
53
54
  "vim-cmd vmsvc/getallvms|grep -q \"^#{machine.id} \" && "\
54
- "vim-cmd vmsvc/power.getstate #{machine.id} || return 254"
55
- power_status = r.stdout
55
+ "vim-cmd vmsvc/power.getstate #{machine.id} || return 254")
56
+ power_status = r
57
+
58
+ return :not_created if r.exitstatus == 254
56
59
 
57
- return :not_created if r.exit_code == 254
58
- if power_status == "" or r.exit_code != 0
60
+ if power_status == "" or r.exitstatus != 0
59
61
  raise Errors::ESXiError,
60
62
  message: 'Unable to get VM Power State!'
61
63
  end
@@ -1,5 +1,5 @@
1
1
  require 'log4r'
2
- require 'net/ssh/simple'
2
+ require 'net/ssh'
3
3
 
4
4
  module VagrantPlugins
5
5
  module ESXi
@@ -37,18 +37,22 @@ module VagrantPlugins
37
37
  env[:ui].info I18n.t('vagrant_vmware_esxi.vagrant_vmware_esxi_message',
38
38
  message: 'Attempting to resume')
39
39
 
40
- Net::SSH::Simple.sync(
41
- user: config.esxi_username,
42
- password: config.esxi_password,
43
- port: config.esxi_hostport,
44
- keys: config.esxi_private_keys
45
- ) do
40
+ #
41
+ Net::SSH.start( config.esxi_hostname, config.esxi_username,
42
+ password: $esxi_password,
43
+ port: config.esxi_hostport,
44
+ keys: config.esxi_private_keys,
45
+ timeout: 60,
46
+ number_of_password_prompts: 0,
47
+ non_interactive: true
48
+ ) do |ssh|
46
49
 
47
- r = ssh config.esxi_hostname, "vim-cmd vmsvc/power.on #{machine.id}"
48
- if r.exit_code != 0
50
+ r = ssh.exec!("vim-cmd vmsvc/power.on #{machine.id}")
51
+
52
+ if r.exitstatus != 0
49
53
  raise Errors::ESXiError,
50
54
  message: "Unable to resume the VM:\n"\
51
- " #{r.stdout}\n#{r.stderr}"
55
+ " #{r}"
52
56
  end
53
57
  env[:ui].info I18n.t('vagrant_vmware_esxi.vagrant_vmware_esxi_message',
54
58
  message: 'VM has been resumed...')
@@ -1,5 +1,5 @@
1
1
  require 'log4r'
2
- require 'net/ssh/simple'
2
+ require 'net/ssh'
3
3
 
4
4
  module VagrantPlugins
5
5
  module ESXi
@@ -33,24 +33,26 @@ module VagrantPlugins
33
33
  env[:ui].info I18n.t('vagrant_vmware_esxi.vagrant_vmware_esxi_message',
34
34
  message: 'Attempting to snapshot_delete')
35
35
 
36
- Net::SSH::Simple.sync(
37
- user: config.esxi_username,
38
- password: config.esxi_password,
39
- port: config.esxi_hostport,
40
- keys: config.esxi_private_keys,
41
- timeout: 300
42
- ) do
36
+ #
37
+ Net::SSH.start( config.esxi_hostname, config.esxi_username,
38
+ password: $esxi_password,
39
+ port: config.esxi_hostport,
40
+ keys: config.esxi_private_keys,
41
+ timeout: 10,
42
+ number_of_password_prompts: 0,
43
+ non_interactive: true
44
+ ) do |ssh|
43
45
 
44
- r = ssh config.esxi_hostname,
46
+ r = ssh.exec!(
45
47
  "vim-cmd vmsvc/snapshot.remove #{machine.id} "\
46
48
  "`vim-cmd vmsvc/snapshot.get #{machine.id} | "\
47
49
  "grep -A1 '.*Snapshot Name : #{env[:snapshot_name]}$' | "\
48
- "grep 'Snapshot Id'|awk '{print $NF}'`"
50
+ "grep 'Snapshot Id'|awk '{print $NF}'`")
49
51
 
50
- if r.exit_code != 0
52
+ if r.exitstatus != 0
51
53
  raise Errors::ESXiError,
52
54
  message: "Unable to remove snapshots of the VM:\n"\
53
- " #{r.stdout}\n#{r.stderr}"
55
+ " #{r}"
54
56
  end
55
57
 
56
58
  env[:ui].info I18n.t('vagrant_vmware_esxi.snapshot_deleted')