Project

General

Profile

Download (17.5 KB) Statistics
| Branch: | Tag: | Revision:

hammer-cli-csv / lib / hammer_cli_csv / content_hosts.rb @ f4fb9c20

1
module HammerCLICsv
2
  class CsvCommand
3
    class ContentHostsCommand < BaseCommand
4
      include ::HammerCLIForemanTasks::Helper
5
      include ::HammerCLICsv::Utils::Subscriptions
6

    
7
      command_name 'content-hosts'
8
      desc         'import or export content hosts'
9

    
10
      def self.supported?
11
        true
12
      end
13

    
14
      option %w(--itemized-subscriptions), :flag, _('Export one subscription per row, only process update subscriptions on import')
15

    
16
      SEARCH = 'Search'
17
      ORGANIZATION = 'Organization'
18
      ENVIRONMENT = 'Environment'
19
      CONTENTVIEW = 'Content View'
20
      HOSTCOLLECTIONS = 'Host Collections'
21
      VIRTUAL = 'Virtual'
22
      HOST = 'Host'
23
      OPERATINGSYSTEM = 'OS'
24
      ARCHITECTURE = 'Arch'
25
      SOCKETS = 'Sockets'
26
      RAM = 'RAM'
27
      CORES = 'Cores'
28
      SLA = 'SLA'
29
      PRODUCTS = 'Products'
30

    
31
      def export(csv)
32
        if option_itemized_subscriptions?
33
          export_itemized_subscriptions csv
34
        else
35
          export_all csv
36
        end
37
      end
38

    
39
      def export_itemized_subscriptions(csv)
40
        csv << shared_headers + [Utils::Subscriptions::SUBS_NAME, Utils::Subscriptions::SUBS_TYPE,
41
                                 Utils::Subscriptions::SUBS_QUANTITY, Utils::Subscriptions::SUBS_SKU,
42
                                 Utils::Subscriptions::SUBS_CONTRACT, Utils::Subscriptions::SUBS_ACCOUNT,
43
                                 Utils::Subscriptions::SUBS_START, Utils::Subscriptions::SUBS_END]
44
        iterate_hosts(csv) do |host|
45
          export_line = shared_columns(host)
46
          if host['subscription_facet_attributes']
47
            subscriptions = @api.resource(:host_subscriptions).call(:index, {
48
                'organization_id' => host['organization_id'],
49
                'host_id' => host['id']
50
            })['results']
51
            if subscriptions.empty?
52
              csv << export_line + [nil, nil, nil, nil, nil, nil]
53
            else
54
              subscriptions.each do |subscription|
55
                subscription_type = subscription['product_id'].to_i == 0 ? 'Red Hat' : 'Custom'
56
                subscription_type += ' Guest' if subscription['type'] == 'STACK_DERIVED'
57
                subscription_type += ' Temporary' if subscription['type'] == 'UNMAPPED_GUEST'
58
                csv << export_line + [subscription['product_name'], subscription_type,
59
                                      subscription['quantity_consumed'], subscription['product_id'],
60
                                      subscription['contract_number'], subscription['account_number'],
61
                                      DateTime.parse(subscription['start_date']).strftime('%m/%d/%Y'),
62
                                      DateTime.parse(subscription['end_date']).strftime('%m/%d/%Y')]
63
              end
64
            end
65
          else
66
            csv << export_line + [nil, nil, nil, nil, nil, nil]
67
          end
68
        end
69
      end
70

    
71
      def export_all(csv)
72
        csv << shared_headers + [Utils::Subscriptions::SUBSCRIPTIONS]
73
        iterate_hosts(csv) do |host|
74
          if host['subscription_facet_attributes']
75
            subscriptions = CSV.generate do |column|
76
              column << @api.resource(:host_subscriptions).call(:index, {
77
                  'organization_id' => host['organization_id'],
78
                  'host_id' => host['id']
79
              })['results'].collect do |subscription|
80
                "#{subscription['quantity_consumed']}"\
81
                "|#{subscription['product_id']}"\
82
                "|#{subscription['product_name']}"\
83
                "|#{subscription['contract_number']}|#{subscription['account_number']}"
84
              end
85
            end
86
            subscriptions.delete!("\n")
87
          else
88
            subscriptions = nil
89
          end
90

    
91
          csv << shared_columns(host) + [subscriptions]
92
        end
93
      end
94

    
95
      def import
96
        remote = @server_status['plugins'].detect { |plugin| plugin['name'] == 'foreman_csv' }
97
        if remote.nil?
98
          import_locally
99
        else
100
          import_remotely
101
        end
102
      end
103

    
104
      def import_remotely
