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

pikvm_comm.py: add local image upload kwd #16

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions pikvm_comm.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,24 @@ def UploadImagePiKVM(
x = requests.post(uri, headers=headers_login, verify=False)
uri = f"https://{pikvm_ip}/api/msd/write_remote?url={img_url}"
x = requests.post(uri, headers=headers_login, verify=False)


@keyword("Copy Image PIKVM")
def CopyImagePiKVM(
Copy link
Member

@macpijan macpijan Oct 23, 2024

Choose a reason for hiding this comment

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

@wiktormowinski It seems like this is almost identical to UploadImagePiKVM with a single change of removing ?url= from the request.

Why not use another function to share the common code between these two?

Also, I would suggest to use better naming, so it's obvious that one kwd works on file, while the second one works on URL. Now you simply changed Upload to Copy. We still perform the upload, but simply from local path, not from the remote URL. It's been changed for the sake of being different, but it's name does not really tell much.

Better names could be something like: Upload From File / Upload From URL. Or even further, if these keywords are so similar, you can make it accept both URL and local path, and then adjust request accordingly. Either automatically based on the argument, or manually, by introducing second argument which switches between file and URL.

pikvm_ip=str, img_path=str, img_name=str, login="admin", password="admin"
):
"""
---\n
pikvm_ip - IP of the piKVM to send key input,\n
img_path - file path of the image to be uploaded,\n
login - piKVM login\n
password - piKVM password\n
"""
headers_login = {"X-KVMD-User": login, "X-KVMD-Passwd": password}
uri = f"https://{pikvm_ip}/api/msd/set_connected?connected=0"
x = requests.post(uri, headers=headers_login, verify=False)
# "remove" added because "write_remote" itself did not overwrite the old image.
uri = f"https://{pikvm_ip}/api/msd/remove?image={img_name}"
x = requests.post(uri, headers=headers_login, verify=False)
uri = f"https://{pikvm_ip}/api/msd/write_remote?image={img_path}"
x = requests.post(uri, headers=headers_login, verify=False)