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'] 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, }
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, }
def read(self, request, version, mac=None): check_version(version) node = get_queried_node(request, for_mac=mac) default_region_ip = get_default_region_ip(request) user_data = get_curtin_userdata(node, default_region_ip) return HttpResponse( user_data, content_type='application/octet-stream')
def get_apt_proxy(request, rack_controller=None, node=None): """Return the APT proxy for the `rack_controller`.""" config = Config.objects.get_configs([ "enable_http_proxy", "http_proxy", "use_peer_proxy", "maas_proxy_port", "maas_internal_domain", "use_rack_proxy", ]) if config["enable_http_proxy"]: http_proxy = config["http_proxy"] if http_proxy is not None: http_proxy = http_proxy.strip() use_peer_proxy = config["use_peer_proxy"] if http_proxy and not use_peer_proxy: return http_proxy else: # Ensure the proxy port is the default if not set. maas_proxy_port = config["maas_proxy_port"] if not maas_proxy_port: maas_proxy_port = 8000 # Use the client requesting the preseed to determine how they # should access the APT proxy. subnet = None remote_ip = get_remote_ip(request) if remote_ip is not None: subnet = Subnet.objects.get_best_subnet_for_ip(remote_ip) use_dns = (subnet is not None and not subnet.dns_servers and subnet.vlan.dhcp_on) if config["use_rack_proxy"] and use_dns: # Client can use the MAAS proxy on the rack controller with # DNS resolution providing better HA. return "http://%s.%s:%d/" % ( get_resource_name_for_subnet(subnet), config["maas_internal_domain"], maas_proxy_port, ) elif (config["use_rack_proxy"] and node is not None and node.boot_cluster_ip): # Client can use the MAAS proxy on the rack controller with # IP address, instead of DNS. return "http://%s:%d/" % ( node.boot_cluster_ip, maas_proxy_port, ) else: # Fallback to sending the APT directly to the # region controller. region_ip = get_default_region_ip(request) url = "http://:%d/" % maas_proxy_port return compose_URL( url, get_maas_facing_server_host(rack_controller, default_region_ip=region_ip), ) else: return None
def get_preseed(self, request, version=None, system_id=None): """Render and return a preseed script for the given node.""" node = get_object_or_404(Node, system_id=system_id) # XXX: Set a charset for text/plain. Django automatically encodes # non-binary content using DEFAULT_CHARSET (which is UTF-8 by default) # but only sets the charset parameter in the content-type header when # a content-type is NOT provided. region_ip = get_default_region_ip(request) preseed = get_preseed(node, region_ip) return HttpResponse(preseed, content_type="text/plain")
def get_enlist_preseed(self, request, version=None): """Render and return a preseed script for enlistment.""" rack_controller = find_rack_controller(request) # XXX: Set a charset for text/plain. Django automatically encodes # non-binary content using DEFAULT_CHARSET (which is UTF-8 by default) # but only sets the charset parameter in the content-type header when # a content-type is NOT provided. region_ip = get_default_region_ip(request) preseed = get_enlist_preseed(rack_controller=rack_controller, default_region_ip=region_ip) return HttpResponse(preseed, content_type="text/plain")
def read(self, request, version): check_version(version) rack_controller = find_rack_controller(request) default_region_ip = get_default_region_ip(request) # XXX: Set a charset for text/plain. Django automatically encodes # non-binary content using DEFAULT_CHARSET (which is UTF-8 by default) # but only sets the charset parameter in the content-type header when # a content-type is NOT provided. return HttpResponse(get_enlist_userdata( rack_controller=rack_controller, default_region_ip=default_region_ip), content_type="text/plain")
def get_apt_proxy(request, rack_controller=None): """Return the APT proxy for the `rack_controller`.""" config = Config.objects.get_configs([ 'enable_http_proxy', 'http_proxy', 'use_peer_proxy', 'maas_proxy_port', 'maas_internal_domain', 'use_rack_proxy' ]) if config["enable_http_proxy"]: http_proxy = config["http_proxy"] if http_proxy is not None: http_proxy = http_proxy.strip() use_peer_proxy = config["use_peer_proxy"] if http_proxy and not use_peer_proxy: return http_proxy else: # Ensure the proxy port is the default if not set. maas_proxy_port = config["maas_proxy_port"] if not maas_proxy_port: maas_proxy_port = 8000 # Use the client requesting the preseed to determine how they # should access the APT proxy. subnet = None remote_ip = get_remote_ip(request) if remote_ip is not None: subnet = Subnet.objects.get_best_subnet_for_ip(remote_ip) if (config['use_rack_proxy'] and subnet is not None and not subnet.dns_servers): # Client can use the MAAS proxy on the rack controller. return "http://%s.%s:%d/" % (get_resource_name_for_subnet( subnet), config["maas_internal_domain"], maas_proxy_port) else: # Client cannot use the MAAS proxy on the rack controller # because rack proxy is disabled, the subnet the IP belongs to # is unknown or the subnet is using DNS servers that are not # MAAS. Fallback to using the old way pre MAAS 2.5. region_ip = get_default_region_ip(request) url = "http://:%d/" % maas_proxy_port return compose_URL( url, get_maas_facing_server_host(rack_controller, default_region_ip=region_ip)) else: return None
def get_apt_proxy(request, rack_controller=None): """Return the APT proxy for the `rack_controller`.""" config = Config.objects.get_configs([ 'enable_http_proxy', 'http_proxy', 'use_peer_proxy', 'maas_proxy_port' ]) if config["enable_http_proxy"]: http_proxy = config["http_proxy"] if http_proxy is not None: http_proxy = http_proxy.strip() use_peer_proxy = config["use_peer_proxy"] if http_proxy and not use_peer_proxy: return http_proxy else: region_ip = get_default_region_ip(request) url = "http://:%d/" % config["maas_proxy_port"] return compose_URL( url, get_maas_facing_server_host(rack_controller, default_region_ip=region_ip)) else: return None
def test_returns_Host_header_if_available_and_strips_port(self): self.assertThat( get_default_region_ip(make_request("127.0.0.1", "localhost:5240")), Equals("localhost"), )
def test_returns_source_ip_based_on_remote_ip_if_no_Host_header(self): # Note: the source IP should resolve to the loopback interface here. self.assertThat( get_default_region_ip(make_request("127.0.0.2")), Equals("127.0.0.1"), )
def test__returns_Host_header_if_available(self): self.assertThat( get_default_region_ip(make_request("127.0.0.1", "localhost")), Equals("localhost"))
def get_rsyslog_host_port(request, node): """Return the rsyslog host and port to use.""" region_ip = get_default_region_ip(request) host = get_maas_facing_server_host(node.get_boot_rack_controller(), default_region_ip=region_ip) return "%s:%d" % (host, RSYSLOG_PORT)