Compare commits

..

No commits in common. "legacy-mode" and "main" have entirely different histories.

2 changed files with 9 additions and 35 deletions

View File

@ -4,4 +4,3 @@ token = '1234'
# path = /path/to/save
# device = your_device_name
# # erase = basic
# legacy = true

View File

@ -13,22 +13,6 @@ import requests
from datetime import datetime
## Legacy Functions ##
def convert_to_legacy_snapshot(snapshot):
snapshot["sid"] = str(uuid.uuid4()).split("-")[0]
snapshot["software"] = "Workbench"
snapshot["version"] = "2023.4.0-alpha"
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):
@ -230,7 +214,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 +234,7 @@ def get_data(all_disks):
hwinfo = 'sudo hwinfo --reallyall'
dmidecode = 'sudo dmidecode'
data = {
'lshw': json.loads(exec_cmd(lshw) or "{}"),
'lshw': exec_cmd(lshw),
'disks': smartctl(all_disks),
'hwinfo': exec_cmd(hwinfo),
'dmidecode': exec_cmd(dmidecode)
@ -298,16 +282,12 @@ 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 = {
"Authorization": f"Bearer {token}",
f"Authorization": "Basic {token}",
"Content-Type": "application/json"
}
try:
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}")
requests.post(url, data=json.dumps(snapshot), headers=headers)
print(f"workbench: INFO: Snapshot sent to '{url}'")
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}")
@ -329,19 +309,17 @@ 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, legacy = None, None, None, None, None
url, token, device, erase = None, None, None, None
return {
'path': path,
'url': url,
'token': token,
'device': device,
'erase': erase,
'legacy': legacy
'erase': erase
}
def parse_args():
@ -376,14 +354,11 @@ def main():
all_disks = get_disks()
snapshot = gen_snapshot(all_disks)
if config['erase'] and config['device'] and not config.get("legacy"):
if config['erase'] and config['device']:
snapshot['erase'] = gen_erase(all_disks, config['erase'], user_disk=config['device'])
elif config['erase'] and not config.get("legacy"):
elif config['erase']:
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']: