Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / content_hosts.rb @ 611959a3

1
#require 'hammer_cli_csv/utils/subscription_utils'
2

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

    
9
      command_name 'content-hosts'
10
      desc         'import or export content hosts'
11

    
12
      def self.supported?
13
        true
14
      end
15

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

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

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

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

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

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

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

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

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

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

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

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

    
140
        update_existing(line)
141

    
142
        count(line[COUNT]).times do |number|
143
          name = namify(line[NAME], number)
144

    
145
          if option_itemized_subscriptions?
146
            update_itemized_subscriptions(name, line)
147
          else
148
            update_or_create(name, line)
149
          end
150
        end
151
      end
152

    
153
      private
154

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

    
158
        print(_("Updating subscriptions for content host '%{name}'...") % {:name => name}) if option_verbose?
159
        host = @api.resource(:hosts).call(:show, {:id => @existing[name]})
160
        update_subscriptions(host, line, false)
161
        puts _('done') if option_verbose?
162
      end
163

    
164
      def update_or_create(name, line)
165
        if !@existing.include? name
166
          print(_("Creating content host '%{name}'...") % {:name => name}) if option_verbose?
167
          params = {
168
            'name' => name,
169
            'facts' => facts(name, line),
170
            'lifecycle_environment_id' => lifecycle_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
171
            'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW])
172
          }
173
          params['installed_products'] = products(line) if line[PRODUCTS]
174
          params['service_level'] = line[SLA] if line[SLA]
175
          host = @api.resource(:host_subscriptions).call(:create, params)
176
          @existing[name] = host['id']
177
        else
178
          print(_("Updating content host '%{name}'...") % {:name => name}) if option_verbose?
179
          params = {
180
            'id' => @existing[name],
181
            'host' => {
182
              'content_facet_attributes' => {
183
                'lifecycle_environment_id' => lifecycle_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
184
                'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW])
185
              },
186
              'subscription_facet_attributes' => {
187
                'facts' => facts(name, line),
188
                'installed_products' => products(line),
189
                'service_level' => line[SLA]
190
              }
191
            }
192
          }
193
          host = @api.resource(:hosts).call(:update, params)
194
        end
195

    
196
        if line[VIRTUAL] == 'Yes' && line[HOST]
197
          raise "Content host '#{line[HOST]}' not found" if !@existing[line[HOST]]
198
          @hypervisor_guests[@existing[line[HOST]]] ||= []
199
          @hypervisor_guests[@existing[line[HOST]]] << "#{line[ORGANIZATION]}/#{name}"
200
        end
201

    
202
        update_host_collections(host, line)
203
        update_subscriptions(host, line, true)
204

    
205
        puts _('done') if option_verbose?
206
      end
207

    
208
      def facts(name, line)
209
        facts = {}
210
        facts['system.certificate_version'] = '3.2'  # Required for auto-attach to work
211
        facts['network.hostname'] = name
212
        facts['cpu.core(s)_per_socket'] = line[CORES]
213
        facts['cpu.cpu_socket(s)'] = line[SOCKETS]
214
        facts['memory.memtotal'] = line[RAM]
215
        facts['uname.machine'] = line[ARCHITECTURE]
216
        (facts['distribution.name'], facts['distribution.version']) = os_name_version(line[OPERATINGSYSTEM])
217
        facts['virt.is_guest'] = line[VIRTUAL] == 'Yes' ? true : false
218
        facts['virt.uuid'] = "#{line[ORGANIZATION]}/#{name}" if facts['virt.is_guest']
219
        facts['cpu.cpu(s)'] = 1
220
        facts
221
      end
222

    
223
      def update_host_collections(host, line)
224
        # TODO: http://projects.theforeman.org/issues/16234
225
        # return nil if line[HOSTCOLLECTIONS].nil? || line[HOSTCOLLECTIONS].empty?
226
        # CSV.parse_line(line[HOSTCOLLECTIONS]).each do |hostcollection_name|
227
        #   @api.resource(:host_collections).call(:add_hosts, {
