1
|
module HammerCLICsv
|
2
|
class CsvCommand
|
3
|
class JobTemplatesCommand < BaseCommand
|
4
|
command_name 'job-templates'
|
5
|
desc 'import or export job templates'
|
6
|
|
7
|
ORGANIZATIONS = 'Organizations'
|
8
|
LOCATIONS = 'Locations'
|
9
|
DESCRIPTION = 'Description'
|
10
|
JOB = 'Job Category'
|
11
|
PROVIDER = 'Provider'
|
12
|
SNIPPET = 'Snippet'
|
13
|
TEMPLATE = 'Template'
|
14
|
INPUT_NAME = 'Input:Name'
|
15
|
INPUT_DESCRIPTION = 'Input:Description'
|
16
|
INPUT_REQUIRED = 'Input:Required'
|
17
|
INPUT_TYPE = 'Input:Type'
|
18
|
INPUT_PARAMETERS = 'Input:Parameters'
|
19
|
|
20
|
def export(csv)
|
21
|
csv << [NAME, ORGANIZATIONS, LOCATIONS, DESCRIPTION, JOB, PROVIDER, SNIPPET, TEMPLATE,
|
22
|
INPUT_NAME, INPUT_DESCRIPTION, INPUT_REQUIRED, INPUT_TYPE, INPUT_PARAMETERS]
|
23
|
@api.resource(:job_templates).call(:index, {
|
24
|
:per_page => 999999
|
25
|
})['results'].each do |template_id|
|
26
|
template = @api.resource(:job_templates).call(:show, {:id => template_id['id']})
|
27
|
next if template['locked']
|
28
|
next unless option_organization.nil? || template['organizations'].detect { |org| org['name'] == option_organization }
|
29
|
name = template['name']
|
30
|
description = template['description_format']
|
31
|
job = template['job_category']
|
32
|
snippet = template['snippet'] ? 'Yes' : 'No'
|
33
|
provider = template['provider_type']
|
34
|
organizations = export_column(template, 'organizations', 'name')
|
35
|
locations = export_column(template, 'locations', 'name')
|
36
|
csv << [name, organizations, locations, description, job, provider, snippet, template['template']]
|
37
|
|
38
|
template_columns = [name] + Array.new(7)
|
39
|
@api.resource(:template_inputs).call(:index, {
|
40
|
:template_id => template['id']
|
41
|
})['results'].each do|input|
|
42
|
input_field = nil
|
43
|
input_options = nil
|
44
|
case input['input_type']
|
45
|
when /user/
|
46
|
input_name = export_column(input, 'options') do |value|
|
47
|
value
|
48
|
end
|
49
|
when /fact/
|
50
|
input_name = input['fact_name']
|
51
|
when /variable/
|
52
|
input_name = input['variable_name']
|
53
|
when /puppet_parameter/
|
54
|
input_name = "#{input['puppet_class_name']}|#{input['puppet_parameter_name']}"
|
55
|
else
|
56
|
raise _("Unknown job template input type '%{type}'") % {:type => input['input_type']}
|
57
|
end
|
58
|
required = input['required'] ? 'Yes' : 'No'
|
59
|
csv << template_columns + [input['name'], input['description'], required, input['input_type'], input_name]
|
60
|
end
|
61
|
end
|
62
|
end
|
63
|
|
64
|
def import
|
65
|
@existing = {}
|
66
|
@api.resource(:job_templates).call(:index, {
|
67
|
:per_page => 999999
|
68
|
})['results'].each do |template|
|
69
|
@existing[template['name']] = template['id'] if template
|
70
|
end
|
71
|
|
72
|
thread_import do |line|
|
73
|
create_templates_from_csv(line)
|
74
|
end
|
75
|
end
|
76
|
|
77
|
def create_templates_from_csv(line)
|
78
|
organizations = collect_column(line[ORGANIZATIONS]) do |organization|
|
79
|
foreman_organization(:name => organization)
|
80
|
end
|
81
|
if option_organization
|
82
|
org_id = foreman_organization(:name => option_organization)
|
83
|
return if org_id.nil? || !organizations.include?(org_id)
|
84
|
organizations = [org_id]
|
85
|
end
|
86
|
locations = collect_column(line[LOCATIONS]) do |location|
|
87
|
foreman_location(:name => location)
|
88
|
end
|
89
|
|
90
|
count(line[COUNT]).times do |number|
|
91
|
if line[INPUT_NAME].nil? || line[INPUT_NAME].empty?
|
92
|
create_template(line, number)
|
93
|
else
|
94
|
create_template_input(line, number)
|
95
|
end
|
96
|
|
97
|
|
98
|
|
99
|
|
100
|
|
101
|
|
102
|
|
103
|
|
104
|
|
105
|
|
106
|
|
107
|
|
108
|
|
109
|
|
110
|
|
111
|
|
112
|
|
113
|
|
114
|
|
115
|
|
116
|
|
117
|
|
118
|
|
119
|
|
120
|
|
121
|
|
122
|
|
123
|
|
124
|
|
125
|
|
126
|
|
127
|
|
128
|
|
129
|
|
130
|
|
131
|
|
132
|
|
133
|
|
134
|
|
135
|
|
136
|
|
137
|
|
138
|
end
|
139
|
end
|
140
|
|
141
|
def create_template(line, number)
|
142
|
name = namify(line[NAME], number)
|
143
|
job_category = namify(line[JOB], number)
|
144
|
options = {
|
145
|
'job_template' => {
|
146
|
'name' => name,
|
147
|
'description_format' => line[DESCRIPTION],
|
148
|
'job_category' => job_category,
|
149
|
'snippet' => line[SNIPPET] == 'Yes' ? true : false,
|
150
|
'provider_type' => line[PROVIDER],
|
151
|
|
152
|
|
153
|
'template' => line[TEMPLATE]
|
154
|
}
|
155
|
}
|
156
|
template_id = @existing[name]
|
157
|
if !template_id
|
158
|
print _("Creating job template '%{name}'...") % {:name => name } if option_verbose?
|
159
|
template_id = @api.resource(:job_templates).call(:create, options)['id']
|
160
|
@existing[name] = template_id
|
161
|
else
|
162
|
print _("Updating job template '%{name}'...") % {:name => name} if option_verbose?
|
163
|
options['id'] = template_id
|
164
|
template_id = @api.resource(:job_templates).call(:update, options)['id']
|
165
|
end
|
166
|
puts _('done') if option_verbose?
|
167
|
end
|
168
|
|
169
|
def create_template_input(line, number)
|
170
|
name = namify(line[NAME], number)
|
171
|
template_id = @existing[name]
|
172
|
raise "Job template '#{name}' must exist before setting inputs" unless template_id
|
173
|
|
174
|
options = {
|
175
|
'template_id' => template_id,
|
176
|
'template_input' => {
|
177
|
'name' => line[INPUT_NAME],
|
178
|
'description' => line[INPUT_DESCRIPTION],
|
179
|
'input_type' => line[INPUT_TYPE],
|
180
|
'required' => line[INPUT_REQUIRED] == 'Yes' ? true : false
|
181
|
}
|
182
|
}
|
183
|
case line[INPUT_TYPE]
|
184
|
when /user/
|
185
|
options['template_input']['options'] = line[INPUT_PARAMETERS]
|
186
|
when /fact/
|
187
|
options['template_input']['fact_name'] = line[INPUT_PARAMETERS]
|
188
|
when /variable/
|
189
|
options['template_input']['variable_name'] = line[INPUT_PARAMETERS]
|
190
|
when /puppet_parameter/
|
191
|
options['template_input']['puppet_class_name'], options['template_input']['puppet_parameter_name'] = line[INPUT_PARAMETERS].split('|')
|
192
|
else
|
193
|
raise _("Unknown job template input type '%{type}'") % {:type => line[INPUT_TYPE]}
|
194
|
end
|
195
|
|
196
|
template_input = @api.resource(:template_inputs).call(:index, {
|
197
|
:template_id => template_id,
|
198
|
:search => "name = \"#{line[INPUT_NAME]}\""
|
199
|
})['results']
|
200
|
if template_input.empty?
|
201
|
print _("Creating job template input '%{input_name}' on '%{name}'...") % {:input_name => line[INPUT_NAME], :name => name}
|
202
|
@api.resource(:template_inputs).call(:create, options)
|
203
|
else
|
204
|
print _("Updating job template input '%{input_name}' on '%{name}'...") % {:input_name => line[INPUT_NAME], :name => name}
|
205
|
options['id'] = template_input[0]['id']
|
206
|
@api.resource(:template_inputs).call(:update, options)
|
207
|
end
|
208
|
puts _('done') if option_verbose?
|
209
|
end
|
210
|
|
211
|
def export_associations(template)
|
212
|
return '' unless template['template_combinations']
|
213
|
values = CSV.generate do |column|
|
214
|
column << template['template_combinations'].collect do |combo|
|
215
|
"#{combo['hostgroup_name']}|#{combo['environment_name']}"
|
216
|
end
|
217
|
end
|
218
|
values.delete!("\n")
|
219
|
end
|
220
|
end
|
221
|
end
|
222
|
end
|