diff --git a/pikvm_comm.py b/pikvm_comm.py index 5186dc0..0d6ad38 100644 --- a/pikvm_comm.py +++ b/pikvm_comm.py @@ -2,6 +2,9 @@ import websocket import time import ssl + import json + from websocket import WebSocketBadStatusException + import requests from robot.api.deco import keyword ROBOT = False except Exception: @@ -200,3 +203,45 @@ def WriteTextPiKVM(text=str, pikvm_ip=str, login="admin", password="admin"): time.sleep(0.2) ws.send('{"event_type": "key", "event": {"key": "' + keymap[char] + '", "state": false}}') ws.close() + +@keyword("Iso image present") +def iso_image_present(image_name, pikvm_ip=str, login="admin", password="admin"): + """ + image_name - name of the iso image to be found + pikvm_ip - IP of the piKVM to send key input,\n + login - piKVM login\n + password - piKVM password\n + """ + images = get_all_images(pikvm_ip, login, password) + return image_name in images + +@keyword("Upload image") +def upload_image(image_name, path_to_image, pikvm_ip, login="admin", password="admin"): + url = f"https://{pikvm_ip}/api/msd/write?image={image_name}" + + with open(path_to_image, "rb") as iso_file: + binary_data = iso_file.read() + + headers = {"X-KVMD-User": login, "X-KVMD-Passwd": password} + + response = requests.post(url, data=binary_data, headers=headers, verify=False) + return response + +def get_all_images(pikvm_ip, login, password): + url = f"https://{pikvm_ip}/api/msd" + headers = {"X-KVMD-User": login, "X-KVMD-Passwd": password} + + response = requests.get(url, headers=headers, verify=False) + + parsed_json = json.loads(response.content) + + return parsed_json["result"]["storage"]["images"].keys() + +@keyword("Iso image mount") +def iso_image_mount(image_name, pikvm_ip, login, password): + url = f"https://{pikvm_ip}/api/msd/set_params?image={image_name}&cdrom=0" + + headers = {"X-KVMD-User": login, "X-KVMD-Passwd": password} + + response = requests.post(url, headers=headers, verify=False) + return response diff --git a/pikvm_comm.robot b/pikvm_comm.robot index 8467df2..31f85f9 100644 --- a/pikvm_comm.robot +++ b/pikvm_comm.robot @@ -57,3 +57,23 @@ Key Combination PiKVM ... possible keys can be found in `lib/pikvm_comm.py` [Arguments] ${key} ${login}=admin ${password}=admin ${press_time}=0.05 Send Key Combination PiKVM ${key_list} ${pikvm_ip} ${login} ${password} ${press_time} + +Check if iso image is present + [Documentation] Checks if image with name ${image_name} exists. + ... Returns True/False + [Arguments] ${image_name} ${pikvm_ip} + ${result}= Iso image present ${image_name} ${pikvm_ip} + [Return] ${result} + +Upload iso image + [Documentation] Uploads given iso image (${path_to_image}) and saves it + ... as ${image_name} + [Arguments] ${image_name} ${path_to_image} ${pikvm_ip} + ${res}= Upload image ${image_name} ${path_to_image} ${pikvm_ip} + [Return] ${res} + +Mount iso image + [Documentation] Mounts given iso image to CD-ROM. Assumes that + ... ${image_name} is already uploaded. + [Arguments] ${image_name} ${pikvm_ip} ${login}=admin ${password}=admin + Iso image mount ${image_name} ${pikvm_ip} ${login} ${password}