Class: JobTemplate

Inherits:
Template
  • Object
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

.acceptable_template_input_typesObject



90
91
92
# File 'app/models/job_template.rb', line 90

def acceptable_template_input_types
  [ :user, :fact, :variable ]
end

.base_classObject

we have to override the base_class because polymorphic associations does not detect it correctly, more details at apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_many#1010-Polymorphic-has-many-within-inherited-class-gotcha



58
59
60
# File 'app/models/job_template.rb', line 58

def base_class
  self
end

.default_render_scope_classObject



94
95
96
# File 'app/models/job_template.rb', line 94

def default_render_scope_class
  ForemanRemoteExecution::Renderer::Scope::Input
end

.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, , options = {})
  transaction do
    # Don't look for existing if we should always create a new template
    existing = self.find_by(:name => name) unless options.delete(:build_new)
    # Don't update if the template already exists, unless we're told to
    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 = {})
   = Template.(contents)
  return unless SeedHelper.test_template_requirements(['name'], ['require'] || [])
  import_parsed(['name'], contents, , 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_taxonomiesObject



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

#default_input_values(ignore_keys) ⇒ Object



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_userObject



136
137
138
# File 'app/models/job_template.rb', line 136

def effective_user
  super || build_effective_user.tap(&:set_defaults)
end

#filenameObject

‘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

#generate_description_formatObject



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

  # this should be moved to core but meanwhile we support default attribute here
  # see http://projects.theforeman.org/issues/23426 for more details
  self.default = options[:default] unless options[:default].nil?

  # job templates have too long metadata, we remove them on parsing until it's stored in separate attribute
  self.template = self.template.sub(/<%\#.+?.-?%>\n?/m, '').strip
end

#metadataObject



106
107
108
# File 'app/models/job_template.rb', line 106

def 
  "<%#\n#{to_export(false).to_yaml.sub(/\A---$/, '').strip}\n%>\n\n"
end

#providerObject



132
133
134
# File 'app/models/job_template.rb', line 132

def provider
  RemoteExecutionProvider.provider_for(provider_type)
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

#sync_foreign_input_sets(input_sets) ⇒ Object



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

  # Sync existing input sets
  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

  # Create new input_sets
  input_sets.each_value { |input_set| self.foreign_input_sets.build(input_set) }
end

#to_erbObject



115
116
117
# File 'app/models/job_template.rb', line 115

def to_erb
   + 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

#validate_unique_inputs!Object



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