djo-vagrant-vsphere 1.6.1

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.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/.bumpversion.cfg +11 -0
  3. data/.gitignore +8 -0
  4. data/.rubocop.yml +13 -0
  5. data/.rubocop_todo.yml +80 -0
  6. data/.travis.yml +11 -0
  7. data/CHANGELOG.md +250 -0
  8. data/DEVELOPMENT.md +67 -0
  9. data/Gemfile +16 -0
  10. data/LICENSE.txt +24 -0
  11. data/README.md +310 -0
  12. data/Rakefile +20 -0
  13. data/example_box/metadata.json +3 -0
  14. data/lib/vSphere/action.rb +195 -0
  15. data/lib/vSphere/action/clone.rb +239 -0
  16. data/lib/vSphere/action/close_vsphere.rb +22 -0
  17. data/lib/vSphere/action/connect_vsphere.rb +29 -0
  18. data/lib/vSphere/action/destroy.rb +41 -0
  19. data/lib/vSphere/action/get_ssh_info.rb +37 -0
  20. data/lib/vSphere/action/get_state.rb +41 -0
  21. data/lib/vSphere/action/is_created.rb +16 -0
  22. data/lib/vSphere/action/is_running.rb +16 -0
  23. data/lib/vSphere/action/message_already_created.rb +18 -0
  24. data/lib/vSphere/action/message_not_created.rb +18 -0
  25. data/lib/vSphere/action/message_not_running.rb +18 -0
  26. data/lib/vSphere/action/power_off.rb +40 -0
  27. data/lib/vSphere/action/power_on.rb +28 -0
  28. data/lib/vSphere/cap/public_address.rb +15 -0
  29. data/lib/vSphere/config.rb +60 -0
  30. data/lib/vSphere/errors.rb +11 -0
  31. data/lib/vSphere/plugin.rb +44 -0
  32. data/lib/vSphere/provider.rb +39 -0
  33. data/lib/vSphere/util/vim_helpers.rb +91 -0
  34. data/lib/vSphere/util/vm_helpers.rb +39 -0
  35. data/lib/vSphere/version.rb +5 -0
  36. data/lib/vagrant-vsphere.rb +18 -0
  37. data/locales/en.yml +64 -0
  38. data/spec/action_spec.rb +162 -0
  39. data/spec/clone_spec.rb +102 -0
  40. data/spec/connect_vsphere_spec.rb +26 -0
  41. data/spec/destroy_spec.rb +31 -0
  42. data/spec/get_ssh_info_spec.rb +31 -0
  43. data/spec/get_state_spec.rb +55 -0
  44. data/spec/is_created_spec.rb +29 -0
  45. data/spec/power_off_spec.rb +35 -0
  46. data/spec/spec_helper.rb +147 -0
  47. data/vSphere.gemspec +30 -0
  48. data/vsphere_screenshot.png +0 -0
  49. metadata +212 -0
