Example #1
0
def get_preseed_context(request, osystem="", release="", rack_controller=None):
    """Return the node-independent context dictionary to be used to render
    preseed templates.

    :param osystem: See `get_preseed_filenames`.
    :param release: See `get_preseed_filenames`.
    :param rack_controller: The rack controller used to generate the preseed.
    :return: The context dictionary.
    :rtype: dict.
    """
    region_ip = get_default_region_ip(request)
    server_host = get_maas_facing_server_host(
        rack_controller=rack_controller, default_region_ip=region_ip
    )
    server_url = request.build_absolute_uri(reverse("machines_handler"))
    metadata_enlist_url = request.build_absolute_uri(reverse("enlist"))
    configs = Config.objects.get_configs(["remote_syslog", "maas_syslog_port"])
    syslog = configs["remote_syslog"]
    http_proxy = get_apt_proxy(request, rack_controller)
    if not syslog:
        syslog_port = configs["maas_syslog_port"]
        if not syslog_port:
            syslog_port = RSYSLOG_PORT
        syslog = "%s:%d" % (server_host, syslog_port)
    return {
        "osystem": osystem,
        "release": release,
        "server_host": server_host,
        "server_url": server_url,
        "syslog_host_port": syslog,
        "metadata_enlist_url": metadata_enlist_url,
        "http_proxy": http_proxy,
    }
Example #2
0
    def test_compose_preseed_for_curtin_and_trusty_aptsources(self):
        # Disable boot source cache signals.
        self.addCleanup(bootsources.signals.enable)
        bootsources.signals.disable()

        rack_controller = factory.make_RackController()
        node = factory.make_Node(
            interface=True,
            status=NODE_STATUS.READY,
            osystem="ubuntu",
            distro_series="trusty",
        )
        nic = node.get_boot_interface()
        nic.vlan.dhcp_on = True
        nic.vlan.primary_rack = rack_controller
        nic.vlan.save()
        self.useFixture(RunningClusterRPCFixture())
        request = make_HttpRequest()
        apt_proxy = get_apt_proxy(request, node.get_boot_rack_controller())
        preseed = yaml.safe_load(
            compose_preseed(request, PRESEED_TYPE.CURTIN, node))

        self.assertIn("apt_sources", preseed)
        self.assertEqual(apt_proxy, preseed["apt_proxy"])
        self.assertSystemInfo(preseed)
Example #3
0
 def test_compose_preseed_for_commissioning_node_produces_yaml(self):
     rack_controller = factory.make_RackController()
     node = factory.make_Node(interface=True,
                              status=NODE_STATUS.COMMISSIONING)
     nic = node.get_boot_interface()
     nic.vlan.dhcp_on = True
     nic.vlan.primary_rack = rack_controller
     nic.vlan.save()
     request = make_HttpRequest()
     apt_proxy = get_apt_proxy(request, node.get_boot_rack_controller())
     preseed = yaml.safe_load(
         compose_preseed(request, PRESEED_TYPE.COMMISSIONING, node))
     self.assertIn("datasource", preseed)
     self.assertIn("MAAS", preseed["datasource"])
     self.assertThat(
         preseed["datasource"]["MAAS"],
         KeysEqual("metadata_url", "consumer_key", "token_key",
                   "token_secret"),
     )
     self.assertThat(
         preseed["reporting"]["maas"],
         KeysEqual("consumer_key", "endpoint", "token_key", "token_secret",
                   "type"),
     )
     self.assertThat(preseed["rsyslog"]["remotes"], KeysEqual("maas"))
     self.assertAptConfig(preseed, apt_proxy)
