Exemple #1
0
 def perform_check(self, data):
     missing_keys = CMSchemaChecker.get_missing_keys(self, data, self.req_keys)
     if missing_keys:
         msg = "Missing key(s): " + ', '.join(str(key) for key in missing_keys)
         raise SchemaError(msg)
     valid_keys = self.req_keys + self.opt_keys
     invalid_keys = CMSchemaChecker.get_invalid_keys(self, data, valid_keys)
     if invalid_keys:
         msg = "Unknown key(s): " + ', '.join(str(key) for key in invalid_keys)
         raise SchemaError(msg)
     return True
Exemple #2
0
 def perform_check(self, data):
     missing_keys = HCSchemaChecker.get_missing_keys(
         self, data, self.req_keys)
     if missing_keys:
         msg = "Missing key(s): " + ', '.join(
             str(key) for key in missing_keys)
         raise SchemaError(msg)
     valid_keys = self.req_keys + self.opt_keys
     invalid_keys = HCSchemaChecker.get_invalid_keys(self, data, valid_keys)
     if invalid_keys:
         msg = "Unknown key(s): " + ', '.join(
             str(key) for key in invalid_keys)
         raise SchemaError(msg)
     if 'mysqldbs' in data:
         if type(data['mysqldbs']) is list:
             keys = ['name', 'user', 'pass']
             for db in data['mysqldbs']:
                 mkeys = HCSchemaChecker.get_missing_keys(self, db, keys)
                 ikeys = HCSchemaChecker.get_invalid_keys(self, db, keys)
             if mkeys:
                 msg = "Missing key(s) in mysqldbs: " + ', '.join(
                     str(key) for key in mkeys)
                 raise SchemaError(msg)
             if ikeys:
                 msg = "Unknown key(s) in mysqldbs: " + ', '.join(
                     str(key) for key in mkeys)
                 raise SchemaError(msg)
         else:
             raise SchemaError(
                 "Invalid format of \'mysqldbs\' section! Must be a list.")
     if 'urls' in data:
         if type(data['urls']) is not list:
             raise SchemaError(
                 "Invalid format of \'urls\' section! Must be a list.")
     if 'ports' in data:
         if type(data['ports']) is list:
             for port in data['ports']:
                 if not isinstance(port, int):
                     msg = "Invalid port %r - must be integer" % (port)
                     raise SchemaError(msg)
         else:
             raise SchemaError(
                 "Invalid format of \'ports\' section! Must be a list.")
     if 'ping' in data:
         if not isinstance(data['ping'], bool):
             raise SchemaError(
                 "Invalid value of \'ping\' section! Must be True/False.")
     if 'timeout' in data:
         if not isinstance(data['timeout'], int):
             raise SchemaError(
                 "Invalid value of \'timeout\' section! Must be integer.")
     return True
Exemple #3
0
 def perform_check(self, data):
     missing_keys = RHSchemaChecker.get_missing_keys(
         self, data, self.req_keys)
     if missing_keys:
         msg = "Missing key(s): " + ', '.join(
             str(key) for key in missing_keys)
         raise SchemaError(msg)
     if "password" not in data and "ssh_key_data" not in data:
         msg = "Missing key(s): either \"password\" or \"ssh_key_data\" must be defined"
         raise SchemaError(msg)
     valid_keys = self.req_keys + self.opt_keys
     invalid_keys = RHSchemaChecker.get_invalid_keys(self, data, valid_keys)
     if invalid_keys:
         msg = "Unknown key(s): " + ', '.join(
             str(key) for key in invalid_keys)
         raise SchemaError(msg)
     return True
Exemple #4
0
    def schema_check(infrastructure_description):
        """This function will validate the infrastructure description upon
        creating this object.

        :raises SchemaError: if the schema is invalid.

        .. todo:: implement schema checking.
        """
        try:
            SchemaChecker.check_infra_desc(infrastructure_description)
        except SchemaError as e:
            print((e.msg))
            raise SchemaError(e.msg)
