Class: ForemanTemplates::TemplateImporter

Inherits:
Action
  • Object
show all
Defined in:
app/services/foreman_templates/template_importer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Action

file_repo_start_with, #get_absolute_repo_path, #git_repo?, git_repo_start_with, #method_missing, repo_start_with, #respond_to_missing?, #verify_path!

Constructor Details

#initialize(args = {}) ⇒ TemplateImporter

Returns a new instance of TemplateImporter.



9
10
11
12
13
14
# File 'app/services/foreman_templates/template_importer.rb', line 9

def initialize(args = {})
  super args
  @verbose = parse_bool(@verbose)
  @force = parse_bool(@force)
  @result_lines = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ForemanTemplates::Action

Instance Attribute Details

#metadataObject

Returns the value of attribute metadata.



3
4
5
# File 'app/services/foreman_templates/template_importer.rb', line 3

def 
  @metadata
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'app/services/foreman_templates/template_importer.rb', line 3

def name
  @name
end

#textObject

Returns the value of attribute text.



3
4
5
# File 'app/services/foreman_templates/template_importer.rb', line 3

def text
  @text
end

Class Method Details

.setting_overridesObject



5
6
7
# File 'app/services/foreman_templates/template_importer.rb', line 5

def self.setting_overrides
  super + %i(associate force lock)
end

Instance Method Details

#auto_prefix(name) ⇒ Object



161
162
163
# File 'app/services/foreman_templates/template_importer.rb', line 161

def auto_prefix(name)
  name.start_with?(@prefix) ? name : [@prefix, name].compact.join
end

#auto_prefix_name(metadata, parse_result) ⇒ Object



152
153
154
155
156
157
158
159
# File 'app/services/foreman_templates/template_importer.rb', line 152

def auto_prefix_name(, parse_result)
  unless (name = ['name'])
    @result_lines << parse_result.
    return nil
  end
  parse_result.name = auto_prefix(name)
  auto_prefix(name)
end

#filtered_out(name, parse_result) ⇒ Object



130
131
132
133
134
135
# File 'app/services/foreman_templates/template_importer.rb', line 130

def filtered_out(name, parse_result)
  if @filter && !name_matching_filter?(name)
    @result_lines << parse_result.matching_filter
    true
  end
end

#import!Object



16
17
18
19
20
21
22
# File 'app/services/foreman_templates/template_importer.rb', line 16

def import!
  if git_repo?
    import_from_git
  else
    import_from_files
  end
end

#import_from_filesObject



24
25
26
27
28
# File 'app/services/foreman_templates/template_importer.rb', line 24

def import_from_files
  @dir = get_absolute_repo_path
  verify_path!(@dir)
  parse_files!
end

#import_from_gitObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/services/foreman_templates/template_importer.rb', line 30

def import_from_git
  # Check out the community templates to a temp location
  @dir = Dir.mktmpdir

  begin
    logger.debug "cloned '#{@repo}' to '#{@dir}'"
    gitrepo = Git.clone(@repo, @dir)
    if @branch
      logger.debug "checking out branch '#{@branch}'"
      gitrepo.checkout(@branch)
    end

    parse_files!
  ensure
    FileUtils.remove_entry_secure(@dir) if File.exist?(@dir)
  end
end

#import_optionsObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/services/foreman_templates/template_importer.rb', line 80

def import_options
  lock_predicate = lambda do |template|
    case @lock
    when 'lock'
      return true
    when 'unlock'
      return false
    when 'keep'
      return template.new_record? ? false : template.locked
    when 'keep_lock_new'
      return template.new_record? ? true : template.locked
    else
      raise ::Foreman::Exception.new("Unknown lock option type, expected one of #{::Setting::TemplateSync.lock_types.keys}, got #{@lock}")
    end
  end

  {
    :force => @force,
    :associate => @associate,
    :lock => lock_predicate,
    :organization_params => @taxonomies[:organizations],
    :location_params => @taxonomies[:locations]
  }
end

#metadata_corrupted?(metadata, parse_result) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
140
141
142
143
# File 'app/services/foreman_templates/template_importer.rb', line 137

def (, parse_result)
  if .empty?
    @result_lines << parse_result.
    return true
  end
  false
end

#name_matching_filter?(name) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
148
149
150
# File 'app/services/foreman_templates/template_importer.rb', line 145

def name_matching_filter?(name)
  matching = name.match(/#{@filter}/i)
  return !matching if @negate

  matching
end

#parse_files!Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/services/foreman_templates/template_importer.rb', line 48

def parse_files!
  Dir["#{@dir}/#{@dirname}/**/*.erb"].each do |template_file|
    logger.debug "Parsing: #{template_file.gsub(%r{#{@dir}/#{@dirname}}, '')}"
    parse_result = ParseResult.new(template_file)

    text = File.read(template_file)
     = Template.(text)

    next if (, parse_result)

    next unless (name = auto_prefix_name(, parse_result))

    next if filtered_out name, parse_result

    begin
      next unless (template_type = template_model(, parse_result))

      template = template_type.import_without_save(name, text, import_options)
      parse_result.template = template
      parse_result.determine_result_diff

      save_template template, @force
      @result_lines << parse_result.check_for_errors
    rescue NameError => e
      @result_lines << parse_result.name_error(e, ['model'])
    rescue StandardError => e
      @result_lines << parse_result.add_exception(e)
    end
  end
  { :results => @result_lines, :repo => @repo, :branch => @branch }
end

#purge!Object



165
166
167
168
169
170
171
172
# File 'app/services/foreman_templates/template_importer.rb', line 165

def purge!
  clause = "name #{@negate ? 'NOT ' : ''}LIKE ?"
  ProvisioningTemplate.where(clause, "#{@prefix}%").find_each do |template|
    puts template if @verbose
    template.destroy
  end
  # :purge
end

#save_template(template, force) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/services/foreman_templates/template_importer.rb', line 114

def save_template(template, force)
  result = nil
  if force
    result = template.ignore_locking { template.save }
  else
    result = template.save
  end

  if result
    logger.debug 'saved'
  else
    logger.error "couldn't save the template because of: #{template.errors.full_messages.join(', ')}"
  end
  result
end

#template_model(metadata, parse_result) ⇒ Object



105
106
107
108
109
110
111
112
# File 'app/services/foreman_templates/template_importer.rb', line 105

def template_model(, parse_result)
  if .key?('model')
    ['model'].constantize
  else
    @result_lines << parse_result.missing_model
    nil
  end
end