def __init__(self, name, group, instance=None): """ :param str name: Name of the group :param dict group: Dict specification of the group :param int instance: Instance number of a template group """ self._log = logging.getLogger('Group') self._log.debug("Initializing Group '%s'", name) if instance: self.is_template = True self.instance = instance else: self.is_template = False # !!! NOTE: ad-groups must be handled externally by caller !!! if "ad-group" in group: group_type = "ad" self.ad_group = group["ad-group"] users = [] if instance: # Template groups # This is the " X" in the spec self.ad_group += " " + str(instance) elif "filename" in group: from adles.utils import read_json group_type = "standard" if instance: # Template group users = [(user, pw) for user, pw in read_json(group["filename"]) [str(instance)].items()] else: # Standard group users = [(user, pw) for user, pw in read_json(group["filename"]).items()] elif "user-list" in group: group_type = "standard" users = group["user-list"] else: self._log.error("Invalid group dict for group '%s': %s", name, str(group)) raise Exception() self.group_type = group_type self.users = users self.size = int(len(self.users)) self.name = str(name) self._log.debug("Finished initializing Group '%s' with %d users", self.name, self.size)
def __init__(self, infra, spec): """ :param dict infra: Dict of infrastructure information :param dict spec: Dict of a parsed specification """ super(DockerInterface, self).__init__(infra=infra, spec=spec) self._log = logging.getLogger(str(self.__class__)) self._log.debug("Initializing %s", self.__class__) # If needed, a wrapper class that simplifies # the creation of containers will be made # Reference: # https://docker-py.readthedocs.io/en/stable/client.html#client-reference # Initialize the Docker client self.client = docker.DockerClient(base_url=infra.get( "url", "unix:///var/run/" "docker.sock"), tls=bool(infra.get("tls", True))) # Verify the connection to the client self.client.ping() self._log.debug("System info : %s", str(self.client.info())) self._log.debug("System version : %s", str(self.client.version())) # Authenticate to registry, if configured if "registry" in self.infra: reg = self.infra["registry"] reg_logins = utils.read_json(reg["login-file"]) self.client.login(username=reg_logins["user"], password=reg_logins["pass"], registry=reg["url"]) # List images currently on the server self._log.debug("Images: %s", str(self.client.images.list()))
def _check_group_file(filename: str) -> Tuple[int, int]: """Verifies user info file for a group. :param filename: Name of user info JSON file :return: Number of errors, Number of warnings""" num_warnings = 0 num_errors = 0 if utils.read_json(filename) is None: logging.error("Invalid user info file %s", filename) num_errors += 1 return num_errors, num_warnings
def verify_infra_syntax(infra): """ Verifies the syntax of an infrastructure specification. :param dict infra: infrastructure :return: Number of errors, Number of warnings :rtype: tuple(int, int) """ num_warnings = 0 num_errors = 0 warnings = [] errors = [] for platform, config in infra.items(): if platform == "vmware-vsphere": # VMware vSphere configurations warnings = [ "port", "login-file", "datacenter", "datastore", "server-root", "vswitch" ] errors = ["hostname", "template-folder"] if "login-file" in config and \ utils.read_json(config["login-file"]) is None: logging.error("Invalid vSphere infrastructure login-file: %s", config["login-file"]) num_errors += 1 if "host-list" in config and \ not isinstance(config["host-list"], list): logging.error("Invalid type for vSphere host-list: %s", type(config["host-list"])) num_errors += 1 if "thresholds" in config: num_errors += _checker(["folder", "service"], "infrastructure", config["thresholds"], "errors") elif platform == "docker": # Docker configurations warnings = ["url"] errors = [] if "registry" in config: num_errors += _checker(["url", "login-file"], "infrastructure", config["registry"], "errors") elif platform in ["hyper-v", "cloud", "libvirt"]: logging.info("Platform %s is not yet implemented", platform) else: logging.error("Unknown infrastructure platform: %s", str(platform)) num_warnings += 1 continue # Skip the syntax verification of unknown platforms num_warnings += _checker(warnings, "infrastructure", config, "warnings") num_errors += _checker(errors, "infrastructure", config, "errors") return num_errors, num_warnings
def make_vsphere(filename=None): """ Creates a vSphere object using either a JSON file or by prompting the user. :param str filename: Name of JSON file with connection info :return: vSphere object :rtype: :class:`Vsphere` """ from adles.vsphere.vsphere_class import Vsphere if filename is not None: info = read_json(filename) if info is None: raise VsphereException("Failed to create vSphere object") return Vsphere(username=info.get("user"), password=info.get("pass"), hostname=info.get("host"), port=info.get("port", 443), datacenter=info.get("datacenter"), datastore=info.get("datastore")) else: logging.info("Enter information to connect to the vSphere environment") datacenter = input("Datacenter : ") datastore = input("Datastore : ") return Vsphere(datacenter=datacenter, datastore=datastore)
def __init__(self, infra, spec): """ .. warning:: The infrastructure and spec are assumed to be valid, therefore checks on key existence and types are NOT performed for REQUIRED elements. :param dict infra: Infrastructure information :param dict spec: The parsed exercise specification """ super(self.__class__, self).__init__(infra=infra, spec=spec) self._log = logging.getLogger(str(self.__class__)) self._log.debug("Initializing %s %s", self.__class__, self.__version__) self.master_folder = None self.template_folder = None # Used to do lookups of Generic networks during deployment self.net_table = {} # Cache containing Master instances (TODO: potential naming conflicts) self.masters = {} if "thresholds" in infra: self.thresholds = infra["thresholds"] else: self.thresholds = { "folder": { "warn": 25, "error": 50 }, "service": { "warn": 50, "error": 70 } } # Read infrastructure login information if "login-file" in infra: logins = read_json(infra["login-file"]) else: self._log.warning("No login-file specified, " "defaulting to user prompts...") logins = {} # Instantiate the vSphere vCenter server instance class self.server = Vsphere(username=logins.get("user"), password=logins.get("pass"), hostname=infra.get("hostname"), port=int(infra.get("port")), datastore=infra.get("datastore"), datacenter=infra.get("datacenter")) # Acquire ESXi hosts if "hosts" in infra: hosts = infra["hosts"] self.host = self.server.get_host(hosts[0]) # Gather all the ESXi hosts self.hosts = [self.server.get_host(h) for h in hosts] else: self.host = self.server.get_host( ) # First host found in Datacenter # Instantiate and initialize Groups self.groups = self._init_groups() # Set the server root folder if "server-root" in infra: self.server_root = self.server.get_folder(infra["server-root"]) if not self.server_root: self._log.error("Could not find server-root folder '%s'", infra["server-root"]) sys.exit(1) else: # Default to Datacenter VM folder self.server_root = self.server.datacenter.vmFolder self._log.info("Server root folder: %s", self.server_root.name) # Set environment root folder (TODO: this can be consolidated) if "folder-name" not in self.metadata: self.root_path, self.root_name = ("", self.metadata["name"]) self.root_folder = self.server_root.traverse_path(self.root_name, generate=True) else: self.root_path, self.root_name = os.path.split( self.metadata["folder-name"]) self.root_folder = self.server_root.traverse_path( self.metadata["folder-name"], generate=True) self._log.debug("Environment root folder name: %s", self.root_name) if not self.root_folder: # Create if it's not found parent = self.server_root.traverse_path(self.root_path) self.root_folder = self.server.create_folder( self.root_name, parent) if not self.root_folder: self._log.error("Could not create root folder '%s'", self.root_name) sys.exit(1) self._log.info("Environment root folder: %s", self.root_folder.name) # Set default vSwitch name if "vswitch" in infra: self.vswitch_name = infra["vswitch"] else: from pyVmomi import vim self.vswitch_name = self.server.get_item(vim.Network).name self._log.debug("Finished initializing VsphereInterface")
def test_read_json(): from adles.utils import read_json # assert isinstance(read_json('../users.json'), dict) assert read_json('lame.jpg') is None