Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / systems.rb @ bf81b9cf

1
# Copyright 2013-2014 Red Hat, Inc.
2
#
3
# This software is licensed to you under the GNU General Public
4
# License as published by the Free Software Foundation; either version
5
# 2 of the License (GPLv2) or (at your option) any later version.
6
# There is NO WARRANTY for this software, express or implied,
7
# including the implied warranties of MERCHANTABILITY,
8
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
9
# have received a copy of GPLv2 along with this software; if not, see
10
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
11

    
12
#
13
# -= Systems CSV =-
14
#
15
# Columns
16
#   Name
17
#     - System name
18
#     - May contain '%d' which will be replaced with current iteration number of Count
19
#     - eg. "os%d" -> "os1"
20
#   Count
21
#     - Number of times to iterate on this line of the CSV file
22
#   MAC Address
23
#     - MAC address
24
#     - May contain '%d' which will be replaced with current iteration number of Count
25
#     - eg. "FF:FF:FF:FF:FF:%02x" -> "FF:FF:FF:FF:FF:0A"
26
#     - Warning: be sure to keep count below 255 or MAC hex will exceed limit
27
#
28

    
29
require 'hammer_cli'
30
require 'json'
31
require 'csv'
32
require 'uri'
33

    
34
module HammerCLICsv
35
  class SystemsCommand < BaseCommand
36

    
37
    ORGANIZATION = 'Organization'
38
    ENVIRONMENT = 'Environment'
39
    CONTENTVIEW = 'Content View'
40
    SYSTEMGROUPS = 'System Groups'
41
    VIRTUAL = 'Virtual'
42
    HOST = 'Host'
43
    OPERATINGSYSTEM = 'OS'
44
    ARCHITECTURE = 'Arch'
45
    SOCKETS = 'Sockets'
46
    RAM = 'RAM'
47
    CORES = 'Cores'
48
    SLA = 'SLA'
49
    PRODUCTS = 'Products'
50
    SUBSCRIPTIONS = 'Subscriptions'
51

    
52
    def export
53
      CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
54
        csv << [NAME, COUNT, ORGANIZATION, ENVIRONMENT, CONTENTVIEW, SYSTEMGROUPS, VIRTUAL, HOST,
55
               OPERATINGSYSTEM, ARCHITECTURE, SOCKETS, RAM, CORES, SLA, PRODUCTS, SUBSCRIPTIONS]
56
        @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
57
          @api.resource(:systems).call(:index, {
58
                                'per_page' => 999999,
59
                                'organization_id' => katello_organization(:name => organization['name'])
60
                               })['results'].each do |system|
61
            system = @api.resource(:systems).call(:show, {
62
                                          'id' => system['uuid'],
63
                                          'fields' => 'full'
64
                                        })
65

    
66
            name = system['name']
67
            count = 1
68
            organization_name = organization['name']
69
            environment = system['environment']['label']
70
            contentview = system['content_view']['name']
71
            systemgroups = CSV.generate do |column|
72
              column << system['systemGroups'].collect do |systemgroup|
73
                systemgroup['name']
74
              end
75
            end.delete!("\n")
76
            virtual = system['facts']['virt.is_guest'] == 'true' ? 'Yes' : 'No'
77
            host = system['host']
78
            operatingsystem = "#{system['facts']['distribution.name']} " if system['facts']['distribution.name']
79
            operatingsystem += system['facts']['distribution.version'] if system['facts']['distribution.version']
80
            architecture = system['facts']['uname.machine']
81
            sockets = system['facts']['cpu.cpu_socket(s)']
82
            ram = system['facts']['memory.memtotal']
83
            cores = system['facts']['cpu.core(s)_per_socket']
84
            sla = ""
85
            products = CSV.generate do |column|
86
              column << system['installedProducts'].collect do |product|
87
                "#{product['productId']}|#{product['productName']}"
88
              end
89
            end.delete!("\n")
90
            subscriptions = CSV.generate do |column|
91
              column << @api.resource(:subscriptions).call(:index, {
92
                                                    'system_id' => system['uuid']
93
                                                  })['results'].collect do |subscription|
94
                "#{subscription['product_id']}|#{subscription['product_name']}"
95
              end
96
            end.delete!("\n")
97
            csv << [name, count, organization_name, environment, contentview, systemgroups, virtual, host,
98
                    operatingsystem, architecture, sockets, ram, cores, sla, products, subscriptions]
99
          end
100
        end
101
      end
102
    end
103

    
104
    def import
105
      @existing = {}
106
      @host_guests = {}
107

    
108
      thread_import do |line|
109
        create_systems_from_csv(line)
110
      end
111

    
112
      print "Updating host and guest associations..." if option_verbose?
113
      @host_guests.each do |host_id, guest_ids|
