Exemplo n.º 1
0
def get_cached_knowledge():
    """Return cached items required to report to the server.

    :return: Tuple of cached items: (maas_url, api_credentials).  Either may
        be None if the information has not been received from the server yet.
    """
    maas_url = get_maas_url()
    if maas_url is None:
        logger.debug("Not reporting boot images: don't have API URL yet.")
    api_credentials = get_recorded_api_credentials()
    if api_credentials is None:
        logger.debug("Not reporting boot images: don't have API key yet.")
    return maas_url, api_credentials
Exemplo n.º 2
0
def get_cached_knowledge():
    """Return cached items required to report to the server.

    :return: Tuple of cached items: (maas_url, api_credentials).  Either may
        be None if the information has not been received from the server yet.
    """
    maas_url = get_maas_url()
    if maas_url is None:
        logger.debug("Not reporting boot images: don't have API URL yet.")
    api_credentials = get_recorded_api_credentials()
    if api_credentials is None:
        logger.debug("Not reporting boot images: don't have API key yet.")
    return maas_url, api_credentials
Exemplo n.º 3
0
def periodic_probe_task():
    """Probe for DHCP servers and set NodeGroupInterface.foriegn_dhcp.

    This should be run periodically so that the database has an up-to-date
    view of any rogue DHCP servers on the network.

    NOTE: This uses blocking I/O with sequential polling of interfaces, and
    hence doesn't scale well.  It's a future improvement to make
    to throw it in parallel threads or async I/O.
    """
    # Items that the server must have sent us before we can do this.
    knowledge = {
        'maas_url': get_maas_url(),
        'api_credentials': get_recorded_api_credentials(),
        'nodegroup_uuid': get_recorded_nodegroup_uuid(),
    }

    if None in knowledge.values():
        # The MAAS server hasn't sent us enough information for us to do
        # this yet.  Leave it for another time.
        logger.info(
            "Not probing for rogue DHCP servers; not all required knowledge "
            "received from server yet.  "
            "Missing: %s" % ', '.join(
                sorted(name
                       for name, value in knowledge.items() if value is None)))
        return

    # Determine all the active interfaces on this cluster (nodegroup).
    interfaces = determine_cluster_interfaces(knowledge)
    if interfaces is None:
        logger.info("No interfaces on cluster, not probing DHCP.")
        return

    # Iterate over interfaces and probe each one.
    for interface, ip in interfaces:
        try:
            servers = probe_interface(interface, ip)
        except socket.error:
            logger.exception(
                "Failed to probe sockets; did you configure authbind as per "
                "HACKING.txt?")
            return
        else:
            if len(servers) > 0:
                # Only send one, if it gets cleared out then the
                # next detection pass will send a different one, if it
                # still exists.
                update_region_controller(knowledge, interface, servers.pop())
            else:
                update_region_controller(knowledge, interface, None)
Exemplo n.º 4
0
def periodic_probe_task():
    """Probe for DHCP servers and set NodeGroupInterface.foriegn_dhcp.

    This should be run periodically so that the database has an up-to-date
    view of any rogue DHCP servers on the network.

    NOTE: This uses blocking I/O with sequential polling of interfaces, and
    hence doesn't scale well.  It's a future improvement to make
    to throw it in parallel threads or async I/O.
    """
    # Items that the server must have sent us before we can do this.
    knowledge = {
        'maas_url': get_maas_url(),
        'api_credentials': get_recorded_api_credentials(),
        'nodegroup_uuid': get_recorded_nodegroup_uuid(),
    }

    if None in knowledge.values():
        # The MAAS server hasn't sent us enough information for us to do
        # this yet.  Leave it for another time.
        logger.info(
            "Not probing for rogue DHCP servers; not all required knowledge "
            "received from server yet.  "
            "Missing: %s" % ', '.join(sorted(
                name for name, value in knowledge.items() if value is None)))
        return

    # Determine all the active interfaces on this cluster (nodegroup).
    interfaces = determine_cluster_interfaces(knowledge)
    if interfaces is None:
        logger.info("No interfaces on cluster, not probing DHCP.")
        return

    # Iterate over interfaces and probe each one.
    for interface, ip in interfaces:
        try:
            servers = probe_interface(interface, ip)
        except socket.error:
            logger.exception(
                "Failed to probe sockets; did you configure authbind as per "
                "HACKING.txt?")
            return
        else:
            if len(servers) > 0:
                # Only send one, if it gets cleared out then the
                # next detection pass will send a different one, if it
                # still exists.
                update_region_controller(knowledge, interface, servers.pop())
            else:
                update_region_controller(knowledge, interface, None)
