Class: ForemanTemplates::TemplateExporter

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

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 = {}) ⇒ TemplateExporter

Returns a new instance of TemplateExporter.



7
8
9
10
11
12
13
# File 'app/services/foreman_templates/template_exporter.rb', line 7

def initialize(args = {})
  super args
  @result_lines = []
  @uri = URI(@repo)
  @redacted_uri = @uri.dup
  @redacted_uri.password = '*****' if @uri.password
end

Dynamic Method Handling

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

Class Method Details

.setting_overridesObject



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

def self.setting_overrides
  super + %i(metadata_export_mode commit_msg)
end

Instance Method Details

#branch_missing?Boolean

Returns:

  • (Boolean)


116
117
118
119
120
121
122
# File 'app/services/foreman_templates/template_exporter.rb', line 116

def branch_missing?
  if @branch.blank?
    @error = "Please specify a branch when exporting into a git repo"
    return true
  end
  false
end

#dump_files!Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/services/foreman_templates/template_exporter.rb', line 79

def dump_files!
  templates_to_dump.map do |template|
    current_dir = get_dump_dir(template)
    FileUtils.mkdir_p current_dir
    filename = File.join(current_dir, template.template_file)
    File.open(filename, 'w+') do |file|
      logger.debug "Writing to file #{filename}"
      bytes = file.write template.public_send(export_method)
      logger.debug "finished writing #{bytes}"
    end
  end
rescue StandardError => e
  raise PathAccessException, e.message
end

#export!Object



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

def export!
  if git_repo?
    export_to_git
  else
    export_to_files
  end
  export_result
end

#export_methodObject

  • refresh - template.to_erb stripping existing metadata,

  • remove - just template.template with stripping existing metadata,

  • keep - taking the whole template.template



127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/services/foreman_templates/template_exporter.rb', line 127

def export_method
  case @metadata_export_mode
    when 'refresh'
      :to_erb
    when 'remove'
      :template_without_metadata
    when 'keep'
      :template
    else
      raise "Unknown metadata export mode #{@metadata_export_mode}"
  end
end

#export_resultObject



140
141
142
143
144
145
# File 'app/services/foreman_templates/template_exporter.rb', line 140

def export_result
  {
    :templates => @result_lines, :repo => @redacted_uri, :branch => @branch,
    :git_user => foreman_git_user, :error => @error, :warning => @warning
  }
end

#export_to_filesObject



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

def export_to_files
  @dir = get_absolute_repo_path
  verify_path!(@dir)
  dump_files!
end

#export_to_gitObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/services/foreman_templates/template_exporter.rb', line 30

def export_to_git
  @dir = Dir.mktmpdir
  return if branch_missing?

  git_repo = init_git_repo
  git_repo.fetch

  setup_git_branch git_repo
  dump_files!
  git_repo.add

  new_repo = false
  begin
    status = git_repo.status
  rescue Git::GitExecuteError # no HEAD for repo without commits, git diff-index HEAD fails
    new_repo = true
  end
  if new_repo || status.added.any? || status.changed.any? || status.deleted.any? || status.untracked.any?
    git_repo.commit commit_msg
    git_repo.push 'origin', branch
  else
    @warning = 'No change detected, skipping the commit and push'
  end
rescue StandardError => e
  @error = if @uri.password
             e.message.gsub(@uri.to_s, @redacted_uri.to_s)
           else
             e.message
           end
ensure
  FileUtils.remove_entry_secure(@dir) if File.exist?(@dir)
end

#foreman_git_userObject



75
76
77
# File 'app/services/foreman_templates/template_exporter.rb', line 75

def foreman_git_user
  User.current.try(:login) || User::ANONYMOUS_ADMIN
end

#get_dump_dir(template) ⇒ Object



94
95
96
97
98
99
# File 'app/services/foreman_templates/template_exporter.rb', line 94

def get_dump_dir(template)
  kind = template.respond_to?(:template_kind) ? template.template_kind.try(:name) || 'snippet' : nil
  template_class_dir = template.model_name.plural
  template_class_dir = 'partition_tables_templates' if template_class_dir == 'ptables'
  File.join(@dir, dirname.to_s, template_class_dir, kind.to_s)
end

#setup_git_branch(git_repo) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'app/services/foreman_templates/template_exporter.rb', line 63

def setup_git_branch(git_repo)
  logger.debug "checking out branch '#{@branch}'"
  if git_repo.is_branch?(@branch) # local branch
    git_repo.checkout(@branch)
  elsif git_repo.is_remote_branch?(@branch) # if we work with remote branch, checkout and sync
    git_repo.branch(@branch).checkout
    git_repo.reset_hard("origin/#{@branch}")
  else # neither local nor remote
    git_repo.checkout(@branch, :new_branch => true)
  end
end

#templates_to_dumpObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/services/foreman_templates/template_exporter.rb', line 101

def templates_to_dump
  find_templates.each do |template|
    if filter.present?
      exportable = /#{filter}/i.match?(template.name) ? !negate : negate
      result = ExportResult.new(template, exportable)
      next @result_lines << result.matching_filter unless exportable

      @result_lines << result
    else
      @result_lines << ExportResult.new(template)
    end
  end
  @result_lines.select(&:exported).map(&:template)
end