Пример #1
0
def get_cost(infraid):
    """Query cost of the infrastructure.

    :param infraid: The identifier of the infrastructure.

    :return type:
        .. code::

            {
                "totalcost": <totalcost>,
                "nodecosts" {
                    "<nodename>": {
                        "<nodeid>": <nodeidcost>,
                        ...
                    },
                    ...
                }
            }
    """
    error_if_infraid_does_not_exist(infraid)
    infrastate = main_info_broker.get('infrastructure.state', infra_id=infraid)
    result = dict()
    result['totalcost'] = 0
    for nodename,instances in infrastate.items():
        result[nodename] = dict()
        for nodeid,nivalue in instances.items():
            cost = 0
            try:
                cost = ib.main_resourcehandler.get_cost(nivalue)
            except Exception as e:
                pass
            result['totalcost'] += cost
            result[nodename][nodeid] = cost
    return jsonify(result)
Пример #2
0
def error_if_nodename_does_not_exist(infraid,nodename):
    sd = main_info_broker.get('infrastructure.static_description',
                               infra_id=infraid)
    if not nodename in [ node['name'] for node in sd.nodes ]:
        raise RequestException(300,
              'ERROR: node \'{0} does not exist in infrastructure \'{1}\'!'.format(
              nodename,infraid))
Пример #3
0
def create_infra_report(infraid):
    infrastate = main_info_broker.get('infrastructure.state', infra_id=infraid)
    result = dict()
    for nodename,instances in infrastate.iteritems():
        nnd = dict()
        instancevals = dict()
        for nodeid,nivalue in instances.iteritems():
            instancevals[nodeid] = dict((item,svalue) for item,svalue in
                    nivalue.iteritems() if item in
                    ['resource_address','state'])
        nnd['instances'] = instancevals
        nnd['scaling'] = scaling.report(infrastate[nodename])
        result[nodename]=nnd
    return result
Пример #4
0
        def context_list():
            # `context_template` is also removed from the definition, as
            # it will be replaced with the rendered `context`
            yield ('node_definition',
                   node_definition.pop(temp_name, None))

            from occo.infobroker import main_info_broker
            sc_data = main_info_broker.get(
                'service_composer.aux_data',
                node_definition['service_composer_id'])
            yield ('service_composer_default',
                   sc_data.get(temp_name, None))

            yield 'default', ''
Пример #5
0
def create_infra_report(infraid):
    infrastate = main_info_broker.get('infrastructure.state', infra_id=infraid)
    result = dict()
    for nodename,instances in infrastate.items():
        nnd = dict()
        instancevals = dict()
        for nodeid,nivalue in instances.items():
            instancevals[nodeid] = dict((item,svalue) for item,svalue in
                    nivalue.items() if item in
                    ['resource_address','state'])
        nnd['instances'] = instancevals
        nnd['scaling'] = scaling.report(infrastate[nodename])
        result[nodename]=nnd
    return result
Пример #6
0
 def find_node_id(node_name):
     """
     Convenience function to be used in templates, to acquire a node id
     based on node name.
     """
     nodes = main_info_broker.get(
         'node.find', infra_id=node_desc['infra_id'], name=node_name)
     if not nodes:
         raise KeyError(
             'No node exists with the given name', node_name)
     elif len(nodes) > 1:
         log.warning(
             'There are multiple nodes with the same node name. '
             'Choosing the first one as default (%s)',
             nodes[0]['node_id'])
     return nodes[0]
Пример #7
0
 def find_node_id(node_name, allnodes=False):
     """
     Convenience function to be used in templates, to acquire a node id
     based on node name.
     """
     nodes = main_info_broker.get(
         'node.find', infra_id=node_desc['infra_id'], name=node_name)
     if not nodes:
         raise KeyError(
             'No node exists with the given name', node_name)
     elif not allnodes and len(nodes) > 1:
         log.warning(
             'There are multiple nodes with the same node name (%s). ' +
             'Multiple nodes are ' +
             ', '.join(item['node_id'] for item in nodes) +
             '. Choosing the first one as default (%s).',
             node_name, nodes[0]['node_id'])
     return nodes[0] if not allnodes else nodes
Пример #8
0
 def find_node_id(node_name):
     """
     Convenience function to be used in templates, to acquire a node id
     based on node name.
     """
     nodes = main_info_broker.get('node.find',
                                  infra_id=node_desc['infra_id'],
                                  name=node_name)
     if not nodes:
         raise KeyError('No node exists with the given name', node_name)
     elif len(nodes) > 1:
         log.warning(
             'There are multiple nodes with the same node name (%s). ' +
             'Multiple nodes are ' + ', '.join(item['node_id']
                                               for item in nodes) +
             '. Choosing the first one as default (%s).', node_name,
             nodes[0]['node_id'])
     return nodes[0]
Пример #9
0
 def find_node_id(node_name):
     """
     Convenience function to be used in templates, to acquire a node id
     based on node name.
     """
     nodes = main_info_broker.get("node.find", infra_id=node_desc["infra_id"], name=node_name)
     if not nodes:
         raise KeyError("No node exists with the given name", node_name)
     elif len(nodes) > 1:
         log.warning(
             "There are multiple nodes with the same node name (%s). "
             + "Multiple nodes are "
             + ", ".join(item["node_id"] for item in nodes)
             + ". Choosing the first one as default (%s).",
             node_name,
             nodes[0]["node_id"],
         )
     return nodes[0]
