Class: VagrantPlugins::VCloudAir::Action::InventoryCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-vcloudair/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-vcloudair/action/inventory_check.rb', line 7

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

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  vcloud_check_inventory(env)

  @app.call env
end

#vcloud_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
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
200
201
202
203
204
205
206
# File 'lib/vagrant-vcloudair/action/inventory_check.rb', line 89

def vcloud_check_inventory(env)
  # Will check each mandatory config value against the vCloud Air
  # Instance and will setup the global environment config values
  cfg = env[:machine].provider_config
  cnx = cfg.vcloudair_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.vdc_name)
  @logger.debug("cfg.org: #{cfg.org}")
  cfg.org_id = cnx.get_organization_id_by_name(cfg.vdc_name)
  @logger.debug("cfg.org_id: #{cfg.org_id}")

  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(I18n.t(
                  'vagrant_vcloudair.catalog.nonexistant_catalog',
                  catalog_name: cfg.catalog_name))

    user_input = env[:ui].ask(
      I18n.t('vagrant_vcloudair.catalog.create_catalog_ask',
             catalog_name: cfg.catalog_name) + ' '
    )

    if user_input.downcase == 'yes' || user_input.downcase == 'y'
      vcloud_create_catalog(env)
    else
      env[:ui].error(I18n.t(
                     'vagrant_vcloudair.catalog.catalog_not_created'))
      fail Errors::WontCreate, :item => 'Catalog'
    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

  cfg.vdc_network_id = cfg.org[:networks][cfg.vdc_network_name]
  unless cfg.vdc_network_id
    # TODO: 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]
    fail Errors::InvalidNetSpecification unless cfg.vdc_network_id
  end

  # Checking Catalog mandatory requirements
  if !cfg.catalog_id
    @logger.info("Catalog [#{cfg.catalog_name}] STILL does not exist!")
    fail Errors::ObjectNotFound,
         :message => 'Catalog not found after creation'
  else
    @logger.info("Catalog [#{cfg.catalog_name}] exists")
  end

  if !cfg.catalog_item
    env[:ui].warn(I18n.t(
                  'vagrant_vcloudair.catalog.nonexistant_catalog_item',
                  catalog_item: box_name,
                  catalog_name: cfg.catalog_name))

    user_input = env[:ui].ask(
                 I18n.t('vagrant_vcloudair.catalog.upload_ask',
                        catalog_item: box_name,
                        catalog_name: cfg.catalog_name) + ' ')

    if user_input.downcase == 'yes' || user_input.downcase == 'y'
      env[:ui].info(I18n.t('vagrant_vcloudair.catalog.uploading',
                           catalog_item: box_name))
      vcloud_upload_box(env)
    else
      env[:ui].error(
        I18n.t('vagrant_vcloudair.catalog.catalog_item_notavailable'))
      fail Errors::WontCreate, :item => 'Box'
    end

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

  # Test if Gateway Edge and its IP are correct
  if !cfg.vdc_edge_gateway.nil? && !cfg.vdc_edge_gateway_ip.nil?
    env[:ui].info(I18n.t('vagrant_vcloudair.edge.network_test'))
    # Test if Edge Gateway exists
    cnx.find_edge_gateway_id(cfg.vdc_edge_gateway, cfg.vdc_id)
    # Test if Edge Gateway IP exists
    cnx.find_edge_gateway_network(cfg.vdc_edge_gateway,
                                  cfg.vdc_id,
                                  cfg.vdc_edge_gateway_ip)
    # Test if Network is connected to Edge Gateway
    cnx.check_edge_gateway_network(cfg.vdc_edge_gateway,
                                   cfg.vdc_id,
                                   cfg.vdc_network_name)
  end
end

#vcloud_create_catalog(env) ⇒ Object



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

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

  catalog_creation = cnx.create_catalog(
    cfg.org_id,
    cfg.catalog_name,
    "Created by #{Etc.getlogin} " +
    "running on #{Socket.gethostname.downcase} " +
    "using vagrant-vcloudair 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(I18n.t('vagrant_vcloudair.catalog.create_catalog',
                       catalog_name: cfg.catalog_name))

  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
66
67
# File 'lib/vagrant-vcloudair/action/inventory_check.rb', line 18

def vcloud_upload_box(env)
  cfg = env[:machine].provider_config
  cnx = cfg.vcloudair_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,
      # Set the chunksize for upload at the configured value or default
      # to 5M
      :chunksize => (cfg.upload_chunksize || 5_242_880)
    }
  )

  env[:ui].info(I18n.t('vagrant_vcloudair.catalog.add_to_catalog',
                       box_name: box_name,
                       catalog_name: 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