vagrant-vsphere 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/.gitignore +5 -5
  2. data/Gemfile +9 -9
  3. data/LICENSE.txt +23 -23
  4. data/README.md +175 -164
  5. data/Rakefile +16 -16
  6. data/example_box/metadata.json +2 -2
  7. data/lib/vSphere/action/clone.rb +127 -83
  8. data/lib/vSphere/action/close_vsphere.rb +23 -23
  9. data/lib/vSphere/action/connect_vsphere.rb +25 -25
  10. data/lib/vSphere/action/destroy.rb +37 -37
  11. data/lib/vSphere/action/get_ssh_info.rb +37 -37
  12. data/lib/vSphere/action/get_state.rb +45 -45
  13. data/lib/vSphere/action/is_created.rb +15 -15
  14. data/lib/vSphere/action/is_running.rb +20 -20
  15. data/lib/vSphere/action/message_already_created.rb +17 -17
  16. data/lib/vSphere/action/message_not_created.rb +17 -17
  17. data/lib/vSphere/action/message_not_running.rb +18 -18
  18. data/lib/vSphere/action/power_off.rb +27 -27
  19. data/lib/vSphere/action/power_on.rb +31 -31
  20. data/lib/vSphere/action/sync_folders.rb +97 -81
  21. data/lib/vSphere/action.rb +172 -172
  22. data/lib/vSphere/config.rb +37 -37
  23. data/lib/vSphere/errors.rb +10 -10
  24. data/lib/vSphere/plugin.rb +29 -29
  25. data/lib/vSphere/provider.rb +38 -38
  26. data/lib/vSphere/util/machine_helpers.rb +15 -15
  27. data/lib/vSphere/util/vim_helpers.rb +36 -36
  28. data/lib/vSphere/version.rb +4 -4
  29. data/lib/vagrant-vsphere.rb +17 -17
  30. data/locales/en.yml +65 -65
  31. data/spec/action_spec.rb +116 -116
  32. data/spec/clone_spec.rb +31 -31
  33. data/spec/connect_vsphere_spec.rb +23 -23
  34. data/spec/destroy_spec.rb +30 -30
  35. data/spec/get_ssh_info_spec.rb +30 -30
  36. data/spec/get_state_spec.rb +54 -54
  37. data/spec/is_created_spec.rb +28 -28
  38. data/spec/spec_helper.rb +98 -97
  39. data/vSphere.gemspec +27 -27
  40. metadata +21 -5
  41. checksums.yaml +0 -15
