diff --git a/service/lib/agama/network.rb b/service/lib/agama/network.rb index 4a7b0053b8..fb2bdc55e7 100644 --- a/service/lib/agama/network.rb +++ b/service/lib/agama/network.rb @@ -71,6 +71,7 @@ def unlink_resolv # @return [Logger] attr_reader :logger + HOSTNAME = "/etc/hostname" RESOLV = "/etc/resolv.conf" RESOLV_FLAG = "/run/agama/manage_resolv" ETC_NM_DIR = "/etc/NetworkManager" @@ -89,6 +90,8 @@ def enable_service # Copies NetworkManager configuration files def copy_files + copy(HOSTNAME) + return unless Dir.exist?(ETC_NM_DIR) # runtime configuration is copied first, so in case of later modification @@ -117,5 +120,21 @@ def copy_directory(source, target) FileUtils.mkdir_p(target) FileUtils.cp(Dir.glob(File.join(source, "*")), target) end + + # Copies a file + # + # This method checks whether the source file exists. It copies the file to the target system if + # it exists + # + # @param source [String] source file + # @param target [String,nil] target directory, only needed in case it is different to the + # original source path in the target system. + def copy(source, target = nil) + return unless File.exist?(source) + + path = target || File.join(Yast::Installation.destdir, source) + FileUtils.mkdir_p(File.dirname(path)) + FileUtils.copy_entry(source, path) + end end end diff --git a/service/package/rubygem-agama-yast.changes b/service/package/rubygem-agama-yast.changes index 166f9a8033..411ae1d402 100644 --- a/service/package/rubygem-agama-yast.changes +++ b/service/package/rubygem-agama-yast.changes @@ -1,3 +1,9 @@ +------------------------------------------------------------------- +Fri Mar 28 19:22:51 UTC 2025 - Knut Anderssen + +- Copy the hostname to the installed system if it exists + (gh#agama-project/agama#2226). + ------------------------------------------------------------------- Fri Mar 28 11:13:48 UTC 2025 - Imobach Gonzalez Sosa diff --git a/service/test/agama/network_test.rb b/service/test/agama/network_test.rb index eee39498ac..be67b98ffd 100644 --- a/service/test/agama/network_test.rb +++ b/service/test/agama/network_test.rb @@ -29,9 +29,13 @@ let(:logger) { Logger.new($stdout, level: :warn) } let(:targetdir) { File.join(rootdir, "mnt") } + let(:fixtures) { File.join(FIXTURES_PATH, "root_dir") } + let(:hostname_path) { File.join(fixtures, "etc", "hostname") } before do allow(Yast::Installation).to receive(:destdir).and_return(targetdir) + stub_const("Agama::Network::HOSTNAME", hostname_path) + stub_const("Agama::Network::RUN_NM_DIR", File.join(rootdir, "run", "NetworkManager")) end after do @@ -84,6 +88,20 @@ ) end end + context "when an static hostname is present" do + let(:test_path) { File.join(fixtures, "etc", "hostname.test") } + + around do |block| + FileUtils.mv test_path, hostname_path + block.call + FileUtils.mv hostname_path, test_path + end + + it "copies it to the target system" do + network.install + expect(File.exist?(File.join(targetdir, Agama::Network::HOSTNAME))).to eql(true) + end + end it "enables the NetworkManager service" do expect(service).to receive(:enable) @@ -103,14 +121,12 @@ describe "#link_resolv" do let(:rootdir) { Dir.mktmpdir } - let(:fixtures) { File.join(FIXTURES_PATH, "root_dir") } let(:resolv_fixture) { File.join(FIXTURES_PATH, "etc", "resolv.conf") } let(:resolv_flag) { File.join(rootdir, "run", "agama", "manage_resolv") } let(:resolv) { File.join(targetdir, "etc", "resolv.conf") } before do stub_const("Agama::Network::RESOLV_FLAG", resolv_flag) - stub_const("Agama::Network::RUN_NM_DIR", File.join(rootdir, "run", "NetworkManager")) FileUtils.mkdir_p targetdir FileUtils.cp_r(Dir["#{fixtures}/*"], rootdir) FileUtils.cp_r(Dir["#{fixtures}/*"], targetdir) diff --git a/service/test/fixtures/root_dir/etc/hostname.test b/service/test/fixtures/root_dir/etc/hostname.test new file mode 100644 index 0000000000..069f0515ad --- /dev/null +++ b/service/test/fixtures/root_dir/etc/hostname.test @@ -0,0 +1 @@ +agama