Пример #10
0
def teardown(infra_id, ip):
    import logging
    from ruamel import yaml
    log = logging.getLogger('occo.occoapp')
    datalog = logging.getLogger('occo.data.occoapp')

    from occo.infobroker import main_info_broker

    state = main_info_broker.get('infrastructure.node_instances', infra_id)
    from occo.util import flatten
    nodes = list(flatten(iter(i.values()) for i in state.values()))
    drop_node_commands = [ip.cri_drop_node(n) for n in nodes]
    log.debug('Dropping nodes: %s', [n['node_id'] for n in nodes])
    datalog.debug('DropNode:\n%s',
                  yaml.dump(drop_node_commands, default_flow_style=False))

    ip.push_instructions(infra_id, drop_node_commands)

    ip.push_instructions(infra_id, ip.cri_drop_infrastructure(infra_id))
Пример #11
0
def info(key):
    """Evaluates a key by the info broker and returns the value

    :param key: The name of the key to be evaluated.

    """
    params=dict((k, v) for k, v in request.args.items())
    log.debug('Serving request %s info/%s with params: %s', request.method, key, str(params))
    try:
        return jsonify({"result": main_info_broker.get(key, **params) })
    except ArgumentError as e:
        log.info('InfoRouter: ArgumentError: %s', str(e))
        raise RequestException(400, 'Invalid parameter value for key "{0}"'.format(key), str(e))
    except KeyNotFoundError as e:
        log.info('InfoRouter: KeyNotFound: %s', str(e))
        raise RequestException(404, 'Key not found: "{0}"'.format(key), str(e))
    except Exception as e:
        log.info('InfoRouter: Exception: %s', str(e))
        log.exception('main_info_broker.get:')
        raise RequestException(404, str(e), 'Request cannot be served.')
Пример #12
0
def info(key):
    """Evaluates a key by the info broker and returns the value

    :param key: The name of the key to be evaluated.

    """
    params=dict((k, v) for k, v in request.args.items())
    log.debug('Serving request %s info/%s with params: %s', request.method, key, str(params))
    try:
        return jsonify({"result": main_info_broker.get(key, **params) })
    except ArgumentError as e:
        log.info('InfoRouter: ArgumentError: %s', str(e))
        raise RequestException(400, 'Invalid parameter value for key "{0}"'.format(key), str(e))
    except KeyNotFoundError as e:
        log.info('InfoRouter: KeyNotFound: %s', str(e))
        raise RequestException(404, 'Key not found: "{0}"'.format(key), str(e))
    except Exception as e:
        log.info('InfoRouter: Exception: %s', str(e))
        log.exception('main_info_broker.get:')
        raise RequestException(404, str(e), 'Request cannot be served.')
Пример #13
0
def teardown(infra_id, ip):
    import logging
    log = logging.getLogger('occo.occoapp')
    datalog = logging.getLogger('occo.data.occoapp')

    log.info('Tearing down infrastructure %r', infra_id)

    from occo.infobroker import main_info_broker
    dynamic_state = main_info_broker.get('infrastructure.state', infra_id)

    from occo.util import flatten
    nodes = list(flatten(i.itervalues() for i in dynamic_state.itervalues()))

    import yaml
    drop_node_commands = [ip.cri_drop_node(n) for n in nodes]
    log.debug('Dropping nodes: %r', [n['node_id'] for n in nodes])
    datalog.debug('DropNode:\n%s',
                  yaml.dump(drop_node_commands, default_flow_style=False))

    ip.push_instructions(drop_node_commands)

    ip.push_instructions(ip.cri_drop_infrastructure(infra_id))
Пример #14
0
 def getip(node_name):
     return main_info_broker.get('node.resource.address',
            find_node_id(node_name, allnodes=False))
Пример #15
0
 def getip(node_name):
     nra = main_info_broker.get('node.resource.address',
                                find_node_id(node_name, allnodes=False))
     return nra[0] if isinstance(nra, list) else nra
Пример #16
0
 def getipall(node_name):
     l = list()
     for node in find_node_id(node_name, allnodes=True):
         nra = main_info_broker.get('node.resource.address', node)
         l = l[:] + [nra[0]] if isinstance(nra, list) else l[:] + [nra]
     return l
Пример #17
0
 def getprivip(node_name):
     return main_info_broker.get(
         'node.resource.ip_address',
         find_node_id(node_name, allnodes=False))
Пример #18
0
def check_nodename_exists(infraid,nodename):
    check_infraid_exists(infraid)
    if not nodename in main_info_broker.get('infrastructure.state',
            infra_id=infraid):
        raise RequestException(300, 'ERROR: invalid parameter(s)')
Пример #19
0
 def getip(node_name):
     return main_info_broker.get("node.resource.address", find_node_id(node_name))
Пример #20
0
 def getipall(node_name):
     return [ main_info_broker.get('node.resource.address', node)
              for node in find_node_id(node_name, allnodes=True) ]
Пример #21
0
 def getip(node_name):
     return main_info_broker.get('node.resource.address',
                                 find_node_id(node_name))
Пример #22
0
 def getipall(node_name):
     return [
         main_info_broker.get('node.resource.address', node)
         for node in find_node_id(node_name, allnodes=True)
     ]