114
        @api.resource(:systems).call(:update, {
115
                               'id' => host_id,
116
                               'guest_ids' => guest_ids
117
                             })
118
      end
119
      puts "done" if option_verbose?
120
    end
121

    
122
    def create_systems_from_csv(line)
123
      if !@existing[line[ORGANIZATION]]
124
        @existing[line[ORGANIZATION]] = {}
125
        @api.resource(:systems).call(:index, {
126
                              'organization_id' => katello_organization(:name => line[ORGANIZATION]),
127
                              'page_size' => 999999})['results'].each do |system|
128
          @existing[line[ORGANIZATION]][system['name']] = system['uuid'] if system
129
        end
130
      end
131

    
132
      line[COUNT].to_i.times do |number|
133
        name = namify(line[NAME], number)
134

    
135
        # TODO w/ @daviddavis p-r
136
        #subscriptions(line).each do |subscription|
137
        #  katello_subscription(line[ORGANIZATION], :name => subscription[:number])
138
        #end
139

    
140
        if !@existing[line[ORGANIZATION]].include? name
141
          print "Creating system '#{name}'..." if option_verbose?
142
          system_id = @api.resource(:systems).call(:create, {
143
                                                     'name' => name,
144
                                                     'organization_id' => katello_organization(:name => line[ORGANIZATION]),
145
                                                     'environment_id' => katello_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
146
                                                     'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
147
                                                     'facts' => facts(line),
148
                                                     'installed_products' => products(line),
149
                                                     'type' => 'system'
150
                                                   })['uuid']
151
          @existing[line[ORGANIZATION]][name] = system_id
152
        else
153
          print "Updating system '#{name}'..." if option_verbose?
154
          system_id = @api.resource(:systems).call(:update, {
155
                                                     'id' => @existing[line[ORGANIZATION]][name],
156
                                                     'system' => {
157
                                                       'name' => name,
158
                                                       'environment_id' => katello_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
159
                                                       'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
160
                                                       'facts' => facts(line),
161
                                                       'installed_products' => products(line)
162
                                                     }
163
                                                   })['uuid']
164
        end
165

    
166
        if line[VIRTUAL] == 'Yes' && line[HOST]
167
          raise "Host system '#{line[HOST]}' not found" if !@existing[line[ORGANIZATION]][line[HOST]]
168
          @host_guests[@existing[line[ORGANIZATION]][line[HOST]]] ||= []
169
          @host_guests[@existing[line[ORGANIZATION]][line[HOST]]] << system_id
170
        end
171

    
172
        set_system_groups(system_id, line)
173

    
174
        puts "done" if option_verbose?
175
      end
176
    rescue RuntimeError => e
177
      raise "#{e}\n       #{line}"
178
    end
179

    
180
    private
181

    
182
    def facts(line)
183
      facts = {}
184
      facts['cpu.core(s)_per_socket'] = line[CORES]
185
      facts['cpu.cpu_socket(s)'] = line[SOCKETS]
186
      facts['memory.memtotal'] = line[RAM]
187
      facts['uname.machine'] = line[ARCHITECTURE]
188
      if line[OPERATINGSYSTEM].index(' ')
189
        (facts['distribution.name'], facts['distribution.version']) = line[OPERATINGSYSTEM].split(' ')
190
      else
191
        (facts['distribution.name'], facts['distribution.version']) = ['RHEL', line[OPERATINGSYSTEM]]
192
      end
193
      facts['virt.is_guest'] = line[VIRTUAL] == 'Yes' ? true : false
194
      facts
195
    end
196

    
197
    def set_system_groups(system_id, line)
198
      CSV.parse_line(line[SYSTEMGROUPS]).each do |systemgroup_name|
199
        @api.resource(:system_groups).call(:add_systems, {
200
                                         'id' => katello_systemgroup(line[ORGANIZATION], :name => systemgroup_name),
201
                                         'system_ids' => [system_id]
202
                                       })
203
      end
204
    end
205

    
206
    def products(line)
207
      products = CSV.parse_line(line[PRODUCTS]).collect do |product_details|
208
        product = {}
209
        # TODO: these get passed straight through to candlepin; probably would be better to process in server
210
        #       to allow underscore product_id here
211
        (product['productId'], product['productName']) = product_details.split('|')
212
        product
213
      end
214
      products
215
    end
216

    
217
    def subscriptions(line)
218
      subscriptions = CSV.parse_line(line[SUBSCRIPTIONS]).collect do |subscription_details|
219
        subscription = {}
220
        (subscription[:number], subscription[:name]) = subscription_details.split('|')
221
        subscription
222
      end
223
      subscriptions
224
    end
225

    
226
  end
227

    
228
  HammerCLI::MainCommand.subcommand("csv:systems", "import/export systems", HammerCLICsv::SystemsCommand)
229
end