Example #4
0
    def test_compose_preseed_with_curtin_installer(self):
        rack_controller = factory.make_RackController(url="")
        node = factory.make_Node(interface=True, status=NODE_STATUS.DEPLOYING)
        nic = node.get_boot_interface()
        nic.vlan.dhcp_on = True
        nic.vlan.primary_rack = rack_controller
        nic.vlan.save()
        self.useFixture(RunningClusterRPCFixture())
        request = make_HttpRequest()
        expected_apt_proxy = get_apt_proxy(request,
                                           node.get_boot_rack_controller())
        preseed = yaml.safe_load(
            compose_preseed(request, PRESEED_TYPE.CURTIN, node))

        self.assertIn("datasource", preseed)
        self.assertIn("MAAS", preseed["datasource"])
        self.assertThat(
            preseed["datasource"]["MAAS"],
            KeysEqual("metadata_url", "consumer_key", "token_key",
                      "token_secret"),
        )
        self.assertDictEqual(
            {
                "delay": "now",
                "mode": "reboot",
                "timeout": 1800,
                "condition": "test ! -e /tmp/block-reboot",
            },
            preseed["power_state"],
        )
        self.assertEqual(
            request.build_absolute_uri(reverse("curtin-metadata")),
            preseed["datasource"]["MAAS"]["metadata_url"],
        )
        self.assertAptConfig(preseed, expected_apt_proxy)
Example #5
0
    def test_compose_preseed_with_curtin_installer(self):
        rack_controller = factory.make_RackController(url='')
        node = factory.make_Node(interface=True, status=NODE_STATUS.DEPLOYING)
        nic = node.get_boot_interface()
        nic.vlan.dhcp_on = True
        nic.vlan.primary_rack = rack_controller
        nic.vlan.save()
        self.useFixture(RunningClusterRPCFixture())
        request = make_HttpRequest()
        expected_apt_proxy = get_apt_proxy(request,
                                           node.get_boot_rack_controller())
        preseed = yaml.safe_load(
            compose_preseed(request, PRESEED_TYPE.CURTIN, node))

        self.assertIn('datasource', preseed)
        self.assertIn('MAAS', preseed['datasource'])
        self.assertThat(
            preseed['datasource']['MAAS'],
            KeysEqual('metadata_url', 'consumer_key', 'token_key',
                      'token_secret'))
        self.assertDictEqual(
            {
                'delay': 'now',
                'mode': 'reboot',
                'timeout': 1800,
                'condition': 'test ! -e /tmp/block-reboot',
            }, preseed['power_state'])
        self.assertEqual(
            request.build_absolute_uri(reverse('curtin-metadata')),
            preseed['datasource']['MAAS']['metadata_url'])
        self.assertAptConfig(preseed, expected_apt_proxy)
Example #6
0
 def test_compose_preseed_for_commissioning_node_produces_yaml(self):
     rack_controller = factory.make_RackController()
     node = factory.make_Node(interface=True,
                              status=NODE_STATUS.COMMISSIONING)
     nic = node.get_boot_interface()
     nic.vlan.dhcp_on = True
     nic.vlan.primary_rack = rack_controller
     nic.vlan.save()
     apt_proxy = get_apt_proxy(node.get_boot_rack_controller())
     preseed = yaml.safe_load(
         compose_preseed(PRESEED_TYPE.COMMISSIONING, node))
     self.assertIn('datasource', preseed)
     self.assertIn('MAAS', preseed['datasource'])
     self.assertThat(
         preseed['datasource']['MAAS'],
         KeysEqual('metadata_url', 'consumer_key', 'token_key',
                   'token_secret'))
     self.assertEqual(apt_proxy, preseed['apt_proxy'])
     self.assertThat(
         preseed['reporting']['maas'],
         KeysEqual('consumer_key', 'endpoint', 'token_key', 'token_secret',
                   'type'))
     self.assertThat(preseed['rsyslog']['remotes'], KeysEqual('maas'))
     self.assertSystemInfo(preseed)
     self.assertAptConfig(preseed, apt_proxy)
