Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation for ImageService.List #206

Merged
merged 13 commits into from
Jan 23, 2025
Merged

Conversation

hdwhdw
Copy link
Contributor

@hdwhdw hdwhdw commented Jan 16, 2025

This PR contains implementation for List method in ImageService host module. The method will list the current, next and available SONiC image on the SONiC device.

It will be used in GNOI library for verifying OS installation.

@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@hdwhdw hdwhdw requested a review from Copilot January 16, 2025 16:50
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

host_modules/image_service.py:171

  • The error message should include the actual error output from the subprocess for better debugging. Suggestion: return errno.EIO, "Failed to list images: {}".format(e.output.decode())
return errno.EIO, "Failed to list images"

host_modules/image_service.py Outdated Show resolved Hide resolved
@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@hdwhdw hdwhdw requested a review from Copilot January 16, 2025 16:59

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.


except subprocess.CalledProcessError as e:
logger.error("Failed to list images: {}".format(e.output.decode()))
return errno.EIO, "Failed to list images"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return errno.EIO, "Failed to list images"

Use a consistent return pattern, such as wrapping the result in a tuple like (return_code, result)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Use json string for success case instead. Such also follows examples from docker.list

try:
output = subprocess.check_output(
["/usr/local/bin/sonic-installer", "list"],
stderr=subprocess.STDOUT,
Copy link
Contributor

@xincunli-sonic xincunli-sonic Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add indent. #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

available_images = []

for line in output:
if line.startswith("Current:"):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you checked all versions are this word? recommend use insensitive, like lower first then compare?

Copy link
Contributor Author

@hdwhdw hdwhdw Jan 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes and checked. Lower case isn't really necessary since, the output method in sonic_installer is from 2017 and haven't changed since then.

elif line.startswith("Available:"):
continue
else:
available_images.append(line.strip())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest to refactor the parsing into a separate helper function, making it easier to test and debug independently

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

logger.info("Listing SONiC images")

try:
output = subprocess.check_output(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

output

Add log for raw output for debugging purpose.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still applicable, before we do parse, we could log it for future debug purpose.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have logs for 1. before calling sonic-installer, 2. the output of sonic-installer before we try to parse it, as well as 3. when sonic-installer have a nonzero rc. Added one for parse result of sonic-installer output.

available_images.append(line.strip())

except subprocess.CalledProcessError as e:
logger.error("Failed to list images: {}".format(e.output.decode()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error

Include more details in the error messages, such as the command that failed or the subprocess exit code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

"Available:\n"
"image1\n"
"image2\n"
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There would be multiple combination for the output, you may have to test and retrieve them in multiple versions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean? All we care about is the service correctly output all of "current", "next" and "available" images, so adding different combinations doesn't really allow us to catch more bugs, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you check the implementation, you will notice that the current, next, available will be the combination, and they may not exist.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification. Added testcase for outputs like "Current: blah\nNext: blah\n".

@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).


for line in output.split("\n"):
if "current:" in line.lower():
current_image = line.split(":")[1].strip()
Copy link
Contributor

@qiluo-msft qiluo-msft Jan 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[1]

Could you add error handling of index out of range? #Closed

@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Contributor

@xincunli-sonic xincunli-sonic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@qiluo-msft qiluo-msft merged commit d88d8d0 into sonic-net:master Jan 23, 2025
5 checks passed
@hdwhdw hdwhdw deleted the image_list branch January 23, 2025 20:22
qiluo-msft pushed a commit to sonic-net/sonic-gnmi that referenced this pull request Jan 28, 2025
Why I did it
Supports OS.Verify for Upgrading OS

How I did it
Implement GNOI server and client for GNOI.OS.Verify

Backend PR: sonic-net/sonic-host-services#206

How to verify it
On Mellanox:
gnoi_client -target 127.0.0.1:50052 -logtostderr -insecure -module OS -rpc Verify
OS Verify
{"version":"SONiC-OS-20240510.23"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

5 participants