105
        params = {'content' => ::File.new(::File.expand_path(option_file), 'rb')}
106
        headers = {:content_type => 'multipart/form-data', :multipart => true}
107
        task_progress(@api.resource(:csv).call(:import_content_hosts, params, headers))
108
      end
109

    
110
      def import_locally
111
        @existing = {}
112
        @hypervisor_guests = {}
113
        @all_subscriptions = {}
114

    
115
        thread_import do |line|
116
          create_from_csv(line)
117
        end
118

    
119
        if !@hypervisor_guests.empty?
120
          print(_('Updating hypervisor and guest associations...')) if option_verbose?
121
          @hypervisor_guests.each do |host_id, guest_ids|
122
            @api.resource(:hosts).call(:update, {
123
              'id' => host_id,
124
              'host' => {
125
                'subscription_facet_attributes' => {
126
                  'autoheal' => false,
127
                  'hypervisor_guest_uuids' => guest_ids
128
                }
129
              }
130
            })
131
          end
132
          puts _('done') if option_verbose?
133
        end
134
      end
135

    
136
      def create_from_csv(line)
137
        return if option_organization && line[ORGANIZATION] != option_organization
138

    
139
        update_existing(line)
140

    
141
        count(line[COUNT]).times do |number|
142
          if  !line[SEARCH].nil? && !line[SEARCH].empty?
143
            search = namify(line[SEARCH], number)
144
            @api.resource(:hosts).call(:index, {
145
                'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
146
                'search' => search
147
            })['results'].each do |host|
148
              if host['subscription_facet_attributes']
149
                create_named_from_csv(host['name'], line)
150
              end
151
            end
152
          else
153
            name = namify(line[NAME], number)
154
            create_named_from_csv(name, line)
155
          end
156
        end
157
      end
158

    
159
      private
160

    
161
      def create_named_from_csv(name, line)
162
        if option_itemized_subscriptions?
163
          update_itemized_subscriptions(name, line)
164
        else
165
          update_or_create(name, line)
166
        end
167
      end
168

    
169
      def update_itemized_subscriptions(name, line)
170
        raise _("Content host '%{name}' must already exist with --itemized-subscriptions") % {:name => name} unless @existing.include? name
171

    
172
        print(_("Updating subscriptions for content host '%{name}'...") % {:name => name}) if option_verbose?
173
        host = @api.resource(:hosts).call(:show, {:id => @existing[name]})
174
        update_subscriptions(host, line, false)
175
        puts _('done') if option_verbose?
176
      end
177

    
178
      def update_or_create(name, line)
179
        if !@existing.include? name
180
          print(_("Creating content host '%{name}'...") % {:name => name}) if option_verbose?
181
          params = {
182
            'name' => name,
183
            'facts' => facts(name, line),
184
            'lifecycle_environment_id' => lifecycle_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
185
            'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW])
186
          }
187
          params['installed_products'] = products(line) if line[PRODUCTS]
188
          params['service_level'] = line[SLA] if line[SLA]
189
          host = @api.resource(:host_subscriptions).call(:create, params)
190
          @existing[name] = host['id']
191
        else
192
          print(_("Updating content host '%{name}'...") % {:name => name}) if option_verbose?
193
          params = {
194
            'id' => @existing[name],
195
            'host' => {
196
              'content_facet_attributes' => {
197
                'lifecycle_environment_id' => lifecycle_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
198
                'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW])
199
              },
200
              'subscription_facet_attributes' => {
201
                'facts' => facts(name, line),
202
                'installed_products' => products(line),
203
                'service_level' => line[SLA]
204
              }
205
            }
206
          }
207
          host = @api.resource(:hosts).call(:update, params)
208
        end
209

    
210
        if line[VIRTUAL] == 'Yes' && line[HOST]
211
          raise "Content host '#{line[HOST]}' not found" if !@existing[line[HOST]]
212
          @hypervisor_guests[@existing[line[HOST]]] ||= []
213
          @hypervisor_guests[@existing[line[HOST]]] << "#{line[ORGANIZATION]}/#{name}"
214
        end
215

    
216
        update_host_collections(host, line)
217
        update_subscriptions(host, line, true)
218

    
219
        puts _('done') if option_verbose?
220
      end
221

    
222
      def facts(name, line)
223
        facts = {}
224
        facts['system.certificate_version'] = '3.2'  # Required for auto-attach to work
225
        facts['network.hostname'] = name
226
        facts['cpu.core(s)_per_socket'] = line[CORES]
227
        facts['cpu.cpu_socket(s)'] = line[SOCKETS]
228
        facts['memory.memtotal'] = line[RAM]
229
        facts['uname.machine'] = line[ARCHITECTURE]
