Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure yum proxy if given for updates #16972

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/models/miq_server/update_management.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) }
Expand Down
35 changes: 35 additions & 0 deletions spec/models/miq_server/update_management_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down