|
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 |
# -= Operating Systems CSV =-
|
|
26 |
#
|
|
27 |
# Columns
|
|
28 |
# Name
|
|
29 |
# - Operating 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 |
# Major
|
|
35 |
# Minor
|
|
36 |
# Family
|
|
37 |
#
|
|
38 |
|
|
39 |
require 'hammer_cli'
|
|
40 |
require 'katello_api'
|
|
41 |
require 'foreman_api'
|
|
42 |
require 'json'
|
|
43 |
require 'csv'
|
|
44 |
|
|
45 |
module HammerCLICsv
|
|
46 |
class OperatingSystemsCommand < BaseCommand
|
|
47 |
|
|
48 |
def initialize(*args)
|
|
49 |
super(args)
|
|
50 |
@operatingsystem_api = ForemanApi::Resources::OperatingSystem.new(@init_options[:foreman])
|
|
51 |
end
|
|
52 |
|
|
53 |
def execute
|
|
54 |
csv_export? ? export : import
|
|
55 |
|
|
56 |
HammerCLI::EX_OK
|
|
57 |
end
|
|
58 |
|
|
59 |
def export
|
|
60 |
CSV.open(csv_file, 'wb') do |csv|
|
|
61 |
csv << ['Name','Count','Major','Minor', 'Family']
|
|
62 |
@operatingsystem_api.index({}, HEADERS)[0].each do |operatingsystem|
|
|
63 |
name = operatingsystem['operatingsystem']['name']
|
|
64 |
count = 1
|
|
65 |
major = operatingsystem['operatingsystem']['major']
|
|
66 |
minor = operatingsystem['operatingsystem']['minor']
|
|
67 |
family = operatingsystem['operatingsystem']['family']
|
|
68 |
csv << [name, count, major, minor, family]
|
|
69 |
end
|
|
70 |
end
|
|
71 |
end
|
|
72 |
|
|
73 |
def import
|
|
74 |
@existing = {}
|
|
75 |
@operatingsystem_api.index[0].each do |operatingsystem|
|
|
76 |
operatingsystem = operatingsystem['operatingsystem']
|
|
77 |
@existing["#{operatingsystem['name']}-#{operatingsystem['major']}-#{operatingsystem['minor']}"] = operatingsystem['id']
|
|
78 |
end
|
|
79 |
|
|
80 |
thread_import do |line|
|
|
81 |
create_operatingsystems_from_csv(line)
|
|
82 |
end
|
|
83 |
end
|
|
84 |
|
|
85 |
def create_operatingsystems_from_csv(line)
|
|
86 |
details = parse_operatingsystem_csv(line)
|
|
87 |
|
|
88 |
details[:count].times do |number|
|
|
89 |
name = namify(details[:name_format], number)
|
|
90 |
if !@existing.include? "#{name}-#{details[:major]}-#{details[:minor]}"
|
|
91 |
print "Creating operating system '#{name}'..." if verbose?
|
|
92 |
@operatingsystem_api.create({
|
|
93 |
'operatingsystem' => {
|
|
94 |
'name' => name,
|
|
95 |
'major' => details[:major],
|
|
96 |
'minor' => details[:minor],
|
|
97 |
'family' => details[:family]
|
|
98 |
}
|
|
99 |
}, HEADERS)
|
|
100 |
print "done\n" if verbose?
|
|
101 |
else
|
|
102 |
print "Updating operatingsystem '#{name}'..." if verbose?
|
|
103 |
@operatingsystem_api.create({
|
|
104 |
'id' => @existing["#{name}-#{details[:major]}-#{details[:minor]}"],
|
|
105 |
'operatingsystem' => {
|
|
106 |
'name' => name,
|
|
107 |
'major' => details[:major],
|
|
108 |
'minor' => details[:minor],
|
|
109 |
'family' => details[:family]
|
|
110 |
}
|
|
111 |
}, HEADERS)
|
|
112 |
print "done\n" if verbose?
|
|
113 |
end
|
|
114 |
end
|
|
115 |
end
|
|
116 |
|
|
117 |
def parse_operatingsystem_csv(line)
|
|
118 |
keys = [:name_format, :count, :major, :minor, :family]
|
|
119 |
details = CSV.parse(line).map { |a| Hash[keys.zip(a)] }[0]
|
|
120 |
|
|
121 |
details[:count] = details[:count].to_i
|
|
122 |
|
|
123 |
details
|
|
124 |
end
|
|
125 |
end
|
|
126 |
|
|
127 |
HammerCLI::MainCommand.subcommand("csv:operatingsystems", "ping the katello server", HammerCLICsv::OperatingSystemsCommand)
|
|
128 |
end
|
foreman-os - out/in/update works