228
        #       'id' => katello_hostcollection(line[ORGANIZATION], :name => hostcollection_name),
229
        #       'host_ids' => [host['id']]
230
        #   })
231
        # end
232
      end
233

    
234
      def os_name_version(operatingsystem)
235
        if operatingsystem.nil?
236
          name = nil
237
          version = nil
238
        elsif operatingsystem.index(' ')
239
          (name, version) = operatingsystem.split(' ')
240
        else
241
          (name, version) = ['RHEL', operatingsystem]
242
        end
243
        [name, version]
244
      end
245

    
246
      def products(line)
247
        return nil if !line[PRODUCTS]
248
        CSV.parse_line(line[PRODUCTS]).collect do |product_details|
249
          product = {}
250
          (product['product_id'], product['product_name']) = product_details.split('|')
251
          product['arch'] = line[ARCHITECTURE]
252
          product['version'] = os_name_version(line[OPERATINGSYSTEM])[1]
253
          product
254
        end
255
      end
256

    
257
      def update_subscriptions(host, line, remove_existing)
258
        existing_subscriptions = @api.resource(:host_subscriptions).call(:index, {
259
            'host_id' => host['id']
260
        })['results']
261
        if remove_existing && existing_subscriptions.length != 0
262
          existing_subscriptions.map! do |existing_subscription|
263
            {:id => existing_subscription['id'], :quantity => existing_subscription['quantity_consumed']}
264
          end
265
          @api.resource(:host_subscriptions).call(:remove_subscriptions, {
266
            'host_id' => host['id'],
267
            'subscriptions' => existing_subscriptions
268
          })
269
          existing_subscriptions = []
270
        end
271

    
272
        if line[Utils::Subscriptions::SUBS_NAME].nil? && line[Utils::Subscriptions::SUBS_SKU].nil?
273
          all_in_one_subscription(host, existing_subscriptions, line)
274
        else
275
          single_subscription(host, existing_subscriptions, line)
276
        end
277
      end
278

    
279
      def single_subscription(host, existing_subscriptions, line)
280
        already_attached = false
281
        if line[Utils::Subscriptions::SUBS_SKU]
282
          already_attached = existing_subscriptions.detect do |subscription|
283
            line[Utils::Subscriptions::SUBS_SKU] == subscription['product_id']
284
          end
285
        elsif line[Utils::Subscriptions::SUBS_NAME]
286
          already_attached = existing_subscriptions.detect do |subscription|
287
            line[Utils::Subscriptions::SUBS_NAME] == subscription['name']
288
          end
289
        end
290
        if already_attached
291
          print _(" '%{name}' already attached...") % {:name => already_attached['name']}
292
          return
293
        end
294

    
295
        available_subscriptions = @api.resource(:subscriptions).call(:index, {
296
          'organization_id' => host['organization_id'],
297
          'host_id' => host['id'],
298
          'available_for' => 'host',
299
          'match_host' => true
300
        })['results']
301

    
302
        matches = matches_by_sku_and_name([], line, available_subscriptions)
303
        matches = matches_by_type(matches, line)
304
        matches = matches_by_account(matches, line)
305
        matches = matches_by_contract(matches, line)
306
        matches = matches_by_quantity(matches, line)
307

    
308
        raise _("No matching subscriptions") if matches.empty?
309

    
310
        match = matches[0]
311
        print _(" attaching '%{name}'...") % {:name => match['name']} if option_verbose?
312

    
313
        @api.resource(:host_subscriptions).call(:add_subscriptions, {
314
            'host_id' => host['id'],
315
            'subscriptions' => existing_subscriptions + [match]
316
        })
317
      end
318

    
319
      def all_in_one_subscription(host, existing_subscriptions, line)
320
        return if line[SUBSCRIPTIONS].nil? || line[SUBSCRIPTIONS].empty?
321

    
322
        subscriptions = CSV.parse_line(line[SUBSCRIPTIONS], {:skip_blanks => true}).collect do |details|
323
          (amount, sku, name, contract, account) = split_subscription_details(details)
