vagrant-vcenter 0.0.2.pre.dev

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 (40) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +4 -0
  3. data/.rubocop.yml +34 -0
  4. data/Gemfile +7 -0
  5. data/LICENSE +20 -0
  6. data/README.md +5 -0
  7. data/Rakefile +17 -0
  8. data/example_box/README.md +13 -0
  9. data/example_box/Vagrantfile +6 -0
  10. data/example_box/metadata.json +1 -0
  11. data/lib/vagrant-vcenter.rb +18 -0
  12. data/lib/vagrant-vcenter/action.rb +232 -0
  13. data/lib/vagrant-vcenter/action/announce_ssh_exec.rb +19 -0
  14. data/lib/vagrant-vcenter/action/build_vm.rb +130 -0
  15. data/lib/vagrant-vcenter/action/connect_vcenter.rb +37 -0
  16. data/lib/vagrant-vcenter/action/destroy.rb +29 -0
  17. data/lib/vagrant-vcenter/action/disconnect_vcenter.rb +29 -0
  18. data/lib/vagrant-vcenter/action/inventory_check.rb +132 -0
  19. data/lib/vagrant-vcenter/action/is_created.rb +26 -0
  20. data/lib/vagrant-vcenter/action/is_paused.rb +23 -0
  21. data/lib/vagrant-vcenter/action/is_running.rb +23 -0
  22. data/lib/vagrant-vcenter/action/message_already_running.rb +17 -0
  23. data/lib/vagrant-vcenter/action/message_cannot_suspend.rb +19 -0
  24. data/lib/vagrant-vcenter/action/message_not_created.rb +17 -0
  25. data/lib/vagrant-vcenter/action/message_will_not_destroy.rb +18 -0
  26. data/lib/vagrant-vcenter/action/power_off.rb +30 -0
  27. data/lib/vagrant-vcenter/action/power_on.rb +30 -0
  28. data/lib/vagrant-vcenter/action/read_ssh_info.rb +37 -0
  29. data/lib/vagrant-vcenter/action/read_state.rb +52 -0
  30. data/lib/vagrant-vcenter/action/resume.rb +30 -0
  31. data/lib/vagrant-vcenter/action/suspend.rb +30 -0
  32. data/lib/vagrant-vcenter/action/sync_folders.rb +135 -0
  33. data/lib/vagrant-vcenter/config.rb +92 -0
  34. data/lib/vagrant-vcenter/errors.rb +88 -0
  35. data/lib/vagrant-vcenter/plugin.rb +67 -0
  36. data/lib/vagrant-vcenter/provider.rb +46 -0
  37. data/lib/vagrant-vcenter/version.rb +6 -0
  38. data/locales/en.yml +50 -0
  39. data/vagrant-vcenter.gemspec +29 -0
  40. metadata +194 -0
