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

#get_absolute_repo_path, #get_default_branch, #git_repo?, #initialize, #method_missing, #respond_to_missing?, #verify_path!

Constructor Details

This class inherits a constructor from ForemanTemplates::Action

Dynamic Method Handling

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

Class Method Details

.setting_overridesObject



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

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

Instance Method Details

#dump_files!Object



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

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, get_template_filename(template))
    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
end

#export!Object



9
10
11
12
13
14
15
16
17
# File 'app/services/foreman_templates/template_exporter.rb', line 9

def export!
  if git_repo?
    export_to_git
  else
    export_to_files
  end

  return true
end

#export_methodObject

  • refresh - template.to_erb stripping existing metadata,

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

  • keep - taking the whole template.template



98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/services/foreman_templates/template_exporter.rb', line 98

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_to_filesObject



19
20
21
22
23
# File 'app/services/foreman_templates/template_exporter.rb', line 19

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

#export_to_gitObject



25
26
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
55
56
57
58
# File 'app/services/foreman_templates/template_exporter.rb', line 25

def export_to_git
  @dir = Dir.mktmpdir

  git_repo = Git.clone(@repo, @dir)
  logger.debug "cloned #{@repo} to #{@dir}"
  branch = @branch ? @branch : get_default_branch(git_repo)
  # either checkout to existing or create a new one and checkout afterwards
  if branch
    if git_repo.is_branch?(branch)
      git_repo.checkout(branch)
    else
      git_repo.branch(branch).checkout
      if git_repo.is_remote_branch?(branch) # if we work with remote branch we need to sync it first
        git_repo.reset_hard("origin/#{branch}")
      end
    end
  end

  dump_files!
  git_repo.add

  status = git_repo.status
  if status.added.any? || status.changed.any? || status.deleted.any? || status.untracked.any?
    logger.debug 'committing changes in cloned repo'
    git_repo.commit "Templates export made by Foreman user #{User.current.try(:login) || User::ANONYMOUS_ADMIN}"

    logger.debug "pushing to branch #{branch} at origin #{@repo}"
    git_repo.push 'origin', branch
  else
    logger.debug 'no change detected, skipping the commit and push'
  end
ensure
  FileUtils.remove_entry_secure(@dir) if File.exist?(@dir)
end

#get_dump_dir(template) ⇒ Object



78
79
80
81
82
83
# File 'app/services/foreman_templates/template_exporter.rb', line 78

def get_dump_dir(template)
  kind = template.respond_to?(:template_kind) ? template.template_kind.try(:name) || 'snippet' : nil
  template_class_dir = template.model_name.human.pluralize.downcase.tr(' ', '_')
  template_class_dir += '_templates' if template_class_dir == 'partition_tables'
  File.join(@dir, dirname.to_s, template_class_dir, kind.to_s)
end

#get_template_filename(template) ⇒ Object



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

def get_template_filename(template)
  Shellwords.escape(template.name.downcase.tr(' /', '_') + '.erb')
end

#templates_to_dumpObject



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

def templates_to_dump
  base = Template.all
  if filter.present?
    method = negate ? :reject : :select
    base.public_send(method) { |template| template.name.match(/#{filter}/i) }
  else
    base
  end
end