@@ -1,83 +1,127 @@
1
- require 'rbvmomi'
2
- require 'i18n'
3
- require 'vSphere/util/vim_helpers'
4
- require 'vSphere/util/machine_helpers'
5
-
6
- module VagrantPlugins
7
- module VSphere
8
- module Action
9
- class Clone
10
- include Util::VimHelpers
11
- include Util::MachineHelpers
12
-
13
- def initialize(app, env)
14
- @app = app
15
- end
16
-
17
- def call(env)
18
- config = env[:machine].provider_config
19
- connection = env[:vSphere_connection]
20
- machine = env[:machine]
21
-
22
- dc = get_datacenter connection, machine
23
- template = dc.find_vm config.template_name
24
-
25
- raise Error::VSphereError, :message => I18n.t('errors.missing_template') if template.nil?
26
-
27
- begin
28
- location = RbVmomi::VIM.VirtualMachineRelocateSpec
29
- location[:pool] = get_resource_pool(connection, machine) unless config.clone_from_vm
30
-
31
- datastore = get_datastore connection, machine
32
- location[:datastore] = datastore unless datastore.nil?
33
-
34
- spec = RbVmomi::VIM.VirtualMachineCloneSpec :location => location, :powerOn => true, :template => false
35
-
36
- customization = get_customization_spec_info_by_name connection, machine
37
- spec[:customization] = configure_networks(customization.spec, machine) unless customization.nil?
38
-
39
- env[:ui].info I18n.t('vsphere.creating_cloned_vm')
40
- env[:ui].info " -- #{config.clone_from_vm ? "Source" : "Template"} VM: #{config.template_name}"
41
- env[:ui].info " -- Name: #{config.name}"
42
-
43
- new_vm = template.CloneVM_Task(:folder => template.parent, :name => config.name, :spec => spec).wait_for_completion
44
- rescue Exception => e
45
- puts e.message
46
- raise Errors::VSphereError, :message => e.message
47
- end
48
-
49
- #TODO: handle interrupted status in the environment, should the vm be destroyed?
50
-
51
- machine.id = new_vm.config.uuid
52
-
53
- # wait for SSH to be available
54
- wait_for_ssh env
55
-
56
- env[:ui].info I18n.t('vsphere.vm_clone_success')
57
-
58
- @app.call env
59
- end
60
-
61
- private
62
-
63
- def configure_networks(spec, machine)
64
- customization_spec = spec.clone
65
-
66
- # find all the configured private networks
67
- private_networks = machine.config.vm.networks.find_all { |n| n[0].eql? :private_network }
68
- return customization_spec if private_networks.nil?
69
-
70
- # make sure we have enough NIC settings to override with the private network settings
71
- raise Error::VSphereError, :message => I18n.t('errors.too_many_private_networks') if private_networks.length > customization_spec.nicSettingMap.length
72
-
73
- # assign the private network IP to the NIC
74
- private_networks.each_index do |idx|
75
- customization_spec.nicSettingMap[idx].adapter.ip.ipAddress = private_networks[idx][1][:ip]
76
- end
77
-
78
- customization_spec
79
- end
80
- end
81
- end
82
- end
83
- end
1
+ require 'rbvmomi'
2
+ require 'i18n'
3
+ require 'vSphere/util/vim_helpers'
4
+ require 'vSphere/util/machine_helpers'
5
+
6
+ module VagrantPlugins
7
+ module VSphere
8
+ module Action
9
+ class Clone
10
+ include Util::VimHelpers
11
+ include Util::MachineHelpers
12
+
13
+ def initialize(app, env)
14
+ @app = app
15
+ end
16
+
17
+ def call(env)
18
+ machine = env[:machine]
19
+ config = machine.provider_config
20
+ connection = env[:vSphere_connection]
21
+ name = get_name machine, config
22
+
23
+ dc = get_datacenter connection, machine
24
+ template = dc.find_vm config.template_name
25
+
26
+ raise Error::VSphereError, :message => I18n.t('errors.missing_template') if template.nil?
27
+
28
+ begin
29
+ if config.linked_clone
30
+ # The API for linked clones is quite strange. We can't create a linked
31
+ # straight from any VM. The disks of the VM for which we can create a
32
+ # linked clone need to be read-only and thus VC demands that the VM we
33
+ # are cloning from uses delta-disks. Only then it will allow us to
34
+ # share the base disk.
35
+ #
36
+ # Thus, this code first create a delta disk on top of the base disk for
37
+ # the to-be-cloned VM, if delta disks aren't used already.
38
+ disks = template.config.hardware.device.grep(RbVmomi::VIM::VirtualDisk)
39
+ disks.select { |disk| disk.backing.parent == nil }.each do |disk|
40
+ spec = {
41
+ :deviceChange => [
42
+ {
43
+ :operation => :remove,
44
+ :device => disk
45
+ },
46
+ {
47
+ :operation => :add,
48
+ :fileOperation => :create,
49
+ :device => disk.dup.tap { |new_disk|
50
+ new_disk.backing = new_disk.backing.dup
51
+ new_disk.backing.fileName = "[#{disk.backing.datastore.name}]"
52
+ new_disk.backing.parent = disk.backing
53
+ },
54
+ }
55
+ ]
56
+ }
57
+ template.ReconfigVM_Task(:spec => spec).wait_for_completion
58
+ end
59
+
60
+ location = RbVmomi::VIM.VirtualMachineRelocateSpec(:diskMoveType => :moveChildMostDiskBacking)
61
+ else
62
+ location = RbVmomi::VIM.VirtualMachineRelocateSpec
63
+ location[:pool] = get_resource_pool(connection, machine) unless config.clone_from_vm
64
+
65
+ datastore = get_datastore connection, machine
66
+ location[:datastore] = datastore unless datastore.nil?
67
+ end
68
+
69
+ spec = RbVmomi::VIM.VirtualMachineCloneSpec :location => location, :powerOn => true, :template => false
70
+
71
+ customization = get_customization_spec_info_by_name connection, machine
72
+ spec[:customization] = configure_networks(customization.spec, machine) unless customization.nil?
73
+
74
+ env[:ui].info I18n.t('vsphere.creating_cloned_vm')
75
+ env[:ui].info " -- #{config.clone_from_vm ? "Source" : "Template"} VM: #{config.template_name}"
76
+ env[:ui].info " -- Name: #{name}"
77
+
78
+ new_vm = template.CloneVM_Task(:folder => template.parent, :name => name, :spec => spec).wait_for_completion
79
+ rescue Exception => e
80
+ puts e.message
81
+ raise Errors::VSphereError, :message => e.message
82
+ end
83
+
84
+ #TODO: handle interrupted status in the environment, should the vm be destroyed?
85
+
86
+ machine.id = new_vm.config.uuid
87
+
88
+ # wait for SSH to be available
89
+ wait_for_ssh env
90
+
91
+ env[:ui].info I18n.t('vsphere.vm_clone_success')
92
+
93
+ @app.call env
94
+ end
95
+
96
+ private
97
+
98
+ def configure_networks(spec, machine)
99
+ customization_spec = spec.clone
100
+
101
+ # find all the configured private networks
102
+ private_networks = machine.config.vm.networks.find_all { |n| n[0].eql? :private_network }
103
+ return customization_spec if private_networks.nil?
104
+
105
+ # make sure we have enough NIC settings to override with the private network settings
106
+ raise Error::VSphereError, :message => I18n.t('errors.too_many_private_networks') if private_networks.length > customization_spec.nicSettingMap.length
107
+
108
+ # assign the private network IP to the NIC
109
+ private_networks.each_index do |idx|
110
+ customization_spec.nicSettingMap[idx].adapter.ip.ipAddress = private_networks[idx][1][:ip]
111
+ end
112
+
113
+ customization_spec
114
+ end
115
+
116
+ def get_name(machine, config)
117
+ return config.name unless config.name.nil?
118
+
119
+ prefix = "#{machine.name}"
120
+ prefix.gsub!(/[^-a-z0-9_]/i, "")
121
+ # milliseconds + random number suffix to allow for simultaneous `vagrant up` of the same box in different dirs
122
+ prefix + "_#{(Time.now.to_f * 1000.0).to_i}_#{rand(100000)}"
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
@@ -1,24 +1,24 @@
1
- require 'rbvmomi'
2
-
3
- module VagrantPlugins
4
- module VSphere
5
- module Action
6
- class CloseVSphere
7
- def initialize(app, env)
8
- @app = app
9
- end
10
-
11
- def call(env)
12
- begin
13
- env[:vSphere_connection].close
14
- @app.call env
15
- rescue Exception => e
16
- puts e
17
- #raise a properly namespaced error for Vagrant
18
- raise Errors::VSphereError, :message => e.message
19
- end
20
- end
21
- end
22
- end
23
- end
1
+ require 'rbvmomi'
2
+
3
+ module VagrantPlugins
4
+ module VSphere
5
+ module Action
6
+ class CloseVSphere
7
+ def initialize(app, env)
8
+ @app = app
9
+ end
10
+
11
+ def call(env)
12
+ begin
13
+ env[:vSphere_connection].close
14
+ @app.call env
15
+ rescue Exception => e
16
+ puts e
17
+ #raise a properly namespaced error for Vagrant
18
+ raise Errors::VSphereError, :message => e.message
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
24
  end