Exemple #5
0
 def perform_check(self, data):
     missing_keys = RHSchemaChecker.get_missing_keys(
         self, data, self.req_keys)
     if missing_keys:
         msg = "Missing key(s): " + ', '.join(
             str(key) for key in missing_keys)
         raise SchemaError(msg)
     valid_keys = self.req_keys + self.opt_keys
     invalid_keys = RHSchemaChecker.get_invalid_keys(self, data, valid_keys)
     if invalid_keys:
         msg = "Unknown key(s): " + ', '.join(
             str(key) for key in invalid_keys)
         raise SchemaError(msg)
     volume_size = data.get('volume_size', None)
     if volume_size is not None:
         try:
             volume_size = int(volume_size)
         except Exception as ex:
             msg = 'Could not convert volume_size value "%s" to integer!' % volume_size
             raise SchemaError(msg)
         if volume_size < 0:
             raise SchemaError(
                 'Negative numbers for volume_size are prohibited!')
     return True
Exemple #6
0
    def topo_order(all_nodes, all_edges):
        """Creates a topological ordering based on the list of nodes and
        the list of edges.

        .. todo:: Using generators instead of lists  (``(...for...)`` instead
            ``[...for...]``) in the generating phase should be more
            memory-efficient. Should try it and use it if it works.
        """

        # Create representation suitable for topological ordering
        ## List of nodes
        nodes = all_nodes
        ## Convert pairs to Edges
        edges = all_edges

        # Return value:
        topo_order = TopologicalOrder()

        # Move all nodes into topo_order, level by level
        while nodes:
            # Negative logic: we need all independent nodes, but it needs
            # calculating a NotExists quantifier, which is always inefficient.
            # So:
            ## All nodes that _do_ depend on _something_
            dependents = [i.dependent for i in edges]
            ## Now it's simple: independent = all \ dependent
            ## These nodes constitute a topological level
            topo_level = TopoLevel(n for n in nodes if not n in dependents)
            # if topo_level is empty while nodes are not, there must be a circle
            # among the nodes through the edges
            if not topo_level:
                raise SchemaError("Cycle detected.", nodes)
            # Remove nodes that were put in this topological level, as now they
            # are satisfied dependencies.
            nodes = [n for n in nodes if not n in topo_level]
            # Remove half edges (edges connected with nodes just removed).
            edges = [e for e in edges if not e.dependee in topo_level]

            # Add to output
            topo_order.add_level(topo_level)

        # Done
        return topo_order
Exemple #7
0
    def check_infra_desc(infra_desc):
        import logging
        log = logging.getLogger('occo')

        keys = list(infra_desc.keys())
        if 'user_id' not in keys:
            log.warning(
                "[SchemaCheck] WARNING: user_id is not defined in infrastructure description"
            )
        if 'infra_name' not in keys:
            raise SchemaError(
                "[SchemaCheck] ERROR: infra_name must be defined in infrastructure description"
            )
        error = is_valid_hostname(infra_desc['infra_name'])
        if error:
            raise SchemaError(
                "[SchemaCheck] ERROR: infra_name \"{0}\" {1}".format(
                    infra_desc['infra_name'], str(error)))
        if 'nodes' not in keys:
            raise SchemaError(
                "[SchemaCheck] ERROR: nodes section must be defined in infrastructure description"
            )
        for node in infra_desc['nodes']:
            nodekeys = list(node.keys())
            if 'name' not in nodekeys:
                raise SchemaError(
                    "[SchemaCheck] ERROR: missing key \"name\" in node")
            error = is_valid_hostname(node['name'])
            if error:
                raise SchemaError(
                    "[SchemaCheck] ERROR: node \"{0}\" {1}".format(
                        node['name'], str(error)))
            if 'type' not in nodekeys:
                raise SchemaError(
                    "[SchemaCheck] ERROR: missing key \"type\" in node %r" %
                    node['name'])
            if 'scaling' not in nodekeys:
                log.warning(
                    "[SchemaCheck] WARNING: missing \"scaling\" parameter in node %r, using default scaling (single instance)",
                    node['name'])
            else:
                for key in node['scaling']:
                    if key not in ['min', 'max']:
                        raise SchemaError(
                            "[SchemaCheck] ERROR: unknown key \"%r\" in scaling of node %r"
                            % (key, node['name']))
            if 'filter' in nodekeys and not isinstance(node['filter'], dict):
                raise SchemaError(
                    "[SchemaCheck] ERROR: unknown type of filter in node %r - has to be a dict!"
                    % node['name'])
            for key in nodekeys:
                if key not in [
                        'name', 'type', 'scaling', 'filter', 'variables'
                ]:
                    raise SchemaError(
                        "[SchemaCheck] ERROR: unknown key \"%r\" in node %r" %
                        (key, node['name']))
        if 'dependencies' not in keys:
            log.warning(
                "[SchemaCheck] WARNING: no dependencies specified - using sequential ordering"
            )
        else:
            for dep in infra_desc['dependencies']:
                if isinstance(dep, dict):
                    depkeys = list(dep.keys())
                    if 'connection' not in depkeys:
                        raise SchemaError(
                            "[SchemaCheck] ERROR: undefined connection ")
        for key in keys:
            if key not in [
                    'user_id', 'infra_name', 'nodes', 'dependencies',
                    'variables'
            ]:
                raise SchemaError(
                    "[SchemaCheck] ERROR: unknown key %r in infastructure description"
                    % key)
