1
|
|
2
|
|
3
|
|
4
|
|
5
|
|
6
|
|
7
|
|
8
|
|
9
|
|
10
|
|
11
|
|
12
|
|
13
|
|
14
|
|
15
|
|
16
|
|
17
|
|
18
|
|
19
|
|
20
|
|
21
|
|
22
|
|
23
|
|
24
|
|
25
|
|
26
|
|
27
|
|
28
|
|
29
|
require 'hammer_cli'
|
30
|
require 'json'
|
31
|
require 'csv'
|
32
|
require 'uri'
|
33
|
|
34
|
module HammerCLICsv
|
35
|
class CsvCommand
|
36
|
class HostsCommand < BaseCommand
|
37
|
command_name 'hosts'
|
38
|
desc 'import or export hosts'
|
39
|
|
40
|
ORGANIZATION = 'Organization'
|
41
|
ENVIRONMENT = 'Environment'
|
42
|
OPERATINGSYSTEM = 'Operating System'
|
43
|
ARCHITECTURE = 'Architecture'
|
44
|
MACADDRESS = 'MAC Address'
|
45
|
DOMAIN = 'Domain'
|
46
|
PARTITIONTABLE = 'Partition Table'
|
47
|
|
48
|
def export
|
49
|
CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
|
50
|
csv << [NAME, COUNT, ORGANIZATION, ENVIRONMENT, OPERATINGSYSTEM, ARCHITECTURE, MACADDRESS, DOMAIN, PARTITIONTABLE]
|
51
|
@api.resource(:hosts).call(:index, {:per_page => 999999})['results'].each do |host|
|
52
|
host = @api.resource(:hosts).call(:show, {'id' => host['id']})
|
53
|
raise "Host 'id=#{host['id']}' not found" if !host || host.empty?
|
54
|
|
55
|
name = host['name']
|
56
|
count = 1
|
57
|
organization = foreman_organization(:id => host['organization_id'])
|
58
|
environment = foreman_environment(:id => host['environment_id'])
|
59
|
operatingsystem = foreman_operatingsystem(:id => host['operatingsystem_id'])
|
60
|
architecture = foreman_architecture(:id => host['architecture_id'])
|
61
|
mac = host['mac']
|
62
|
domain = foreman_domain(:id => host['domain_id'])
|
63
|
ptable = foreman_partitiontable(:id => host['ptable_id'])
|
64
|
|
65
|
csv << [name, count, organization, environment, operatingsystem, architecture, mac, domain, ptable]
|
66
|
end
|
67
|
end
|
68
|
end
|
69
|
|
70
|
def import
|
71
|
@existing = {}
|
72
|
@api.resource(:hosts).call(:index, {:per_page => 999999})['results'].each do |host|
|
73
|
@existing[host['name']] = host['id'] if host
|
74
|
end
|
75
|
|
76
|
thread_import do |line|
|
77
|
create_hosts_from_csv(line)
|
78
|
end
|
79
|
end
|
80
|
|
81
|
def create_hosts_from_csv(line)
|
82
|
line[COUNT].to_i.times do |number|
|
83
|
name = namify(line[NAME], number)
|
84
|
if !@existing.include? name
|
85
|
print "Creating host '#{name}'..." if option_verbose?
|
86
|
@api.resource(:hosts).call(:create, {
|
87
|
'name' => name,
|
88
|
'root_pass' => 'changeme',
|
89
|
'mac' => namify(line[MACADDRESS], number),
|
90
|
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
|
91
|
'environment_id' => foreman_environment(:name => line[ENVIRONMENT]),
|
92
|
'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATINGSYSTEM]),
|
93
|
'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]),
|
94
|
'domain_id' => foreman_domain(:name => line[DOMAIN]),
|
95
|
'ptable_id' => foreman_partitiontable(:name => line[PARTITIONTABLE])
|
96
|
})
|
97
|
else
|
98
|
print "Updating host '#{name}'..." if option_verbose?
|
99
|
@api.resource(:hosts).call(:update, {
|
100
|
'id' => @existing[name],
|
101
|
'name' => name,
|
102
|
'mac' => namify(line[MACADDRESS], number),
|
103
|
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
|
104
|
'environment_id' => foreman_environment(:name => line[ENVIRONMENT]),
|
105
|
'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATINGSYSTEM]),
|
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
|
end
|
111
|
print "done\n" if option_verbose?
|
112
|
end
|
113
|
rescue RuntimeError => e
|
114
|
raise "#{e}\n #{line}"
|
115
|
end
|
116
|
end
|
117
|
end
|
118
|
end
|