Exemplo n.º 5
0
def create_node(mac, arch, power_type, power_parameters):
    api_credentials = get_recorded_api_credentials()
    if api_credentials is None:
        raise Exception('Not creating node: no API key yet.')
    client = MAASClient(MAASOAuth(*api_credentials), MAASDispatcher(),
                        get_maas_url())

    data = {
        'op': 'new',
        'architecture': arch,
        'power_type': power_type,
        'power_parameters': power_parameters,
        'mac_addresses': mac
    }
    return client.post('/api/1.0/nodes/', data)
Exemplo n.º 6
0
def create_node(mac, arch, power_type, power_parameters):
    api_credentials = get_recorded_api_credentials()
    if api_credentials is None:
        raise Exception('Not creating node: no API key yet.')
    client = MAASClient(
        MAASOAuth(*api_credentials), MAASDispatcher(),
        get_maas_url())

    data = {
        'op': 'new',
        'architecture': arch,
        'power_type': power_type,
        'power_parameters': power_parameters,
        'mac_addresses': mac
    }
    return client.post('/api/1.0/nodes/', data)
Exemplo n.º 7
0
def get_cached_knowledge():
    """Get all the information that we need to know, or raise an error.

    :return: (client, nodegroup_uuid)
    """
    api_credentials = get_recorded_api_credentials()
    if api_credentials is None:
        logger.error("Not updating tags: don't have API key yet.")
        return None, None
    nodegroup_uuid = get_recorded_nodegroup_uuid()
    if nodegroup_uuid is None:
        logger.error("Not updating tags: don't have UUID yet.")
        return None, None
    client = MAASClient(MAASOAuth(*api_credentials), MAASDispatcher(),
                        get_maas_url())
    return client, nodegroup_uuid
Exemplo n.º 8
0
def get_cached_knowledge():
    """Get all the information that we need to know, or raise an error.

    :return: (client, nodegroup_uuid)
    """
    api_credentials = get_recorded_api_credentials()
    if api_credentials is None:
        logger.error("Not updating tags: don't have API key yet.")
        return None, None
    nodegroup_uuid = get_recorded_nodegroup_uuid()
    if nodegroup_uuid is None:
        logger.error("Not updating tags: don't have UUID yet.")
        return None, None
    client = MAASClient(
        MAASOAuth(*api_credentials), MAASDispatcher(),
        get_maas_url())
    return client, nodegroup_uuid
Exemplo n.º 9
0
def send_leases(leases):
    """Send lease updates to the server API."""
    # Items that the server must have sent us before we can do this.
    knowledge = {
        'maas_url': get_maas_url(),
        'api_credentials': get_recorded_api_credentials(),
        'nodegroup_uuid': get_recorded_nodegroup_uuid(),
    }
    if None in knowledge.values():
        # The MAAS server hasn't sent us enough information for us to do
        # this yet.  Leave it for another time.
        logger.info(
            "Not sending DHCP leases to server: not all required knowledge "
            "received from server yet.  "
            "Missing: %s"
            % ', '.join(list_missing_items(knowledge)))
        return

    api_path = 'api/1.0/nodegroups/%s/' % knowledge['nodegroup_uuid']
    oauth = MAASOAuth(*knowledge['api_credentials'])
    MAASClient(oauth, MAASDispatcher(), knowledge['maas_url']).post(
        api_path, 'update_leases', leases=json.dumps(leases))
Exemplo n.º 10
0
def send_leases(leases):
    """Send lease updates to the server API."""
    # Items that the server must have sent us before we can do this.
    knowledge = {
        'maas_url': get_maas_url(),
        'api_credentials': get_recorded_api_credentials(),
        'nodegroup_uuid': get_recorded_nodegroup_uuid(),
    }
    if None in knowledge.values():
        # The MAAS server hasn't sent us enough information for us to do
        # this yet.  Leave it for another time.
        logger.info(
            "Not sending DHCP leases to server: not all required knowledge "
            "received from server yet.  "
            "Missing: %s" % ', '.join(list_missing_items(knowledge)))
        return

    api_path = 'api/1.0/nodegroups/%s/' % knowledge['nodegroup_uuid']
    oauth = MAASOAuth(*knowledge['api_credentials'])
    MAASClient(oauth, MAASDispatcher(),
               knowledge['maas_url']).post(api_path,
                                           'update_leases',
                                           leases=json.dumps(leases))
Exemplo n.º 11
0
 def test_get_maas_url_reads_MAAS_URL(self):
     maas_url = factory.make_name('maas_url')
     self.useFixture(EnvironmentVariableFixture('MAAS_URL', maas_url))
     self.assertEqual(maas_url, get_maas_url())
Exemplo n.º 12
0
 def test_get_maas_url_reads_MAAS_URL(self):
     maas_url = factory.make_name('maas_url')
     self.useFixture(EnvironmentVariableFixture('MAAS_URL', maas_url))
     self.assertEqual(maas_url, get_maas_url())