Exemplo n.º 1
0
    def fetch(self, params):
        """Fetch the releases and the arches from the provided source."""
        # Must be administrator.
        assert self.user.is_superuser, "Permission denied."
        # Build a source, but its not saved into the database.
        boot_source = self.get_bootsource(params, from_db=False)
        try:
            # Validate the boot source fields without committing it.
            boot_source.clean_fields()
        except ValidationError as error:
            raise HandlerValidationError(error)
        source = boot_source.to_dict_without_selections()

        # FIXME: This modifies the environment of the entire process, which is
        # Not Cool. We should integrate with simplestreams in a more
        # Pythonic manner.
        set_simplestreams_env()
        with tempdir("keyrings") as keyrings_path:
            [source] = write_all_keyrings(keyrings_path, [source])
            try:
                descriptions = download_all_image_descriptions(
                    [source], user_agent=get_maas_user_agent())
            except Exception as error:
                raise HandlerError(str(error))
        items = list(descriptions.items())
        err_msg = "Mirror provides no Ubuntu images."
        if len(items) == 0:
            raise HandlerError(err_msg)
        releases = {}
        arches = {}
        for image_spec, product_info in items:
            # Only care about Ubuntu images.
            if image_spec.os != "ubuntu":
                continue
            releases[image_spec.release] = {
                "name":
                image_spec.release,
                "title":
                product_info.get(
                    "release_title",
                    format_ubuntu_distro_series(image_spec.release),
                ),
                "checked":
                False,
                "deleted":
                False,
            }
            arches[image_spec.arch] = {
                "name": image_spec.arch,
                "title": image_spec.arch,
                "checked": False,
                "deleted": False,
            }
        if len(releases) == 0 or len(arches) == 0:
            raise HandlerError(err_msg)
        return json.dumps({
            "releases": list(releases.values()),
            "arches": list(arches.values()),
        })
Exemplo n.º 2
0
 def test_get_maas_user_agent_with_uuid(self):
     region = factory.make_RegionController()
     self.useFixture(MAASIDFixture(region.system_id))
     RegionController.objects.get_or_create_uuid()
     user_agent = get_maas_user_agent()
     composed_user_agent = "%s/%s" % (get_maas_version_user_agent(),
                                      Config.objects.get_config('uuid'))
     self.assertEquals(user_agent, composed_user_agent)
Exemplo n.º 3
0
 def test__passes_user_agent_with_maas_version(self):
     mock_download = self.patch(bootsources,
                                'download_all_image_descriptions')
     factory.make_BootSource(keyring_data=b'1234')
     cache_boot_sources()
     self.assertThat(
         mock_download,
         MockCalledOnceWith(ANY, user_agent=get_maas_user_agent()))
Exemplo n.º 4
0
def make_maas_user_agent_request():
    headers = {"User-Agent": get_maas_user_agent()}
    params = get_request_params()
    try:
        requests.get("https://stats.images.maas.io/",
                     params=params,
                     headers=headers)
    except Exception:
        # Do not fail if for any reason requests does.
        pass
Exemplo n.º 5
0
def make_maas_user_agent_request():
    headers = {
        'User-Agent': get_maas_user_agent(),
    }
    params = get_request_params()
    try:
        requests.get('https://stats.images.maas.io/',
                     params=params,
                     headers=headers)
    except:
        # Do not fail if for any reason requests does.
        pass
Exemplo n.º 6
0
 def test_get_maas_user_agent_without_uuid(self):
     user_agent = get_maas_user_agent()
     uuid = Config.objects.get_config("uuid")
     self.assertEqual(uuid, None)
     self.assertThat(user_agent, IsNonEmptyString)
     self.assertThat(user_agent, Not(Contains(uuid)))