Exemple #1
0
 def test_set_None_clears_cache_if_maas_id_file_does_not_exist(self):
     contents = factory.make_name("contents")
     env.set_maas_id(contents)
     self.assertThat(env.get_maas_id(), Equals(contents))
     os.unlink(self.maas_id_path)
     env.set_maas_id(None)
     self.assertThat(env.get_maas_id(), Is(None))
Exemple #2
0
 def test_get_caches_result(self):
     contents = factory.make_name("contents")
     with open(self.maas_id_path, "w") as fd:
         fd.write(contents)
     self.assertEquals(contents, env.get_maas_id())
     os.unlink(self.maas_id_path)
     self.assertEquals(contents, env.get_maas_id())
Exemple #3
0
 def test_set_none_clears_cache(self):
     contents = factory.make_name("contents")
     env.set_maas_id(contents)
     self.assertEquals(contents, env.get_maas_id())
     env.set_maas_id(None)
     self.assertIsNone(env.get_maas_id())
     self.assertFalse(os.path.exists(self.maas_id_path))
Exemple #4
0
 def test_set_none_works_with_missing_file(self):
     contents = factory.make_name("contents")
     env.set_maas_id(contents)
     atomic_delete(self.maas_id_path)
     env.set_maas_id(None)
     self.assertIsNone(env.get_maas_id())
     self.assertFalse(os.path.exists(self.maas_id_path))
Exemple #5
0
 def test_get_strips_contents_if_maas_id_file_contains_something(self):
     contents = factory.make_name("contents")
     with open(self.maas_id_path, "w") as fd:
         fd.write(string.whitespace)
         fd.write(contents)
         fd.write(string.whitespace)
     self.assertThat(env.get_maas_id(), Equals(contents))
Exemple #6
0
 def test__creates_maas_uuid(self):
     start_up.is_master_process.return_value = False
     self.assertThat(get_maas_id(), Is(None))
     with post_commit_hooks:
         start_up.inner_start_up()
     uuid = Config.objects.get_config('uuid')
     self.assertThat(uuid, Not(Is(None)))
Exemple #7
0
 def test_set_does_not_cache_when_write_fails(self):
     mock_atomic_write = self.patch_autospec(env, "atomic_write")
     exception = factory.make_exception()
     mock_atomic_write.side_effect = exception
     contents = factory.make_name("contents")
     with ExpectedException(type(exception)):
         env.set_maas_id(contents)
     self.assertIsNone(env.get_maas_id())
Exemple #8
0
 def create_region_event(self, event_type, event_description="", user=None):
     """Helper to register event and event type for the running region."""
     self.create_node_event(
         system_id=get_maas_id(),
         event_type=event_type,
         event_description=event_description,
         user=user,
     )
Exemple #9
0
def send_rack_event(event_type, description=""):
    """Send an event about the running rack to the region.

    :param event_type: The type of the event.
    :type event_type: unicode
    :param description: An optional description of the event.
    :type description: unicode
    """
    return nodeEventHub.logByID(event_type, get_maas_id(), description)
Exemple #10
0
def touch_last_image_sync_timestamp():
    """Inform the region that images have just been synchronised.

    :return: :class:`Deferred` that can fail with `NoConnectionsAvailable` or
        any exception arising from an `UpdateLastImageSync` RPC.
    """
    try:
        client = getRegionClient()
    except Exception:
        return fail()
    else:
        return client(UpdateLastImageSync, system_id=get_maas_id())
Exemple #11
0
 def test_set_caches_to_normalized_value(self):
     contents = "  %s  " % factory.make_name("contents")
     env.set_maas_id(contents)
     self.assertEquals(env._normalise_maas_id(contents), env.get_maas_id())