@@ -1,25 +1,25 @@
1
- require 'rbvmomi'
2
-
3
- module VagrantPlugins
4
- module VSphere
5
- module Action
6
- class ConnectVSphere
7
- def initialize(app, env)
8
- @app = app
9
- end
10
-
11
- def call(env)
12
- config = env[:machine].provider_config
13
-
14
- begin
15
- env[:vSphere_connection] = RbVmomi::VIM.connect host: config.host, user: config.user, password: config.password, insecure: config.insecure
16
- @app.call env
17
- rescue Exception => e
18
- puts e.backtrace
19
- raise VagrantPlugins::VSphere::Errors::VSphereError, :message => e.message
20
- end
21
- end
22
- end
23
- end
24
- end
25
- end
1
+ require 'rbvmomi'
2
+
3
+ module VagrantPlugins
4
+ module VSphere
5
+ module Action
6
+ class ConnectVSphere
7
+ def initialize(app, env)
8
+ @app = app
9
+ end
10
+
11
+ def call(env)
12
+ config = env[:machine].provider_config
13
+
14
+ begin
15
+ env[:vSphere_connection] = RbVmomi::VIM.connect host: config.host, user: config.user, password: config.password, insecure: config.insecure
16
+ @app.call env
17
+ rescue Exception => e
18
+ puts e.backtrace
19
+ raise VagrantPlugins::VSphere::Errors::VSphereError, :message => e.message
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,38 +1,38 @@
1
- require 'rbvmomi'
2
- require 'i18n'
3
- require 'vSphere/util/vim_helpers'
4
-
5
- module VagrantPlugins
6
- module VSphere
7
- module Action
8
- class Destroy
9
- include Util::VimHelpers
10
-
11
- def initialize(app, env)
12
- @app = app
13
- end
14
-
15
- def call(env)
16
- destroy_vm env
17
- env[:machine].id = nil
18
-
19
- @app.call env
20
- end
21
-
22
- private
23
-
24
- def destroy_vm(env)
25
- vm = get_vm_by_uuid env[:vSphere_connection], env[:machine]
26
- return if vm.nil?
27
-
28
- begin
29
- env[:ui].info I18n.t('vsphere.destroy_vm')
30
- vm.Destroy_Task.wait_for_completion
31
- rescue Exception => e
32
- raise Errors::VSphereError, :message => e.message
33
- end
34
- end
35
- end
36
- end
37
- end
1
+ require 'rbvmomi'
2
+ require 'i18n'
3
+ require 'vSphere/util/vim_helpers'
4
+
5
+ module VagrantPlugins
6
+ module VSphere
7
+ module Action
8
+ class Destroy
9
+ include Util::VimHelpers
10
+
11
+ def initialize(app, env)
12
+ @app = app
13
+ end
14
+
15
+ def call(env)
16
+ destroy_vm env
17
+ env[:machine].id = nil
18
+
19
+ @app.call env
20
+ end
21
+
22
+ private
23
+
24
+ def destroy_vm(env)
25
+ vm = get_vm_by_uuid env[:vSphere_connection], env[:machine]
26
+ return if vm.nil?
27
+
28
+ begin
29
+ env[:ui].info I18n.t('vsphere.destroy_vm')
30
+ vm.Destroy_Task.wait_for_completion
31
+ rescue Exception => e
32
+ raise Errors::VSphereError, :message => e.message
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
38
  end
