Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / subscriptions.rb @ 502df2e4

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
              if product['redhat']
24
                name = product['name']
25
                @api.resource(:repository_sets).call(:index, {
26
                    'per_page' => 999999,
27
                    'organization_id' => organization['id'],
28
                    'product_id' => product['id']
29
                })['results'].each do |repository_set|
30
                  content_set = repository_set['name']
31
                  repository_set['repositories'].each do |repository|
32
                    name_split = repository['name'].split(' ')
33
                    arch = name_split[-2]
34
                    release = name_split[-1]
35
                    csv << [name, organization['name'], nil, content_set, arch, release]
36
                  end
37
                end
38
              end
39
            end
40
          end
41
        end
42
      end
43

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

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

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

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

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

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

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

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

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

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