Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / systems.rb @ 9d9e4494

1
# Copyright (c) 2013 Red Hat
2
#
3
# MIT License
4
#
5
# Permission is hereby granted, free of charge, to any person obtaining
6
# a copy of this software and associated documentation files (the
7
# "Software"), to deal in the Software without restriction, including
8
# without limitation the rights to use, copy, modify, merge, publish,
9
# distribute, sublicense, and/or sell copies of the Software, and to
10
# permit persons to whom the Software is furnished to do so, subject to
11
# the following conditions:
12
#
13
# The above copyright notice and this permission notice shall be
14
# included in all copies or substantial portions of the Software.
15
#
16
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
#
24
#
25
# -= Systems CSV =-
26
#
27
# Columns
28
#   Name
29
#     - System name
30
#     - May contain '%d' which will be replaced with current iteration number of Count
31
#     - eg. "os%d" -> "os1"
32
#   Count
33
#     - Number of times to iterate on this line of the CSV file
34
#   MAC Address
35
#     - MAC address
36
#     - May contain '%d' which will be replaced with current iteration number of Count
37
#     - eg. "FF:FF:FF:FF:FF:%02x" -> "FF:FF:FF:FF:FF:0A"
38
#     - Warning: be sure to keep count below 255 or MAC hex will exceed limit
39
#
40

    
41
require 'hammer_cli'
42
require 'katello_api'
43
require 'foreman_api'
44
require 'json'
45
require 'csv'
46
require 'uri'
47

    
48
module HammerCLICsv
49
  class SystemsCommand < BaseCommand
50

    
51
    ORGANIZATION = 'Organization'
52
    ENVIRONMENT = 'Environment'
53
    CONTENTVIEW = 'Content View'
54
    VIRTUAL = 'Virtual'
55
    HOST = 'Host'
56
    OPERATINGSYSTEM = 'OS'
57
    ARCHITECTURE = 'Arch'
58
    SOCKETS = 'Sockets'
59
    RAM = 'RAM'
60
    CORES = 'Cores'
61
    SLA = 'SLA'
62
    PRODUCTS = 'Products'
63
    SUBSCRIPTIONS = 'Subscriptions'
64

    
65
    def export
66
      # TODO
67
    end
68

    
69
    def import
70
      @existing = {}
71

    
72
      thread_import do |line|
73
        create_systems_from_csv(line)
74
      end
75
    end
76

    
77
    def create_systems_from_csv(line)
78
      if !@existing[line[ORGANIZATION]]
79
        @existing[line[ORGANIZATION]] = {}
80
        @k_system_api.index({'organization_id' => line[ORGANIZATION], 'page_size' => 999999, 'paged' => true})[0]['results'].each do |system|
81
          @existing[line[ORGANIZATION]][system['name']] = system['uuid'] if system
82
        end
83
      end
84

    
85
      line[COUNT].to_i.times do |number|
86
        name = namify(line[NAME], number)
87

    
88
        subscriptions(line).each do |subscription|
89
          puts subscription
90
          katello_subscription(line[ORGANIZATION], :name => subscription[:number])
91
        end
92

    
93
        if !@existing[line[ORGANIZATION]].include? name
94
          print "Creating system '#{name}'..." if verbose?
95
          @k_system_api.create({
96
                                 'name' => name,
97
                                 'organization_id' => line[ORGANIZATION],
98
                                 'environment_id' => katello_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
99
                                 'content_view_id' => 2, # TODO: katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
100
                                 'facts' => facts(line),
101
                                 'installedProducts' => products(line),
102
                                 'type' => 'system'
103
                               })
104
        else
105
          print "Updating host '#{name}'..." if verbose?
106
          @k_system_api.update({
107
                                 'id' => @existing[line[ORGANIZATION]][name],
108
                                 'name' => name,
109
                                 'organization_id' => line[ORGANIZATION],
110
                                 'environment_id' => katello_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
111
                                 'content_view_id' => 2, # TODO: katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
112
                                 'facts' => facts(line),
113
                                 'installedProducts' => products(line)
114
                               })
115
        end
116
        print "done\n" if verbose?
117
      end
118
    rescue RuntimeError => e
119
      raise RuntimeError.new("#{e}\n       #{line}")
120
    end
121

    
122
    private
123

    
124
    def facts(line)
125
      facts = {}
126
      facts['cpu.core(s)_per_socket'] = line[CORES]
127
      facts['cpu.cpu_socket(s)'] = line[SOCKETS]
128
      facts['memory.memtotal'] = line[RAM]
129
      facts['uname.machine'] = line[ARCHITECTURE]
130
      if line[OPERATINGSYSTEM].index(' ')
131
        (facts['distribution.name'], facts['distribution.version']) = line[OPERATINGSYSTEM].split(' ')
132
      else
133
        (facts['distribution.name'], facts['distribution.version']) = ['RHEL', line[OPERATINGSYSTEM]]
134
      end
135
      facts
136
    end
137

    
138
    def products(line)
139
      products = CSV.parse_line(line[PRODUCTS]).collect do |product_details|
140
        product = {}
141
        (product[:product_id], product[:productName]) = product_details.split('|')
142
        product
143
      end
144
      products
145
    end
146

    
147
    def subscriptions(line)
148
      subscriptions = CSV.parse_line(line[SUBSCRIPTIONS]).collect do |subscription_details|
149
        subscription = {}
150
        (subscription[:number], subscription[:name]) = subscription_details.split('|')
151
        subscription
152
      end
153
      subscriptions
154
    end
155

    
156
  end
157

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