Example #7
0
    def test__returns_correct_url(self):
        import maasserver.compose_preseed as cp_module

        # Disable boot source cache signals.
        self.addCleanup(bootsources.signals.enable)
        bootsources.signals.disable()
        # Force the server host to be our test data.
        self.patch(cp_module, 'get_maas_facing_server_host').return_value = (
            self.rack if self.rack else self.default_region_ip)
        # Now setup the configuration and arguments, and see what we get back.
        node = factory.make_Node(interface=True,
                                 status=NODE_STATUS.COMMISSIONING)
        Config.objects.set_config("enable_http_proxy", self.enable)
        Config.objects.set_config("http_proxy", self.http_proxy)
        Config.objects.set_config("use_peer_proxy", self.use_peer_proxy)
        Config.objects.set_config("use_rack_proxy", self.use_rack_proxy)
        if self.maas_proxy_port:
            Config.objects.set_config("maas_proxy_port", self.maas_proxy_port)
        request = make_HttpRequest(http_host=self.default_region_ip)
        if self.use_rack_proxy:
            subnet = None
            if self.cidr:
                subnet = factory.make_Subnet(cidr=self.cidr)
                if self.subnet_dns:
                    subnet.dns_servers = [self.subnet_dns]
                else:
                    subnet.dns_servers = []
                subnet.save()
                request.META['REMOTE_ADDR'] = factory.pick_ip_in_Subnet(subnet)
            else:
                request.META['REMOTE_ADDR'] = factory.make_ipv4_address()
        actual = get_apt_proxy(request, node.get_boot_rack_controller())
        self.assertEqual(self.result, actual)
Example #8
0
def get_preseed_context(request, osystem='', release='', rack_controller=None):
    """Return the node-independent context dictionary to be used to render
    preseed templates.

    :param osystem: See `get_preseed_filenames`.
    :param release: See `get_preseed_filenames`.
    :param rack_controller: The rack controller used to generate the preseed.
    :return: The context dictionary.
    :rtype: dict.
    """
    region_ip = get_default_region_ip(request)
    server_host = get_maas_facing_server_host(rack_controller=rack_controller,
                                              default_region_ip=region_ip)
    server_url = request.build_absolute_uri(reverse('machines_handler'))
    metadata_enlist_url = request.build_absolute_uri(reverse('enlist'))
    configs = Config.objects.get_configs(['remote_syslog', 'maas_syslog_port'])
    syslog = configs['remote_syslog']
    http_proxy = get_apt_proxy(request, rack_controller)
    if not syslog:
        syslog_port = configs['maas_syslog_port']
        if not syslog_port:
            syslog_port = RSYSLOG_PORT
        syslog = '%s:%d' % (server_host, syslog_port)
    return {
        'osystem': osystem,
        'release': release,
        'server_host': server_host,
        'server_url': server_url,
        'syslog_host_port': syslog,
        'metadata_enlist_url': metadata_enlist_url,
        'http_proxy': http_proxy,
    }
Example #9
0
def get_enlist_userdata(rack_controller=None):
    """Return the enlistment preseed.

    :param rack_controller: The rack controller used to generate the preseed.
    :return: The rendered enlistment user-data string.
    :rtype: unicode.
    """
    http_proxy = get_apt_proxy(rack_controller=rack_controller)
    enlist_userdata = render_enlistment_preseed(
        USERDATA_TYPE.ENLIST, rack_controller=rack_controller)
    config = get_system_info()
    config.update({'apt_proxy': http_proxy})
    config.update(get_enlist_archive_config(http_proxy))
    return enlist_userdata + yaml.safe_dump(config).encode('utf-8')
Example #10
0
    def test__returns_correct_url(self):
        import maasserver.compose_preseed as cp_module

        # Disable boot source cache signals.
        self.addCleanup(bootsources.signals.enable)
        bootsources.signals.disable()
        # Force the server host to be our test data.
        self.patch(cp_module,
                   'get_maas_facing_server_host').return_value = self.rack
        # Now setup the configuration and arguments, and see what we get back.
        node = factory.make_Node(interface=True,
                                 status=NODE_STATUS.COMMISSIONING)
        Config.objects.set_config("enable_http_proxy", self.enable)
        Config.objects.set_config("http_proxy", self.http_proxy)
        Config.objects.set_config("use_peer_proxy", self.use_peer_proxy)
        actual = get_apt_proxy(node.get_boot_rack_controller())
        self.assertEqual(self.result, actual)
