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
# File 'app/services/foreman_templates/template_exporter.rb', line 7

def initialize(args = {})
  super args
  @result_lines = []
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)


109
110
111
112
113
114
115
# File 'app/services/foreman_templates/template_exporter.rb', line 109

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/services/foreman_templates/template_exporter.rb', line 72

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



12
13
14
15
16
17
18
19
# File 'app/services/foreman_templates/template_exporter.rb', line 12

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



120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/services/foreman_templates/template_exporter.rb', line 120

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



133
134
135
136
137
138
# File 'app/services/foreman_templates/template_exporter.rb', line 133

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

#export_to_filesObject



21
22
23
24
25
# File 'app/services/foreman_templates/template_exporter.rb', line 21

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

#export_to_gitObject



27
28
29
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
# File 'app/services/foreman_templates/template_exporter.rb', line 27

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

  git_repo = Git.clone(@repo, @dir)
  logger.debug "cloned '#{@repo}' to '#{@dir}'"

  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 = e.message
ensure
  FileUtils.remove_entry_secure(@dir) if File.exist?(@dir)
end

#foreman_git_userObject



68
69
70
# File 'app/services/foreman_templates/template_exporter.rb', line 68

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

#get_dump_dir(template) ⇒ Object



87
88
89
90
91
92
# File 'app/services/foreman_templates/template_exporter.rb', line 87

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



56
57
58
59
60
61
62
63
64
65
66
# File 'app/services/foreman_templates/template_exporter.rb', line 56

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



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/services/foreman_templates/template_exporter.rb', line 94

def templates_to_dump
  find_templates.each do |template|
    if filter.present?
      exportable = template.name =~ /#{filter}/i ? !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