From 5798d83abcf15d099dc04135cc4a61c66209e881 Mon Sep 17 00:00:00 2001 From: Brandon Dunne Date: Thu, 8 Feb 2018 11:11:47 -0500 Subject: [PATCH] Configure yum proxy if given for updates https://bugzilla.redhat.com/show_bug.cgi?id=1540550 --- app/models/miq_server/update_management.rb | 11 ++++++ .../miq_server/update_management_spec.rb | 35 +++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/app/models/miq_server/update_management.rb b/app/models/miq_server/update_management.rb index 746c1f9ee3b..0519abbf6f3 100644 --- a/app/models/miq_server/update_management.rb +++ b/app/models/miq_server/update_management.rb @@ -60,6 +60,7 @@ def update_registration_status def attempt_registration return unless register attach_products + configure_yum_proxy # HACK: #enable_repos is not always successful immediately after #attach_products, retry to ensure they are enabled. 5.times { repos_enabled? ? break : enable_repos } end @@ -102,6 +103,16 @@ def attach_products LinuxAdmin::SubscriptionManager.subscribe(assemble_registration_options) end + def configure_yum_proxy + registration_options = assemble_registration_options + return unless registration_options[:proxy_address] + conf = IniFile.load("/etc/yum.conf") + conf["main"]["proxy"] = registration_options[:proxy_address] + conf["main"]["proxy_username"] = registration_options[:proxy_username] if registration_options[:proxy_username] + conf["main"]["proxy_password"] = registration_options[:proxy_password] if registration_options[:proxy_password] + conf.save + end + def repos_enabled? enabled = LinuxAdmin::SubscriptionManager.enabled_repos if MiqDatabase.first.update_repo_names.all? { |desired| enabled.include?(desired) } diff --git a/spec/models/miq_server/update_management_spec.rb b/spec/models/miq_server/update_management_spec.rb index 672fedee44f..5563cfe12b5 100644 --- a/spec/models/miq_server/update_management_spec.rb +++ b/spec/models/miq_server/update_management_spec.rb @@ -139,6 +139,41 @@ end end + context "#configure_yum_proxy" do + it "with no proxy server" do + expect(IniFile).not_to receive(:load) + + @server.configure_yum_proxy + end + + it "with proxy server but no credentials" do + database.update_attributes(:registration_http_proxy_server => "http://my_proxy:port") + + Tempfile.open do |tempfile| + stub_inifile = IniFile.new(:filename => tempfile.path) + expect(IniFile).to receive(:load).and_return(stub_inifile) + + @server.configure_yum_proxy + + expect(File.read(tempfile)).to eq("[main]\nproxy = http://my_proxy:port\n\n") + end + end + + it "with proxy server and credentials" do + database.update_authentication(:registration_http_proxy => {:userid => "user", :password => "pass"}) + database.update_attributes(:registration_http_proxy_server => "http://my_proxy:port") + + Tempfile.open do |tempfile| + stub_inifile = IniFile.new(:filename => tempfile.path) + expect(IniFile).to receive(:load).and_return(stub_inifile) + + @server.configure_yum_proxy + + expect(File.read(tempfile)).to eq("[main]\nproxy = http://my_proxy:port\nproxy_username = user\nproxy_password = pass\n\n") + end + end + end + context "#repo_enabled?" do it "true" do expect(reg_system).to receive(:enabled_repos).and_return(["abc", database.update_repo_names].flatten)