@@ -0,0 +1,18 @@
1
+ require 'pathname'
2
+
3
+ require 'vSphere/plugin'
4
+
5
+ module VagrantPlugins
6
+ module VSphere
7
+ lib_path = Pathname.new(File.expand_path('../vSphere', __FILE__))
8
+ autoload :Action, lib_path.join('action')
9
+ autoload :Errors, lib_path.join('errors')
10
+
11
+ # This returns the path to the source of this plugin.
12
+ #
13
+ # @return [Pathname]
14
+ def self.source_root
15
+ @source_root ||= Pathname.new(File.expand_path('../../', __FILE__))
16
+ end
17
+ end
18
+ end
data/locales/en.yml ADDED
@@ -0,0 +1,64 @@
1
+ en:
2
+ vsphere:
3
+ creating_cloned_vm: |-
4
+ Calling vSphere CloneVM with the following settings:
5
+ creating_cloned_vm_sdrs: |-
6
+ Calling vSphere ApplyStorageDrsRecommendation with the following settings:
7
+ requesting_sdrs_recommendation: |-
8
+ Calling vSphere RecommendDatastores with StoragePlacementSpec of the following settings:
9
+ vm_clone_success: |-
10
+ New virtual machine successfully cloned
11
+ destroy_vm: |-
12
+ Calling vSphere Destroy
13
+ power_off_vm: |-
14
+ Calling vSphere PowerOff
15
+ power_on_vm: |-
16
+ Calling vSphere PowerOn
17
+ vm_already_created: |-
18
+ The VM is already created
19
+ vm_not_created: |-
20
+ The VM has not been created
21
+ vm_not_running: |-
22
+ The VM is not running
23
+
24
+ errors:
25
+ missing_template: |-
26
+ Configured template/source VM could not be found
27
+ invalid_base_path: |-
28
+ Could not find base path for target VM, check 'vm_base_path' configuration value
29
+ missing_datacenter: |-
30
+ Configured data center not found
31
+ missing_compute_resource: |-
32
+ Configured compute resource not found
33
+ missing_resource_pool: |-
34
+ Configured resource pool not found
35
+ null_configuration_spec_manager: |-
36
+ Configuration spec manager not configured in the ServiceInstance
37
+ missing_configuration_spec: |-
38
+ Configured configuration spec not found
39
+ missing_datastore: |-
40
+ Configured data store not found
41
+ too_many_private_networks: |-
42
+ There a more private networks configured than can be assigned to the customization spec
43
+ missing_vlan: |-
44
+ Configured vlan not found
45
+ missing_network_card: |-
46
+ Cannot find network card to customize
47
+ invalid_configuration_linked_clone_with_sdrs: |-
48
+ Cannot use Linked Clone with Storage DRS
49
+
50
+ config:
51
+ host: |-
52
+ Configuration must specify a vSphere host
53
+ user: |-
54
+ Configuration must specify a vSphere user
55
+ password: |-
56
+ Configuration must specify a vSphere password
57
+ name: |-
58
+ Configuration must specify a VM name
59
+ template: |-
60
+ Configuration must specify a template name
61
+ compute_resource: |-
62
+ Configuration must specify a compute resource name
63
+ resource_pool: |-
64
+ Configuration must specify a resource pool name
@@ -0,0 +1,162 @@
1
+ require 'spec_helper'
2
+ require 'vagrant'
3
+
4
+ describe VagrantPlugins::VSphere::Action do
5
+ def run(action)
6
+ Vagrant::Action::Runner.new.run described_class.send("action_#{action}"), @env
7
+ end
8
+
9
+ before :each do
10
+ @machine.stub(:id).and_return(EXISTING_UUID)
11
+ end
12
+
13
+ describe 'up' do
14
+ def run_up
15
+ run :up
16
+ end
17
+
18
+ it 'should connect to vSphere' do
19
+ VagrantPlugins::VSphere::Action::ConnectVSphere.any_instance.should_receive(:call)
20
+
21
+ run_up
22
+ end
23
+
24
+ it 'should check if the VM exits' do
25
+ VagrantPlugins::VSphere::Action::IsCreated.any_instance.should_receive(:call)
26
+
27
+ run_up
28
+ end
29
+
30
+ it 'should create the VM when the VM does already not exist' do
31
+ @machine.state.stub(:id).and_return(:not_created)
32
+
33
+ VagrantPlugins::VSphere::Action::Clone.any_instance.should_receive(:call)
34
+
35
+ run_up
36
+ end
37
+
38
+ it 'should not create the VM when the VM already exists' do
39
+ @machine.state.stub(:id).and_return(:running)
40
+
41
+ VagrantPlugins::VSphere::Action::Clone.any_instance.should_not_receive(:call)
42
+
43
+ run_up
44
+ end
45
+ end
46
+
47
+ describe 'destroy' do
48
+ def run_destroy
49
+ run :destroy
50
+ end
51
+
52
+ it 'should connect to vSphere' do
53
+ VagrantPlugins::VSphere::Action::ConnectVSphere.any_instance.should_receive(:call)
54
+
55
+ run_destroy
56
+ end
57
+
58
+ it 'should power off the VM' do
59
+ @machine.state.stub(:id).and_return(:running)
60
+ VagrantPlugins::VSphere::Action::PowerOff.any_instance.should_receive(:call)
61
+
62
+ run_destroy
63
+ end
64
+
65
+ it 'should destroy the VM' do
66
+ VagrantPlugins::VSphere::Action::Destroy.any_instance.should_receive(:call)
67
+
68
+ run_destroy
69
+ end
70
+ end
71
+
72
+ describe 'halt' do
73
+ after :each do
74
+ run :halt
75
+ end
76
+
77
+ it 'should connect to vSphere' do
78
+ VagrantPlugins::VSphere::Action::ConnectVSphere.any_instance.should_receive(:call)
79
+ end
80
+
81
+ it 'should gracefully power off the VM' do
82
+ @machine.state.stub(:id).and_return(:running)
83
+ VagrantPlugins::VSphere::Action::GracefulHalt.any_instance.should_receive(:call)
84
+ end
85
+ end
86
+
87
+ describe 'get state' do
88
+ def run_get_state
89
+ run :get_state
90
+ end
91
+
92
+ it 'should connect to vSphere' do
93
+ VagrantPlugins::VSphere::Action::ConnectVSphere.any_instance.should_receive(:call)
94
+
95
+ run_get_state
96
+ end
97
+
98
+ it 'should get the power state' do
99
+ VagrantPlugins::VSphere::Action::GetState.any_instance.should_receive(:call)
100
+
101
+ run_get_state
102
+ end
103
+
104
+ it 'should handle the values in a base vagrant box' do
105
+ Vagrant::Action::Builtin::HandleBox.any_instance.should_receive(:call)
106
+
107
+ run_get_state
108
+ end
109
+ end
110
+
111
+ describe 'get ssh info' do
112
+ def run_get_ssh_info
113
+ run :get_ssh_info
114
+ end
115
+
116
+ it 'should connect to vSphere' do
117
+ VagrantPlugins::VSphere::Action::ConnectVSphere.any_instance.should_receive(:call)
118
+
119
+ run_get_ssh_info
120
+ end
121
+
122
+ it 'should get the power state' do
123
+ VagrantPlugins::VSphere::Action::GetSshInfo.any_instance.should_receive(:call)
124
+
125
+ run_get_ssh_info
126
+ end
127
+ end
128
+
129
+ describe 'reload' do
130
+ after :each do
131
+ run :reload
132
+ end
133
+
134
+ it 'should gracefully power off the VM' do
135
+ @machine.state.stub(:id).and_return(:running)
136
+
137
+ VagrantPlugins::VSphere::Action::GracefulHalt.any_instance.should_receive(:call)
138
+ end
139
+
140
+ it 'should connect to vSphere' do
141
+ VagrantPlugins::VSphere::Action::ConnectVSphere.any_instance.should_receive(:call)
142
+ end
143
+
144
+ it 'should check if the VM exits' do
145
+ VagrantPlugins::VSphere::Action::IsCreated.any_instance.should_receive(:call)
146
+ end
147
+
148
+ it 'should not create the VM when the VM does already not exist' do
149
+ @machine.state.stub(:id).and_return(:not_created)
150
+
151
+ VagrantPlugins::VSphere::Action::Clone.any_instance.should_not_receive(:call)
152
+ VagrantPlugins::VSphere::Action::MessageNotCreated.any_instance.should_receive(:call)
153
+ end
154
+
155
+ it 'should not create the VM when the VM already exists' do
156
+ @machine.state.stub(:id).and_return(:running)
157
+
158
+ VagrantPlugins::VSphere::Action::Clone.any_instance.should_not_receive(:call)
159
+ VagrantPlugins::VSphere::Action::MessageAlreadyCreated.any_instance.should_receive(:call)
160
+ end
161
+ end
162
+ end
@@ -0,0 +1,102 @@
1
+ require 'spec_helper'
2
+
3
+ CUSTOM_VM_FOLDER = 'custom_vm_folder'
4
+
5
+ describe VagrantPlugins::VSphere::Action::Clone do
6
+ before :each do
7
+ @env[:vSphere_connection] = @vim
8
+ end
9
+
10
+ it "should create a CloneVM task with template's parent" do
11
+ call
12
+ expect(@template).to have_received(:CloneVM_Task).with(
13
+ folder: @data_center,
14
+ name: NAME,
15
+ spec: { location: { pool: @child_resource_pool },
16
+ config: RbVmomi::VIM.VirtualMachineConfigSpec }
17
+ )
18
+ end
19
+
20
+ it 'should create a CloneVM task with custom folder when given vm base path' do
21
+ custom_base_folder = double(CUSTOM_VM_FOLDER,
22
+ pretty_path: "#{@data_center.pretty_path}/#{CUSTOM_VM_FOLDER}")
23
+ @machine.provider_config.stub(:vm_base_path).and_return(CUSTOM_VM_FOLDER)
24
+ @data_center.vmFolder.stub(:traverse).with(CUSTOM_VM_FOLDER, RbVmomi::VIM::Folder, true).and_return(custom_base_folder)
25
+ call
26
+ expect(@template).to have_received(:CloneVM_Task).with(
27
+ folder: custom_base_folder,
28
+ name: NAME,
29
+ spec: { location: { pool: @child_resource_pool },
30
+ config: RbVmomi::VIM.VirtualMachineConfigSpec }
31
+ )
32
+ end
33
+
34
+ it 'should set the machine id to be the new UUID' do
35
+ call
36
+ expect(@machine).to have_received(:id=).with(NEW_UUID)
37
+ end
38
+
39
+ it 'should call the next item in the middleware stack' do
40
+ call
41
+ expect(@app).to have_received :call
42
+ end
43
+
44
+ it 'should create a CloneVM spec with configured vlan' do
45
+ @machine.provider_config.stub(:vlan).and_return('vlan')
46
+ network = double('network', name: 'vlan')
47
+ network.stub(:config).and_raise(StandardError)
48
+ @data_center.stub(:network).and_return([network])
49
+ call
50
+
51
+ expected_config = RbVmomi::VIM.VirtualMachineConfigSpec(deviceChange: Array.new)
52
+ expected_dev_spec = RbVmomi::VIM.VirtualDeviceConfigSpec(device: @device, operation: 'edit')
53
+ expected_config[:deviceChange].push expected_dev_spec
54
+
55
+ expect(@template).to have_received(:CloneVM_Task).with(
56
+ folder: @data_center,
57
+ name: NAME,
58
+ spec: { location: { pool: @child_resource_pool },
59
+ config: expected_config
60
+ }
61
+ )
62
+ end
63
+
64
+ it 'should create a CloneVM spec with configured memory_mb' do
65
+ @machine.provider_config.stub(:memory_mb).and_return(2048)
66
+ call
67
+ expect(@template).to have_received(:CloneVM_Task).with(
68
+ folder: @data_center,
69
+ name: NAME,
70
+ spec: { location: { pool: @child_resource_pool },
71
+ config: RbVmomi::VIM.VirtualMachineConfigSpec(memoryMB: 2048) }
72
+ )
73
+ end
74
+
75
+ it 'should create a CloneVM spec with configured number of cpus' do
76
+ @machine.provider_config.stub(:cpu_count).and_return(4)
77
+ call
78
+ expect(@template).to have_received(:CloneVM_Task).with(
79
+ folder: @data_center,
80
+ name: NAME,
81
+ spec: { location: { pool: @child_resource_pool },
82
+ config: RbVmomi::VIM.VirtualMachineConfigSpec(numCPUs: 4) }
83
+ )
84
+ end
85
+
86
+ it 'should set static IP when given config spec' do
87
+ @machine.provider_config.stub(:customization_spec_name).and_return('spec')
88
+ call
89
+ expect(@ip).to have_received(:ipAddress=).with('0.0.0.0')
90
+ end
91
+
92
+ it 'should use root resource pool when cloning from template and no resource pool specified' do
93
+ @machine.provider_config.stub(:resource_pool_name).and_return(nil)
94
+ call
95
+ expect(@template).to have_received(:CloneVM_Task).with(
96
+ folder: @data_center,
97
+ name: NAME,
98
+ spec: { location: { pool: @root_resource_pool },
99
+ config: RbVmomi::VIM.VirtualMachineConfigSpec }
100
+ )
101
+ end
102
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe VagrantPlugins::VSphere::Action::ConnectVSphere do
4
+ before :each do
5
+ described_class.new(@app, @env).call(@env)
6
+ end
7
+
8
+ it 'should connect to vSphere' do
9
+ expect(VIM).to have_received(:connect).with(
10
+ host: @env[:machine].provider_config.host,
11
+ user: @env[:machine].provider_config.user,
12
+ password: @env[:machine].provider_config.password,
13
+ insecure: @env[:machine].provider_config.insecure,
14
+ proxyHost: @env[:machine].provider_config.proxy_host,
15
+ proxyPort: @env[:machine].provider_config.proxy_port
16
+ )
17
+ end
18
+
19
+ it 'should add the vSphere connection to the environment' do
20
+ expect(@env[:vSphere_connection]).to be @vim
21
+ end
22
+
23
+ it 'should call the next item in the middleware stack' do
24
+ expect(@app).to have_received :call
25
+ end
26
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe VagrantPlugins::VSphere::Action::Destroy do
4
+ before :each do
5
+ @env[:vSphere_connection] = @vim
6
+ end
7
+
8
+ it 'should set the machine id to nil' do
9
+ @env[:machine].stub(:id).and_return(MISSING_UUID)
10
+
11
+ call
12
+
13
+ expect(@env[:machine]).to have_received(:id=).with(nil)
14
+ end
15
+
16
+ it 'should not create a Destroy task if VM is not found' do
17
+ @env[:machine].stub(:id).and_return(MISSING_UUID)
18
+
19
+ call
20
+
21
+ expect(@vm).not_to have_received :Destroy_Task
22
+ end
23
+
24
+ it 'should create a VM Destroy task if the VM exists' do
25
+ @env[:machine].stub(:id).and_return(EXISTING_UUID)
26
+
27
+ call
28
+
29
+ expect(@vm).to have_received :Destroy_Task
30
+ end
31
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe VagrantPlugins::VSphere::Action::GetSshInfo do
4
+ before :each do
5
+ @env[:vSphere_connection] = @vim
6
+ end
7
+
8
+ it 'should set the ssh info to nil if machine ID is not set' do
9
+ call
10
+
11
+ expect(@env.key?(:machine_ssh_info)).to be true
12
+ expect(@env[:machine_ssh_info]).to be nil
13
+ end
14
+
15
+ it 'should set the ssh info to nil for a VM that does not exist' do
16
+ @env[:machine].stub(:id).and_return(MISSING_UUID)
17
+
18
+ call
19
+
20
+ expect(@env.key?(:machine_ssh_info)).to be true
21
+ expect(@env[:machine_ssh_info]).to be nil
22
+ end
23
+
24
+ it 'should set the ssh info host to the IP an existing VM' do
25
+ @env[:machine].stub(:id).and_return(EXISTING_UUID)
26
+
27
+ call
28
+
29
+ expect(@env[:machine_ssh_info][:host]).to be IP_ADDRESS
30
+ end
31
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+ require 'vSphere/util/vim_helpers'
3
+
4
+ describe VagrantPlugins::VSphere::Action::GetState do
5
+ before :each do
6
+ @env[:vSphere_connection] = @vim
7
+ end
8
+
9
+ it 'should set state id to not created if machine ID is not set' do
10
+ call
11
+
12
+ expect(@env[:machine_state_id]).to be :not_created
13
+ end
14
+
15
+ it 'should set state id to not created if VM is not found' do
16
+ @env[:machine].stub(:id).and_return(MISSING_UUID)
17
+
18
+ call
19
+
20
+ expect(@env[:machine_state_id]).to be :not_created
21
+ end
22
+
23
+ it 'should set state id to running if machine is powered on' do
24
+ @env[:machine].stub(:id).and_return(EXISTING_UUID)
25
+ @vm.runtime.stub(:powerState).and_return(VagrantPlugins::VSphere::Util::VmState::POWERED_ON)
26
+
27
+ call
28
+
29
+ expect(@env[:machine_state_id]).to be :running
30
+ end
31
+
32
+ it 'should set state id to powered off if machine is powered off' do
33
+ @env[:machine].stub(:id).and_return(EXISTING_UUID)
34
+ @vm.runtime.stub(:powerState).and_return(VagrantPlugins::VSphere::Util::VmState::POWERED_OFF)
35
+
36
+ call
37
+
38
+ expect(@env[:machine_state_id]).to be :poweroff
39
+ end
40
+
41
+ it 'should set state id to powered off if machine is suspended' do
42
+ @env[:machine].stub(:id).and_return(EXISTING_UUID)
43
+ @vm.runtime.stub(:powerState).and_return(VagrantPlugins::VSphere::Util::VmState::SUSPENDED)
44
+
45
+ call
46
+
47
+ expect(@env[:machine_state_id]).to be :poweroff
48
+ end
49
+
50
+ it 'should call the next item in the middleware stack' do
51
+ call
52
+
53
+ expect(@app).to have_received :call
54
+ end
55
+ end