Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / puppet_reports.rb @ d76bdbc0

1
module HammerCLICsv
2
  class CsvCommand
3
    class PuppetReportsCommand < BaseCommand
4
      command_name 'puppet-reports'
5
      desc         'import or export puppet reports'
6

    
7
      ORGANIZATION = 'Organization'
8
      ENVIRONMENT = 'Environment'
9
      CONTENTVIEW = 'Content View'
10
      SYSTEMGROUPS = 'System Groups'
11
      VIRTUAL = 'Virtual'
12
      HOST = 'Host'
13
      OPERATINGSYSTEM = 'OS'
14
      ARCHITECTURE = 'Arch'
15
      SOCKETS = 'Sockets'
16
      RAM = 'RAM'
17
      CORES = 'Cores'
18
      SLA = 'SLA'
19
      PRODUCTS = 'Products'
20
      SUBSCRIPTIONS = 'Subscriptions'
21

    
22
      def export(csv)
23
        csv << [NAME, ORGANIZATION, ENVIRONMENT, CONTENTVIEW, SYSTEMGROUPS, VIRTUAL, HOST,
24
                OPERATINGSYSTEM, ARCHITECTURE, SOCKETS, RAM, CORES, SLA, PRODUCTS, SUBSCRIPTIONS]
25
        @api.resource(:organizations).call(:index, {
26
            :per_page => 999999
27
        })['results'].each do |organization|
28
          @api.resource(:systems).call(:index, {
29
              'per_page' => 999999,
30
              'organization_id' => organization['id']
31
          })['results'].each do |system|
32
            system = @api.resource(:systems).call(:show, {
33
                'id' => system['uuid'],
34
                'fields' => 'full'
35
            })
36

    
37
            name = system['name']
38
            organization_label = organization['label']
39
            environment = system['environment']['label']
40
            contentview = system['content_view']['name']
41
            hostcollections = CSV.generate do |column|
42
              column << system['systemGroups'].collect do |hostcollection|
43
                hostcollection['name']
44
              end
45
            end
46
            hostcollections.delete!("\n")
47
            virtual = system['facts']['virt.is_guest'] == 'true' ? 'Yes' : 'No'
48
            host = system['host']
49
            operatingsystem = "#{system['facts']['distribution.name']} " if system['facts']['distribution.name']
50
            operatingsystem += system['facts']['distribution.version'] if system['facts']['distribution.version']
51
            architecture = system['facts']['uname.machine']
52
            sockets = system['facts']['cpu.cpu_socket(s)']
53
            ram = system['facts']['memory.memtotal']
54
            cores = system['facts']['cpu.core(s)_per_socket']
55
            sla = ''
56
            products = CSV.generate do |column|
57
              column << system['installedProducts'].collect do |product|
58
                "#{product['productId']}|#{product['productName']}"
59
              end
60
            end
61
            products.delete!("\n")
62
            subscriptions = CSV.generate do |column|
63
              column << @api.resource(:subscriptions).call(:index, {
64
                  'system_id' => system['uuid']
65
              })['results'].collect do |subscription|
66
                "#{subscription['product_id']}|#{subscription['product_name']}"
67
              end
68
            end
69
            subscriptions.delete!("\n")
70
            csv << [name, organization_label, environment, contentview, hostcollections, virtual, host,
71
                    operatingsystem, architecture, sockets, ram, cores, sla, products, subscriptions]
72
          end
73
        end
74
      end
75

    
76
      def import
77
        @existing = {}
78
        @host_guests = {}
79

    
80
        thread_import do |line|
81
          create_systems_from_csv(line)
82
        end
83

    
84
        print 'Updating host and guest associations...' if option_verbose?
85
        @host_guests.each do |host_id, guest_ids|
86
          @api.resource(:systems).call(:update, {
87
              'id' => host_id,
88
              'guest_ids' => guest_ids
89
          })
90
        end
91
        puts 'done' if option_verbose?
92
      end
93

    
94
      def create_systems_from_csv(line)
95
        if !@existing[line[ORGANIZATION]]
96
          @existing[line[ORGANIZATION]] = {}