@@ -1,38 +1,38 @@
1
- require 'rbvmomi'
2
- require 'vSphere/util/vim_helpers'
3
-
4
- module VagrantPlugins
5
- module VSphere
6
- module Action
7
- class GetSshInfo
8
- include Util::VimHelpers
9
-
10
-
11
- def initialize(app, env)
12
- @app = app
13
- end
14
-
15
- def call(env)
16
- env[:machine_ssh_info] = get_ssh_info(env[:vSphere_connection], env[:machine])
17
-
18
- @app.call env
19
- end
20
-
21
- private
22
-
23
- def get_ssh_info(connection, machine)
24
- return nil if machine.id.nil?
25
-
26
- vm = get_vm_by_uuid connection, machine
27
-
28
- return nil if vm.nil?
29
-
30
- return {
31
- :host => vm.guest.ipAddress,
32
- :port => 22
33
- }
34
- end
35
- end
36
- end
37
- end
1
+ require 'rbvmomi'
2
+ require 'vSphere/util/vim_helpers'
3
+
4
+ module VagrantPlugins
5
+ module VSphere
6
+ module Action
7
+ class GetSshInfo
8
+ include Util::VimHelpers
9
+
10
+
11
+ def initialize(app, env)
12
+ @app = app
13
+ end
14
+
15
+ def call(env)
16
+ env[:machine_ssh_info] = get_ssh_info(env[:vSphere_connection], env[:machine])
17
+
18
+ @app.call env
19
+ end
20
+
21
+ private
22
+
23
+ def get_ssh_info(connection, machine)
24
+ return nil if machine.id.nil?
25
+
26
+ vm = get_vm_by_uuid connection, machine
27
+
28
+ return nil if vm.nil?
29
+
30
+ return {
31
+ :host => vm.guest.ipAddress,
32
+ :port => 22
33
+ }
34
+ end
35
+ end
36
+ end
37
+ end
38
38
  end
