Class: ForemanTemplates::TemplateExporter
- Inherits:
-
Action
- Object
- Action
- ForemanTemplates::TemplateExporter
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, #repond_to_missing?, #verify_path!
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class ForemanTemplates::Action
Class Method Details
.setting_overrides ⇒ Object
3
4
5
|
# File 'app/services/foreman_templates/template_exporter.rb', line 3
def self.setting_overrides
super + %i(metadata_export_mode)
end
|
Instance Method Details
#dump_files! ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'app/services/foreman_templates/template_exporter.rb', line 58
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
7
8
9
10
11
12
13
14
15
|
# File 'app/services/foreman_templates/template_exporter.rb', line 7
def export!
if git_repo?
export_to_git
else
export_to_files
end
return true
end
|
#export_method ⇒ Object
-
refresh - template.to_erb stripping existing metadata,
-
remove - just template.template with stripping existing metadata,
-
keep - taking the whole template.template
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'app/services/foreman_templates/template_exporter.rb', line 96
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_files ⇒ Object
17
18
19
20
21
|
# File 'app/services/foreman_templates/template_exporter.rb', line 17
def export_to_files
@dir = get_absolute_repo_path
verify_path!(@dir)
dump_files!
end
|
#export_to_git ⇒ Object
23
24
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
|
# File 'app/services/foreman_templates/template_exporter.rb', line 23
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)
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) 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
76
77
78
79
80
81
|
# File 'app/services/foreman_templates/template_exporter.rb', line 76
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
72
73
74
|
# File 'app/services/foreman_templates/template_exporter.rb', line 72
def get_template_filename(template)
template.name.downcase.tr(' ', '_') + '.erb'
end
|
#templates_to_dump ⇒ Object
83
84
85
86
87
88
89
90
91
|
# File 'app/services/foreman_templates/template_exporter.rb', line 83
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
|