Exemple #8
0
    def check_node_def(node_defs):
        for nodename, node_def in list(node_defs.items()):
            if ':' not in nodename or nodename.split(':', 1)[0] != 'node_def':
                context = "[SchemaCheck] ERROR in node %r: " % nodename
                msg = "Node definition must begin with 'node_def:<nodename>'!"
                raise SchemaError(msg, context)
            realnodename = nodename.split(':', 1)[1]
            if type(node_def) != list:
                context = "[SchemaCheck] ERROR in node %r: " % realnodename
                msg = "Node definition has to be a list of dictionaries!"
                raise SchemaError(msg, context)
            for node in node_def:
                #check for invalid sections:
                for key in node:
                    if key not in [
                            "resource", "config_management",
                            "contextualisation", "health_check"
                    ]:
                        context = "[SchemaCheck] ERROR in node %r: " % realnodename
                        msg = "Invalid section %r" % key
                        raise SchemaError(msg, context)
                nodeindex = node_def.index(node)
                #resource section
                try:
                    if 'resource' not in node:
                        raise SchemaError("Missing 'resource' section!")
                    else:
                        resource = node['resource']
                        if 'type' not in resource:
                            raise SchemaError("Missing key \'type\'")
                        else:
                            protocol = resource['type']
                            libname = "occo.plugins.resourcehandler." + protocol
                            importlib.import_module(libname)
                            checker = RHSchemaChecker.instantiate(
                                protocol=protocol)
                            checker.perform_check(resource)
                except SchemaError as e:
                    context = "[SchemaCheck] ERROR in 'resource' section of node %r[%d]: " % (
                        realnodename, nodeindex)
                    raise SchemaError(e.msg, context)
                #config_manager section
                try:
                    if 'config_management' in node:
                        cm = node['config_management']
                        if 'type' not in cm:
                            raise SchemaError("Missing key \'type\'!")
                        else:
                            protocol = cm['type']
                            libname = "occo.plugins.configmanager." + protocol
                            importlib.import_module(libname)
                            checker = CMSchemaChecker.instantiate(
                                protocol=protocol)
                            checker.perform_check(cm)
                except SchemaError as e:
                    context = "[SchemaCheck] ERROR in 'config_management' section of node %r[%d]: " % (
                        realnodename, nodeindex)
                    raise SchemaError(e.msg, context)

                #contextualization section
                try:
                    if 'contextualisation' in node:
                        cont = node['contextualisation']
                        if 'type' not in cont:
                            raise SchemaError("Missing key \'type\'!")
                        else:
                            protocol = cont['type']
                            libname = "occo.plugins.infraprocessor.node_resolution." + protocol
                            importlib.import_module(libname)
                            checker = ContextSchemaChecker.instantiate(
                                protocol=protocol)
                            checker.perform_check(cont)
                except SchemaError as e:
                    context = "[SchemaCheck] ERROR in 'contextualisation' section of node %r[%d]: " % (
                        realnodename, nodeindex)
                    raise SchemaError(e.msg, context)

                #health_check section
                try:
                    if 'health_check' in node:
                        hc = node['health_check']
                        if 'type' not in hc:
                            hc['type'] = 'basic'
                        if hc['type'] == 'basic':
                            libname = "occo.infraprocessor.synchronization"
                        else:
                            libname = "occo.infraprocessor.synchronization." + hc[
                                'type']
                        importlib.import_module(libname)
                        checker = HCSchemaChecker.instantiate(
                            protocol=hc['type'])
                        checker.perform_check(hc)
                except SchemaError as e:
                    context = "[SchemaCheck] ERROR in 'health_check' section of node %r[%d]: " % (
                        realnodename, nodeindex)
                    raise SchemaError(e.msg, context)