Skip to content

Commit

Permalink
Fixes #37900 - Allow syncing templates through HTTP proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
adamlazik1 committed Oct 9, 2024
1 parent 6dedbaa commit fa2aab3
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 10 deletions.
4 changes: 3 additions & 1 deletion app/controllers/api/v2/template_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class TemplateController < ::Api::V2::BaseController
param :repo, String, :required => false, :desc => N_("Override the default repo from settings.")
param :filter, String, :required => false, :desc => N_("Export templates with names matching this regex (case-insensitive; snippets are not filtered).")
param :negate, :bool, :required => false, :desc => N_("Negate the prefix (for purging).")
param :dirname, String, :required => false, :desc => N_("The directory within Git repo containing the templates")
param :dirname, String, :required => false, :desc => N_("Directory within Git repo containing the templates.")
param :http_proxy_policy, ForemanTemplates.http_proxy_policy_types.keys, :required => false, :desc => N_("HTTP proxy policy for template sync.")
param :http_proxy_id, :number, :required => false, :desc => N_("ID of an HTTP proxy to use for template sync.")
end

api :POST, "/templates/import/", N_("Initiate Import")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module TemplateParams

class_methods do
def filter_params_list
%i(verbose repo branch dirname filter negate metadata_export_mode)
%i(verbose repo branch dirname filter negate metadata_export_mode http_proxy_policy http_proxy_id)
end

def extra_import_params
Expand Down
15 changes: 14 additions & 1 deletion app/controllers/ui_template_syncs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ def render_errors(messages, severity = 'danger')
private

def setting_definitions(short_names)
short_names.map { |name| Foreman.settings.find("template_sync_#{name}") }
settings = short_names.map { |name| Foreman.settings.find("template_sync_#{name}") }
proxy_policy_index = settings.find_index { |setting| setting.name == 'template_sync_http_proxy_policy' }
settings.insert(proxy_policy_index + 1, select_http_proxy_setting)
end

def select_http_proxy_setting
OpenStruct.new(id: 'template_sync_http_proxy_id',
name: 'template_sync_http_proxy_id',
value: "",
description: N_('Select an HTTP proxy to use for template sync'),
settings_type: :string,
default: "",
full_name: N_('HTTP proxy'),
select_values: HttpProxy.authorized(:view_http_proxies).with_taxonomy_scope.each_with_object({}) { |proxy, hash| hash[proxy.id] = proxy.name })
end
end
27 changes: 26 additions & 1 deletion app/services/foreman_templates/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def self.repo_start_with
end

def self.setting_overrides
%i(verbose prefix dirname filter repo negate branch)
%i(verbose prefix dirname filter repo negate branch http_proxy_policy http_proxy_id)
end

def method_missing(method, *args, &block)
Expand Down Expand Up @@ -53,9 +53,34 @@ def verify_path!(path)
private

def assign_attributes(args = {})
@http_proxy_id = args[:http_proxy_id]
self.class.setting_overrides.each do |attribute|
instance_variable_set("@#{attribute}", args[attribute.to_sym] || Setting["template_sync_#{attribute}".to_sym])
end
end

protected

def init_git_repo
git_repo = Git.init(@dir)
clone_msg = "cloned '#{@repo}' to '#{@dir}'"

http_proxy = case @http_proxy_policy
when 'global'
Setting[:http_proxy]
when 'selected'
HttpProxy.authorized(:view_http_proxies).with_taxonomy_scope.find(@http_proxy_id)
end

if http_proxy.present?
git_repo.config('http.proxy', http_proxy)
clone_msg += " through http proxy '#{http_proxy}'"
end

git_repo.add_remote('origin', @repo)
git_repo.fetch
logger.debug clone_msg
git_repo
end
end
end
3 changes: 1 addition & 2 deletions app/services/foreman_templates/template_exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ def export_to_git
@dir = Dir.mktmpdir
return if branch_missing?

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

setup_git_branch git_repo
dump_files!
Expand Down
5 changes: 2 additions & 3 deletions app/services/foreman_templates/template_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ def import_from_git
@dir = Dir.mktmpdir

begin
logger.debug "cloned '#{@repo}' to '#{@dir}'"
gitrepo = Git.clone(@repo, @dir)
if @branch
gitrepo = init_git_repo
if @branch.present?
logger.debug "checking out branch '#{@branch}'"
gitrepo.checkout(@branch)
end
Expand Down
6 changes: 5 additions & 1 deletion lib/foreman_templates.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'foreman_templates/engine'

module ForemanTemplates
BASE_SETTING_NAMES = %w(repo branch dirname filter negate).freeze
BASE_SETTING_NAMES = %w(repo branch dirname filter negate http_proxy_policy).freeze
IMPORT_SETTING_NAMES = (BASE_SETTING_NAMES | %w(prefix associate force lock)).freeze
EXPORT_SETTING_NAMES = (BASE_SETTING_NAMES | %w(metadata_export_mode commit_msg)).freeze

Expand All @@ -16,4 +16,8 @@ def self.lock_types
def self.metadata_export_mode_types
{ 'refresh' => _('Refresh'), 'keep' => _('Keep'), 'remove' => _('Remove') }
end

def self.http_proxy_policy_types
{ 'global' => _('Global default HTTP proxy'), 'none' => _('No HTTP proxy'), 'selected' => _('Use selected HTTP proxy') }
end
end
6 changes: 6 additions & 0 deletions lib/foreman_templates/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ class Engine < ::Rails::Engine
description: N_('Custom commit message for templates export'),
default: 'Templates export made by a Foreman user',
full_name: N_('Commit message'))
setting('template_sync_http_proxy_policy',
type: :string,
description: N_('HTTP proxy policy for template sync'),
default: 'global',
full_name: N_('HTTP proxy policy'),
collection: -> { ForemanTemplates.http_proxy_policy_types })
end
end

Expand Down

0 comments on commit fa2aab3

Please sign in to comment.