Skip to content

Commit 9fae56b

Browse files
committed
Handle guest logs
Fetch log content,save it to provided location or current dir
1 parent 9519817 commit 9fae56b

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

tmt/steps/finish/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ def go(self, force: bool = False) -> None:
189189

190190
# Stop and remove provisioned guests
191191
for guest in self.plan.provision.guests():
192+
guest.handle_guest_logs()
192193
guest.stop()
193194
guest.remove()
194195

tmt/steps/provision/__init__.py

+16
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,22 @@ def requires(cls) -> list['tmt.base.Dependency']:
11031103
""" All requirements of the guest implementation """
11041104
return []
11051105

1106+
def acquire_log(self, log_name: str) -> Optional[str]:
1107+
"""fetch and return content of a requested log"""
1108+
raise NotImplementedError
1109+
1110+
def store_log(
1111+
self,
1112+
log_name: Optional[str],
1113+
log_path: Optional[Path],
1114+
log_content: str) -> None:
1115+
"""save log content and return the path"""
1116+
raise NotImplementedError
1117+
1118+
def handle_guest_logs(self, log_list: Optional[list[str]] = None) -> None:
1119+
"""get log content and save it to the workdir/provision/,
1120+
list the log dir in guests.yaml"""
1121+
11061122

11071123
@dataclasses.dataclass
11081124
class GuestSshData(GuestData):

tmt/steps/provision/local.py

+37
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,43 @@ def pull(
133133
extend_options: Optional[list[str]] = None) -> None:
134134
""" Nothing to be done to pull workdir """
135135

136+
def acquire_log(self, log_name: str) -> Optional[str]:
137+
"""fetch and return content of a requested log"""
138+
if log_name == 'dmesg':
139+
return self.execute(Command('dmesg')).stdout
140+
return None
141+
142+
def store_log(
143+
self,
144+
log_name: Optional[str],
145+
log_path: Optional[Path],
146+
log_content: str) -> None:
147+
"""save log content and return the path"""
148+
if log_path and log_name:
149+
# if log_path contains log file name
150+
if log_path.name == log_name:
151+
log_path.write_text(log_content)
152+
else:
153+
(log_path / log_name).write_text(log_content)
154+
elif log_path and not log_name:
155+
log_path.write_text(log_content)
156+
elif log_name and not log_path:
157+
self.info('no log_path provided,store log to current dir')
158+
(Path.cwd() / log_name).write_text(log_content)
159+
else:
160+
self.warn('log_name and log_path cannot both be None')
161+
162+
def handle_guest_logs(self, logs: Optional[list[str]] = None) -> None:
163+
"""get log content and save it to the workdir/provision/,
164+
list the log dir in guests.yaml"""
165+
logs = logs or []
166+
for log_name in logs:
167+
log_content = self.acquire_log(log_name)
168+
if log_content:
169+
self.store_log(log_name, self.workdir, log_content)
170+
else:
171+
self.debug(f'no content in {log_name}')
172+
136173

137174
@tmt.steps.provides_method('local')
138175
class ProvisionLocal(tmt.steps.provision.ProvisionPlugin[ProvisionLocalData]):

0 commit comments

Comments
 (0)