Class: VagrantPlugins::VCloud::Action::InventoryCheck

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

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ InventoryCheck

Returns a new instance of InventoryCheck.


7
8
9
10
# File 'lib/vagrant-vcloud/action/inventory_check.rb', line 7

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

Instance Method Details

#call(env) ⇒ Object


12
13
14
15
16
# File 'lib/vagrant-vcloud/action/inventory_check.rb', line 12

def call(env)
  vcloud_check_inventory(env)

  @app.call env
end

#vcloud_check_inventory(env) ⇒ Object


86
87
88
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/vagrant-vcloud/action/inventory_check.rb', line 86

def vcloud_check_inventory(env)
  # Will check each mandatory config value against the vCloud Director
  # Instance and will setup the global environment config values
  cfg = env[:machine].provider_config
  cnx = cfg.vcloud_cnx.driver

  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
  else
    box_file = env[:machine].box.name.to_s
    box_name = box_file
  end

  cfg.org = cnx.get_organization_by_name(cfg.org_name)
  cfg.org_id = cnx.get_organization_id_by_name(cfg.org_name)

  cfg.vdc = cnx.get_vdc_by_name(cfg.org, cfg.vdc_name)
  cfg.vdc_id = cnx.get_vdc_id_by_name(cfg.org, cfg.vdc_name)

  cfg.catalog = cnx.get_catalog_by_name(cfg.org, cfg.catalog_name)
  cfg.catalog_id = cnx.get_catalog_id_by_name(cfg.org, cfg.catalog_name)

  if cfg.catalog_id.nil?
    env[:ui].warn("Catalog [#{cfg.catalog_name}] does not exist!")

    user_input = env[:ui].ask(
      "Would you like to create the [#{cfg.catalog_name}] catalog?\n" +
      'Choice (yes/no): '
    )

    if user_input.downcase == 'yes' || user_input.downcase == 'y'
      vcloud_create_catalog(env)
    else
      env[:ui].error('Catalog not created, exiting...')

      # FIXME: wrong error message
      raise VagrantPlugins::VCloud::Errors::VCloudError,
            :message => 'Catalog not available, exiting...'

    end
  end

  @logger.debug(
    "Getting catalog item with cfg.catalog_id: [#{cfg.catalog_id}] " +
    "and machine name [#{box_name}]"
  )
  cfg.catalog_item = cnx.get_catalog_item_by_name(
    cfg.catalog_id,
    box_name
  )

  @logger.debug("Catalog item is now #{cfg.catalog_item}")

  # This only works with Org Admin role or higher
  if cfg.vdc_network_name
    cfg.vdc_network_id = cfg.org[:networks][cfg.vdc_network_name]
    if !cfg.vdc_network_id
      # TEMP FIX: permissions issues at the Org Level for vApp authors
      #           to "view" Org vDC Networks but they can see them at the
      #           Organization vDC level (tsugliani)
      cfg.vdc_network_id = cfg.vdc[:networks][cfg.vdc_network_name]
      if !cfg.vdc_network_id
        raise 'vCloud User credentials has insufficient privileges'
      end
    end
  end

  # Checking Catalog mandatory requirements
  if !cfg.catalog_id
    @logger.info("Catalog [#{cfg.catalog_name}] STILL does not exist!")

      # FIXME: wrong error message
      raise VagrantPlugins::VCloud::Errors::VCloudError,
            :message => 'Catalog not available, exiting...'

  else
    @logger.info("Catalog [#{cfg.catalog_name}] exists")
  end

  if !cfg.catalog_item
    env[:ui].warn(
      "Catalog item [#{box_name}] " +
      "in Catalog [#{cfg.catalog_name}] does not exist!"
    )

    if cfg.auto_yes_for_upload.nil? || cfg.auto_yes_for_upload == false
     user_input = env[:ui].ask(
       "Would you like to upload the [#{box_name}] " +
       "box to [#{cfg.catalog_name}] Catalog?\n" +
       'Choice (yes/no): '
     )
 else
 	auto_upload = cfg.auto_yes_for_upload
 end

    if !auto_upload.nil? || user_input.downcase == 'yes' || user_input.downcase == 'y'
      env[:ui].info("Uploading [#{box_name}]...")
      vcloud_upload_box(env)
    else
      env[:ui].error('Catalog item not available, exiting...')

      # FIXME: wrong error message
      raise VagrantPlugins::VCloud::Errors::VCloudError,
            :message => 'Catalog item not available, exiting...'
    end

  else
    @logger.info(
      "Using catalog item [#{box_name}] " +
      "in Catalog [#{cfg.catalog_name}]..."
    )
  end
end

#vcloud_create_catalog(env) ⇒ Object


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/vagrant-vcloud/action/inventory_check.rb', line 67

def vcloud_create_catalog(env)
  cfg = env[:machine].provider_config
  cnx = cfg.vcloud_cnx.driver

  catalog_creation = cnx.create_catalog(
    cfg.org_id,
    cfg.catalog_name,
    "Created by #{Etc.getlogin} " +
    "running on #{Socket.gethostname.downcase} " +
    "using vagrant-vcloud on #{Time.now.strftime("%B %d, %Y")}"
  )
  cnx.wait_task_completion(catalog_creation[:task_id])

  @logger.debug("Catalog Creation result: #{catalog_creation.inspect}")
  env[:ui].info("Catalog [#{cfg.catalog_name}] successfully created.")

  cfg.catalog_id = catalog_creation[:catalog_id]
end

#vcloud_upload_box(env) ⇒ Object


18
19
20
21
22
23
24
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
# File 'lib/vagrant-vcloud/action/inventory_check.rb', line 18

def vcloud_upload_box(env)
  cfg = env[:machine].provider_config
  cnx = cfg.vcloud_cnx.driver

  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
  else
    box_file = env[:machine].box.name.to_s
    box_name = box_file
  end

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

  # Still relying on ruby-progressbar because report_progress
  # basically sucks.
  @logger.debug("OVF File: #{box_ovf}")
  upload_ovf = cnx.upload_ovf(
    cfg.vdc_id,
    box_name,
    'Vagrant Box',
    box_ovf,
    cfg.catalog_id,
    {
      :progressbar_enable => true,
      :chunksize => (cfg.upload_chunksize || 1_048_576)
    }
  )

  env[:ui].info(
    "Adding [#{box_name}] to " +
    "Catalog [#{cfg.catalog_name}]"
  )
  add_ovf_to_catalog = cnx.wait_task_completion(upload_ovf)

  unless add_ovf_to_catalog[:errormsg].nil?
    fail Errors::CatalogAddError,
         :message => add_ovf_to_catalog[:errormsg]
  end

  # Retrieve catalog_item ID
  cfg.catalog_item = cnx.get_catalog_item_by_name(
    cfg.catalog_id,
    box_name
  )
end