230
        (facts['distribution.name'], facts['distribution.version']) = os_name_version(line[OPERATINGSYSTEM])
231
        facts['virt.is_guest'] = line[VIRTUAL] == 'Yes' ? true : false
232
        facts['virt.uuid'] = "#{line[ORGANIZATION]}/#{name}" if facts['virt.is_guest']
233
        facts['cpu.cpu(s)'] = 1
234
        facts
235
      end
236

    
237
      def update_host_collections(host, line)
238
        # TODO: http://projects.theforeman.org/issues/16234
239
        # return nil if line[HOSTCOLLECTIONS].nil? || line[HOSTCOLLECTIONS].empty?
240
        # CSV.parse_line(line[HOSTCOLLECTIONS]).each do |hostcollection_name|
241
        #   @api.resource(:host_collections).call(:add_hosts, {
242
        #       'id' => katello_hostcollection(line[ORGANIZATION], :name => hostcollection_name),
243
        #       'host_ids' => [host['id']]
244
        #   })
245
        # end
246
      end
247

    
248
      def os_name_version(operatingsystem)
249
        if operatingsystem.nil?
250
          name = nil
251
          version = nil
252
        elsif operatingsystem.index(' ')
253
          (name, version) = operatingsystem.split(' ')
254
        else
255
          (name, version) = ['RHEL', operatingsystem]
256
        end
257
        [name, version]
258
      end
259

    
260
      def products(line)
261
        return nil if !line[PRODUCTS]
262
        CSV.parse_line(line[PRODUCTS]).collect do |product_details|
263
          product = {}
264
          (product['product_id'], product['product_name']) = product_details.split('|')
265
          product['arch'] = line[ARCHITECTURE]
266
          product['version'] = os_name_version(line[OPERATINGSYSTEM])[1]
267
          product
268
        end
269
      end
270

    
271
      def update_subscriptions(host, line, remove_existing)
272
        existing_subscriptions = @api.resource(:host_subscriptions).call(:index, {
273
            'host_id' => host['id']
274
        })['results']
275
        if remove_existing && existing_subscriptions.length != 0
276
          existing_subscriptions.map! do |existing_subscription|
277
            {:id => existing_subscription['id'], :quantity => existing_subscription['quantity_consumed']}
278
          end
279
          @api.resource(:host_subscriptions).call(:remove_subscriptions, {
280
            'host_id' => host['id'],
281
            'subscriptions' => existing_subscriptions
282
          })
283
          existing_subscriptions = []
284
        end
285

    
286
        if line[Utils::Subscriptions::SUBS_NAME].nil? && line[Utils::Subscriptions::SUBS_SKU].nil?
287
          all_in_one_subscription(host, existing_subscriptions, line)
288
        else
289
          single_subscription(host, existing_subscriptions, line)
290
        end
291
      end
292

    
293
      def single_subscription(host, existing_subscriptions, line)
294
        already_attached = false
295
        if line[Utils::Subscriptions::SUBS_SKU]
296
          already_attached = existing_subscriptions.detect do |subscription|
297
            line[Utils::Subscriptions::SUBS_SKU] == subscription['product_id']
298
          end
299
        elsif line[Utils::Subscriptions::SUBS_NAME]
300
          already_attached = existing_subscriptions.detect do |subscription|
301
            line[Utils::Subscriptions::SUBS_NAME] == subscription['name']
302
          end
303
        end
304
        if already_attached
305
          print _(" '%{name}' already attached...") % {:name => already_attached['name']}
306
          return
307
        end
308

    
309
        available_subscriptions = @api.resource(:subscriptions).call(:index, {
310
          'organization_id' => host['organization_id'],
311
          'host_id' => host['id'],
312
          'available_for' => 'host',
313
          'match_host' => true
314
        })['results']
315

    
316
        matches = matches_by_sku_and_name([], line, available_subscriptions)
317
        matches = matches_by_type(matches, line)
318
        matches = matches_by_account(matches, line)
319
        matches = matches_by_contract(matches, line)
320
        matches = matches_by_quantity(matches, line)
321

    
322
        raise _("No matching subscriptions") if matches.empty?
323

    
324
        match = matches[0]
325
        print _(" attaching '%{name}'...") % {:name => match['name']} if option_verbose?
326

    
327
        amount = line[SUBS_QUANTITY]
328
        quantity = (amount.nil? || amount.empty? || amount == 'Automatic') ? 0 : amount.to_i
329

    
330
        @api.resource(:host_subscriptions).call(:add_subscriptions, {
331
            'host_id' => host['id'],
332
            'subscriptions' => [{:id => match['id'], :quantity => quantity}]
333
        })
334
      end
