Class: JobTemplate
- Inherits:
-
Template
- Object
- Template
- JobTemplate
show all
- Extended by:
- FriendlyId
- Includes:
- Authorizable, Exportable, Parameterizable::ByIdName, Taxonomix, TemplateTax
- Defined in:
- app/models/job_template.rb
Defined Under Namespace
Classes: NonUniqueInputsError
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
90
91
92
|
# File 'app/models/job_template.rb', line 90
def acceptable_template_input_types
[ :user, :fact, :variable ]
end
|
.base_class ⇒ Object
58
59
60
|
# File 'app/models/job_template.rb', line 58
def base_class
self
end
|
.default_render_scope_class ⇒ Object
.import_parsed(name, text, _metadata, options = {}) ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'app/models/job_template.rb', line 77
def import_parsed(name, text, _metadata, options = {})
transaction do
existing = self.find_by(:name => name) unless options.delete(:build_new)
return if !options.delete(:update) && existing
template = existing || self.new(:name => name)
template.import_without_save(text, options)
template
end
end
|
.import_raw(contents, options = {}) ⇒ Object
Import a template from ERB, with YAML metadata in the first comment. It will overwrite (sync) an existing template if options is true.
65
66
67
68
69
|
# File 'app/models/job_template.rb', line 65
def import_raw(contents, options = {})
metadata = Template.parse_metadata(contents)
return unless SeedHelper.test_template_requirements(metadata['name'], metadata['require'] || [])
import_parsed(metadata['name'], contents, metadata, options)
end
|
.import_raw!(contents, options = {}) ⇒ Object
71
72
73
74
75
|
# File 'app/models/job_template.rb', line 71
def import_raw!(contents, options = {})
template = import_raw(contents, options)
template&.save!
template
end
|
Instance Method Details
#assign_taxonomies ⇒ Object
125
126
127
128
129
130
|
# File 'app/models/job_template.rb', line 125
def assign_taxonomies
if default
organizations << Organization.all
locations << Location.all
end
end
|
199
200
201
202
203
204
205
206
|
# File 'app/models/job_template.rb', line 199
def default_input_values(ignore_keys)
result = self.template_inputs_with_foreign.select { |ti| !ti.required? && ti.input_type == 'user' }.map { |ti| ti.name.to_s }
result -= ignore_keys.map(&:to_s)
default_values = self.template_inputs_with_foreign.reduce({}) do |acc, input|
acc.merge(input.name.to_s => input.default)
end
Hash[result.map { |k| [ k, default_values[k]] }]
end
|
#effective_user ⇒ Object
136
137
138
|
# File 'app/models/job_template.rb', line 136
def effective_user
super || build_effective_user.tap(&:set_defaults)
end
|
#filename ⇒ Object
‘Package Action - SSH Default’ => ‘package_action_ssh_default.erb’
111
112
113
|
# File 'app/models/job_template.rb', line 111
def filename
name.downcase.delete('-').gsub(/\s+/, '_') + '.erb'
end
|
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'app/models/job_template.rb', line 140
def generate_description_format
if description_format.blank?
generated_description = '%{template_name}'
unless template_inputs_with_foreign.empty?
inputs = template_inputs_with_foreign.map(&:name).map { |name| %{#{name}="%{#{name}}"} }.join(' ')
generated_description << " with inputs #{inputs}"
end
generated_description
else
description_format
end
end
|
#import_custom_data(options) ⇒ Object
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
# File 'app/models/job_template.rb', line 181
def import_custom_data(options)
super
sync_foreign_input_sets(@importing_metadata['foreign_input_sets'])
sync_feature(@importing_metadata['feature'])
%w(job_category description_format provider_type).each do |attribute|
value = @importing_metadata[attribute]
self.public_send "#{attribute}=", value if @importing_metadata.key?(attribute)
end
self.default = options[:default] unless options[:default].nil?
self.template = self.template.sub(/<%\#.+?.-?%>\n?/m, '').strip
end
|
106
107
108
|
# File 'app/models/job_template.rb', line 106
def metadata
"<%#\n#{to_export(false).to_yaml.sub(/\A---$/, '').strip}\n%>\n\n"
end
|
#sync_feature(feature_name) ⇒ Object
175
176
177
178
179
|
# File 'app/models/job_template.rb', line 175
def sync_feature(feature_name)
if feature_name && (feature = RemoteExecutionFeature.feature(feature_name)) && feature.job_template.blank?
self.remote_execution_features << feature
end
end
|
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
# File 'app/models/job_template.rb', line 153
def sync_foreign_input_sets(input_sets)
input_sets ||= []
input_sets = input_sets.inject({}) do |h, input_set|
target_template = JobTemplate.find_by!(:name => input_set.delete('template'))
input_set['target_template_id'] = target_template.id
h.update(target_template.id => input_set)
end
foreign_input_sets.each do |existing_input_set|
if input_sets.include?(existing_input_set.target_template_id)
existing_input_set.assign_attributes(input_sets.delete(existing_input_set.target_template_id))
else
existing_input_set.mark_for_destruction
end
end
input_sets.each_value { |input_set| self.foreign_input_sets.build(input_set) }
end
|
#to_erb ⇒ Object
115
116
117
|
# File 'app/models/job_template.rb', line 115
def to_erb
metadata + template
end
|
#used_taxonomy_ids(type) ⇒ Object
Override method in Taxonomix as Template is not used attached to a Host, and matching a Host does not prevent removing a template from its taxonomy.
121
122
123
|
# File 'app/models/job_template.rb', line 121
def used_taxonomy_ids(type)
[]
end
|
99
100
101
102
103
104
|
# File 'app/models/job_template.rb', line 99
def validate_unique_inputs!
duplicated_inputs = template_inputs_with_foreign.group_by(&:name).values.select { |values| values.size > 1 }.map(&:first)
unless duplicated_inputs.empty?
raise NonUniqueInputsError.new(N_('Duplicated inputs detected: %{duplicated_inputs}'), :duplicated_inputs => duplicated_inputs.map(&:name))
end
end
|