-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli: refactor collect-logs to the new approach
Signed-off-by: Renan Rodrigo <[email protected]>
- Loading branch information
1 parent
41f9d52
commit 6c5095d
Showing
4 changed files
with
70 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Feature: Pro Client help text | ||
|
||
Scenario Outline: Help text for the Pro Client commands | ||
Given a `<release>` `<machine_type>` machine with ubuntu-advantage-tools installed | ||
When I run `pro collect-logs --help` as non-root | ||
Then stdout matches regexp: | ||
""" | ||
usage: pro collect-logs \[flags\] | ||
Collect logs and relevant system information into a tarball. | ||
(optional arguments|options): | ||
-h, --help show this help message and exit | ||
-o OUTPUT, --output OUTPUT | ||
tarball where the logs will be stored. \(Defaults to | ||
./pro_logs.tar.gz\) | ||
""" | ||
|
||
Examples: ubuntu release | ||
| release | machine_type | | ||
| xenial | lxd-container | | ||
| bionic | lxd-container | | ||
| focal | lxd-container | | ||
| jammy | lxd-container | | ||
| noble | lxd-container | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import argparse | ||
import logging | ||
import tarfile | ||
import tempfile | ||
|
||
from uaclient.actions import collect_logs | ||
from uaclient.cli.commands import ProArgument, ProCommand | ||
from uaclient.config import UAConfig | ||
from uaclient.messages import ( | ||
CLI_COLLECT_LOGS_DESC, | ||
CLI_COLLECT_LOGS_OUTPUT, | ||
CLI_ROOT_COLLECT_LOGS, | ||
) | ||
from uaclient.util import replace_top_level_logger_name | ||
|
||
PRO_COLLECT_LOGS_FILE = "pro_logs.tar.gz" | ||
LOG = logging.getLogger(replace_top_level_logger_name(__name__)) | ||
|
||
|
||
def action_collect_logs(args: argparse.Namespace, cfg: UAConfig): | ||
output_file = args.output or PRO_COLLECT_LOGS_FILE | ||
with tempfile.TemporaryDirectory() as output_dir: | ||
collect_logs(cfg, output_dir) | ||
try: | ||
with tarfile.open(output_file, "w:gz") as results: | ||
results.add(output_dir, arcname="logs/") | ||
except PermissionError as e: | ||
LOG.error(e) | ||
return 1 | ||
return 0 | ||
|
||
|
||
collect_logs_command = ProCommand( | ||
"collect-logs", | ||
help=CLI_ROOT_COLLECT_LOGS, | ||
description=CLI_COLLECT_LOGS_DESC, | ||
action=action_collect_logs, | ||
arguments=[ | ||
ProArgument("--output", short_name="-o", help=CLI_COLLECT_LOGS_OUTPUT) | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters