hammer-cli-csv / lib / hammer_cli_csv / operating_systems.rb @ 370106bb
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 |
operatingsystem = operatingsystem['operatingsystem']
|
64 |
name = operatingsystem['name']
|
65 |
count = 1
|
66 |
major = operatingsystem['major']
|
67 |
minor = operatingsystem['minor']
|
68 |
family = operatingsystem['family']
|
69 |
csv << [name, count, major, minor, family] |
70 |
end
|
71 |
end
|
72 |
end
|
73 |
|
74 |
def import |
75 |
@existing = {}
|
76 |
@operatingsystem_api.index[0].each do |operatingsystem| |
77 |
operatingsystem = operatingsystem['operatingsystem']
|
78 |
@existing["#{operatingsystem['name']}-#{operatingsystem['major']}-#{operatingsystem['minor']}"] = operatingsystem['id'] |
79 |
end
|
80 |
|
81 |
thread_import do |line|
|
82 |
create_operatingsystems_from_csv(line) |
83 |
end
|
84 |
end
|
85 |
|
86 |
def create_operatingsystems_from_csv(line) |
87 |
details = parse_operatingsystem_csv(line) |
88 |
|
89 |
details[:count].times do |number| |
90 |
name = namify(details[:name_format], number)
|
91 |
if !@existing.include? "#{name}-#{details[:major]}-#{details[:minor]}" |
92 |
print "Creating operating system '#{name}'..." if verbose? |
93 |
@operatingsystem_api.create({
|
94 |
'operatingsystem' => {
|
95 |
'name' => name,
|
96 |
'major' => details[:major], |
97 |
'minor' => details[:minor], |
98 |
'family' => details[:family] |
99 |
} |
100 |
}, HEADERS)
|
101 |
print "done\n" if verbose? |
102 |
else
|
103 |
print "Updating operatingsystem '#{name}'..." if verbose? |
104 |
@operatingsystem_api.create({
|
105 |
'id' => @existing["#{name}-#{details[:major]}-#{details[:minor]}"], |
106 |
'operatingsystem' => {
|
107 |
'name' => name,
|
108 |
'major' => details[:major], |
109 |
'minor' => details[:minor], |
110 |
'family' => details[:family] |
111 |
} |
112 |
}, HEADERS)
|
113 |
print "done\n" if verbose? |
114 |
end
|
115 |
end
|
116 |
end
|
117 |
|
118 |
def parse_operatingsystem_csv(line) |
119 |
keys = [:name_format, :count, :major, :minor, :family] |
120 |
details = CSV.parse(line).map { |a| Hash[keys.zip(a)] }[0] |
121 |
|
122 |
details[:count] = details[:count].to_i |
123 |
|
124 |
details |
125 |
end
|
126 |
end
|
127 |
|
128 |
HammerCLI::MainCommand.subcommand("csv:operatingsystems", "ping the katello server", HammerCLICsv::OperatingSystemsCommand) |
129 |
end
|