vagrant-vcenter 0.3.2 → 0.3.3

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.
@@ -1,6 +1,3 @@
1
- require 'rbvmomi'
2
- require 'log4r'
3
-
4
1
  module VagrantPlugins
5
2
  module VCenter
6
3
  module Action
@@ -9,25 +6,29 @@ module VagrantPlugins
9
6
  def initialize(app, env)
10
7
  @app = app
11
8
  @logger = Log4r::Logger.new(
12
- 'vagrant_vcenter::action::connect_vCenter')
9
+ 'vagrant_vcenter::action::connect_vcenter'
10
+ )
13
11
  end
14
12
 
15
13
  def call(env)
16
- config = env[:machine].provider_config
14
+ cfg = env[:machine].provider_config
15
+
17
16
  # Avoid recreating a new session each time.
18
- unless config.vcenter_cnx
17
+ unless cfg.vcenter_cnx
19
18
  @logger.info('Connecting to vCenter...')
20
19
 
21
- @logger.debug("config.hostname: #{config.hostname}")
22
- @logger.debug("config.username: #{config.username}")
23
- @logger.debug('config.password: <hidden>')
20
+ @logger.debug("hostname: #{cfg.hostname}")
21
+ @logger.debug("username: #{cfg.username}")
22
+ @logger.debug('password: <hidden>')
24
23
 
25
24
  # 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)
25
+ cfg.vcenter_cnx = RbVmomi::VIM.connect(
26
+ host: cfg.hostname,
27
+ user: cfg.username,
28
+ password: cfg.password,
29
+ insecure: true
30
+ )
31
+
31
32
  end
32
33
  @app.call env
33
34
  end
@@ -1,5 +1,3 @@
1
- require 'i18n'
2
-
3
1
  module VagrantPlugins
4
2
  module VCenter
5
3
  module Action
@@ -11,12 +9,11 @@ module VagrantPlugins
11
9
  end
12
10
 
13
11
  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)
12
+ cfg = env[:machine].provider_config
13
+
14
+ vm = cfg.vmfolder.findByUuid(env[:machine].id) or
15
+ fail Errors::VMNotFound,
16
+ :vm_name => env[:machine].name
20
17
 
21
18
  # Poweron VM
22
19
  env[:ui].info('Destroying VM...')
@@ -6,18 +6,19 @@ module VagrantPlugins
6
6
  def initialize(app, env)
7
7
  @app = app
8
8
  @logger = Log4r::Logger.new(
9
- 'vagrant_vcenter::action::disconnect_vcenter')
9
+ 'vagrant_vcenter::action::disconnect_vcenter'
10
+ )
10
11
  end
11
12
 
12
13
  def call(env)
13
14
  @logger.info('Disconnecting from vCenter ...')
14
15
 
15
- config = env[:machine].provider_config
16
+ cfg = env[:machine].provider_config
16
17
 
17
- if !config.vcenter_cnx
18
- @logger.info('Session not open, impossible to disconnect')
18
+ if !cfg.vcenter_cnx
19
+ @logger.info('No session active')
19
20
  else
20
- config.vcenter_cnx.close
21
+ cfg.vcenter_cnx.close
21
22
  @logger.info('Succesfully disconnected from vCenter...')
22
23
  end
23
24
 
@@ -14,7 +14,8 @@ module VagrantPlugins
14
14
  def initialize(app, env)
15
15
  @app = app
16
16
  @logger = Log4r::Logger.new(
17
- 'vagrant_vcenter::action::inventory_check')
17
+ 'vagrant_vcenter::action::inventory_check'
18
+ )
18
19
  end
19
20
 
20
21
  def call(env)
@@ -23,106 +24,110 @@ module VagrantPlugins
23
24
  end
24
25
 
25
26
  def vcenter_upload_box(env)
26
- config = env[:machine].provider_config
27
-
28
- box_dir = env[:machine].box.directory.to_s
27
+ cfg = env[:machine].provider_config
29
28
 
