From 627171dc89fbfb07863c8df6b23cf0e10709139a Mon Sep 17 00:00:00 2001 From: Raven Hodgdon Date: Thu, 2 Jun 2022 16:23:23 -0400 Subject: rebuilt host import admin util Signed-off-by: jhodgon Change-Id: I6f602266fd5b62a5193afeb842c79e11c408dd16 --- src/dashboard/admin_utils.py | 120 +++++++++++++++++++++++++++++++++++++++++++ src/static/package-lock.json | 91 +++++++++++++++++++++++++++++++- 2 files changed, 210 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/dashboard/admin_utils.py b/src/dashboard/admin_utils.py index 821ca03..75e4f3e 100644 --- a/src/dashboard/admin_utils.py +++ b/src/dashboard/admin_utils.py @@ -27,6 +27,7 @@ from resource_inventory.models import ( ) import json +import yaml import sys import inspect import pydoc @@ -443,6 +444,125 @@ def print_dict_pretty(a_dict): print(json.dumps(a_dict, sort_keys=True, indent=4)) +def import_host(filenames): + """ + Imports host from an array of converted inspection files and if needed creates a new profile for the host. + NOTE: CONVERT INSPECTION FILES USING convert_inspect_results(["file", "file"]) + (original file names not including "-import.yaml" i.e. hpe44) AND FILL IN BEFORE THIS + @filenames: array of host import file names to import + """ + + for filename in filenames: + + # open import file + file = open("dashboard/" + filename + "-import.yaml", "r") + data = yaml.safe_load(file) + + # if a new profile is needed create one and a matching template + if (data["new_profile"]): + add_profile(data) + print("Profile: " + data["name"] + " created!") + make_default_template( + ResourceProfile.objects.get(name=data["name"]), + Image.objects.get(lab_id=data["image"]).id, + None, + None, + False, + False, + data["owner"], + "unh_iol", + True, + False, + data["temp_desc"] + ) + + print(" Template: " + data["temp_name"] + " created!") + + # add the server + add_server( + ResourceProfile.objects.get(name=data["name"]), + data["hostname"], + data["interfaces"], + data["lab"], + data["vendor"], + data["model"] + ) + + print(data["hostname"] + " imported!") + + +def convert_inspect_results(files): + """ + Converts an array of inspection result files into templates (filename-import.yaml) to be filled out for importing the servers into the dashboard + @files an array of file names (not including the file type. i.e hpe44). Default: [] + """ + for filename in files: + # open host inspect file + file = open("dashboard/" + filename + ".yaml") + output = open("dashboard/" + filename + "-import.yaml", "w") + data = json.load(file) + + # gather data about disks + disk_data = {} + for i in data["disk"]: + + # don't include loops in disks + if "loop" not in i: + disk_data[i["name"]] = { + "capacity": i["size"][:-3], + "media_type": "<\"SSD\" or \"HDD\">", + "interface": "<\"sata\", \"sas\", \"ssd\", \"nvme\", \"scsi\", or \"iscsi\">", + } + + # gather interface data + interface_data = {} + for i in data["interfaces"]: + interface_data[data["interfaces"][i]["name"]] = { + "speed": data["interfaces"][i]["speed"], + "nic_type": "<\"onboard\" or \"pcie\">", + "order": "", + "mac_address": data["interfaces"][i]["mac"], + "bus_addr": data["interfaces"][i]["busaddr"], + } + + # gather cpu data + cpu_data = { + "cores": data["cpu"]["cores"], + "architecture": data["cpu"]["arch"], + "cpus": data["cpu"]["cpus"], + "cflags": "", + } + + # gather ram data + ram_data = { + "amount": data["memory"][:-1], + "channels": "", + } + + # assemble data for host import file + import_data = { + "new_profile": " (Set to True to create a new profile for the host's type)", + "name": " (Used to set the profile of a host and for creating a new profile)", + "description": "", + "labs": "", + "temp_name": "