From 6bf1746829088b52f2e376a33f7151844050c6a5 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 27 Sep 2024 18:07:48 +0200 Subject: [PATCH 01/11] add legacy mode make workbench-script compatible with the old devicehub --- settings.ini.example | 1 + workbench-script.py | 29 +++++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/settings.ini.example b/settings.ini.example index 81dac5e..a51397b 100644 --- a/settings.ini.example +++ b/settings.ini.example @@ -4,3 +4,4 @@ token = '1234' # path = /path/to/save # device = your_device_name # # erase = basic +# legacy = true diff --git a/workbench-script.py b/workbench-script.py index 743a505..8ba38ee 100644 --- a/workbench-script.py +++ b/workbench-script.py @@ -13,6 +13,22 @@ import requests from datetime import datetime +## Legacy Functions ## +def convert_to_legacy_snapshot(snapshot): + snapshot["sid"] = str(uuid.uuid4()).split("-")[0] + snapshot["software"] = "workbench-script" + snapshot["version"] = "dev" + snapshot["schema_api"] = "1.0.0" + snapshot["settings_version"] = "No Settings Version (NaN)" + snapshot["timestamp"] = snapshot["timestamp"].replace(" ", "T") + snapshot["data"]["smart"] = snapshot["data"]["disks"] + snapshot["data"].pop("disks") + snapshot.pop("code") + snapshot.pop("erase") + +## End Legacy Functions ## + + ## Utility Functions ## def logs(f): def wrapper(*args, **kwargs): @@ -309,17 +325,19 @@ def load_config(config_file="settings.ini"): # TODO validate that the device exists? device = config.get('settings', 'device', fallback=None) erase = config.get('settings', 'erase', fallback=None) + legacy = config.get('settings', 'legacy', fallback=None) else: print(f"workbench: ERROR: Config file '{config_file}' not found. Using default values.") path = os.path.join(os.getcwd()) - url, token, device, erase = None, None, None, None + url, token, device, erase, legacy = None, None, None, None, None return { 'path': path, 'url': url, 'token': token, 'device': device, - 'erase': erase + 'erase': erase, + 'legacy': legacy } def parse_args(): @@ -354,11 +372,14 @@ def main(): all_disks = get_disks() snapshot = gen_snapshot(all_disks) - if config['erase'] and config['device']: + if config['erase'] and config['device'] and not config.get("legacy"): snapshot['erase'] = gen_erase(all_disks, config['erase'], user_disk=config['device']) - elif config['erase']: + elif config['erase'] and not config.get("legacy"): snapshot['erase'] = gen_erase(all_disks, config['erase']) + if config.get("legacy"): + convert_to_legacy_snapshot(snapshot) + save_snapshot_in_disk(snapshot, config['path']) if config['url']: -- 2.30.2 From fb69206f5392e9851601ca065b920460e9cc2fda Mon Sep 17 00:00:00 2001 From: pedro Date: Mon, 30 Sep 2024 08:09:23 -0300 Subject: [PATCH 02/11] workbench-script: verify if snapshot was received --- workbench-script.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/workbench-script.py b/workbench-script.py index 8ba38ee..030845b 100644 --- a/workbench-script.py +++ b/workbench-script.py @@ -302,8 +302,12 @@ def send_snapshot_to_devicehub(snapshot, token, url): "Content-Type": "application/json" } try: - requests.post(url, data=json.dumps(snapshot), headers=headers) - print(f"workbench: INFO: Snapshot sent to '{url}'") + response = requests.post(url, data=json.dumps(snapshot), headers=headers) + if response.status_code == 200: + print(f"workbench: INFO: Snapshot successfully sent to '{url}'") + else: + raise Exception(f"workbench: ERROR: Failed to send snapshot. HTTP {response.status_code}: {response.text}") + except Exception as e: print(f"workbench: ERROR: Snapshot not remotely sent. URL '{url}' is unreachable. Do you have internet? Is your server up & running?\n {e}") -- 2.30.2 From e4203df44cd366536106b6b363eb97f967345a28 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 30 Sep 2024 17:48:38 +0200 Subject: [PATCH 03/11] fix parsing to old devicehub --- workbench-script.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/workbench-script.py b/workbench-script.py index 030845b..398c414 100644 --- a/workbench-script.py +++ b/workbench-script.py @@ -230,7 +230,7 @@ def exec_smart(disk): def smartctl(all_disks, disk=None): if disk: - return exec_smart(disk) + return [exec_smart(disk)] data_list = [] for disk in all_disks: @@ -250,7 +250,7 @@ def get_data(all_disks): hwinfo = 'sudo hwinfo --reallyall' dmidecode = 'sudo dmidecode' data = { - 'lshw': exec_cmd(lshw), + 'lshw': json.loads(exec_cmd(lshw) or "{}"), 'disks': smartctl(all_disks), 'hwinfo': exec_cmd(hwinfo), 'dmidecode': exec_cmd(dmidecode) @@ -298,7 +298,7 @@ def save_snapshot_in_disk(snapshot, path): # url = 'http://127.0.0.1:8000/api/snapshot/' def send_snapshot_to_devicehub(snapshot, token, url): headers = { - f"Authorization": "Basic {token}", + "Authorization": f"Bearer {token}", "Content-Type": "application/json" } try: -- 2.30.2 From 46447cebaafeefcb29fd0aedb47270887771da45 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 1 Oct 2024 14:11:03 +0200 Subject: [PATCH 04/11] qr for workbench --- workbench-script.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/workbench-script.py b/workbench-script.py index 398c414..901781f 100644 --- a/workbench-script.py +++ b/workbench-script.py @@ -306,7 +306,21 @@ def send_snapshot_to_devicehub(snapshot, token, url): if response.status_code == 200: print(f"workbench: INFO: Snapshot successfully sent to '{url}'") else: - raise Exception(f"workbench: ERROR: Failed to send snapshot. HTTP {response.status_code}: {response.text}") + txt = "workbench: ERROR: Failed to send snapshot. HTTP {}: {}".format( + response.status_code, + response.text + ) + raise Exception(txt) + + try: + res = json.loads(response.text) + if res.get('public_url'): + qr = "echo {} | qrencode -t ANSI".format(res['public_url']) + print(exec_cmd(qr)) + if res.get("dhid"): + print(res['dhid']) + except Exception: + print(response.text) except Exception as e: print(f"workbench: ERROR: Snapshot not remotely sent. URL '{url}' is unreachable. Do you have internet? Is your server up & running?\n {e}") -- 2.30.2 From 47a8e24850eba5605acdb046c11dfe5968b06f36 Mon Sep 17 00:00:00 2001 From: pedro Date: Thu, 10 Oct 2024 19:09:44 +0200 Subject: [PATCH 05/11] allow more response codes --- workbench-script.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/workbench-script.py b/workbench-script.py index 901781f..d7a823d 100644 --- a/workbench-script.py +++ b/workbench-script.py @@ -303,7 +303,7 @@ def send_snapshot_to_devicehub(snapshot, token, url): } try: response = requests.post(url, data=json.dumps(snapshot), headers=headers) - if response.status_code == 200: + if 200 <= response.status_code < 300: print(f"workbench: INFO: Snapshot successfully sent to '{url}'") else: txt = "workbench: ERROR: Failed to send snapshot. HTTP {}: {}".format( -- 2.30.2 From 2291d0994b4e97bc504fa767ec80daef95545e8d Mon Sep 17 00:00:00 2001 From: pedro Date: Thu, 10 Oct 2024 19:10:46 +0200 Subject: [PATCH 06/11] add lspci command which was required by legacy server --- workbench-script.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/workbench-script.py b/workbench-script.py index d7a823d..f46be36 100644 --- a/workbench-script.py +++ b/workbench-script.py @@ -249,11 +249,13 @@ def get_data(all_disks): lshw = 'sudo lshw -json' hwinfo = 'sudo hwinfo --reallyall' dmidecode = 'sudo dmidecode' + lspci = 'sudo lspci -vv' data = { 'lshw': json.loads(exec_cmd(lshw) or "{}"), 'disks': smartctl(all_disks), 'hwinfo': exec_cmd(hwinfo), - 'dmidecode': exec_cmd(dmidecode) + 'dmidecode': exec_cmd(dmidecode), + 'lspci': exec_cmd(lspci) } return data -- 2.30.2 From 1f88aae0ac08603efda256624453c6b96d15027b Mon Sep 17 00:00:00 2001 From: pedro Date: Tue, 15 Oct 2024 13:32:36 +0200 Subject: [PATCH 07/11] remove requests dependency, use vanilla urllib --- workbench-script.py | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/workbench-script.py b/workbench-script.py index f46be36..dc7b3b3 100644 --- a/workbench-script.py +++ b/workbench-script.py @@ -6,9 +6,7 @@ import uuid import hashlib import argparse import configparser - -import requests - +import urllib.request from datetime import datetime @@ -304,25 +302,32 @@ def send_snapshot_to_devicehub(snapshot, token, url): "Content-Type": "application/json" } try: - response = requests.post(url, data=json.dumps(snapshot), headers=headers) - if 200 <= response.status_code < 300: + data = json.dumps(snapshot).encode('utf-8') + request = urllib.request.Request(url, data=data, headers=headers) + with urllib.request.urlopen(request) as response: + status_code = response.getcode() + response_text = response.read().decode('utf-8') + + if 200 <= status_code < 300: print(f"workbench: INFO: Snapshot successfully sent to '{url}'") else: txt = "workbench: ERROR: Failed to send snapshot. HTTP {}: {}".format( - response.status_code, - response.text + status_code, + response_text ) raise Exception(txt) try: - res = json.loads(response.text) - if res.get('public_url'): - qr = "echo {} | qrencode -t ANSI".format(res['public_url']) + response = json.loads(response_text) + if response.get('public_url'): + # apt install qrencode + qr = "echo {} | qrencode -t ANSI".format(response['public_url']) print(exec_cmd(qr)) - if res.get("dhid"): - print(res['dhid']) + print("public_url: {}".format(response['public_url'])) + if response.get("dhid"): + print("dhid: {}".format(response['dhid'])) except Exception: - print(response.text) + print(response_text) except Exception as e: print(f"workbench: ERROR: Snapshot not remotely sent. URL '{url}' is unreachable. Do you have internet? Is your server up & running?\n {e}") -- 2.30.2 From ddcd95622007343afde8fca54591100dcb9a8f7b Mon Sep 17 00:00:00 2001 From: pedro Date: Tue, 15 Oct 2024 13:38:01 +0200 Subject: [PATCH 08/11] inform of executed command and discovered a command without the sudo --- workbench-script.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/workbench-script.py b/workbench-script.py index dc7b3b3..15388df 100644 --- a/workbench-script.py +++ b/workbench-script.py @@ -41,6 +41,7 @@ def logs(f): @logs def exec_cmd(cmd): + print(f'workbench: INFO: running command `{cmd}`') return os.popen(cmd).read() @logs @@ -220,7 +221,7 @@ def gen_erase(all_disks, type_erase, user_disk=None): @logs def exec_smart(disk): - cmd = f'smartctl -x --json=cosviu /dev/{disk}' + cmd = f'sudo smartctl -x --json=cosviu /dev/{disk}' return json.loads(exec_cmd(cmd)) -- 2.30.2 From cc02b652637247ef030fe67e565ae5302b9b89b5 Mon Sep 17 00:00:00 2001 From: pedro Date: Tue, 15 Oct 2024 13:53:57 +0200 Subject: [PATCH 09/11] workbench: use url instead of public_url --- workbench-script.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/workbench-script.py b/workbench-script.py index 15388df..275647a 100644 --- a/workbench-script.py +++ b/workbench-script.py @@ -320,11 +320,11 @@ def send_snapshot_to_devicehub(snapshot, token, url): try: response = json.loads(response_text) - if response.get('public_url'): + if response.get('url'): # apt install qrencode - qr = "echo {} | qrencode -t ANSI".format(response['public_url']) + qr = "echo {} | qrencode -t ANSI".format(response['url']) print(exec_cmd(qr)) - print("public_url: {}".format(response['public_url'])) + print("url: {}".format(response['url'])) if response.get("dhid"): print("dhid: {}".format(response['dhid'])) except Exception: -- 2.30.2 From 92658ebc86c51be20f2d6e46c2398c68bc7746ba Mon Sep 17 00:00:00 2001 From: pedro Date: Tue, 15 Oct 2024 13:54:09 +0200 Subject: [PATCH 10/11] improve error msg --- workbench-script.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/workbench-script.py b/workbench-script.py index 275647a..0fecfa6 100644 --- a/workbench-script.py +++ b/workbench-script.py @@ -331,7 +331,7 @@ def send_snapshot_to_devicehub(snapshot, token, url): print(response_text) except Exception as e: - print(f"workbench: ERROR: Snapshot not remotely sent. URL '{url}' is unreachable. Do you have internet? Is your server up & running?\n {e}") + print(f"workbench: ERROR: Snapshot not remotely sent to URL '{url}'. Do you have internet? Is your server up & running? Is the url token authorized?\n {e}") def load_config(config_file="settings.ini"): """ -- 2.30.2 From ba83ccba883ee88691b84882b406ad111dbc7faa Mon Sep 17 00:00:00 2001 From: pedro Date: Tue, 15 Oct 2024 13:54:16 +0200 Subject: [PATCH 11/11] we know lshw might fail, do not load as json --- workbench-script.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/workbench-script.py b/workbench-script.py index 0fecfa6..7cc19bf 100644 --- a/workbench-script.py +++ b/workbench-script.py @@ -250,7 +250,7 @@ def get_data(all_disks): dmidecode = 'sudo dmidecode' lspci = 'sudo lspci -vv' data = { - 'lshw': json.loads(exec_cmd(lshw) or "{}"), + 'lshw': exec_cmd(lshw) or "{}", 'disks': smartctl(all_disks), 'hwinfo': exec_cmd(hwinfo), 'dmidecode': exec_cmd(dmidecode), -- 2.30.2