335

    
336
      def all_in_one_subscription(host, existing_subscriptions, line)
337
        return if line[SUBSCRIPTIONS].nil? || line[SUBSCRIPTIONS].empty?
338

    
339
        subscriptions = CSV.parse_line(line[SUBSCRIPTIONS], {:skip_blanks => true}).collect do |details|
340
          (amount, sku, name, contract, account) = split_subscription_details(details)
341
          {
342
            :id => get_subscription(line[ORGANIZATION], :name => name, :contract => contract,
343
                                                        :account => account),
344
            :quantity => (amount.nil? || amount.empty? || amount == 'Automatic') ? 0 : amount.to_i
345
          }
346
        end
347

    
348
        @api.resource(:host_subscriptions).call(:add_subscriptions, {
349
            'host_id' => host['id'],
350
            'subscriptions' => subscriptions
351
        })
352
      end
353

    
354
      def update_existing(line)
355
        if !@existing[line[ORGANIZATION]]
356
          @existing[line[ORGANIZATION]] = true
357
          # Fetching all content hosts can be too slow and times so page
358
          # http://projects.theforeman.org/issues/6307
359
          total = @api.resource(:hosts).call(:index, {
360
              'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
361
              'per_page' => 1
362
          })['total'].to_i
363
          (total / 20 + 1).to_i.times do |page|
364
            @api.resource(:hosts).call(:index, {
365
                'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
366
                'page' => page + 1,
367
                'per_page' => 20
368
            })['results'].each do |host|
369
              if host['subscription_facet_attributes']
370
                @existing[host['name']] = host['id']
371
              end
372
            end
373
          end
374
        end
375
      end
376

    
377
      def iterate_hosts(csv)
378
        hypervisors = []
379
        hosts = []
380
        @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
381
          next if option_organization && organization['name'] != option_organization
382

    
383
          @api.resource(:hosts).call(:index, {
384
              'per_page' => 999999,
385
              'organization_id' => foreman_organization(:name => organization['name'])
386
          })['results'].each do |host|
387
            host = @api.resource(:hosts).call(:show, {
388
                'id' => host['id']
389
            })
390
            host['facts'] ||= {}
391
            unless host['subscription_facet_attributes'].nil?
392
              if host['subscription_facet_attributes']['virtual_guests'].empty?
393
                hosts.push(host)
394
              else
395
                hypervisors.push(host)
396
              end
397
            end
398
          end
399
        end
400
        hypervisors.each do |host|
401
          yield host
402
        end
403
        hosts.each do |host|
404
          yield host
405
        end
406
      end
407

    
408
      def shared_headers
409
        [NAME, ORGANIZATION, ENVIRONMENT, CONTENTVIEW, HOSTCOLLECTIONS, VIRTUAL, HOST,
410
         OPERATINGSYSTEM, ARCHITECTURE, SOCKETS, RAM, CORES, SLA, PRODUCTS]
411
      end
412

    
413
      def shared_columns(host)
414
        name = host['name']
415
        organization_name = host['organization_name']
416
        if host['content_facet_attributes']
417
          environment = host['content_facet_attributes']['lifecycle_environment']['name']
418
          contentview = host['content_facet_attributes']['content_view']['name']
419
          hostcollections = export_column(host['content_facet_attributes'], 'host_collections', 'name')
420
        else
421
          environment = nil
422
          contentview = nil
423
          hostcollections = nil
424
        end
425
        if host['subscription_facet_attributes']
426
          hypervisor_host = host['subscription_facet_attributes']['virtual_host'].nil? ? nil : host['subscription_facet_attributes']['virtual_host']['name']
427
          products = export_column(host['subscription_facet_attributes'], 'installed_products') do |product|
428
            "#{product['productId']}|#{product['productName']}"
429
          end
430
        else
431
          hypervisor_host = nil
432
          products = nil
433
        end
434
        virtual = host['facts']['virt::is_guest'] == 'true' ? 'Yes' : 'No'
435
        operatingsystem = host['facts']['distribution::name'] if host['facts']['distribution::name']
436
        operatingsystem += " #{host['facts']['distribution::version']}" if host['facts']['distribution::version']
437
        architecture = host['facts']['uname::machine']
438
        sockets = host['facts']['cpu::cpu_socket(s)']
439
        ram = host['facts']['memory::memtotal']
440
        cores = host['facts']['cpu::core(s)_per_socket'] || 1
441
        sla = ''
442

    
443
        [name, organization_name, environment, contentview, hostcollections, virtual, hypervisor_host,
444
         operatingsystem, architecture, sockets, ram, cores, sla, products]
445
      end
446

    
447
    end
448
  end
449
end