97
          @api.resource(:systems).call(:index, {
98
              'organization_id' => line[ORGANIZATION],
99
              'per_page' => 999999
100
          })['results'].each do |system|
101
            @existing[line[ORGANIZATION]][system['name']] = system['uuid'] if system
102
          end
103
        end
104

    
105
        count(line[COUNT]).times do |number|
106
          name = namify(line[NAME], number)
107

    
108
          # TODO: w/ @daviddavis p-r
109
          #subscriptions(line).each do |subscription|
110
          #  get_subscription(line[ORGANIZATION], :name => subscription[:number])
111
          #end
112

    
113
          if !@existing[line[ORGANIZATION]].include? name
114
            print "Creating system '#{name}'..." if option_verbose?
115
            system_id = @api.resource(:systems).call(:create, {
116
                'name' => name,
117
                'organization_id' => line[ORGANIZATION],
118
                'environment_id' => lifecycle_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
119
                'content_view_id' => lifecycle_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
120
                'facts' => facts(line),
121
                'installed_products' => products(line),
122
                'type' => 'system'
123
            })['uuid']
124
            @existing[line[ORGANIZATION]][name] = system_id
125
          else
126
            print "Updating system '#{name}'..." if option_verbose?
127
            puts line
128
            system_id = @api.resource(:systems).call(:update, {
129
                'id' => @existing[line[ORGANIZATION]][name],
130
                'name' => name,
131
                'environment_id' => katello_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
132
                'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
133
                'facts' => facts(line),
134
                'installed_products' => products(line)
135
            })['uuid']
136
          end
137

    
138
          if line[VIRTUAL] == 'Yes' && line[HOST]
139
            raise "Host system '#{line[HOST]}' not found" if !@existing[line[ORGANIZATION]][line[HOST]]
140
            @host_guests[@existing[line[ORGANIZATION]][line[HOST]]] ||= []
141
            @host_guests[@existing[line[ORGANIZATION]][line[HOST]]] << system_id
142
          end
143

    
144
          set_host_collections(system_id, line)
145

    
146
          puts 'done' if option_verbose?
147
        end
148
      end
149

    
150
      private
151

    
152
      def facts(line)
153
        facts = {}
154
        facts['cpu.core(s)_per_socket'] = line[CORES]
155
        facts['cpu.cpu_socket(s)'] = line[SOCKETS]
156
        facts['memory.memtotal'] = line[RAM]
157
        facts['uname.machine'] = line[ARCHITECTURE]
158
        if line[OPERATINGSYSTEM].index(' ')
159
          (facts['distribution.name'], facts['distribution.version']) = line[OPERATINGSYSTEM].split(' ')
160
        else
161
          (facts['distribution.name'], facts['distribution.version']) = ['RHEL', line[OPERATINGSYSTEM]]
162
        end
163
        facts['virt.is_guest'] = line[VIRTUAL] == 'Yes' ? true : false
164
        facts
165
      end
166

    
167
      def set_host_collections(system_id, line)
168
        CSV.parse_line(line[SYSTEMGROUPS]).each do |hostcollection_name|
169
          @api.resource(:hostcollections).call(:add_systems, {
170
              'id' => katello_hostcollection(line[ORGANIZATION], :name => hostcollection_name),
171
              'system_ids' => [system_id]
172
          })
173
        end
174
      end
175

    
176
      def products(line)
177
        products = CSV.parse_line(line[PRODUCTS]).collect do |product_details|
178
          product = {}
179
          # TODO: these get passed straight through to candlepin; probably would be better to process in server
180
          #       to allow underscore product_id here
181
          (product['productId'], product['productName']) = product_details.split('|')
182
          product
183
        end
184
        products
185
      end
186

    
187
      def subscriptions(line)
188
        subscriptions = CSV.parse_line(line[SUBSCRIPTIONS]).collect do |subscription_details|
189
          subscription = {}
190
          (subscription[:number], subscription[:name]) = subscription_details.split('|')
191
          subscription
192
        end
193
        subscriptions
194
      end
195
    end
196
  end
197
end