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



85
86
87
# File 'app/models/job_template.rb', line 85

def acceptable_template_input_types
  [ :user, :fact, :variable, :puppet_parameter ]
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



54
55
56
# File 'app/models/job_template.rb', line 54

def base_class
  self
end

.default_render_scope_classObject



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

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

.import_parsed(name, text, _metadata, options = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/job_template.rb', line 72

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.



61
62
63
64
# File 'app/models/job_template.rb', line 61

def import_raw(contents, options = {})
   = Template.(contents)
  import_parsed(['name'], contents, , options)
end

.import_raw!(contents, options = {}) ⇒ Object



66
67
68
69
70
# File 'app/models/job_template.rb', line 66

def import_raw!(contents, options = {})
  template = import_raw(contents, options)
  template.save! if template
  template
end

Instance Method Details

#assign_taxonomiesObject



120
121
122
123
124
125
# File 'app/models/job_template.rb', line 120

def assign_taxonomies
  if default
    organizations << Organization.all if SETTINGS[:organizations_enabled]
    locations << Location.all if SETTINGS[:locations_enabled]
  end
end

#default_input_values(ignore_keys) ⇒ Object



194
195
196
197
198
# File 'app/models/job_template.rb', line 194

def default_input_values(ignore_keys)
  result = self.template_inputs_with_foreign.select { |ti| !ti.required? && ti.user_template_input? }.map { |ti| ti.name.to_s }
  result -= ignore_keys.map(&:to_s)
  Hash[result.map { |k| [ k, nil ] }]
end

#effective_userObject



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

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

#filenameObject

‘Package Action - SSH Default’ => ‘package_action_ssh_default.erb’



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

def filename
  name.downcase.delete('-').gsub(/\s+/, '_') + '.erb'
end

#generate_description_formatObject



135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/models/job_template.rb', line 135

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



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'app/models/job_template.rb', line 176

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.gsub(/<%\#.+?.-?%>\n?/m, '').strip
end

#metadataObject



101
102
103
# File 'app/models/job_template.rb', line 101

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

#providerObject



127
128
129
# File 'app/models/job_template.rb', line 127

def provider
  RemoteExecutionProvider.provider_for(provider_type)
end

#sync_feature(feature_name) ⇒ Object



170
171
172
173
174
# File 'app/models/job_template.rb', line 170

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



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'app/models/job_template.rb', line 148

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



110
111
112
# File 'app/models/job_template.rb', line 110

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.



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

def used_taxonomy_ids(type)
  []
end

#validate_unique_inputs!Object



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

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