From f35db4b0a1d1e54f3bd6b05e31fa6e84c85fafc6 Mon Sep 17 00:00:00 2001 From: Knut Anderssen Date: Mon, 21 Aug 2023 08:14:32 +0100 Subject: [PATCH 01/10] Copy proxy config to the target system --- service/lib/agama/network.rb | 3 +++ service/lib/agama/proxy_setup.rb | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/service/lib/agama/network.rb b/service/lib/agama/network.rb index c8c4435a72..23eef44e37 100644 --- a/service/lib/agama/network.rb +++ b/service/lib/agama/network.rb @@ -23,6 +23,7 @@ require "yast" require "yast2/systemd/service" require "y2network/proposal_settings" +require "agama/proxy_setup" Yast.import "Installation" @@ -41,6 +42,8 @@ def initialize(logger) def install copy_files enable_service + + ProxySetup.instance.install end private diff --git a/service/lib/agama/proxy_setup.rb b/service/lib/agama/proxy_setup.rb index 9852db68ca..80075c63c8 100644 --- a/service/lib/agama/proxy_setup.rb +++ b/service/lib/agama/proxy_setup.rb @@ -33,6 +33,8 @@ class ProxySetup CMDLINE_PATH = "/proc/cmdline" CMDLINE_MENU_CONF = "/etc/cmdline-menu.conf" + PACKAGES = ["microos-tools"].freeze + CONFIG_PATH = "/etc/sysconfig/proxy" # @return [URI::Generic] attr_accessor :proxy @@ -105,5 +107,21 @@ def write Proxy.Write end + + def install + return unless proxy + + copy_files + add_packages + end + + def add_packages + log.info "Selecting these packages for installation: #{PACKAGES}" + Yast::PackagesProposal.SetResolvables(PROPOSAL_ID, :package, PACKAGES) + end + + def copy_files + FileUtils.cp(CONFIG_PATH, File.join(Yast::Installation.destdir, CONFIG_PATH)) + end end end From 2e4dcadafd0d62dd4b91c40ad887868f921fd1aa Mon Sep 17 00:00:00 2001 From: Knut Anderssen Date: Mon, 21 Aug 2023 12:17:03 +0100 Subject: [PATCH 02/10] Read the current proxy configuration --- service/lib/agama/proxy_setup.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/service/lib/agama/proxy_setup.rb b/service/lib/agama/proxy_setup.rb index 80075c63c8..ef8a7a5141 100644 --- a/service/lib/agama/proxy_setup.rb +++ b/service/lib/agama/proxy_setup.rb @@ -109,7 +109,8 @@ def write end def install - return unless proxy + Proxy.Read + return unless Proxy.enabled copy_files add_packages @@ -121,6 +122,7 @@ def add_packages end def copy_files + log.info "Copying proxy configuration to the target system" FileUtils.cp(CONFIG_PATH, File.join(Yast::Installation.destdir, CONFIG_PATH)) end end From 178630106ce942a721ccfd10e9114957d8f96107 Mon Sep 17 00:00:00 2001 From: Knut Anderssen Date: Tue, 22 Aug 2023 09:36:31 +0100 Subject: [PATCH 03/10] Make install method public --- service/lib/agama/proxy_setup.rb | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/service/lib/agama/proxy_setup.rb b/service/lib/agama/proxy_setup.rb index ef8a7a5141..15b852746d 100644 --- a/service/lib/agama/proxy_setup.rb +++ b/service/lib/agama/proxy_setup.rb @@ -21,6 +21,7 @@ require "yast" require "uri" +require "fileutils" module Agama # This class is responsible of parsing the proxy url from the kernel cmdline or configured @@ -35,6 +36,7 @@ class ProxySetup CMDLINE_MENU_CONF = "/etc/cmdline-menu.conf" PACKAGES = ["microos-tools"].freeze CONFIG_PATH = "/etc/sysconfig/proxy" + PROPOSAL_ID = "network_proposal" # @return [URI::Generic] attr_accessor :proxy @@ -42,6 +44,8 @@ class ProxySetup # Constructor def initialize Yast.import "Proxy" + Yast.import "Installation" + Yast.import "PackagesProposal" end def run @@ -49,6 +53,14 @@ def run write end + def install + Proxy.Read + return unless Proxy.enabled + + copy_files + add_packages + end + private def read @@ -108,14 +120,6 @@ def write Proxy.Write end - def install - Proxy.Read - return unless Proxy.enabled - - copy_files - add_packages - end - def add_packages log.info "Selecting these packages for installation: #{PACKAGES}" Yast::PackagesProposal.SetResolvables(PROPOSAL_ID, :package, PACKAGES) @@ -123,7 +127,7 @@ def add_packages def copy_files log.info "Copying proxy configuration to the target system" - FileUtils.cp(CONFIG_PATH, File.join(Yast::Installation.destdir, CONFIG_PATH)) + ::FileUtils.cp(CONFIG_PATH, File.join(Yast::Installation.destdir, CONFIG_PATH)) end end end From 3a89c8924f50ac728dc8d55734c36168d925040f Mon Sep 17 00:00:00 2001 From: Knut Anderssen Date: Tue, 22 Aug 2023 10:11:59 +0100 Subject: [PATCH 04/10] Added ProxySetup#install unit test --- service/test/agama/proxy_setup_test.rb | 51 +++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/service/test/agama/proxy_setup_test.rb b/service/test/agama/proxy_setup_test.rb index 484541296a..14b0e08b96 100644 --- a/service/test/agama/proxy_setup_test.rb +++ b/service/test/agama/proxy_setup_test.rb @@ -28,15 +28,15 @@ proxy.proxy = nil end + before do + allow(Yast::Proxy).to receive(:Read) + allow(Yast::Proxy).to receive(:Write) + end + describe "#run" do let(:file_content) { "proxy=#{proxy_url}" } let(:proxy_url) { "https://yast:1234@192.168.122.1:3128" } - before do - allow(Yast::Proxy).to receive(:Read) - allow(Yast::Proxy).to receive(:Write) - end - context "when some configuration is given through the kernel command line" do before do allow(proxy).to receive(:proxy_from_cmdline).and_return(URI(proxy_url)) @@ -75,4 +75,45 @@ end end end + + describe "#install" do + let(:config) do + { + "enabled" => false + } + end + + before do + Yast::Proxy.Import(config) + allow(Yast::Installation).to receive(:destdir).and_return("/mnt") + end + + it "reads the current Proxy configuration from the inst-sys" do + expect(Yast::Proxy).to receive(:Read) + + proxy.install + end + + context "when the use of proxy is disabled" do + it "does not copy the configuration to the target system" do + expect(FileUtils).to_not receive(:cp) + proxy.install + end + end + + context "when the use of proxy is enabled" do + let(:config) do + { + "enabled" => true, + "http_proxy" => "http://192.168.122.1:3128" + } + end + + it "copies the configuration to the target system" do + expect(FileUtils).to receive(:cp).with(described_class::CONFIG_PATH, + File.join("/mnt", described_class::CONFIG_PATH)) + proxy.install + end + end + end end From 7ea9c0fc4b71e4209a61893ee3299c71268fc655 Mon Sep 17 00:00:00 2001 From: Knut Anderssen Date: Tue, 22 Aug 2023 11:39:45 +0100 Subject: [PATCH 05/10] Ensure the read is done out of the chroot --- service/lib/agama/helpers.rb | 19 +++++++++++++++++++ service/lib/agama/manager.rb | 1 + service/lib/agama/proxy_setup.rb | 12 ++++++++---- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/service/lib/agama/helpers.rb b/service/lib/agama/helpers.rb index e8cd993b11..d60bbbd107 100644 --- a/service/lib/agama/helpers.rb +++ b/service/lib/agama/helpers.rb @@ -45,5 +45,24 @@ def on_target(&block) Yast::WFM.SCRClose(handle) end end + + # Run a block in the local system + # + # @param block [Proc] Block to run on the local system + def on_local(&block) + Yast.import "WFM" + old_handle = Yast::WFM.SCRGetDefault + handle = Yast::WFM.SCROpen("chroot=/:scr", false) + Yast::WFM.SCRSetDefault(handle) + + begin + block.call + rescue StandardError => e + logger.error "Error while running on target tasks: #{e.inspect}" + ensure + Yast::WFM.SCRSetDefault(old_handle) + Yast::WFM.SCRClose(handle) + end + end end end diff --git a/service/lib/agama/manager.rb b/service/lib/agama/manager.rb index 6eea3dba66..187b402c4e 100644 --- a/service/lib/agama/manager.rb +++ b/service/lib/agama/manager.rb @@ -22,6 +22,7 @@ require "yast" require "agama/config" require "agama/network" +require "agama/proxy_setup" require "agama/with_progress" require "agama/installation_phase" require "agama/service_status_recorder" diff --git a/service/lib/agama/proxy_setup.rb b/service/lib/agama/proxy_setup.rb index 15b852746d..cdc32a252d 100644 --- a/service/lib/agama/proxy_setup.rb +++ b/service/lib/agama/proxy_setup.rb @@ -22,6 +22,7 @@ require "yast" require "uri" require "fileutils" +require "agama/helpers" module Agama # This class is responsible of parsing the proxy url from the kernel cmdline or configured @@ -31,6 +32,7 @@ class ProxySetup include Singleton include Yast include Logger + include Helpers CMDLINE_PATH = "/proc/cmdline" CMDLINE_MENU_CONF = "/etc/cmdline-menu.conf" @@ -54,11 +56,13 @@ def run end def install - Proxy.Read - return unless Proxy.enabled + on_local do + Proxy.Read + return unless Proxy.enabled - copy_files - add_packages + copy_files + add_packages + end end private From b92528e37a6f26ecfc234c05ff50628c23cd795b Mon Sep 17 00:00:00 2001 From: Knut Anderssen Date: Wed, 23 Aug 2023 11:36:48 +0100 Subject: [PATCH 06/10] Add the needed packages before installing the software --- service/lib/agama/manager.rb | 2 ++ service/lib/agama/proxy_setup.rb | 23 +++++++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/service/lib/agama/manager.rb b/service/lib/agama/manager.rb index 187b402c4e..429652d1a4 100644 --- a/service/lib/agama/manager.rb +++ b/service/lib/agama/manager.rb @@ -109,6 +109,8 @@ def install_phase software.propose end + ProxySetup.instance.propose + progress.step("Installing Software") { software.install } on_target do diff --git a/service/lib/agama/proxy_setup.rb b/service/lib/agama/proxy_setup.rb index cdc32a252d..46704f47b1 100644 --- a/service/lib/agama/proxy_setup.rb +++ b/service/lib/agama/proxy_setup.rb @@ -48,6 +48,8 @@ def initialize Yast.import "Proxy" Yast.import "Installation" Yast.import "PackagesProposal" + + Proxy.Read end def run @@ -55,13 +57,16 @@ def run write end + def propose + on_target { add_packages } if Proxy.enabled + end + def install - on_local do - Proxy.Read - return unless Proxy.enabled + return unless Proxy.enabled + on_local do copy_files - add_packages + enable_services end end @@ -133,5 +138,15 @@ def copy_files log.info "Copying proxy configuration to the target system" ::FileUtils.cp(CONFIG_PATH, File.join(Yast::Installation.destdir, CONFIG_PATH)) end + + def enable_services + service = Yast2::Systemd::Service.find("setup-systemd-proxy-env") + if service.nil? + logger.error "setup-systemd-proxy-env service was not found" + return + end + + Yast::Execute.on_target!("systemctl", "enable", "setup-systemd-proxy-env.path") + end end end From bab9875d180852bd23a258e41884b844c4955bf8 Mon Sep 17 00:00:00 2001 From: Knut Anderssen Date: Thu, 24 Aug 2023 10:25:08 +0100 Subject: [PATCH 07/10] Move the proxy propose before software proposal is done --- service/lib/agama/manager.rb | 10 ++++++++-- service/lib/agama/proxy_setup.rb | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/service/lib/agama/manager.rb b/service/lib/agama/manager.rb index 429652d1a4..dfb2210de3 100644 --- a/service/lib/agama/manager.rb +++ b/service/lib/agama/manager.rb @@ -104,13 +104,12 @@ def install_phase progress.step("Partitioning") do storage.install + proxy.propose # propose software after /mnt is already separated, so it uses proper # target software.propose end - ProxySetup.instance.propose - progress.step("Installing Software") { software.install } on_target do @@ -140,6 +139,13 @@ def software end end + # ProxySetup instance + # + # @return [ProxySetup] + def proxy + ProxySetup.instance + end + # Language manager # # @return [DBus::Clients::Locale] diff --git a/service/lib/agama/proxy_setup.rb b/service/lib/agama/proxy_setup.rb index 46704f47b1..4b7e72d993 100644 --- a/service/lib/agama/proxy_setup.rb +++ b/service/lib/agama/proxy_setup.rb @@ -58,7 +58,7 @@ def run end def propose - on_target { add_packages } if Proxy.enabled + add_packages if Proxy.enabled end def install From 6821ba258acb82e44279b6652339e2c4f8e30a46 Mon Sep 17 00:00:00 2001 From: Knut Anderssen Date: Thu, 24 Aug 2023 10:55:09 +0100 Subject: [PATCH 08/10] Logger fix and more unit tests --- service/lib/agama/proxy_setup.rb | 4 +++- service/test/agama/manager_test.rb | 10 ++++++++ service/test/agama/proxy_setup_test.rb | 32 ++++++++++++++++++++++---- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/service/lib/agama/proxy_setup.rb b/service/lib/agama/proxy_setup.rb index 4b7e72d993..cfb59e26df 100644 --- a/service/lib/agama/proxy_setup.rb +++ b/service/lib/agama/proxy_setup.rb @@ -43,6 +43,8 @@ class ProxySetup # @return [URI::Generic] attr_accessor :proxy + alias_method :logger, :log + # Constructor def initialize Yast.import "Proxy" @@ -142,7 +144,7 @@ def copy_files def enable_services service = Yast2::Systemd::Service.find("setup-systemd-proxy-env") if service.nil? - logger.error "setup-systemd-proxy-env service was not found" + log.error "setup-systemd-proxy-env service was not found" return end diff --git a/service/test/agama/manager_test.rb b/service/test/agama/manager_test.rb index cf57345473..b050600515 100644 --- a/service/test/agama/manager_test.rb +++ b/service/test/agama/manager_test.rb @@ -34,6 +34,9 @@ end let(:config) { Agama::Config.from_file(config_path) } let(:logger) { Logger.new($stdout, level: :warn) } + let(:proxy) do + instance_double(Agama::ProxySetup, propose: nil, install: nil) + end let(:software) do instance_double( @@ -61,6 +64,7 @@ before do allow(Agama::Network).to receive(:new).and_return(network) + allow(Agama::ProxySetup).to receive(:instance).and_return(proxy) allow(Agama::DBus::Clients::Locale).to receive(:new).and_return(locale) allow(Agama::DBus::Clients::Software).to receive(:new).and_return(software) allow(Agama::DBus::Clients::Storage).to receive(:new).and_return(storage) @@ -115,6 +119,12 @@ expect(subject.installation_phase.install?).to eq(true) end + it "calls #propose on proxy and software modules" do + expect(proxy).to receive(:propose) + expect(software).to receive(:propose) + subject.install_phase + end + it "calls #install (or #write) method of each module" do expect(network).to receive(:install) expect(software).to receive(:install) diff --git a/service/test/agama/proxy_setup_test.rb b/service/test/agama/proxy_setup_test.rb index 14b0e08b96..c4246921dc 100644 --- a/service/test/agama/proxy_setup_test.rb +++ b/service/test/agama/proxy_setup_test.rb @@ -76,7 +76,7 @@ end end - describe "#install" do + describe "#propose" do let(:config) do { "enabled" => false @@ -88,10 +88,34 @@ allow(Yast::Installation).to receive(:destdir).and_return("/mnt") end - it "reads the current Proxy configuration from the inst-sys" do - expect(Yast::Proxy).to receive(:Read) + context "when the use of proxy is enabled" do + let(:config) do + { + "enabled" => true, + "http_proxy" => "http://192.168.122.1:3128" + } + end + + it "adds microos-tools package to the set of resolvables" do + expect(Yast::PackagesProposal).to receive(:SetResolvables) do |_, _, packages| + expect(packages).to contain_exactly("microos-tools") + end + + proxy.propose + end + end + end + + describe "#install" do + let(:config) do + { + "enabled" => false + } + end - proxy.install + before do + Yast::Proxy.Import(config) + allow(Yast::Installation).to receive(:destdir).and_return("/mnt") end context "when the use of proxy is disabled" do From 1b91c2444f897fd1fc466fefb7ab2ea5a05851a9 Mon Sep 17 00:00:00 2001 From: Knut Anderssen Date: Thu, 24 Aug 2023 13:00:07 +0100 Subject: [PATCH 09/10] Enable service in target --- service/lib/agama/proxy_setup.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/service/lib/agama/proxy_setup.rb b/service/lib/agama/proxy_setup.rb index cfb59e26df..00ae9e3f88 100644 --- a/service/lib/agama/proxy_setup.rb +++ b/service/lib/agama/proxy_setup.rb @@ -66,10 +66,8 @@ def propose def install return unless Proxy.enabled - on_local do - copy_files - enable_services - end + on_local { copy_files } + enable_services end private @@ -148,6 +146,7 @@ def enable_services return end + Yast::Execute.on_target!("systemctl", "enable", "setup-systemd-proxy-env.service") Yast::Execute.on_target!("systemctl", "enable", "setup-systemd-proxy-env.path") end end From 393e0213d8856f6cc27468b57cf413fa281ea870 Mon Sep 17 00:00:00 2001 From: Knut Anderssen Date: Mon, 28 Aug 2023 09:01:33 +0100 Subject: [PATCH 10/10] Added changelog --- service/package/rubygem-agama.changes | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/service/package/rubygem-agama.changes b/service/package/rubygem-agama.changes index 292cb806d8..fd7da96943 100644 --- a/service/package/rubygem-agama.changes +++ b/service/package/rubygem-agama.changes @@ -1,3 +1,9 @@ +------------------------------------------------------------------- +Mon Aug 28 07:59:26 UTC 2023 - Knut Anderssen + +- Copy the proxy configuration to the target system when needed + (bsc#1212677, gh#openSUSE/agama#711). + ------------------------------------------------------------------- Wed Aug 23 10:39:46 UTC 2023 - Imobach Gonzalez Sosa