Skip to content

Commit 5adf2ac

Browse files
authored
feat(ruby): collect the logs at the end of the installation (#2148)
## Problem Agama does not copy the logs to the installed system, so they are lost. Additionally, the /var/log/agama-installation permissions are way too open. ## Solution * Save the logs into the installed system. * Set the proper permissions (700) to the /var/log/agama-installation folder (#2140). ## Testing - Added a new unit test - Tested manually
2 parents 96ee1fa + 5cba5fb commit 5adf2ac

File tree

6 files changed

+62
-18
lines changed

6 files changed

+62
-18
lines changed

service/lib/agama/manager.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,6 @@ def collect_logs(path: nil)
258258
# @param method [HALT, POWEROFF, STOP, REBOOT]
259259
# @return [Boolean]
260260
def finish_installation(method)
261-
logs = collect_logs(path: "/tmp/var/logs/")
262-
263-
logger.info("Installation logs stored in #{logs}")
264-
265261
unless installation_phase.finish?
266262
logger.error "The installer has not finished correctly. Please check logs"
267263
return false

service/lib/agama/storage/finisher.rb

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828
require "agama/helpers"
2929
require "agama/http"
3030
require "abstract_method"
31+
require "fileutils"
3132

3233
Yast.import "Arch"
34+
Yast.import "Installation"
3335

3436
module Agama
3537
module Storage
@@ -217,21 +219,31 @@ def label
217219
end
218220

219221
def run
220-
wfm_write("copy_logs_finish")
221-
copy_agama_scripts
222+
FileUtils.mkdir_p(logs_dir, mode: 0o700)
223+
collect_logs
224+
copy_scripts
222225
end
223226

224227
private
225228

226-
def copy_agama_scripts
229+
def copy_scripts
227230
return unless Dir.exist?(SCRIPTS_DIR)
228231

229-
Yast.import "Installation"
230-
require "fileutils"
231-
logs_dir = File.join(Yast::Installation.destdir, "var", "log", "agama-installation")
232-
FileUtils.mkdir_p(logs_dir)
233232
FileUtils.cp_r(SCRIPTS_DIR, logs_dir)
234233
end
234+
235+
def collect_logs
236+
path = File.join(logs_dir, "logs")
237+
Yast::Execute.locally(
238+
"agama", "logs", "store", "--destination", path
239+
)
240+
end
241+
242+
def logs_dir
243+
@logs_dir ||= File.join(
244+
Yast::Installation.destdir, "var", "log", "agama-installation"
245+
)
246+
end
235247
end
236248

237249
# Executes post-installation scripts

service/package/rubygem-agama-yast.changes

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
-------------------------------------------------------------------
2+
Wed Mar 12 00:44:33 UTC 2025 - Imobach Gonzalez Sosa <igonzalezsosa@suse.com>
3+
4+
- Copy Agama logs to the installed system (gh#agama/agama-project#2148).
5+
- Set /var/log/agama-installation permissions to 0700
6+
(gh#agama/agama-project#2140).
7+
18
-------------------------------------------------------------------
29
Wed Mar 5 14:50:04 UTC 2025 - Ladislav Slezák <lslezak@suse.com>
310

service/test/agama/manager_test.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,17 +228,11 @@
228228
let(:method) { "reboot" }
229229

230230
before do
231-
allow(subject).to receive(:collect_logs)
232231
allow(subject).to receive(:iguana?).and_return(iguana)
233232
allow(subject.installation_phase).to receive(:finish?).and_return(finished)
234233
allow(logger).to receive(:error)
235234
end
236235

237-
it "collects the logs" do
238-
expect(subject).to receive(:collect_logs)
239-
subject.finish_installation(method)
240-
end
241-
242236
context "when it is not in finish the phase" do
243237
it "logs the error and returns false" do
244238
expect(logger).to receive(:error).with(/not finished/)

service/test/agama/storage/finisher_test.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,35 @@
141141

142142
include_examples "progress"
143143
end
144+
145+
describe Agama::Storage::Finisher::CopyLogsStep do
146+
let(:logger) { Logger.new($stdout, level: :warn) }
147+
let(:scripts_dir) { File.join(tmp_dir, "run", "agama", "scripts") }
148+
let(:tmp_dir) { Dir.mktmpdir }
149+
150+
subject { Agama::Storage::Finisher::CopyLogsStep.new(logger) }
151+
152+
before do
153+
allow(Yast::Installation).to receive(:destdir).and_return(File.join(tmp_dir, "mnt"))
154+
allow(Yast::Execute).to receive(:locally)
155+
stub_const("Agama::Storage::Finisher::CopyLogsStep::SCRIPTS_DIR",
156+
File.join(tmp_dir, "run", "agama", "scripts"))
157+
end
158+
159+
after do
160+
FileUtils.remove_entry(tmp_dir)
161+
end
162+
163+
context "when scripts artifacts exist" do
164+
before do
165+
FileUtils.mkdir_p(scripts_dir)
166+
FileUtils.touch(File.join(scripts_dir, "test.sh"))
167+
end
168+
169+
it "copies the artifacts to the installed system" do
170+
subject.run
171+
expect(File).to exist(File.join(tmp_dir, "mnt", "var", "log", "agama-installation",
172+
"scripts"))
173+
end
174+
end
175+
end

service/test/agama/storage/manager_test.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@
369369
allow(File).to receive(:directory?).with("/iguana").and_return iguana
370370
allow(copy_files_class).to receive(:new).and_return(copy_files)
371371
allow(Yast::Execute).to receive(:on_target!)
372+
allow(Yast::Execute).to receive(:local)
372373
end
373374
let(:copy_files_class) { Agama::Storage::Finisher::CopyFilesStep }
374375
let(:copy_files) { instance_double(copy_files_class, run?: true, run: true, label: "Copy") }
@@ -386,8 +387,10 @@
386387
expect(scripts_client).to receive(:run).with("post")
387388
expect(Yast::Execute).to receive(:on_target!)
388389
.with("systemctl", "enable", "agama-scripts", allowed_exitstatus: [0, 1])
389-
expect(Yast::WFM).to receive(:CallFunction).with("copy_logs_finish", ["Write"])
390390
expect(Yast::WFM).to receive(:CallFunction).with("umount_finish", ["Write"])
391+
expect(Yast::Execute).to receive(:locally).with(
392+
"agama", "logs", "store", "--destination", /\/var\/log\/agama-installation\/logs/
393+
)
391394
storage.finish
392395
end
393396

0 commit comments

Comments
 (0)