@@ -1,46 +1,46 @@
1
- require 'rbvmomi'
2
- require 'vSphere/util/vim_helpers'
3
-
4
- module VagrantPlugins
5
- module VSphere
6
- module Action
7
- class GetState
8
- include Util::VimHelpers
9
-
10
- # the three possible values of a vSphere VM's power state
11
- POWERED_ON = 'poweredOn'
12
- POWERED_OFF = 'poweredOff'
13
- SUSPENDED = 'suspended'
14
-
15
- def initialize(app, env)
16
- @app = app
17
- end
18
-
19
- def call(env)
20
- env[:machine_state_id] = get_state(env[:vSphere_connection], env[:machine])
21
-
22
- @app.call env
23
- end
24
-
25
- private
26
-
27
- def get_state(connection, machine)
28
- return :not_created if machine.id.nil?
29
-
30
- vm = get_vm_by_uuid connection, machine
31
-
32
- if vm.nil?
33
- return :not_created
34
- end
35
-
36
- if vm.runtime.powerState.eql?(POWERED_ON)
37
- :running
38
- else
39
- # If the VM is powered off or suspended, we consider it to be powered off. A power on command will either turn on or resume the VM
40
- :poweroff
41
- end
42
- end
43
- end
44
- end
45
- end
1
+ require 'rbvmomi'
2
+ require 'vSphere/util/vim_helpers'
3
+
4
+ module VagrantPlugins
5
+ module VSphere
6
+ module Action
7
+ class GetState
8
+ include Util::VimHelpers
9
+
10
+ # the three possible values of a vSphere VM's power state
11
+ POWERED_ON = 'poweredOn'
12
+ POWERED_OFF = 'poweredOff'
13
+ SUSPENDED = 'suspended'
14
+
15
+ def initialize(app, env)
16
+ @app = app
17
+ end
18
+
19
+ def call(env)
20
+ env[:machine_state_id] = get_state(env[:vSphere_connection], env[:machine])
21
+
22
+ @app.call env
23
+ end
24
+
25
+ private
26
+
27
+ def get_state(connection, machine)
28
+ return :not_created if machine.id.nil?
29
+
30
+ vm = get_vm_by_uuid connection, machine
31
+
32
+ if vm.nil?
33
+ return :not_created
34
+ end
35
+
36
+ if vm.runtime.powerState.eql?(POWERED_ON)
37
+ :running
38
+ else
39
+ # If the VM is powered off or suspended, we consider it to be powered off. A power on command will either turn on or resume the VM
40
+ :poweroff
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
46
  end
@@ -1,16 +1,16 @@
1
- module VagrantPlugins
2
- module VSphere
3
- module Action
4
- class IsCreated
5
- def initialize(app, env)
6
- @app = app
7
- end
8
-
9
- def call(env)
10
- env[:result] = env[:machine].state.id != :not_created
11
- @app.call env
12
- end
13
- end
14
- end
15
- end
1
+ module VagrantPlugins
2
+ module VSphere
3
+ module Action
4
+ class IsCreated
5
+ def initialize(app, env)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ env[:result] = env[:machine].state.id != :not_created
11
+ @app.call env
12
+ end
13
+ end
14
+ end
15
+ end
16
16
  end
@@ -1,20 +1,20 @@
1
- require 'vSphere/util/machine_helpers'
2
-
3
- module VagrantPlugins
4
- module VSphere
5
- module Action
6
- class IsRunning
7
- include Util::MachineHelpers
8
-
9
- def initialize(app, env)
10
- @app = app
11
- end
12
-
13
- def call(env)
14
- env[:result] = env[:machine].state.id == :running
15
- @app.call env
16
- end
17
- end
18
- end
19
- end
20
- end
1
+ require 'vSphere/util/machine_helpers'
2
+
3
+ module VagrantPlugins
4
+ module VSphere
5
+ module Action
6
+ class IsRunning
7
+ include Util::MachineHelpers
8
+
9
+ def initialize(app, env)
10
+ @app = app
11
+ end
12
+
13
+ def call(env)
14
+ env[:result] = env[:machine].state.id == :running
15
+ @app.call env
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,18 +1,18 @@
1
- require 'i18n'
2
-
3
- module VagrantPlugins
4
- module VSphere
5
- module Action
6
- class MessageAlreadyCreated
7
- def initialize(app, env)
8
- @app = app
9
- end
10
-
11
- def call(env)
12
- env[:ui].info I18n.t('vsphere.vm_already_created')
13
- @app.call(env)
14
- end
15
- end
16
- end
17
- end
1
+ require 'i18n'
2
+
3
+ module VagrantPlugins
4
+ module VSphere
5
+ module Action
6
+ class MessageAlreadyCreated
7
+ def initialize(app, env)
8
+ @app = app
9
+ end
10
+
11
+ def call(env)
12
+ env[:ui].info I18n.t('vsphere.vm_already_created')
13
+ @app.call(env)
14
+ end
15
+ end
16
+ end
17
+ end
18
18
  end