Example #11
0
 def test_compose_enlistment_preseed(self):
     rack_controller = factory.make_RackController()
     url = factory.make_simple_http_url()
     request = make_HttpRequest()
     apt_proxy = get_apt_proxy(request, rack_controller)
     preseed = yaml.safe_load(
         compose_enlistment_preseed(
             request,
             rack_controller,
             {
                 "metadata_enlist_url": url,
                 "syslog_host_port": url
             },
         ))
     self.assertDictEqual({"MAAS": {
         "metadata_url": url
     }}, preseed["datasource"])
     self.assertTrue(preseed["manage_etc_hosts"])
     self.assertDictEqual({"remotes": {"maas": url}}, preseed["rsyslog"])
     self.assertDictEqual(
         {
             "delay": "now",
             "mode": "poweroff",
             "timeout": 1800,
             "condition": "test ! -e /tmp/block-poweroff",
         },
         preseed["power_state"],
     )
     self.assertItemsEqual(
         [
             "python3-yaml",
             "python3-oauthlib",
             "freeipmi-tools",
             "ipmitool",
             "sshpass",
             "archdetect-deb",
             "jq",
         ],
         preseed["packages"],
     )
     default = PackageRepository.get_main_archive().url
     ports = PackageRepository.get_ports_archive().url
     self.assertThat(
         preseed,
         ContainsDict({
             "apt":
             ContainsDict({
                 "preserve_sources_list":
                 Equals(False),
                 "primary":
                 MatchesListwise([
                     MatchesDict({
                         "arches": Equals(["amd64", "i386"]),
                         "uri": Equals(default),
                     }),
                     MatchesDict({
                         "arches": Equals(["default"]),
                         "uri": Equals(ports),
                     }),
                 ]),
                 "proxy":
                 Equals(apt_proxy),
                 "security":
                 MatchesListwise([
                     MatchesDict({
                         "arches": Equals(["amd64", "i386"]),
                         "uri": Equals(default),
                     }),
                     MatchesDict({
                         "arches": Equals(["default"]),
                         "uri": Equals(ports),
                     }),
                 ]),
             })
         }),
     )
Example #12
0
 def test_compose_enlistment_preseed(self):
     rack_controller = factory.make_RackController()
     url = factory.make_simple_http_url()
     request = make_HttpRequest()
     apt_proxy = get_apt_proxy(request, rack_controller)
     preseed = yaml.safe_load(
         compose_enlistment_preseed(request, rack_controller, {
             'metadata_enlist_url': url,
             'syslog_host_port': url,
         }))
     self.assertDictEqual({'MAAS': {
         'metadata_url': url
     }}, preseed['datasource'])
     self.assertTrue(preseed['manage_etc_hosts'])
     self.assertDictEqual({'remotes': {'maas': url}}, preseed['rsyslog'])
     self.assertDictEqual(
         {
             'delay': 'now',
             'mode': 'poweroff',
             'timeout': 1800,
             'condition': 'test ! -e /tmp/block-poweroff',
         }, preseed['power_state'])
     self.assertItemsEqual([
         'python3-yaml', 'python3-oauthlib', 'freeipmi-tools', 'ipmitool',
         'sshpass', 'archdetect-deb', 'jq'
     ], preseed['packages'])
     default = PackageRepository.get_main_archive().url
     ports = PackageRepository.get_ports_archive().url
     self.assertThat(
         preseed,
         ContainsDict({
             'apt':
             ContainsDict({
                 'preserve_sources_list':
                 Equals(False),
                 'primary':
                 MatchesListwise([
                     MatchesDict({
                         "arches": Equals(["amd64", "i386"]),
                         "uri": Equals(default),
                     }),
                     MatchesDict({
                         "arches": Equals(["default"]),
                         "uri": Equals(ports),
                     }),
                 ]),
                 'proxy':
                 Equals(apt_proxy),
                 'security':
                 MatchesListwise([
                     MatchesDict({
                         "arches": Equals(["amd64", "i386"]),
                         "uri": Equals(default),
                     }),
                     MatchesDict({
                         "arches": Equals(["default"]),
                         "uri": Equals(ports),
                     }),
                 ]),
             })
         }))