vagrant-vsphere 0.19.0 → 0.19.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.bumpversion.cfg +1 -1
- data/.rubocop.yml +13 -0
- data/.rubocop_todo.yml +37 -0
- data/CHANGELOG.md +189 -0
- data/DEVELOPMENT.md +67 -0
- data/Gemfile +2 -3
- data/README.md +86 -175
- data/Rakefile +4 -1
- data/lib/vSphere/action.rb +22 -28
- data/lib/vSphere/action/clone.rb +46 -46
- data/lib/vSphere/action/close_vsphere.rb +8 -10
- data/lib/vSphere/action/connect_vsphere.rb +6 -6
- data/lib/vSphere/action/destroy.rb +6 -6
- data/lib/vSphere/action/get_ssh_info.rb +4 -5
- data/lib/vSphere/action/get_state.rb +3 -5
- data/lib/vSphere/action/is_created.rb +2 -2
- data/lib/vSphere/action/is_running.rb +2 -2
- data/lib/vSphere/action/message_already_created.rb +2 -2
- data/lib/vSphere/action/message_not_created.rb +1 -1
- data/lib/vSphere/action/message_not_running.rb +1 -1
- data/lib/vSphere/action/power_off.rb +2 -2
- data/lib/vSphere/action/power_on.rb +4 -4
- data/lib/vSphere/config.rb +2 -2
- data/lib/vSphere/plugin.rb +5 -6
- data/lib/vSphere/provider.rb +1 -1
- data/lib/vSphere/util/machine_helpers.rb +6 -6
- data/lib/vSphere/util/vim_helpers.rb +27 -23
- data/lib/vSphere/util/vm_helpers.rb +1 -2
- data/lib/vSphere/version.rb +1 -1
- data/lib/vagrant-vsphere.rb +1 -1
- data/spec/action_spec.rb +1 -0
- data/spec/clone_spec.rb +40 -42
- data/spec/connect_vsphere_spec.rb +8 -8
- data/spec/get_ssh_info_spec.rb +2 -2
- data/spec/spec_helper.rb +59 -63
- data/vSphere.gemspec +5 -4
- metadata +19 -1
data/Rakefile
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'bundler/setup'
|
3
3
|
require 'rspec/core/rake_task'
|
4
|
+
require 'rubocop/rake_task'
|
4
5
|
|
5
6
|
# Immediately sync all stdout so that tools like buildbot can
|
6
7
|
# immediately load in the output.
|
@@ -14,4 +15,6 @@ Bundler::GemHelper.install_tasks
|
|
14
15
|
|
15
16
|
RSpec::Core::RakeTask.new
|
16
17
|
|
17
|
-
|
18
|
+
RuboCop::RakeTask.new
|
19
|
+
|
20
|
+
task default: %w(rubocop spec)
|
data/lib/vSphere/action.rb
CHANGED
@@ -6,18 +6,16 @@ module VagrantPlugins
|
|
6
6
|
module Action
|
7
7
|
include Vagrant::Action::Builtin
|
8
8
|
|
9
|
-
#Vagrant commands
|
9
|
+
# Vagrant commands
|
10
10
|
def self.action_destroy
|
11
11
|
Vagrant::Action::Builder.new.tap do |b|
|
12
12
|
b.use ConfigValidate
|
13
13
|
b.use ConnectVSphere
|
14
14
|
|
15
15
|
b.use Call, IsRunning do |env, b2|
|
16
|
-
if [:result]
|
17
|
-
b2.use Call, GracefulHalt, :poweroff, :running do |
|
18
|
-
|
19
|
-
b3.use PowerOff
|
20
|
-
end
|
16
|
+
if env[:result]
|
17
|
+
b2.use Call, GracefulHalt, :poweroff, :running do |env2, b3|
|
18
|
+
b3.use PowerOff unless env2[:result]
|
21
19
|
end
|
22
20
|
end
|
23
21
|
end
|
@@ -29,13 +27,13 @@ module VagrantPlugins
|
|
29
27
|
Vagrant::Action::Builder.new.tap do |b|
|
30
28
|
b.use ConfigValidate
|
31
29
|
b.use Call, IsCreated do |env, b2|
|
32
|
-
|
30
|
+
unless env[:result]
|
33
31
|
b2.use MessageNotCreated
|
34
32
|
next
|
35
33
|
end
|
36
34
|
|
37
|
-
b2.use Call, IsRunning do |
|
38
|
-
|
35
|
+
b2.use Call, IsRunning do |env2, b3|
|
36
|
+
unless env2[:result]
|
39
37
|
b3.use MessageNotRunning
|
40
38
|
next
|
41
39
|
end
|
@@ -51,13 +49,13 @@ module VagrantPlugins
|
|
51
49
|
Vagrant::Action::Builder.new.tap do |b|
|
52
50
|
b.use ConfigValidate
|
53
51
|
b.use Call, IsCreated do |env, b2|
|
54
|
-
|
52
|
+
unless env[:result]
|
55
53
|
b2.use MessageNotCreated
|
56
54
|
next
|
57
55
|
end
|
58
56
|
|
59
|
-
b2.use Call, IsRunning do |
|
60
|
-
|
57
|
+
b2.use Call, IsRunning do |env2, b3|
|
58
|
+
unless env2[:result]
|
61
59
|
b3.use MessageNotRunning
|
62
60
|
next
|
63
61
|
end
|
@@ -72,13 +70,13 @@ module VagrantPlugins
|
|
72
70
|
Vagrant::Action::Builder.new.tap do |b|
|
73
71
|
b.use ConfigValidate
|
74
72
|
b.use Call, IsCreated do |env, b2|
|
75
|
-
|
73
|
+
unless env[:result]
|
76
74
|
b2.use MessageNotCreated
|
77
75
|
next
|
78
76
|
end
|
79
77
|
|
80
|
-
b2.use Call, IsRunning do |
|
81
|
-
|
78
|
+
b2.use Call, IsRunning do |env2, b3|
|
79
|
+
unless env2[:result]
|
82
80
|
b3.use MessageNotRunning
|
83
81
|
next
|
84
82
|
end
|
@@ -103,9 +101,7 @@ module VagrantPlugins
|
|
103
101
|
b2.use Clone
|
104
102
|
end
|
105
103
|
b.use Call, IsRunning do |env, b2|
|
106
|
-
|
107
|
-
b2.use PowerOn
|
108
|
-
end
|
104
|
+
b2.use PowerOn unless env[:result]
|
109
105
|
end
|
110
106
|
b.use CloseVSphere
|
111
107
|
b.use Provision
|
@@ -119,21 +115,19 @@ module VagrantPlugins
|
|
119
115
|
b.use ConfigValidate
|
120
116
|
b.use ConnectVSphere
|
121
117
|
b.use Call, IsCreated do |env, b2|
|
122
|
-
|
118
|
+
unless env[:result]
|
123
119
|
b2.use MessageNotCreated
|
124
120
|
next
|
125
121
|
end
|
126
122
|
|
127
|
-
b2.use Call, IsRunning do |
|
128
|
-
|
123
|
+
b2.use Call, IsRunning do |env2, b3|
|
124
|
+
unless env2[:result]
|
129
125
|
b3.use MessageNotRunning
|
130
126
|
next
|
131
127
|
end
|
132
128
|
|
133
|
-
b3.use Call, GracefulHalt, :poweroff, :running do |
|
134
|
-
|
135
|
-
b4.use PowerOff
|
136
|
-
end
|
129
|
+
b3.use Call, GracefulHalt, :poweroff, :running do |env3, b4|
|
130
|
+
b4.use PowerOff unless env3[:result]
|
137
131
|
end
|
138
132
|
end
|
139
133
|
end
|
@@ -145,7 +139,7 @@ module VagrantPlugins
|
|
145
139
|
Vagrant::Action::Builder.new.tap do |b|
|
146
140
|
b.use ConnectVSphere
|
147
141
|
b.use Call, IsCreated do |env, b2|
|
148
|
-
|
142
|
+
unless env[:result]
|
149
143
|
b2.use MessageNotCreated
|
150
144
|
next
|
151
145
|
end
|
@@ -155,7 +149,7 @@ module VagrantPlugins
|
|
155
149
|
end
|
156
150
|
end
|
157
151
|
|
158
|
-
#vSphere specific actions
|
152
|
+
# vSphere specific actions
|
159
153
|
def self.action_get_state
|
160
154
|
Vagrant::Action::Builder.new.tap do |b|
|
161
155
|
b.use HandleBox
|
@@ -175,7 +169,7 @@ module VagrantPlugins
|
|
175
169
|
end
|
176
170
|
end
|
177
171
|
|
178
|
-
#autoload
|
172
|
+
# autoload
|
179
173
|
action_root = Pathname.new(File.expand_path('../action', __FILE__))
|
180
174
|
autoload :Clone, action_root.join('clone')
|
181
175
|
autoload :CloseVSphere, action_root.join('close_vsphere')
|
data/lib/vSphere/action/clone.rb
CHANGED
@@ -10,7 +10,7 @@ module VagrantPlugins
|
|
10
10
|
include Util::VimHelpers
|
11
11
|
include Util::MachineHelpers
|
12
12
|
|
13
|
-
def initialize(app,
|
13
|
+
def initialize(app, _env)
|
14
14
|
@app = app
|
15
15
|
end
|
16
16
|
|
@@ -21,17 +21,17 @@ module VagrantPlugins
|
|
21
21
|
name = get_name machine, config, env[:root_path]
|
22
22
|
dc = get_datacenter connection, machine
|
23
23
|
template = dc.find_vm config.template_name
|
24
|
-
|
24
|
+
fail Errors::VSphereError, :'missing_template' if template.nil?
|
25
25
|
vm_base_folder = get_vm_base_folder dc, template, config
|
26
|
-
|
26
|
+
fail Errors::VSphereError, :'invalid_base_path' if vm_base_folder.nil?
|
27
27
|
|
28
28
|
begin
|
29
|
-
# Storage DRS does not support vSphere linked clones. http://www.vmware.com/files/pdf/techpaper/vsphere-storage-drs-interoperability.pdf
|
29
|
+
# Storage DRS does not support vSphere linked clones. http://www.vmware.com/files/pdf/techpaper/vsphere-storage-drs-interoperability.pdf
|
30
30
|
ds = get_datastore dc, machine
|
31
|
-
|
31
|
+
fail Errors::VSphereError, :'invalid_configuration_linked_clone_with_sdrs' if config.linked_clone && datastore.is_a?(RbVmomi::VIM::StoragePod)
|
32
32
|
|
33
33
|
location = get_location ds, dc, machine, template
|
34
|
-
spec = RbVmomi::VIM.VirtualMachineCloneSpec :
|
34
|
+
spec = RbVmomi::VIM.VirtualMachineCloneSpec location: location, powerOn: true, template: false
|
35
35
|
spec[:config] = RbVmomi::VIM.VirtualMachineConfigSpec
|
36
36
|
customization_info = get_customization_spec_info_by_name connection, machine
|
37
37
|
|
@@ -41,47 +41,47 @@ module VagrantPlugins
|
|
41
41
|
add_custom_memory(spec, config.memory_mb) unless config.memory_mb.nil?
|
42
42
|
add_custom_cpu(spec, config.cpu_count) unless config.cpu_count.nil?
|
43
43
|
|
44
|
-
if !config.clone_from_vm
|
44
|
+
if !config.clone_from_vm && ds.is_a?(RbVmomi::VIM::StoragePod)
|
45
45
|
|
46
|
-
|
47
|
-
|
48
|
-
# TODO: May want to add option on type?
|
49
|
-
|
46
|
+
storage_mgr = connection.serviceContent.storageResourceManager
|
47
|
+
pod_spec = RbVmomi::VIM.StorageDrsPodSelectionSpec(storagePod: ds)
|
48
|
+
# TODO: May want to add option on type?
|
49
|
+
storage_spec = RbVmomi::VIM.StoragePlacementSpec(type: 'clone', cloneName: name, folder: vm_base_folder, podSelectionSpec: pod_spec, vm: template, cloneSpec: spec)
|
50
50
|
|
51
51
|
env[:ui].info I18n.t('vsphere.requesting_sdrs_recommendation')
|
52
52
|
env[:ui].info " -- DatastoreCluster: #{ds.name}"
|
53
53
|
env[:ui].info " -- Template VM: #{template.pretty_path}"
|
54
54
|
env[:ui].info " -- Target VM: #{vm_base_folder.pretty_path}/#{name}"
|
55
55
|
|
56
|
-
result =
|
56
|
+
result = storage_mgr.RecommendDatastores(storageSpec: storage_spec)
|
57
57
|
|
58
58
|
recommendation = result.recommendations[0]
|
59
59
|
key = recommendation.key ||= ''
|
60
60
|
if key == ''
|
61
|
-
|
61
|
+
fail Errors::VSphereError, :missing_datastore_recommendation
|
62
62
|
end
|
63
63
|
|
64
64
|
env[:ui].info I18n.t('vsphere.creating_cloned_vm_sdrs')
|
65
65
|
env[:ui].info " -- Storage DRS recommendation: #{recommendation.target.name} #{recommendation.reasonText}"
|
66
66
|
|
67
|
-
|
68
|
-
new_vm =
|
67
|
+
apply_sr_result = storage_mgr.ApplyStorageDrsRecommendation_Task(key: [key]).wait_for_completion
|
68
|
+
new_vm = apply_sr_result.vm
|
69
69
|
|
70
70
|
else
|
71
71
|
|
72
72
|
env[:ui].info I18n.t('vsphere.creating_cloned_vm')
|
73
|
-
env[:ui].info " -- #{config.clone_from_vm ?
|
73
|
+
env[:ui].info " -- #{config.clone_from_vm ? 'Source' : 'Template'} VM: #{template.pretty_path}"
|
74
74
|
env[:ui].info " -- Target VM: #{vm_base_folder.pretty_path}/#{name}"
|
75
75
|
|
76
|
-
new_vm = template.CloneVM_Task(:
|
76
|
+
new_vm = template.CloneVM_Task(folder: vm_base_folder, name: name, spec: spec).wait_for_completion
|
77
77
|
end
|
78
|
-
rescue Errors::VSphereError
|
78
|
+
rescue Errors::VSphereError
|
79
79
|
raise
|
80
|
-
rescue
|
80
|
+
rescue StandardError => e
|
81
81
|
raise Errors::VSphereError.new, e.message
|
82
82
|
end
|
83
83
|
|
84
|
-
#TODO: handle interrupted status in the environment, should the vm be destroyed?
|
84
|
+
# TODO: handle interrupted status in the environment, should the vm be destroyed?
|
85
85
|
|
86
86
|
machine.id = new_vm.config.uuid
|
87
87
|
|
@@ -103,7 +103,7 @@ module VagrantPlugins
|
|
103
103
|
return customization_spec if private_networks.nil?
|
104
104
|
|
105
105
|
# make sure we have enough NIC settings to override with the private network settings
|
106
|
-
|
106
|
+
fail Errors::VSphereError, :'too_many_private_networks' if private_networks.length > customization_spec.nicSettingMap.length
|
107
107
|
|
108
108
|
# assign the private network IP to the NIC
|
109
109
|
private_networks.each_index do |idx|
|
@@ -124,28 +124,28 @@ module VagrantPlugins
|
|
124
124
|
# Thus, this code first create a delta disk on top of the base disk for
|
125
125
|
# the to-be-cloned VM, if delta disks aren't used already.
|
126
126
|
disks = template.config.hardware.device.grep(RbVmomi::VIM::VirtualDisk)
|
127
|
-
disks.select { |disk| disk.backing.parent
|
127
|
+
disks.select { |disk| disk.backing.parent.nil? }.each do |disk|
|
128
128
|
spec = {
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
129
|
+
deviceChange: [
|
130
|
+
{
|
131
|
+
operation: :remove,
|
132
|
+
device: disk
|
133
|
+
},
|
134
|
+
{
|
135
|
+
operation: :add,
|
136
|
+
fileOperation: :create,
|
137
|
+
device: disk.dup.tap do |new_disk|
|
138
|
+
new_disk.backing = new_disk.backing.dup
|
139
|
+
new_disk.backing.fileName = "[#{disk.backing.datastore.name}]"
|
140
|
+
new_disk.backing.parent = disk.backing
|
141
|
+
end
|
142
|
+
}
|
143
|
+
]
|
144
144
|
}
|
145
|
-
template.ReconfigVM_Task(:
|
145
|
+
template.ReconfigVM_Task(spec: spec).wait_for_completion
|
146
146
|
end
|
147
147
|
|
148
|
-
location = RbVmomi::VIM.VirtualMachineRelocateSpec(:
|
148
|
+
location = RbVmomi::VIM.VirtualMachineRelocateSpec(diskMoveType: :moveChildMostDiskBacking)
|
149
149
|
elsif datastore.is_a? RbVmomi::VIM::StoragePod
|
150
150
|
location = RbVmomi::VIM.VirtualMachineRelocateSpec
|
151
151
|
else
|
@@ -160,17 +160,17 @@ module VagrantPlugins
|
|
160
160
|
def get_name(machine, config, root_path)
|
161
161
|
return config.name unless config.name.nil?
|
162
162
|
|
163
|
-
prefix = "#{root_path.basename
|
164
|
-
prefix.gsub!(/[^-a-z0-9_\.]/i,
|
163
|
+
prefix = "#{root_path.basename}_#{machine.name}"
|
164
|
+
prefix.gsub!(/[^-a-z0-9_\.]/i, '')
|
165
165
|
# milliseconds + random number suffix to allow for simultaneous `vagrant up` of the same box in different dirs
|
166
|
-
prefix + "_#{(Time.now.to_f * 1000.0).to_i}_#{rand(
|
166
|
+
prefix + "_#{(Time.now.to_f * 1000.0).to_i}_#{rand(100_000)}"
|
167
167
|
end
|
168
168
|
|
169
169
|
def get_vm_base_folder(dc, template, config)
|
170
170
|
if config.vm_base_path.nil?
|
171
171
|
template.parent
|
172
172
|
else
|
173
|
-
dc.vmFolder.traverse(config.vm_base_path, RbVmomi::VIM::Folder,
|
173
|
+
dc.vmFolder.traverse(config.vm_base_path, RbVmomi::VIM::Folder, true)
|
174
174
|
end
|
175
175
|
end
|
176
176
|
|
@@ -182,7 +182,7 @@ module VagrantPlugins
|
|
182
182
|
|
183
183
|
yield(@card)
|
184
184
|
|
185
|
-
dev_spec = RbVmomi::VIM.VirtualDeviceConfigSpec(:
|
185
|
+
dev_spec = RbVmomi::VIM.VirtualDeviceConfigSpec(device: @card, operation: 'edit')
|
186
186
|
spec[:config][:deviceChange].push dev_spec
|
187
187
|
spec[:config][:deviceChange].uniq!
|
188
188
|
end
|
@@ -198,11 +198,11 @@ module VagrantPlugins
|
|
198
198
|
|
199
199
|
modify_network_card(template, spec) do |card|
|
200
200
|
begin
|
201
|
-
switch_port = RbVmomi::VIM.DistributedVirtualSwitchPortConnection(:
|
201
|
+
switch_port = RbVmomi::VIM.DistributedVirtualSwitchPortConnection(switchUuid: network.config.distributedVirtualSwitch.uuid, portgroupKey: network.key)
|
202
202
|
card.backing.port = switch_port
|
203
203
|
rescue
|
204
204
|
# not connected to a distibuted switch?
|
205
|
-
card.backing = RbVmomi::VIM::VirtualEthernetCardNetworkBackingInfo(:
|
205
|
+
card.backing = RbVmomi::VIM::VirtualEthernetCardNetworkBackingInfo(network: network, deviceName: network.name)
|
206
206
|
end
|
207
207
|
end
|
208
208
|
end
|
@@ -4,21 +4,19 @@ module VagrantPlugins
|
|
4
4
|
module VSphere
|
5
5
|
module Action
|
6
6
|
class CloseVSphere
|
7
|
-
def initialize(app,
|
7
|
+
def initialize(app, _env)
|
8
8
|
@app = app
|
9
9
|
end
|
10
10
|
|
11
11
|
def call(env)
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
raise Errors::VSphereError.new, e.message
|
19
|
-
end
|
12
|
+
env[:vSphere_connection].close
|
13
|
+
@app.call env
|
14
|
+
rescue Errors::VSphereError
|
15
|
+
raise
|
16
|
+
rescue StandardError => e
|
17
|
+
raise Errors::VSphereError.new, e.message
|
20
18
|
end
|
21
19
|
end
|
22
20
|
end
|
23
21
|
end
|
24
|
-
end
|
22
|
+
end
|
@@ -4,7 +4,7 @@ module VagrantPlugins
|
|
4
4
|
module VSphere
|
5
5
|
module Action
|
6
6
|
class ConnectVSphere
|
7
|
-
def initialize(app,
|
7
|
+
def initialize(app, _env)
|
8
8
|
@app = app
|
9
9
|
end
|
10
10
|
|
@@ -13,13 +13,13 @@ module VagrantPlugins
|
|
13
13
|
|
14
14
|
begin
|
15
15
|
env[:vSphere_connection] = RbVmomi::VIM.connect host: config.host,
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
user: config.user, password: config.password,
|
17
|
+
insecure: config.insecure, proxyHost: config.proxy_host,
|
18
|
+
proxyPort: config.proxy_port
|
19
19
|
@app.call env
|
20
|
-
rescue Errors::VSphereError
|
20
|
+
rescue Errors::VSphereError
|
21
21
|
raise
|
22
|
-
rescue
|
22
|
+
rescue StandardError => e
|
23
23
|
raise Errors::VSphereError.new, e.message
|
24
24
|
end
|
25
25
|
end
|
@@ -8,7 +8,7 @@ module VagrantPlugins
|
|
8
8
|
class Destroy
|
9
9
|
include Util::VimHelpers
|
10
10
|
|
11
|
-
def initialize(app,
|
11
|
+
def initialize(app, _env)
|
12
12
|
@app = app
|
13
13
|
end
|
14
14
|
|
@@ -19,8 +19,8 @@ module VagrantPlugins
|
|
19
19
|
@app.call env
|
20
20
|
end
|
21
21
|
|
22
|
-
private
|
23
|
-
|
22
|
+
private
|
23
|
+
|
24
24
|
def destroy_vm(env)
|
25
25
|
vm = get_vm_by_uuid env[:vSphere_connection], env[:machine]
|
26
26
|
return if vm.nil?
|
@@ -28,13 +28,13 @@ module VagrantPlugins
|
|
28
28
|
begin
|
29
29
|
env[:ui].info I18n.t('vsphere.destroy_vm')
|
30
30
|
vm.Destroy_Task.wait_for_completion
|
31
|
-
rescue Errors::VSphereError
|
31
|
+
rescue Errors::VSphereError
|
32
32
|
raise
|
33
|
-
rescue
|
33
|
+
rescue StandardError => e
|
34
34
|
raise Errors::VSphereError.new, e.message
|
35
35
|
end
|
36
36
|
end
|
37
37
|
end
|
38
38
|
end
|
39
39
|
end
|
40
|
-
end
|
40
|
+
end
|
@@ -7,8 +7,7 @@ module VagrantPlugins
|
|
7
7
|
class GetSshInfo
|
8
8
|
include Util::VimHelpers
|
9
9
|
|
10
|
-
|
11
|
-
def initialize(app, env)
|
10
|
+
def initialize(app, _env)
|
12
11
|
@app = app
|
13
12
|
end
|
14
13
|
|
@@ -27,9 +26,9 @@ module VagrantPlugins
|
|
27
26
|
|
28
27
|
return nil if vm.nil?
|
29
28
|
return nil if vm.guest.ipAddress.nil? || vm.guest.ipAddress.empty?
|
30
|
-
|
31
|
-
|
32
|
-
|
29
|
+
{
|
30
|
+
host: vm.guest.ipAddress,
|
31
|
+
port: 22
|
33
32
|
}
|
34
33
|
end
|
35
34
|
end
|