class JobTemplate
def acceptable_template_input_types
def acceptable_template_input_types [ :user, :fact, :variable ] end
def assign_taxonomies
def assign_taxonomies if default organizations << Organization.all locations << Location.all end end
def base_class
we have to override the base_class because polymorphic associations does not detect it correctly, more details at
def base_class self end
def default_input_values(ignore_keys)
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
def default_render_scope_class
def default_render_scope_class ForemanRemoteExecution::Renderer::Scope::Input end
def effective_user
def effective_user super || build_effective_user.tap(&:set_defaults) end
def filename
def filename name.downcase.delete('-').gsub(/\s+/, '_') + '.erb' end
def generate_description_format
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
def import_custom_data(options)
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
def import_parsed(name, text, _metadata, options = {})
def import_parsed(name, text, _metadata, 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
def import_raw(contents, options = {})
Import a template from ERB, with YAML metadata in the first comment. It
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
def import_raw!(contents, options = {})
def import_raw!(contents, options = {}) template = import_raw(contents, options) template&.save! template end
def metadata
def metadata "<%#\n#{to_export(false).to_yaml.sub(/\A---$/, '').strip}\n%>\n\n" end
def provider
def provider RemoteExecutionProvider.provider_for(provider_type) end
def provider_type_whitelist
def provider_type_whitelist errors.add :provider_type, :uniq unless RemoteExecutionProvider.provider_names.include?(self.provider_type) end
def sync_feature(feature_name)
def sync_feature(feature_name) if feature_name && (feature = RemoteExecutionFeature.feature(feature_name)) && feature.job_template.blank? self.remote_execution_features << feature end end
def sync_foreign_input_sets(input_sets)
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
def to_erb
def to_erb metadata + template end
def used_taxonomy_ids(type)
Override method in Taxonomix as Template is not used attached to a Host,
def used_taxonomy_ids(type) [] end
def validate_unique_inputs!
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