Skip to content

Commit

Permalink
Fixed actions logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tsugliani authored and frapposelli committed Jul 6, 2016
1 parent 7eb4e6d commit 440c6ab
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 107 deletions.
21 changes: 19 additions & 2 deletions lib/vagrant-vcenter/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ def self.action_reload
Vagrant::Action::Builder.new.tap do |b|
b.use ConfigValidate
b.use ConnectvCenter
b.use InventoryCheck
b.use Call, IsCreated do |env, b2|
unless env[:result]
b2.use MessageNotCreated
next
end
b2.use action_halt
b2.use action_start
b2.use DisconnectvCenter
end
end
end
Expand All @@ -41,6 +41,7 @@ def self.action_start
Vagrant::Action::Builder.new.tap do |b|
b.use ConfigValidate
b.use ConnectvCenter
b.use InventoryCheck
b.use Call, IsRunning do |env, b2|
# If the VM is running, then our work here is done, exit
if env[:result]
Expand All @@ -61,16 +62,24 @@ def self.action_start
def self.action_halt
Vagrant::Action::Builder.new.tap do |b|
b.use ConnectvCenter
b.use InventoryCheck

# If the VM suspend, Resume first
b.use Call, IsPaused do |env, b2|
b2.use Resume if env[:result]
b2.use PowerOff

# Only halt when VM is running.
b2.use Call, IsRunning do |env2, b3|
b3.use PowerOff if env2[:result]
end
end
end
end

def self.action_suspend
Vagrant::Action::Builder.new.tap do |b|
b.use ConnectvCenter
b.use InventoryCheck
b.use Call, IsRunning do |env, b2|
# If the VM is stopped, can't suspend
if !env[:result]
Expand All @@ -85,6 +94,7 @@ def self.action_suspend
def self.action_resume
Vagrant::Action::Builder.new.tap do |b|
b.use ConnectvCenter
b.use InventoryCheck
b.use Resume
end
end
Expand All @@ -95,6 +105,7 @@ def self.action_destroy
if env[:result]
b2.use ConfigValidate
b2.use ConnectvCenter
b2.use InventoryCheck
b2.use Call, IsCreated do |env2, b3|
unless env2[:result]
b3.use MessageNotCreated
Expand All @@ -110,13 +121,15 @@ def self.action_destroy
b2.use MessageWillNotDestroy
end
end
# b.use DisconnectvCenter
end
end

def self.action_provision
Vagrant::Action::Builder.new.tap do |b|
b.use ConfigValidate
b.use ConnectvCenter
b.use InventoryCheck
b.use Call, IsCreated do |env, b2|
unless env[:result]
b2.use MessageNotCreated
Expand All @@ -136,6 +149,7 @@ def self.action_read_ssh_info
Vagrant::Action::Builder.new.tap do |b|
b.use ConfigValidate
b.use ConnectvCenter
b.use InventoryCheck
b.use ReadSSHInfo
end
end
Expand All @@ -147,6 +161,7 @@ def self.action_read_state
Vagrant::Action::Builder.new.tap do |b|
b.use ConfigValidate
b.use ConnectvCenter
b.use InventoryCheck
b.use ReadState
end
end
Expand All @@ -155,6 +170,7 @@ def self.action_ssh
Vagrant::Action::Builder.new.tap do |b|
b.use ConfigValidate
b.use ConnectvCenter
b.use InventoryCheck
b.use Call, IsCreated do |env, b2|
unless env[:result]
b2.use MessageNotCreated
Expand All @@ -169,6 +185,7 @@ def self.action_ssh_run
Vagrant::Action::Builder.new.tap do |b|
b.use ConfigValidate
b.use ConnectvCenter
b.use InventoryCheck
b.use Call, IsCreated do |env, b2|
unless env[:result]
b2.use MessageNotCreated
Expand Down
167 changes: 86 additions & 81 deletions lib/vagrant-vcenter/action/inventory_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class InventoryCheck
def initialize(app, env)
@app = app
@logger = Log4r::Logger.new(
'vagrant_vcenter::action::inventory_check')
'vagrant_vcenter::action::inventory_check'
)
end

def call(env)
Expand All @@ -23,106 +24,110 @@ def call(env)
end

def vcenter_upload_box(env)
config = env[:machine].provider_config

box_dir = env[:machine].box.directory.to_s
cfg = env[:machine].provider_config