324
          {
325
            :id => get_subscription(line[ORGANIZATION], :name => name, :contract => contract,
326
                                                        :account => account),
327
            :quantity => (amount.nil? || amount.empty? || amount == 'Automatic') ? 0 : amount.to_i
328
          }
329
        end
330

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

    
337
      def update_existing(line)
338
        if !@existing[line[ORGANIZATION]]
339
          @existing[line[ORGANIZATION]] = true
340
          # Fetching all content hosts can be too slow and times so page
341
          # http://projects.theforeman.org/issues/6307
342
          total = @api.resource(:hosts).call(:index, {
343
              'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
344
              'per_page' => 1
345
          })['total'].to_i
346
          (total / 20 + 1).to_i.times do |page|
347
            @api.resource(:hosts).call(:index, {
348
                'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
349
                'page' => page + 1,
350
                'per_page' => 20
351
            })['results'].each do |host|
352
              if host['subscription_facet_attributes']
353
                @existing[host['name']] = host['id']
354
              end
355
            end
356
          end
357
        end
358
      end
359

    
360
      def iterate_hosts(csv)
361
        hypervisors = []
362
        hosts = []
363
        @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
364
          next if option_organization && organization['name'] != option_organization
365

    
366
          @api.resource(:hosts).call(:index, {
367
              'per_page' => 999999,
368
              'organization_id' => foreman_organization(:name => organization['name'])
369
          })['results'].each do |host|
370
            host = @api.resource(:hosts).call(:show, {
371
                'id' => host['id']
372
            })
373
            host['facts'] ||= {}
374
            if host['subscription_facet_attributes']['virtual_guests'].empty?
375
              hosts.push(host)
376
            else
377
              hypervisors.push(host)
378
            end
379
          end
380
        end
381
        hypervisors.each do |host|
382
          yield host
383
        end
384
        hosts.each do |host|
385
          yield host
386
        end
387
      end
388

    
389
      def shared_headers
390
        [NAME, ORGANIZATION, ENVIRONMENT, CONTENTVIEW, HOSTCOLLECTIONS, VIRTUAL, HOST,
391
         OPERATINGSYSTEM, ARCHITECTURE, SOCKETS, RAM, CORES, SLA, PRODUCTS]
392
      end
393

    
394
      def shared_columns(host)
395
        name = host['name']
396
        organization_name = host['organization_name']
397
        if host['content_facet_attributes']
398
          environment = host['content_facet_attributes']['lifecycle_environment']['name']
399
          contentview = host['content_facet_attributes']['content_view']['name']
400
          hostcollections = export_column(host['content_facet_attributes'], 'host_collections', 'name')
401
        else
402
          environment = nil
403
          contentview = nil
404
          hostcollections = nil
405
        end
406
        if host['subscription_facet_attributes']
407
          hypervisor_host = host['subscription_facet_attributes']['virtual_host'].nil? ? nil : host['subscription_facet_attributes']['virtual_host']['name']
408
          products = export_column(host['subscription_facet_attributes'], 'installed_products') do |product|
409
            "#{product['productId']}|#{product['productName']}"
410
          end
411
        else
412
          hypervisor_host = nil
413
          products = nil
414
        end
415
        virtual = host['facts']['virt::is_guest'] == 'true' ? 'Yes' : 'No'
416
        operatingsystem = host['facts']['distribution::name'] if host['facts']['distribution::name']
417
        operatingsystem += " #{host['facts']['distribution::version']}" if host['facts']['distribution::version']
418
        architecture = host['facts']['uname::machine']
419
        sockets = host['facts']['cpu::cpu_socket(s)']
420
        ram = host['facts']['memory::memtotal']
421
        cores = host['facts']['cpu::core(s)_per_socket'] || 1
422
        sla = ''
423

    
424
        [name, organization_name, environment, contentview, hostcollections, virtual, hypervisor_host,
425
         operatingsystem, architecture, sockets, ram, cores, sla, products]
426
      end
427

    
428
    end
429
  end
430
end