@@ -0,0 +1,37 @@
1
+ require 'rbvmomi'
2
+ require 'log4r'
3
+
4
+ module VagrantPlugins
5
+ module VCenter
6
+ module Action
7
+ # This class connects the vagrant-vcenter provider to vCenter.
8
+ class ConnectvCenter
9
+ def initialize(app, env)
10
+ @app = app
11
+ @logger = Log4r::Logger.new(
12
+ 'vagrant_vcenter::action::connect_vCenter')
13
+ end
14
+
15
+ def call(env)
16
+ config = env[:machine].provider_config
17
+ # Avoid recreating a new session each time.
18
+ unless config.vcenter_cnx
19
+ @logger.info('Connecting to vCenter...')
20
+
21
+ @logger.debug("config.hostname: #{config.hostname}")
22
+ @logger.debug("config.username: #{config.username}")
23
+ @logger.debug("config.password: #{config.password}")
24
+
25
+ # FIXME: fix the insecure flag, catch the exception
26
+ config.vcenter_cnx = RbVmomi::VIM.connect(
27
+ host: config.hostname,
28
+ user: config.username,
29
+ password: config.password,
30
+ insecure: true)
31
+ end
32
+ @app.call env
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,29 @@
1
+ require 'i18n'
2
+
3
+ module VagrantPlugins
4
+ module VCenter
5
+ module Action
6
+ # This class destroy the VM created by the Vagrant provider.
7
+ class Destroy
8
+ def initialize(app, env)
9
+ @app = app
10
+ @logger = Log4r::Logger.new('vagrant_vcenter::action::destroy')
11
+ end
12
+
13
+ def call(env)
14
+ config = env[:machine].provider_config
15
+ # FIXME: Raise a correct exception
16
+ dc = config.vcenter_cnx.serviceInstance.find_datacenter(
17
+ config.datacenter_name) or abort 'datacenter not found'
18
+ root_vm_folder = dc.vmFolder
19
+ vm = root_vm_folder.findByUuid(env[:machine].id)
20
+
21
+ # Poweron VM
22
+ env[:ui].info('Destroying VM...')
23
+ vm.Destroy_Task.wait_for_completion
24
+ env[:machine].id = nil
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ module VagrantPlugins
2
+ module VCenter
3
+ module Action
4
+ # This class disconnects the vagrant-vcenter provider from vCenter.
5
+ class DisconnectvCenter
6
+ def initialize(app, env)
7
+ @app = app
8
+ @logger = Log4r::Logger.new(
9
+ 'vagrant_vcenter::action::disconnect_vcenter')
10
+ end
11
+
12
+ def call(env)
13
+ @logger.info('Disconnecting from vCenter ...')
14
+
15
+ config = env[:machine].provider_config
16
+
17
+ if !config.vcenter_cnx
18
+ @logger.info('Session not open, impossible to disconnect')
19
+ else
20
+ config.vcenter_cnx.close
21
+ @logger.info('Succesfully disconnected from vCenter...')
22
+ end
23
+
24
+ @app.call env
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,132 @@
1
+ require 'etc'
2
+ require 'log4r'
3
+ require 'rbvmomi'
4
+ require 'rbvmomi/utils/deploy'
5
+ require 'rbvmomi/utils/admission_control'
6
+ require 'yaml'
7
+
8
+ module VagrantPlugins
9
+ module VCenter
10
+ module Action
11
+ # This Class inspect the vCenter inventory for templates and uploads it
12
+ # if needed.
13
+ class InventoryCheck
14
+ def initialize(app, env)
15
+ @app = app
16
+ @logger = Log4r::Logger.new(
17
+ 'vagrant_vcenter::action::inventory_check')
18
+ end
19
+
20
+ def call(env)
21
+ vcenter_check_inventory(env)
22
+ @app.call env
23
+ end
24
+
25
+ def vcenter_upload_box(env)
26
+ config = env[:machine].provider_config
27
+
28
+ box_dir = env[:machine].box.directory.to_s
29
+ box_file = env[:machine].box.name.to_s
30
+
31
+ box_ovf = "file://#{box_dir}/#{box_file}.ovf"
32
+
33
+ # Still relying on ruby-progressbar because report_progress basically
34
+ # sucks.
35
+
36
+ @logger.debug("OVF File: #{box_ovf}")
37
+
38
+ env[:ui].info("Adding [#{env[:machine].box.name.to_s}]")
39
+
40
+ # FIXME: Raise a correct exception
41
+ dc = config.vcenter_cnx.serviceInstance.find_datacenter(
42
+ config.datacenter_name) or fail 'datacenter not found'
43
+
44
+ root_vm_folder = dc.vmFolder
45
+ vm_folder = root_vm_folder
46
+ if config.template_folder_name.nil?
47
+ vm_folder = root_vm_folder.traverse(config.template_folder_name,
48
+ RbVmomi::VIM::Folder)
49
+ end
50
+ template_folder = root_vm_folder.traverse!(
51
+ config.template_folder_name,
52
+ RbVmomi::VIM::Folder)
53
+
54
+ template_name = box_file
55
+
56
+ # FIXME: Raise a correct exception
57
+ datastore = dc.find_datastore(
58
+ config.datastore_name) or fail 'datastore not found'
59
+ # FIXME: Raise a correct exception
60
+ computer = dc.find_compute_resource(
61
+ config.computer_name) or fail 'Host not found'
62
+
63
+ network = computer.network.find { |x| x.name == config.network_name }
64
+
65
+ deployer = CachedOvfDeployer.new(
66
+ config.vcenter_cnx,
67
+ network,
68
+ computer,
69
+ template_folder,
70
+ vm_folder,
71
+ datastore
72
+ )
73
+
74
+ deployer_opts = {
75
+ :run_without_interruptions => true,
76
+ :simple_vm_name => true
77
+ }
78
+
79
+ deployer.upload_ovf_as_template(
80
+ box_ovf,
81
+ template_name,
82
+ deployer_opts)
83
+ # FIXME: Progressbar??
84
+ end
85
+
86
+ def vcenter_check_inventory(env)
87
+ # Will check each mandatory config value against the vcenter
88
+ # Instance and will setup the global environment config values
89
+ config = env[:machine].provider_config
90
+ # FIXME: Raise a correct exception
91
+ dc = config.vcenter_cnx.serviceInstance.find_datacenter(
92
+ config.datacenter_name) or fail 'datacenter not found'
93
+
94
+ if config.template_folder_name.nil?
95
+ box_to_search = env[:machine].box.name.to_s
96
+ else
97
+ box_to_search = config.template_folder_name +
98
+ '/' + env[:machine].box.name.to_s
99
+ end
100
+
101
+ @logger.debug("This is the box we're looking for: #{box_to_search}")
102
+
103
+ config.template_id = dc.find_vm(box_to_search)
104
+
105
+ if config.template_id.nil?
106
+ env[:ui].warn("Template [#{env[:machine].box.name.to_s}] " +
107
+ 'does not exist!')
108
+
109
+ user_input = env[:ui].ask(
110
+ "Would you like to upload the [#{env[:machine].box.name.to_s}]" +
111
+ " box?\nChoice (yes/no): "
112
+ )
113
+
114
+ if user_input.downcase == 'yes' || user_input.downcase == 'y'
115
+ env[:ui].info("Uploading [#{env[:machine].box.name.to_s}]...")
116
+ vcenter_upload_box(env)
117
+ else
118
+ env[:ui].error('Template not uploaded, exiting...')
119
+
120
+ # FIXME: wrong error message
121
+ fail VagrantPlugins::VCenter::Errors::VCenterError,
122
+ :message => 'Catalog not available, exiting...'
123
+
124
+ end
125
+ else
126
+ @logger.debug("Template found at #{box_to_search}")
127
+ end
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,26 @@
1
+ module VagrantPlugins
2
+ module VCenter
3
+ module Action
4
+ # This class verifies if the VM has been created.
5
+ class IsCreated
6
+ def initialize(app, env)
7
+ @app = app
8
+ @logger = Log4r::Logger.new('vagrant_vcenter::action::is_created')
9
+ end
10
+
11
+ def call(env)
12
+ vm_id = env[:machine].id
13
+ if vm_id
14
+ @logger.info("VM has been created and ID is: [#{vm_id}]")
15
+ env[:result] = true
16
+ else
17
+ @logger.warn('VM has not been created')
18
+ env[:result] = false
19
+ end
20
+
21
+ @app.call env
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,23 @@
1
+ require 'i18n'
2
+
3
+ module VagrantPlugins
4
+ module VCenter
5
+ module Action
6
+ # This class sets the state to suspended if the VM is suspended.
7
+ class IsPaused
8
+ def initialize(app, env)
9
+ @app = app
10
+ end
11
+
12
+ def call(env)
13
+ # Set the result to be true if the machine is suspended.
14
+ env[:result] = env[:machine].state.id == :suspended
15
+
16
+ # Call the next if we have one (but we shouldn't, since this
17
+ # middleware is built to run with the Call-type middlewares)
18
+ @app.call(env)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ require 'i18n'
2
+
3
+ module VagrantPlugins
4
+ module VCenter
5
+ module Action
6
+ # This class sets the state to running if the VM is running.
7
+ class IsRunning
8
+ def initialize(app, env)
9
+ @app = app
10
+ end
11
+
12
+ def call(env)
13
+ # Set the result to be true if the machine is running.
14
+ env[:result] = env[:machine].state.id == :running
15
+
16
+ # Call the next if we have one (but we shouldn't, since this
17
+ # middleware is built to run with the Call-type middlewares)
18
+ @app.call(env)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ module VagrantPlugins
2
+ module VCenter
3
+ module Action
4
+ # Prints out a message that the VM is already running.
5
+ class MessageAlreadyRunning
6
+ def initialize(app, env)
7
+ @app = app
8
+ end
9
+
10
+ def call(env)
11
+ env[:ui].info(I18n.t('vagrant_vcenter.power.vm_already_running'))
12
+ @app.call(env)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ module VagrantPlugins
2
+ module VCenter
3
+ module Action
4
+ # Prints out a message that the VM is already halted and cannot be
5
+ # suspended
6
+ class MessageCannotSuspend
7
+ def initialize(app, env)
8
+ @app = app
9
+ end
10
+
11
+ def call(env)
12
+ env[:ui].info(I18n.t(
13
+ 'vagrant_vcenter.power.vm_halted_cannot_suspend'))
14
+ @app.call(env)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ module VagrantPlugins
2
+ module VCenter
3
+ module Action
4
+ # Prints out a message that the VM has not yet been created.
5
+ class MessageNotCreated
6
+ def initialize(app, env)
7
+ @app = app
8
+ end
9
+
10
+ def call(env)
11
+ env[:ui].info(I18n.t('vagrant_vcenter.power.vm_not_created'))
12
+ @app.call(env)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ module VagrantPlugins
2
+ module VCenter
3
+ module Action
4
+ # Prints out a message that the VM will not be destroyed.
5
+ class MessageWillNotDestroy
6
+ def initialize(app, env)
7
+ @app = app
8
+ end
9
+
10
+ def call(env)
11
+ env[:ui].info(I18n.t('vagrant_vcenter.power.will_not_destroy',
12
+ name: env[:machine].name))
13
+ @app.call(env)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,30 @@
1
+ require 'i18n'
2
+
3
+ module VagrantPlugins
4
+ module VCenter
5
+ module Action
6
+ # This class powers off the VM that the Vagrant provider is managing.
7
+ class PowerOff
8
+ def initialize(app, env)
9
+ @app = app
10
+ @logger = Log4r::Logger.new('vagrant_vcenter::action::poweroff')
11
+ end
12
+
13
+ def call(env)
14
+ config = env[:machine].provider_config
15
+ # FIXME: Raise a correct exception
16
+ dc = config.vcenter_cnx.serviceInstance.find_datacenter(
17
+ config.datacenter_name) or abort 'datacenter not found'
18
+ root_vm_folder = dc.vmFolder
19
+ vm = root_vm_folder.findByUuid(env[:machine].id)
20
+
21
+ # Poweroff VM
22
+ env[:ui].info('Powering off VM...')
23
+ vm.PowerOffVM_Task.wait_for_completion
24
+
25
+ @app.call env
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ require 'i18n'
2
+
3
+ module VagrantPlugins
4
+ module VCenter
5
+ module Action
6
+ # This class powers on the VM that the Vagrant provider is managing.
7
+ class PowerOn
8
+ def initialize(app, env)
9
+ @app = app
10
+ @logger = Log4r::Logger.new('vagrant_vcenter::action::power_on')
11
+ end
12
+
13
+ def call(env)
14
+ config = env[:machine].provider_config
15
+ # FIXME: Raise a correct exception
16
+ dc = config.vcenter_cnx.serviceInstance.find_datacenter(
17
+ config.datacenter_name) or abort 'datacenter not found'
18
+ root_vm_folder = dc.vmFolder
19
+ vm = root_vm_folder.findByUuid(env[:machine].id)
20
+
21
+ # Poweron VM
22
+ env[:ui].info('Powering on VM...')
23
+ vm.PowerOnVM_Task.wait_for_completion
24
+ sleep(3) until env[:machine].communicate.ready?
25
+ @app.call env
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end