1
|
module HammerCLICsv
|
2
|
class CsvCommand
|
3
|
class HostsCommand < BaseCommand
|
4
|
command_name 'hosts'
|
5
|
desc 'import or export hosts'
|
6
|
|
7
|
ORGANIZATION = 'Organization'
|
8
|
LOCATION = 'Location'
|
9
|
ENVIRONMENT = 'Puppet Environment'
|
10
|
OPERATINGSYSTEM = 'Operating System'
|
11
|
ARCHITECTURE = 'Architecture'
|
12
|
MACADDRESS = 'MAC Address'
|
13
|
DOMAIN = 'Domain'
|
14
|
PARTITIONTABLE = 'Partition Table'
|
15
|
SUBNET = 'Subnet'
|
16
|
REALM = 'Realm'
|
17
|
MEDIUM = 'Medium'
|
18
|
HOSTGROUP = 'Hostgroup'
|
19
|
COMPUTERESOURCE = 'Compute Resource'
|
20
|
COMPUTEPROFILE = 'Compute Profile'
|
21
|
IMAGE = 'Image'
|
22
|
ENABLED = 'Enabled'
|
23
|
MANAGED = 'Managed'
|
24
|
|
25
|
def export(csv)
|
26
|
csv << [NAME, ORGANIZATION, LOCATION, ENVIRONMENT, OPERATINGSYSTEM, ARCHITECTURE,
|
27
|
MACADDRESS, DOMAIN, PARTITIONTABLE, SUBNET, REALM, MEDIUM, HOSTGROUP,
|
28
|
COMPUTERESOURCE, COMPUTEPROFILE, IMAGE, ENABLED, MANAGED]
|
29
|
search_options = {:per_page => 999999}
|
30
|
search_options['search'] = "organization=\"#{option_organization}\"" if option_organization
|
31
|
@api.resource(:hosts).call(:index, search_options)['results'].each do |host|
|
32
|
host = @api.resource(:hosts).call(:show, {'id' => host['id']})
|
33
|
raise "Host 'id=#{host['id']}' not found" if !host || host.empty?
|
34
|
|
35
|
name = host['name']
|
36
|
organization = host['organization_name']
|
37
|
location = host['location_name']
|
38
|
environment = host['environment_name']
|
39
|
operatingsystem = host['operatingsystem_name']
|
40
|
architecture = host['architecture_name']
|
41
|
mac = host['mac']
|
42
|
domain = host['domain_name']
|
43
|
ptable = host['ptable_name']
|
44
|
subnet = host['subnet_name']
|
45
|
realm = host['realm_name']
|
46
|
medium = host['medium_name']
|
47
|
hostgroup = host['hostgroup_name']
|
48
|
compute_resource = host['compute_resource_name']
|
49
|
compute_profile = host['compute_profile_name']
|
50
|
image = host['image_name']
|
51
|
|
52
|
enabled = host['enabled'] ? 'Yes' : 'No'
|
53
|
managed = host['managed'] ? 'Yes' : 'No'
|
54
|
|
55
|
csv << [name, organization, location, environment, operatingsystem, architecture,
|
56
|
mac, domain, ptable, subnet, realm, medium, hostgroup, compute_resource,
|
57
|
compute_profile, image, enabled, managed]
|
58
|
end
|
59
|
end
|
60
|
|
61
|
def import
|
62
|
@existing = {}
|
63
|
@api.resource(:hosts).call(:index, {:per_page => 999999})['results'].each do |host|
|
64
|
@existing[host['name']] = host['id'] if host
|
65
|
end
|
66
|
|
67
|
thread_import do |line|
|
68
|
create_hosts_from_csv(line)
|
69
|
end
|
70
|
end
|
71
|
|
72
|
def create_hosts_from_csv(line)
|
73
|
return if option_organization && line[ORGANIZATION] != option_organization
|
74
|
|
75
|
count(line[COUNT]).times do |number|
|
76
|
name = namify(line[NAME], number)
|
77
|
if !@existing.include? name
|
78
|
print "Creating host '#{name}'..." if option_verbose?
|
79
|
@api.resource(:hosts).call(:create, {
|
80
|
'host' => {
|
81
|
'name' => name,
|
82
|
'root_pass' => 'changeme',
|
83
|
'mac' => namify(line[MACADDRESS], number),
|
84
|
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
|
85
|
'location_id' => foreman_location(:name => line[LOCATION]),
|
86
|
'environment_id' => foreman_environment(:name => line[ENVIRONMENT]),
|
87
|
'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATINGSYSTEM]),
|
88
|
'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]),
|
89
|
'domain_id' => foreman_domain(:name => line[DOMAIN]),
|
90
|
'ptable_id' => foreman_partitiontable(:name => line[PARTITIONTABLE])
|
91
|
}
|
92
|
})
|
93
|
else
|
94
|
print "Updating host '#{name}'..." if option_verbose?
|
95
|
@api.resource(:hosts).call(:update, {
|
96
|
'id' => @existing[name],
|
97
|
'host' => {
|
98
|
'name' => name,
|
99
|
'mac' => namify(line[MACADDRESS], number),
|
100
|
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
|
101
|
'environment_id' => foreman_environment(:name => line[ENVIRONMENT]),
|
102
|
'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATINGSYSTEM]),
|
103
|
'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]),
|
104
|
'domain_id' => foreman_domain(:name => line[DOMAIN]),
|
105
|
'ptable_id' => foreman_partitiontable(:name => line[PARTITIONTABLE])
|
106
|
}
|
107
|
})
|
108
|
end
|
109
|
print "done\n" if option_verbose?
|
110
|
end
|
111
|
end
|
112
|
end
|
113
|
end
|
114
|
end
|