Class: HammerCLICsv::CsvCommand::SubscriptionsCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/hammer_cli_csv/subscriptions.rb

Constant Summary collapse

ORGANIZATION =
'Organization'
MANIFEST =
'Manifest File'
CONTENT_SET =
'Content Set'
ARCH =
'Arch'
RELEASE =
'Release'

Constants inherited from BaseCommand

BaseCommand::COUNT, BaseCommand::NAME

Instance Method Summary collapse

Methods inherited from BaseCommand

#associate_locations, #associate_organizations, #build_os_name, #check_server_status, #collect_column, #execute, #export_column, #foreman_architecture, #foreman_domain, #foreman_environment, #foreman_filter, #foreman_location, #foreman_operatingsystem, #foreman_organization, #foreman_partitiontable, #foreman_permission, #foreman_role, #foreman_template_kind, #hammer, #hammer_context, #katello_contentview, #katello_hostcollection, #katello_repository, #katello_subscription, #labelize, #lifecycle_environment, #namify, #pluralize, #split_os_name, #thread_import

Instance Method Details

#enable_products_from_csv(line) ⇒ Object



81
82
83
84
85
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
# File 'lib/hammer_cli_csv/subscriptions.rb', line 81

def enable_products_from_csv(line)
  results = @api.resource(:products).call(:index, {
                                            'per_page' => 999999,
                                            'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
                                            'name' => line[NAME]
                                          })['results']
  raise "No match for product '#{line[NAME]}'" if results.length == 0
  raise "Multiple matches for product '#{line[NAME]}'" if results.length != 1
  product = results[0]

  results = @api.resource(:repository_sets).call(:index, {
                                                   'per_page' => 999999,
                                                   'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
                                                   'product_id' => product['id'],
                                                   'name' => line[CONTENT_SET]
                                                 })['results']
  raise "No match for content set '#{line[CONTENT_SET]}'" if results.length == 0
  raise "Multiple matches for content set '#{line[CONTENT_SET]}'" if results.length != 1
  repository_set = results[0]

  repository = repository_set['repositories'].find do |repo|
    repo['name'].end_with?("#{line[ARCH]} #{line[RELEASE]}")
  end

  if repository.nil?
    print "Enabling repository #{line[CONTENT_SET]} #{line[ARCH]} #{line[RELEASE]}..." if option_verbose?
    product_content = product['product_content'].find do |content|
      content['content']['name'] == line[CONTENT_SET]
    end
    raise "No match for content set '#{line[CONTENT_SET]}'" if !product_content

    @api.resource(:repository_sets).call(:enable, {
                                           'id' => product_content['content']['id'],
                                           'product_id' => product['id'],
                                           'basearch' => line[ARCH],
                                           'releasever' => line[RELEASE]
                                         })
    puts 'done' if option_verbose?
  else
    puts "Repository #{repository['name']} already enabled" if option_verbose?
  end
end

#exportObject



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
# File 'lib/hammer_cli_csv/subscriptions.rb', line 40

def export
  CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
    csv << [NAME, COUNT, ORGANIZATION, MANIFEST, CONTENT_SET, ARCH, RELEASE]
    @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
      @api.resource(:products).call(:index, {
                                      'per_page' => 999999,
                                      'organization_id' => foreman_organization(:name => organization['name']),
                                      'enabled' => true
                                    })['results'].each do |product|
        if product['provider']['name'] == 'Red Hat'
          product['product_content'].each do |product_content|
            if product_content['enabled']
              puts product_content
              content_set = product_content['content']['name']
              release = '?????'
              arches = product_content['content']['arches']
              if arches.nil?
                csv << [product['name'], 1, organization['name'], nil, content_set, nil, release]
              else
                arches.split(',').each do |arch|
                  csv << [product['name'], 1, organization['name'], nil, content_set, arch, release]
                end
              end
            end
          end
        end
      end
    end
  end
end

#importObject



71
72
73
74
75
76
77
78
79
# File 'lib/hammer_cli_csv/subscriptions.rb', line 71

def import
  thread_import do |line|
    if line[MANIFEST] && !line[MANIFEST].empty?
      import_manifest_from_csv(line)
    else
      enable_products_from_csv(line)
    end
  end
end

#import_manifest_from_csv(line) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/hammer_cli_csv/subscriptions.rb', line 124

def import_manifest_from_csv(line)
  # TODO: --server needs to come from config/settings
  args = %W{ subscription upload --file #{ line[MANIFEST] }
             --organization-id #{ foreman_organization(:name => line[ORGANIZATION]) } }
  hammer.run(args)

rescue RuntimeError => e
  raise "#{e}\n       #{line}"
end