hammer-cli-csv / lib / hammer_cli_csv / partition_tables.rb @ 561a8ac9
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 |
# -= Partition Tables CSV =-
|
14 |
#
|
15 |
# Columns
|
16 |
# Name
|
17 |
# - Partition table 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 |
#
|
23 |
|
24 |
require 'hammer_cli'
|
25 |
require 'json'
|
26 |
require 'csv'
|
27 |
|
28 |
module HammerCLICsv |
29 |
class CsvCommand |
30 |
class PartitionTablesCommand < BaseCommand |
31 |
command_name 'partition-tables'
|
32 |
desc 'import or export partition tables'
|
33 |
|
34 |
OSFAMILY = 'OS Family' |
35 |
OPERATINGSYSTEMS = 'Operating Systems' |
36 |
LAYOUT = 'Layout' |
37 |
|
38 |
def export |
39 |
CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| |
40 |
csv << [NAME, COUNT, OSFAMILY, OPERATINGSYSTEMS, LAYOUT] |
41 |
@api.resource(:ptables).call(:index, {:per_page => 999999})['results'].each do |ptable| |
42 |
ptable = @api.resource(:ptables).call(:show, {'id' => ptable['id']}) |
43 |
name = ptable['name']
|
44 |
count = 1
|
45 |
osfamily = ptable['os_family']
|
46 |
layout = ptable['layout']
|
47 |
csv << [name, count, osfamily, layout] |
48 |
end
|
49 |
end
|
50 |
end
|
51 |
|
52 |
def import |
53 |
@existing = {}
|
54 |
@api.resource(:ptables).call(:index, {:per_page => 999999})['results'].each do |ptable| |
55 |
@existing[ptable['name']] = ptable['id'] if ptable |
56 |
end
|
57 |
|
58 |
thread_import do |line|
|
59 |
create_ptables_from_csv(line) |
60 |
end
|
61 |
end
|
62 |
|
63 |
def create_ptables_from_csv(line) |
64 |
line[COUNT].to_i.times do |number| |
65 |
name = namify(line[NAME], number)
|
66 |
operatingsystem_ids = CSV.parse_line(line[OPERATINGSYSTEMS]).collect do |operatingsystem_name| |
67 |
foreman_operatingsystem(:name => operatingsystem_name)
|
68 |
end if line[OPERATINGSYSTEMS] |
69 |
if !@existing.include? name |
70 |
print "Creating ptable '#{name}'... " if option_verbose? |
71 |
@api.resource(:ptables).call(:create, { |
72 |
'ptable' => {
|
73 |
'name' => name,
|
74 |
'os_family' => line[OSFAMILY], |
75 |
'operatingsystem_ids' => operatingsystem_ids,
|
76 |
'layout' => line[LAYOUT] |
77 |
} |
78 |
}) |
79 |
else
|
80 |
print "Updating ptable '#{name}'..." if option_verbose? |
81 |
@api.resource(:ptables).call(:update, { |
82 |
'id' => @existing[name], |
83 |
'ptable' => {
|
84 |
'name' => name,
|
85 |
'os_family' => line[OSFAMILY], |
86 |
'operatingsystem_ids' => operatingsystem_ids,
|
87 |
'layout' => line[LAYOUT] |
88 |
} |
89 |
}) |
90 |
end
|
91 |
print "done\n" if option_verbose? |
92 |
end
|
93 |
rescue RuntimeError => e |
94 |
raise "#{e}\n #{line}"
|
95 |
end
|
96 |
end
|
97 |
end
|
98 |
end
|