if env[:machine].box.name.to_s.include? '/'
box_file = env[:machine].box.name.rpartition('/').last.to_s
box_name = env[:machine].box.name.to_s.gsub(/\//, '-')
if env[:machine].box.name.include? '/'
box_file = env[:machine].box.name.rpartition('/').last
box_name = env[:machine].box.name.gsub(/\//, '-')
else
box_file = env[:machine].box.name.to_s
box_file = env[:machine].box.name
box_name = box_file
end

box_ovf = "file://#{box_dir}/#{box_file}.ovf"

@logger.debug("OVF File: #{box_ovf}")

env[:ui].info("Adding [#{box_name}]")

# FIXME: Raise a correct exception
dc = config.vcenter_cnx.serviceInstance.find_datacenter(
config.datacenter_name) or fail 'datacenter not found'

root_vm_folder = dc.vmFolder
vm_folder = root_vm_folder
if config.template_folder_name.nil?
template_folder = root_vm_folder
if cfg.template_folder_name.nil?
box_to_search = box_name
cfg.template_folder = cfg.vmFolder
else
template_folder = root_vm_folder.traverse!(
config.template_folder_name, RbVmomi::VIM::Folder)
box_to_search = cfg.template_folder_name + '/' + box_name
cfg.template_folder = cfg.vmfolder.traverse!(
cfg.template_folder_name,
RbVmomi::VIM::Folder
)
end

template_name = box_name
@logger.debug("Checking for box: #{box_to_search}...")

# FIXME: Raise a correct exception
datastore = dc.find_datastore(
config.datastore_name) or fail 'datastore not found'
# FIXME: Raise a correct exception
computer = dc.find_compute_resource(
config.computer_name) or fail 'Host not found'
# Check for the template object and add it the provider config
cfg.template = cfg.datacenter.find_vm(box_to_search)

network = computer.network.find { |x| x.name == config.network_name }
if cfg.template.nil?
# Roll a dice to get a winner in the race.
sleep_time = rand * (3 - 1) + 1
@logger.debug("Sleeping #{sleep_time} to avoid race conditions.")
sleep(sleep_time)

deployer = CachedOvfDeployer.new(
config.vcenter_cnx,
network,
computer,
template_folder,
vm_folder,
datastore
)
box_dir = env[:machine].box.directory
box_ovf = "file://#{box_dir}/#{box_file}.ovf"

deployer_opts = {
:run_without_interruptions => true,
:simple_vm_name => true
}
env[:ui].info("Uploading [#{box_name}]...")
@logger.debug("OVF File: #{box_ovf}")

deployer = CachedOvfDeployer.new(
cfg.vcenter_cnx,
cfg.network,
cfg.compute,
cfg.template_folder,
cfg.vmfolder,
cfg.datastore
)

deployer_opts = {
:run_without_interruptions => true,
:simple_vm_name => true
}

deployer.upload_ovf_as_template(
box_ovf,
box_name,
deployer_opts
)

# Re Fetch the template object and add it the provider config
cfg.template = cfg.datacenter.find_vm(box_to_search)
else
@logger.debug('Box already exists at target endpoint')
end

deployer.upload_ovf_as_template(
box_ovf,
template_name,
deployer_opts)
# FIXME: Progressbar??
end

def vcenter_check_inventory(env)
# Will check each mandatory config value against the vcenter
# Instance and will setup the global environment config values
config = env[:machine].provider_config
# FIXME: Raise a correct exception
dc = config.vcenter_cnx.serviceInstance.find_datacenter(
config.datacenter_name) or fail 'datacenter not found'

if env[:machine].box.name.to_s.include? '/'
box_file = env[:machine].box.name.rpartition('/').last.to_s
box_name = env[:machine].box.name.to_s.gsub(/\//, '-')
else
box_file = env[:machine].box.name.to_s
box_name = box_file
end

if config.template_folder_name.nil?
box_to_search = box_name
else
box_to_search = config.template_folder_name + '/' + box_name
end

@logger.debug("This is the box we're looking for: #{box_to_search}")

config.template_id = dc.find_vm(box_to_search)

if config.template_id.nil?
# Roll a dice to get a winner in the race.
sleep_time = rand * (3 - 1) + 1
@logger.debug("Sleeping #{sleep_time} to avoid race conditions.")
sleep(sleep_time)

env[:ui].info("Uploading [#{box_name}]...")
vcenter_upload_box(env)
else
@logger.debug("Template found at #{box_to_search}")
end
cfg = env[:machine].provider_config
cnx = cfg.vcenter_cnx

# Fetch Datacenter handle and add it to provider config
cfg.datacenter = cnx.serviceInstance.find_datacenter(
cfg.datacenter_name
) or fail Errors::DatacenterNotFound,
:datacenter_name => cfg.datacenter_name

# Fetch vmFolder handle for the specific Datacenter and add it to
# provider config
cfg.vmfolder = cfg.datacenter.vmFolder

# Fetch compute resource handle and add it to the provider config
cfg.compute = cfg.datacenter.find_compute_resource(
cfg.compute_name
) or fail Errors::ComputeNotFound,
:compute_name => cfg.compute_name

# Fetch datastore handle and add it to the provider config
cfg.datastore = cfg.datacenter.find_datastore(
cfg.datastore_name
) or fail Errors::DatastoreNotFound,
:datastore_name => cfg.datastore_name

# Fetch network portgroup handle and add it to the provider config
cfg.network = cfg.compute.network.find {
|x| x.name == cfg.network_name
} or fail Errors::NetworkNotFound,
:network_name => cfg.network_name

# Use this method to take care of the template/boxes
vcenter_upload_box(env)
end
end
end
Expand Down
26 changes: 2 additions & 24 deletions lib/vagrant-vcenter/action/is_created.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

module VagrantPlugins
module VCenter
module Action
Expand All @@ -10,33 +11,10 @@ def initialize(app, env)

def call(env)
vm_id = env[:machine].id
if vm_id

# VM is in the vagrant registry, now we need to check if it's
# actually in vcenter

# FIXME: this part needs some cleanup
config = env[:machine].provider_config

# FIXME: Raise a correct exception
dc = config.vcenter_cnx.serviceInstance.find_datacenter(
config.datacenter_name) or abort 'datacenter not found'

root_vm_folder = dc.vmFolder

vm = root_vm_folder.findByUuid(env[:machine].id)

unless vm
@logger.info('VM is in the vagrant registry but not in vcenter')
# Clear the ID
env[:machine].id = nil
env[:result] = false
end

# VM is in the registry AND in vcenter
if vm_id
@logger.info("VM has been created and ID is: [#{vm_id}]")
env[:result] = true

else
# VM is not in the registry
@logger.warn('VM has not been created')
Expand Down

0 comments on commit 440c6ab

Please sign in to comment.