Class: VagrantPlugins::VCenter::Action::InventoryCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-vcenter/action/inventory_check.rb

Overview

This Class inspect the vCenter inventory for templates and uploads it if needed.

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ InventoryCheck

Returns a new instance of InventoryCheck.



14
15
16
17
18
# File 'lib/vagrant-vcenter/action/inventory_check.rb', line 14

def initialize(app, env)
  @app = app
  @logger = Log4r::Logger.new(
            'vagrant_vcenter::action::inventory_check')
end

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
# File 'lib/vagrant-vcenter/action/inventory_check.rb', line 20

def call(env)
  vcenter_check_inventory(env)
  @app.call env
end

#vcenter_check_inventory(env) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/vagrant-vcenter/action/inventory_check.rb', line 89

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
end

#vcenter_upload_box(env) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/vagrant-vcenter/action/inventory_check.rb', line 25

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

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

  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

  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
  else
    template_folder = root_vm_folder.traverse!(
                      config.template_folder_name, RbVmomi::VIM::Folder)
  end

  template_name = box_name

  # 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'

  network = computer.network.find { |x| x.name == config.network_name }

  deployer = CachedOvfDeployer.new(
    config.vcenter_cnx,
    network,
    computer,
    template_folder,
    vm_folder,
    datastore
  )

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

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