-
Notifications
You must be signed in to change notification settings - Fork 89
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
DBUS API for GNOI System.SetPackage #171
Conversation
""" | ||
|
||
@host_service.method(host_service.bus_name(MOD_NAME), in_signature='ss', out_signature='is') | ||
def download(self, image_url, save_as): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to verify this image_url?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think dbus services should be a thin layer above CLI and we can push the complexity of such to the caller (gNOI). So this one (download) is basically a wrapper for curl and we don't do additional logic here. What do you think?
host_modules/image_service.py
Outdated
|
||
|
||
@host_service.method(host_service.bus_name(MOD_NAME), in_signature='s', out_signature='is') | ||
def install(self, path): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this path be a url?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes (didn't know that). Similar to above this is a thin layer around "sonic-installer install" so if sonic-installer supports url this also can be a url. Renamed the variable to reflect that.
host_modules/image_service.py
Outdated
|
||
MOD_NAME = "image_service" | ||
|
||
DEFAULT_IMAGE_SAVE_AS = "/host/downloaded-sonic" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
@host_service.method( | ||
host_service.bus_name(MOD_NAME), in_signature="ss", out_signature="is" | ||
) | ||
def download(self, image_url, save_as): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, and added unit tests.
host_modules/image_service.py
Outdated
dir = os.path.dirname(save_as) | ||
if not os.path.exists(dir): | ||
os.makedirs(dir) | ||
cmd = ["/usr/bin/curl", "-Lo", save_as, image_url] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
host_modules/image_service.py
Outdated
dir = os.path.dirname(save_as) | ||
if not os.path.exists(dir): | ||
os.makedirs(dir) | ||
cmd = ["/usr/bin/curl", "-Lo", save_as, image_url] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If save_as == TMP_IMAGE_FILE, what will happen? Does it harm?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I use the tempfile library instead now. The library is designed to prevent collision.
@host_service.method( | ||
host_service.bus_name(MOD_NAME), in_signature="ss", out_signature="is" | ||
) | ||
def download(self, image_url, save_as): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
host_modules/image_service.py
Outdated
# Create parent directory. | ||
dir = os.path.dirname(save_as) | ||
if not os.path.exists(dir): | ||
os.makedirs(dir) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
host_modules/image_service.py
Outdated
where: either a local path or a remote url pointing to the image. | ||
""" | ||
logger.info("Using sonic-installer to install the image at {}.".format(where)) | ||
cmd = ["sudo", "/usr/local/bin/sonic-installer", "install", "-y", where] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting. I deem it necessary because it is needed for manual run, but so as many config
command in other modules. This is not needed.
41b449f
to
b443096
Compare
To more accurately capture what this service really does: everything related to a sonic image.
sonic-installer command support image through url. Update variable names and documentation to reflect that.
Using black.
For the record: Force push to reset the email. |
host_modules/image_service.py
Outdated
for chunk in response.iter_content(chunk_size=8192): | ||
tmp_file.write(chunk) | ||
temp_file_path = tmp_file.name | ||
os.rename(temp_file_path, save_as) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What will happen if this save_as already exists?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
os.rename will do nothing. Using os.replace instead.
with open(file_path, "rb") as f: | ||
for chunk in iter(lambda: f.read(4096), b""): | ||
hash_func.update(chunk) | ||
return 0, hash_func.hexdigest() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use md5sum directly? Is this hash result the same as md5sum result? How long will it take to calculate checksum for a typical sonic image?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1-2 seconds. md5sum is slightly faster but not by a noticeable margin. And since this is python code, I think we should use python library to have better control over the logic.
$ python3
Python 3.11.2 (main, May 2 2024, 11:59:08) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from md5 import calculate_md5
>>> calculate_md5('sonic.bin')
(2.589256763458252, '489a131693a4c98803d5a520df409bf9')
>>>
$ time md5sum sonic.bin
489a131693a4c98803d5a520df409bf9 sonic.bin
real 0m1.879s
user 0m1.621s
sys 0m0.257s
Create image_service class for supporting GNOI implementation for SetPackage.
TESTED: unit test.