Exemple #12
0
def get_maas_facing_server_addresses(
    rack_controller=None,
    ipv4=True,
    ipv6=True,
    link_local=False,
    include_alternates=False,
    default_region_ip=None,
):
    """Return addresses for the MAAS server.

    The address is taken from the configured MAAS URL or `controller.url`.
    Consult the 'maas-region local_config_set' command for details on
    how to set the MAAS URL.

    If there is more than one IP address for the host, all of the addresses for
    the appropriate family will be returned.  (If link_local is False, then
    only non-link-local addresses are returned.

    To get the "best" address, see get_maas_facing_server_address().

    :param rack_controller: The rack controller from the point of view of
        which the server address should be computed.
    :param ipv4: Include IPv4 addresses?  Defaults to `True`.
    :param ipv6: Include IPv6 addresses?  Defaults to `True`.
    :param link_local: Include link-local addresses?   Defaults to `False`.
    :param include_alternates: Include secondary region controllers on the same
        subnet?  Defaults to `False`.
    :param default_region_ip: The default source IP address to be used, if a
        specific URL is not defined.
    :return: IP addresses as a list: [IPAddress, ...]  If the configured URL
        uses a hostname, this function will resolve that hostname.
    :raise UnresolvableHost: if no IP addresses could be found for
        the hostname.

    """
    hostname = get_maas_facing_server_host(rack_controller,
                                           default_region_ip=default_region_ip)
    if ipv6 or ipv4:
        addresses = resolve_hostname(
            hostname, 0 if (ipv6 and ipv4) else 4 if ipv4 else 6)
    else:
        addresses = set()
    if len(addresses) == 0:
        raise UnresolvableHost("No address found for host %s." % hostname)
    if not link_local:
        addresses = [ip for ip in addresses if not ip.is_link_local()]
    # Addresses must be returned in a consistent order, with the set of IP
    # addresses found from get_maas_facing_server_host() (i.e. maas_url)
    # coming first.
    addresses = sorted(addresses)
    if include_alternates:
        maas_id = get_maas_id()
        if maas_id is not None:
            # Circular imports
            from maasserver.models import Subnet
            from maasserver.models import StaticIPAddress

            # Don't include more than one alternate IP address, per region,
            # per address-family.
            regions = set()
            alternate_ips = []
            for ip in addresses:
                if not ip.is_link_local():
                    # Since we only know that the IP address given in the MAAS
                    # URL is reachable, alternates must be pulled from the same
                    # subnet (until we know the "full mesh" of connectivity.)
                    subnet = Subnet.objects.get_best_subnet_for_ip(ip)
                    region_ips = StaticIPAddress.objects.filter(
                        subnet=subnet,
                        interface__node__node_type__in=(
                            NODE_TYPE.REGION_AND_RACK_CONTROLLER,
                            NODE_TYPE.REGION_CONTROLLER,
                        ),
                    )
                    region_ips = region_ips.prefetch_related(
                        "interface_set__node")
                    region_ips = region_ips.order_by("ip")
                    for region_ip in region_ips:
                        for iface in region_ip.interface_set.all():
                            ipa = region_ip.get_ipaddress()
                            if ipa is None:
                                continue
                            # Pick at most one alternate IP address for each
                            # region, per address family.
                            id_plus_family = iface.node.system_id + str(
                                ipa.version)
                            if id_plus_family in regions:
                                continue
                            else:
                                regions.add(id_plus_family)
                                alternate_ips.append(ipa)
            # Append non-duplicate region IP addresses to the list of
            # addresses to return. We don't want duplicates, but we
            # also need to preserve the existing order.
            for address in alternate_ips:
                if address not in addresses:
                    addresses.append(address)
    return addresses
Exemple #13
0
 def test_set_None_clears_cache(self):
     contents = factory.make_name("contents")
     env.set_maas_id(contents)
     self.assertThat(env.get_maas_id(), Equals(contents))
     env.set_maas_id(None)
     self.assertThat(env.get_maas_id(), Is(None))
Exemple #14
0
 def test_set_caches(self):
     contents = factory.make_name("contents")
     env.set_maas_id(contents)
     os.unlink(self.maas_id_path)
     self.assertEquals(contents, env.get_maas_id())
Exemple #15
0
 def test_set_deletes_maas_id_file_if_argument_is_whitespace(self):
     with open(self.maas_id_path, "w") as fd:
         fd.write("This file will be deleted.")
     env.set_maas_id(string.whitespace)
     self.assertThat(self.maas_id_path, Not(FileExists()))
     self.assertIsNone(env.get_maas_id())
Exemple #16
0
 def test__deletes_maas_id_file(self):
     self.useFixture(MAASIDFixture(factory.make_string()))
     url = factory.make_simple_http_url()
     secret = factory.make_bytes()
     register_command.run(self.make_args(url=url, secret=to_hex(secret)))
     self.assertIsNone(get_maas_id())
Exemple #17
0
 def test_get_returns_None_if_maas_id_file_does_not_exist(self):
     self.assertThat(self.maas_id_path, Not(FileExists()))
     self.assertThat(env.get_maas_id(), Is(None))
Exemple #18
0
 def test_creates_maas_uuid(self):
     self.assertThat(get_maas_id(), Is(None))
     with post_commit_hooks:
         start_up.inner_start_up(master=False)
     uuid = Config.objects.get_config("uuid")
     self.assertThat(uuid, Not(Is(None)))
Exemple #19
0
 def test_creates_maas_id_file(self):
     self.assertThat(get_maas_id(), Is(None))
     with post_commit_hooks:
         start_up.inner_start_up(master=False)
     self.assertThat(get_maas_id(), Not(Is(None)))
Exemple #20
0
 def _setUp(self):
     super()._setUp()
     self.addCleanup(env.set_maas_id, env.get_maas_id())
     env.set_maas_id(self.system_id)
Exemple #21
0
 def test_get_returns_None_if_maas_id_file_is_whitespace(self):
     with open(self.maas_id_path, "w") as fd:
         fd.write(string.whitespace)
     self.assertThat(env.get_maas_id(), Is(None))
Exemple #22
0
 def _setUp(self):
     super(MAASIDFixture, self)._setUp()
     self.addCleanup(env.set_maas_id, env.get_maas_id())
     env.set_maas_id(self.system_id)
Exemple #23
0
 def test_get_returns_None_if_maas_id_file_is_empty(self):
     with open(self.maas_id_path, "w"):
         pass  # Write nothing.
     self.assertThat(env.get_maas_id(), Is(None))
Exemple #24
0
 def test__creates_maas_id_file(self):
     start_up.is_master_process.return_value = False
     self.assertThat(get_maas_id(), Is(None))
     with post_commit_hooks:
         start_up.inner_start_up()
     self.assertThat(get_maas_id(), Not(Is(None)))