Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Commit

Permalink
Make debug logs get behave more intuitively (#3573)
Browse files Browse the repository at this point in the history
* Update the printed message for debug logs get to be more accurate

* Fix min() that was being passed an Optional[int]

* Negate equality operator

* Change != to 'is not'

* Make --all and --last noops, add --out_dir, and always download all log files

* Add del statements for unused parameters
  • Loading branch information
kananb authored Oct 18, 2023
1 parent 09c8579 commit 86a642b
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions src/cli/onefuzz/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from azure.applicationinsights.models import QueryBody
from azure.identity import AzureCliCredential
from azure.storage.blob import ContainerClient
from onefuzztypes import models, requests, responses
from onefuzztypes import models, primitives, requests, responses
from onefuzztypes.enums import ContainerType, TaskType
from onefuzztypes.models import (
BlobRef,
Expand Down Expand Up @@ -635,20 +635,26 @@ def get(
job_id: Optional[str],
task_id: Optional[str],
machine_id: Optional[str],
last: Optional[int] = 1,
out_dir: Optional[primitives.Directory],
last: Optional[int] = None,
all: bool = False,
) -> None:
"""
Download the latest agent logs.
Download all of the agent logs.
Make sure you have Storage Blob Data Reader permission.
:param str job_id: Which job you would like the logs for.
:param str task_id: Which task you would like the logs for.
:param str machine_id: Which machine you would like the logs for.
:param int last: The logs are split in files. Starting with the newest files, how many files you would you like to download.
:param bool all: Download all log files.
:param str out_dir: The directory where you would like to download the logs.
:param int last: (DEPRECATED) This option is a no-op. Now that machines have just one log file, getting the most recent logs implies downloading all of the log files.
:param bool all: (DEPRECATED) This option is a no-op. Now that machines have just one log file, getting the most recent logs implies downloading all of the log files.
"""

# Appease mypy while these options are still part of the API
del last
del all

from typing import cast
from urllib import parse

Expand All @@ -667,11 +673,14 @@ def get(
f"Job with id {job_id} does not have a logging location configured"
)

granularity = "job"
file_path = None
if task_id is not None:
granularity = "task"
file_path = f"{task_id}/"

if machine_id is not None:
granularity = "machine"
file_path += f"{machine_id}/"

container_name = parse.urlsplit(container_url).path[1:]
Expand Down Expand Up @@ -707,14 +716,23 @@ def __init__(self, name: str, creation_time: datetime):
self.logger.info("Did not find any matching files to download")
return None

if not all:
self.logger.info(f"Downloading only the {last} most recent files")
files = files[:last]
if granularity == "job":
self.logger.info(
f"Downloading all of the log files for each task associated with the job with id {job_id}"
)
elif granularity == "task":
self.logger.info(
f"Downloading the log file for each machine associated with the task with id {task_id}"
)
else:
self.logger.info(
f"Downloading the log file for the machine with id {machine_id}"
)

for f in files:
self.logger.info(f"Downloading {f.name}")

local_path = os.path.join(os.getcwd(), f.name)
local_path = os.path.join(out_dir or os.getcwd(), f.name)
local_directory = os.path.dirname(local_path)
if not os.path.exists(local_directory):
os.makedirs(local_directory)
Expand Down

0 comments on commit 86a642b

Please sign in to comment.