Skip to content

Commit 50cc4be

Browse files
committed
Use checkout_git_repository block option
1 parent 6fd1b36 commit 50cc4be

File tree

1 file changed

+25
-46
lines changed

1 file changed

+25
-46
lines changed

app/models/manageiq/providers/embedded_terraform/automation_manager/configuration_script_source.rb

Lines changed: 25 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -76,64 +76,43 @@ def find_templates_in_git_repo
7676
template_dirs = {}
7777

7878
# checkout repo, for sending files to terraform-runner to parse for input/ouput vars.
79-
git_checkout_tempdir = checkout_git_repo
80-
81-
# traverse through files in git-worktree
82-
git_repository.with_worktree do |worktree|
83-
worktree.ref = scm_branch
84-
85-
# Find all dir's with .tf/.tf.json files
86-
worktree.blob_list
87-
.group_by { |file| File.dirname(file) }
88-
.select { |_dir, files| files.any? { |f| f.end_with?(".tf", ".tf.json") } }
89-
.transform_values! { |files| files.map { |f| File.basename(f) } }
90-
.each do |relative_path, files|
91-
name = self.class.template_name_from_git_repo_url(git_repository.url, relative_path)
92-
93-
template_full_path = File.join(git_checkout_tempdir, relative_path)
94-
95-
input_vars, output_vars, terraform_version = parse_vars_in_template(template_full_path)
96-
97-
template_dirs[name] = {
98-
:relative_path => relative_path,
99-
:files => files,
100-
:input_vars => input_vars,
101-
:output_vars => output_vars,
102-
:terraform_version => terraform_version,
103-
}
79+
checkout_git_repository do |git_checkout_tempdir|
80+
# traverse through files in git-worktree
81+
git_repository.with_worktree do |worktree|
82+
worktree.ref = scm_branch
83+
84+
# Find all dir's with .tf/.tf.json files
85+
worktree.blob_list
86+
.group_by { |file| File.dirname(file) }
87+
.select { |_dir, files| files.any? { |f| f.end_with?(".tf", ".tf.json") } }
88+
.transform_values! { |files| files.map { |f| File.basename(f) } }
89+
.each do |relative_path, files|
90+
name = self.class.template_name_from_git_repo_url(git_repository.url, relative_path)
91+
92+
template_full_path = File.join(git_checkout_tempdir, relative_path)
93+
94+
input_vars, output_vars, terraform_version = parse_vars_in_template(template_full_path)
95+
96+
template_dirs[name] = {
97+
:relative_path => relative_path,
98+
:files => files,
99+
:input_vars => input_vars,
100+
:output_vars => output_vars,
101+
:terraform_version => terraform_version,
102+
}
103+
end
104104
end
105105
end
106106

107107
template_dirs
108108
rescue => error
109109
_log.error("Failing scaning for terraform templates in the git repo: #{error}")
110110
raise
111-
ensure
112-
cleanup_git_repo(git_checkout_tempdir)
113111
end
114112

115113
# Parse template and return input-vars, output-vars & terraform-version
116114
def parse_vars_in_template(template_path)
117115
response = Terraform::Runner.parse_template_variables(template_path)
118116
return response['template_input_params'], response['template_output_params'], response['terraform_version']
119117
end
120-
121-
# checkout git repo to temp dir
122-
def checkout_git_repo
123-
git_checkout_tempdir = Dir.mktmpdir("embedded-terraform-runner-git")
124-
125-
_log.debug("Checking out git repository to #{git_checkout_tempdir}...")
126-
checkout_git_repository(git_checkout_tempdir)
127-
git_checkout_tempdir
128-
end
129-
130-
# clean temp dir
131-
def cleanup_git_repo(git_checkout_tempdir)
132-
return if git_checkout_tempdir.nil?
133-
134-
_log.debug("Cleaning up git repository checked out at #{git_checkout_tempdir}...")
135-
FileUtils.rm_rf(git_checkout_tempdir)
136-
rescue Errno::ENOENT
137-
nil
138-
end
139118
end

0 commit comments

Comments
 (0)