Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / subscriptions.rb @ ae7775e3

1
module HammerCLICsv
2
  class CsvCommand
3
    class SubscriptionsCommand < BaseCommand
4
      command_name 'subscriptions'
5
      desc         'import or export subscriptions'
6

    
7
      ORGANIZATION = 'Organization'
8
      MANIFEST = 'Manifest File'
9
      CONTENT_SET = 'Content Set'
10
      ARCH = 'Arch'
11
      RELEASE = 'Release'
12

    
13
      def export
14
        CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
15
          csv << [NAME, ORGANIZATION, MANIFEST, CONTENT_SET, ARCH, RELEASE]
16
          @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
17
            next if option_organization && organization['name'] != option_organization
18
            @api.resource(:products).call(:index, {
19
                'per_page' => 999999,
20
                'organization_id' => organization['id'],
21
                'enabled' => true
22
            })['results'].each do |product|
23
              product = @api.resource(:products).call(:show, {:id => product['id']})
24
              if product['redhat']
25
                name = product['name']
26
                @api.resource(:repository_sets).call(:index, {
27
                    'per_page' => 999999,
28
                    'organization_id' => organization['id'],
29
                    'product_id' => product['id']
30
                })['results'].each do |repository_set|
31
                  content_set = repository_set['name']
32
                  repository_set['repositories'].each do |repository|
33
                    name_split = repository['name'].split(' ')
34
                    arch = name_split[-2]
35
                    release = name_split[-1]
36
                    csv << [name, organization['name'], nil, content_set, arch, release]
37
                  end
38
                end
39
              end
40
            end
41
          end
42
        end
43
      end
44

    
45
      def import
46
        thread_import do |line|
47
          if line[MANIFEST] && !line[MANIFEST].empty?
48
            import_manifest_from_csv(line)
49
          else
50
            enable_products_from_csv(line)
51
          end
52
        end
53
      end
54

    
55
      # FIXME: TODO remove this rubocop
56
      # rubocop:disable CyclomaticComplexity
57
      def enable_products_from_csv(line)
58
        organization = line[ORGANIZATION] || option_organization
59
        raise "Organization is required in either input CSV or by option --organization" if organization.nil? || organization.empty?
60
        line[ORGANIZATION] = organization
61
        return if option_organization && line[ORGANIZATION] != option_organization
62

    
63
        results = @api.resource(:products).call(:index, {
64
            'per_page' => 999999,
65
            'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
66
            'name' => line[NAME]
67
        })['results']
68
        raise "No match for product '#{line[NAME]}'" if results.length == 0
69
        raise "Multiple matches for product '#{line[NAME]}'" if results.length != 1
70
        product = @api.resource(:products).call(:show, {'id' => results[0]['id']})
71

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

    
82
        repository = repository_set['repositories'].find do |repo|
83
          if line[RELEASE].nil? || line[RELEASE].empty?
84
            repo['name'].end_with?("#{line[ARCH]}")
85
          else
86
            repo['name'].end_with?("#{line[ARCH]} #{line[RELEASE]}")
87
          end
88
        end
89

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

    
97
          params = {
98
              'id' => product_content['content']['id'],
99
              'product_id' => product['id'],
100
              'basearch' => line[ARCH]
101
          }
102
          params['releasever'] = line[RELEASE] unless line[RELEASE].nil? || line[RELEASE].empty?
103

    
104
          @api.resource(:repository_sets).call(:enable, params)
105
          puts 'done' if option_verbose?
106
        else
107
          puts "Repository #{repository['name']} already enabled" if option_verbose?
108
        end
109
      end
110

    
111
      def import_manifest_from_csv(line)
112
        return if option_organization && line[ORGANIZATION] != option_organization
113
        args = %W{
114
          --server #{ @server } --username #{ @username } --password #{ @password }
115
          subscription upload --file #{ line[MANIFEST] }
116
          --organization-id #{ foreman_organization(:name => line[ORGANIZATION]) }
117
        }
118
        hammer.run(args)
119

    
120
      rescue RuntimeError => e
121
        raise "#{e}\n       #{line}"
122
      end
123
    end
124
  end
125
end