hammer-cli-csv / lib / hammer_cli_csv / hosts.rb @ bfc065ce
1 |
# Copyright (c) 2013-2014 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 |
# -= Hosts CSV =-
|
26 |
#
|
27 |
# Columns
|
28 |
# Name
|
29 |
# - Host 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 HostsCommand < BaseCommand |
50 |
|
51 |
ORGANIZATION = 'Organization' |
52 |
ENVIRONMENT = 'Environment' |
53 |
OPERATINGSYSTEM = 'Operating System' |
54 |
ARCHITECTURE = 'Architecture' |
55 |
MACADDRESS = 'MAC Address' |
56 |
DOMAIN = 'Domain' |
57 |
PARTITIONTABLE = 'Partition Table' |
58 |
|
59 |
def export |
60 |
CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| |
61 |
csv << [NAME, COUNT, ORGANIZATION, ENVIRONMENT, OPERATINGSYSTEM, ARCHITECTURE, MACADDRESS, DOMAIN, PARTITIONTABLE] |
62 |
@f_host_api.index({:per_page => 999999})[0]['results'].each do |host| |
63 |
host = @f_host_api.show({'id' => host['id']})[0] |
64 |
raise "Host 'id=#{host['id']}' not found" if !host || host.empty? |
65 |
|
66 |
name = host['name']
|
67 |
count = 1
|
68 |
organization = foreman_organization(:id => host['organization_id']) |
69 |
environment = foreman_environment(:id => host['environment_id']) |
70 |
operatingsystem = foreman_operatingsystem(:id => host['operatingsystem_id']) |
71 |
architecture = foreman_architecture(:id => host['architecture_id']) |
72 |
mac = host['mac']
|
73 |
domain = foreman_domain(:id => host['domain_id']) |
74 |
ptable = foreman_partitiontable(:id => host['ptable_id']) |
75 |
|
76 |
csv << [name, count, organization, environment, operatingsystem, architecture, mac, domain, ptable] |
77 |
end
|
78 |
end
|
79 |
end
|
80 |
|
81 |
def import |
82 |
@existing = {}
|
83 |
@f_host_api.index({:per_page => 999999})[0]['results'].each do |host| |
84 |
@existing[host['name']] = host['id'] if host |
85 |
end
|
86 |
|
87 |
thread_import do |line|
|
88 |
create_hosts_from_csv(line) |
89 |
end
|
90 |
end
|
91 |
|
92 |
def create_hosts_from_csv(line) |
93 |
line[COUNT].to_i.times do |number| |
94 |
name = namify(line[NAME], number)
|
95 |
if !@existing.include? name |
96 |
print "Creating host '#{name}'..." if option_verbose? |
97 |
@f_host_api.create({
|
98 |
'host' => {
|
99 |
'name' => name,
|
100 |
'root_pass' => 'changeme', |
101 |
'mac' => namify(line[MACADDRESS], number), |
102 |
'organization_id' => foreman_organization(:name => line[ORGANIZATION]), |
103 |
'environment_id' => foreman_environment(:name => line[ENVIRONMENT]), |
104 |
'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATINGSYSTEM]), |
105 |
'environment_id' => foreman_environment(:name => line[ENVIRONMENT]), |
106 |
'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]), |
107 |
'domain_id' => foreman_domain(:name => line[DOMAIN]), |
108 |
'ptable_id' => foreman_partitiontable(:name => line[PARTITIONTABLE]) |
109 |
} |
110 |
}) |
111 |
else
|
112 |
print "Updating host '#{name}'..." if option_verbose? |
113 |
@f_host_api.update({
|
114 |
'id' => @existing[name], |
115 |
'host' => {
|
116 |
'name' => name,
|
117 |
'mac' => namify(line[MACADDRESS], number), |
118 |
'organization_id' => foreman_organization(:name => line[ORGANIZATION]), |
119 |
'environment_id' => foreman_environment(:name => line[ENVIRONMENT]), |
120 |
'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATINGSYSTEM]), |
121 |
'environment_id' => foreman_environment(:name => line[ENVIRONMENT]), |
122 |
'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]), |
123 |
'domain_id' => foreman_domain(:name => line[DOMAIN]), |
124 |
'ptable_id' => foreman_partitiontable(:name => line[PARTITIONTABLE]) |
125 |
} |
126 |
}) |
127 |
end
|
128 |
print "done\n" if option_verbose? |
129 |
end
|
130 |
rescue RuntimeError => e |
131 |
raise "#{e}\n #{line}"
|
132 |
end
|
133 |
end
|
134 |
|
135 |
HammerCLI::MainCommand.subcommand("csv:hosts", "import/export hosts", HammerCLICsv::HostsCommand) |
136 |
end
|