30
- if env[:machine].box.name.to_s.include? '/'
31
- box_file = env[:machine].box.name.rpartition('/').last.to_s
32
- box_name = env[:machine].box.name.to_s.gsub(/\//, '-')
29
+ if env[:machine].box.name.include? '/'
30
+ box_file = env[:machine].box.name.rpartition('/').last
31
+ box_name = env[:machine].box.name.gsub(/\//, '-')
33
32
  else
34
- box_file = env[:machine].box.name.to_s
33
+ box_file = env[:machine].box.name
35
34
  box_name = box_file
36
35
  end
37
36
 
38
- box_ovf = "file://#{box_dir}/#{box_file}.ovf"
39
-
40
- @logger.debug("OVF File: #{box_ovf}")
41
-
42
- env[:ui].info("Adding [#{box_name}]")
43
-
44
- # FIXME: Raise a correct exception
45
- dc = config.vcenter_cnx.serviceInstance.find_datacenter(
46
- config.datacenter_name) or fail 'datacenter not found'
47
-
48
- root_vm_folder = dc.vmFolder
49
- vm_folder = root_vm_folder
50
- if config.template_folder_name.nil?
51
- template_folder = root_vm_folder
37
+ if cfg.template_folder_name.nil?
38
+ box_to_search = box_name
39
+ cfg.template_folder = cfg.vmfolder
52
40
  else
53
- template_folder = root_vm_folder.traverse!(
54
- config.template_folder_name, RbVmomi::VIM::Folder)
41
+ box_to_search = cfg.template_folder_name + '/' + box_name
42
+ cfg.template_folder = cfg.vmfolder.traverse!(
43
+ cfg.template_folder_name,
44
+ RbVmomi::VIM::Folder
45
+ )
55
46
  end
56
47
 
57
- template_name = box_name
48
+ @logger.debug("Checking for box: #{box_to_search}...")
58
49
 
59
- # FIXME: Raise a correct exception
60
- datastore = dc.find_datastore(
61
- config.datastore_name) or fail 'datastore not found'
62
- # FIXME: Raise a correct exception
63
- computer = dc.find_compute_resource(
64
- config.computer_name) or fail 'Host not found'
50
+ # Check for the template object and add it the provider config
51
+ cfg.template = cfg.datacenter.find_vm(box_to_search)
65
52
 
66
- network = computer.network.find { |x| x.name == config.network_name }
53
+ if cfg.template.nil?
54
+ # Roll a dice to get a winner in the race.
55
+ sleep_time = rand * (3 - 1) + 1
56
+ @logger.debug("Sleeping #{sleep_time} to avoid race conditions.")
57
+ sleep(sleep_time)
67
58
 
68
- deployer = CachedOvfDeployer.new(
69
- config.vcenter_cnx,
70
- network,
71
- computer,
72
- template_folder,
73
- vm_folder,
74
- datastore
75
- )
59
+ box_dir = env[:machine].box.directory
60
+ box_ovf = "file://#{box_dir}/#{box_file}.ovf"
76
61
 
77
- deployer_opts = {
78
- :run_without_interruptions => true,
79
- :simple_vm_name => true
80
- }
62
+ env[:ui].info("Uploading [#{box_name}]...")
63
+ @logger.debug("OVF File: #{box_ovf}")
64
+
65
+ deployer = CachedOvfDeployer.new(
66
+ cfg.vcenter_cnx,
67
+ cfg.network,
68
+ cfg.compute,
69
+ cfg.template_folder,
70
+ cfg.vmfolder,
71
+ cfg.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
+ box_name,
82
+ deployer_opts
83
+ )
84
+
85
+ # Re Fetch the template object and add it the provider config
86
+ cfg.template = cfg.datacenter.find_vm(box_to_search)
87
+ else
88
+ @logger.debug('Box already exists at target endpoint')
89
+ end
81
90
 
82
- deployer.upload_ovf_as_template(
83
- box_ovf,
84
- template_name,
85
- deployer_opts)
86
91
  # FIXME: Progressbar??
87
92
  end
88
93
 
89
94
  def vcenter_check_inventory(env)
90
95
  # Will check each mandatory config value against the vcenter
91
96
  # Instance and will setup the global environment config values
92
- config = env[:machine].provider_config
93
- # FIXME: Raise a correct exception
94
- dc = config.vcenter_cnx.serviceInstance.find_datacenter(
95
- config.datacenter_name) or fail 'datacenter not found'
96
-
97
- if env[:machine].box.name.to_s.include? '/'
98
- box_file = env[:machine].box.name.rpartition('/').last.to_s
99
- box_name = env[:machine].box.name.to_s.gsub(/\//, '-')
100
- else
101
- box_file = env[:machine].box.name.to_s
102
- box_name = box_file
103
- end
104
-
105
- if config.template_folder_name.nil?
106
- box_to_search = box_name
107
- else
108
- box_to_search = config.template_folder_name + '/' + box_name
109
- end
110
-
111
- @logger.debug("This is the box we're looking for: #{box_to_search}")
112
-
113
- config.template_id = dc.find_vm(box_to_search)
114
97
 
115
- if config.template_id.nil?
116
- # Roll a dice to get a winner in the race.
117
- sleep_time = rand * (3 - 1) + 1
118
- @logger.debug("Sleeping #{sleep_time} to avoid race conditions.")
119
- sleep(sleep_time)
120
-
121
- env[:ui].info("Uploading [#{box_name}]...")
122
- vcenter_upload_box(env)
123
- else
124
- @logger.debug("Template found at #{box_to_search}")
125
- end
98
+ cfg = env[:machine].provider_config
99
+ cnx = cfg.vcenter_cnx
100
+
101
+ # Fetch Datacenter handle and add it to provider config
102
+ cfg.datacenter = cnx.serviceInstance.find_datacenter(
103
+ cfg.datacenter_name
104
+ ) or fail Errors::DatacenterNotFound,
105
+ :datacenter_name => cfg.datacenter_name
106
+
107
+ # Fetch vmFolder handle for the specific Datacenter and add it to
108
+ # provider config
109
+ cfg.vmfolder = cfg.datacenter.vmFolder
110
+
111
+ # Fetch compute resource handle and add it to the provider config
112
+ cfg.compute = cfg.datacenter.find_compute_resource(
113
+ cfg.compute_name
114
+ ) or fail Errors::ComputeNotFound,
115
+ :compute_name => cfg.compute_name
116
+
117
+ # Fetch datastore handle and add it to the provider config
118
+ cfg.datastore = cfg.datacenter.find_datastore(
119
+ cfg.datastore_name
120
+ ) or fail Errors::DatastoreNotFound,
121
+ :datastore_name => cfg.datastore_name
122
+
123
+ # Fetch network portgroup handle and add it to the provider config
124
+ cfg.network = cfg.compute.network.find {
125
+ |x| x.name == cfg.network_name
126
+ } or fail Errors::NetworkNotFound,
127
+ :network_name => cfg.network_name
128
+
129
+ # Use this method to take care of the template/boxes
130
+ vcenter_upload_box(env)
126
131
  end
127
132
  end
128
133
  end
@@ -1,3 +1,4 @@
1
+
1
2
  module VagrantPlugins
2
3
  module VCenter
3
4
  module Action
@@ -10,33 +11,10 @@ module VagrantPlugins
10
11
 
11
12
  def call(env)
12
13
  vm_id = env[:machine].id
13
- if vm_id
14
-
15
- # VM is in the vagrant registry, now we need to check if it's
16
- # actually in vcenter
17
-
18
- # FIXME: this part needs some cleanup
19
- config = env[:machine].provider_config
20
-
21
- # FIXME: Raise a correct exception
22
- dc = config.vcenter_cnx.serviceInstance.find_datacenter(
23
- config.datacenter_name) or abort 'datacenter not found'
24
14
 
25
- root_vm_folder = dc.vmFolder
26
-
27
- vm = root_vm_folder.findByUuid(env[:machine].id)
28
-
29
- unless vm
30
- @logger.info('VM is in the vagrant registry but not in vcenter')
31
- # Clear the ID
32
- env[:machine].id = nil
33
- env[:result] = false
34
- end
35
-
36
- # VM is in the registry AND in vcenter
15
+ if vm_id
37
16
  @logger.info("VM has been created and ID is: [#{vm_id}]")
38
17
  env[:result] = true
39
-
40
18
  else
41
19
  # VM is not in the registry
42
20
  @logger.warn('VM has not been created')
@@ -1,5 +1,3 @@
1
- require 'i18n'
2
-
3
1
  module VagrantPlugins
4
2
  module VCenter
5
3
  module Action
@@ -9,8 +9,9 @@ module VagrantPlugins
9
9
  end
10
10
 
11
11
  def call(env)
12
- env[:ui].info(I18n.t(
13
- 'vagrant_vcenter.power.vm_halted_cannot_suspend'))
12
+ env[:ui].info(
13
+ I18n.t('vagrant_vcenter.power.vm_halted_cannot_suspend')
14
+ )
14
15
  @app.call(env)
15
16
  end
16
17
  end
@@ -8,8 +8,12 @@ module VagrantPlugins
8
8
  end
9
9
 
10
10
  def call(env)
11
- env[:ui].info(I18n.t('vagrant_vcenter.power.will_not_destroy',
12
- name: env[:machine].name))
11
+ env[:ui].info(
12
+ I18n.t(
13
+ 'vagrant_vcenter.power.will_not_destroy',
14
+ name: env[:machine].name
15
+ )
16
+ )
13
17
  @app.call(env)
14
18
  end
15
19
  end
@@ -1,5 +1,3 @@
1
- require 'i18n'
2
-
3
1
  module VagrantPlugins
4
2
  module VCenter
5
3
  module Action
@@ -11,12 +9,11 @@ module VagrantPlugins
11
9
  end
12
10
 
13
11
  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)
12
+ cfg = env[:machine].provider_config
13
+
14
+ vm = cfg.vmfolder.findByUuid(env[:machine].id) or
15
+ fail Errors::VMNotFound,
16
+ :vm_name => env[:machine].name
20
17
 
21
18
  # Poweroff VM
22
19
  env[:ui].info('Powering off VM...')
@@ -1,5 +1,3 @@
1
- require 'i18n'
2
-
3
1
  module VagrantPlugins
4
2
  module VCenter
5
3
  module Action
@@ -11,17 +9,17 @@ module VagrantPlugins
11
9
  end
12
10
 
13
11
  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)
12
+ cfg = env[:machine].provider_config
13
+
14
+ vm = cfg.vmfolder.findByUuid(env[:machine].id) or
15
+ fail Errors::VMNotFound,
16
+ :vm_name => env[:machine].name
20
17
 
21
18
  # Poweron VM
22
19
  env[:ui].info('Powering on VM...')
23
20
  vm.PowerOnVM_Task.wait_for_completion
24
21
  sleep(20) until env[:machine].communicate.ready?
22
+
25
23
  @app.call env
26
24
  end
27
25
  end
@@ -1,5 +1,3 @@
1
- require 'socket'
2
-
3
1
  module VagrantPlugins
4
2
  module VCenter
5
3
  module Action
@@ -12,11 +10,19 @@ module VagrantPlugins
12
10
  end
13
11
 
14
12
  def call(env)
15
- host_ip = Socket.ip_address_list.find { |ai| ai.ipv4? && !ai.ipv4_loopback? }.ip_address
13
+ if env[:machine].state.id != :running
14
+ fail Errors::MachineNotRunning,
15
+ :machine_name => env[:machine].name
16
+ end
17
+
18
+ host_ip = Socket.ip_address_list.find {
19
+ |ai| ai.ipv4? && !ai.ipv4_loopback?
20
+ }.ip_address
16
21
 
17
22
  @logger.debug("Setting host_ip to #{host_ip}")
18
23
 
19
- env[:nfs_host_ip] = host_ip
24
+ env[:nfs_host_ip] = host_ip
25
+
20
26
  @app.call env
21
27
  end
22
28
  end