コード例 #1
0
ファイル: seamicro.py プロジェクト: tai271828/maas
def power_control_seamicro15k_v09(
    ip,
    username,
    password,
    server_id,
    power_change,
    retry_count=5,
    retry_wait=1,
):
    server_id = "%s/0" % server_id
    api = SeaMicroAPIV09(compose_URL("http:///v0.9/", ip))

    while retry_count > 0:
        api.login(username, password)
        try:
            if power_change == "on":
                api.power_on(server_id, do_pxe=True)
            elif power_change == "off":
                api.power_off(server_id, force=True)
        except SeaMicroAPIV09Error as e:
            # Chance that multiple login's are at once, the api
            # only supports one at a time. So lets try again after
            # a second, up to max retry count.
            if e.response_code == 401:
                retry_count -= 1
                time.sleep(retry_wait)
                continue
            else:
                raise
        break
コード例 #2
0
 def test__preserves_port_with_hostname(self):
     hostname = factory.make_name("host")
     port = factory.pick_port()
     self.assertEqual(
         "https://%s:%s/" % (hostname, port),
         compose_URL("https://:%s/" % port, hostname),
     )
コード例 #3
0
ファイル: preseed.py プロジェクト: kcns008/maas
def get_curtin_installer_url(node):
    """Return the URL where curtin on the node can download its installer."""
    osystem = node.get_osystem()
    series = node.get_distro_series()
    arch, subarch = node.architecture.split('/')
    # XXX rvb(?): The path shouldn't be hardcoded like this, but rather synced
    # somehow with the content of contrib/maas-cluster-http.conf.
    # Per etc/services cluster is opening port 5248 to serve images via HTTP
    image = get_curtin_image(node)
    if image['xinstall_type'] == 'squashfs':
        return 'cp:///media/root-ro'
    elif image['xinstall_type'] == 'tgz':
        url_prepend = ''
    else:
        url_prepend = '%s:' % image['xinstall_type']
    dyn_uri = '/'.join([
        osystem,
        arch,
        subarch,
        series,
        image['label'],
        image['xinstall_path'],
    ])
    url = compose_URL('http://:5248/images/%s' % dyn_uri,
                      str(node.boot_cluster_ip))
    return url_prepend + url
コード例 #4
0
def absolute_reverse(view_name,
                     default_region_ip=None,
                     query=None,
                     base_url=None,
                     *args,
                     **kwargs):
    """Return the absolute URL (i.e. including the URL scheme specifier and
    the network location of the MAAS server).  Internally this method simply
    calls Django's 'reverse' method and prefixes the result of that call with
    the configured MAAS URL.

    Consult the 'maas-region local_config_set --default-url' command for
    details on how to set the MAAS URL.

    :param view_name: Django's view function name/reference or URL pattern
        name for which to compute the absolute URL.
    :param default_region_ip: The default source IP address that should be
        used for the region controller.
    :param query: Optional query argument which will be passed down to
        urllib.urlencode.  The result of that call will be appended to the
        resulting url.
    :param base_url: Optional url used as base.  If None is provided, then
        configured MAAS URL will be used.
    :param args: Positional arguments for Django's 'reverse' method.
    :param kwargs: Named arguments for Django's 'reverse' method.
    """
    if not base_url:
        with RegionConfiguration.open() as config:
            base_url = config.maas_url
        if default_region_ip is not None:
            base_url = compose_URL(base_url, default_region_ip)
    url = urljoin(base_url, reverse(view_name, *args, **kwargs))
    if query is not None:
        url += '?%s' % urlencode(query, doseq=True)
    return url
コード例 #5
0
 def test__preserves_port_with_IPv6(self):
     ip = factory.make_ipv6_address()
     port = factory.pick_port()
     self.assertEqual(
         "https://[%s]:%s/" % (ip, port),
         compose_URL("https://:%s/" % port, ip),
     )
コード例 #6
0
 def test__inserts_hostname(self):
     hostname = factory.make_name("host")
     path = self.make_path()
     self.assertEqual(
         "http://%s/%s" % (hostname, path),
         compose_URL("http:///%s" % path, hostname),
     )
コード例 #7
0
 def test__escapes_IPv6_zone_index(self):
     ip = factory.make_ipv6_address()
     zone = self.make_network_interface()
     hostname = '%s%%%s' % (ip, zone)
     path = self.make_path()
     self.assertEqual('http://[%s%%25%s]/%s' % (ip, zone, path),
                      compose_URL('http:///%s' % path, hostname))
コード例 #8
0
 def test__does_not_escape_bracketed_IPv6_zone_index(self):
     ip = factory.make_ipv6_address()
     zone = self.make_network_interface()
     path = self.make_path()
     hostname = '[%s%%25%s]' % (ip, zone)
     self.assertEqual('http://%s/%s' % (hostname, path),
                      compose_URL('http:///%s' % path, hostname))
コード例 #9
0
ファイル: preseed.py プロジェクト: sfeole/maas
def get_curtin_installer_url(node):
    """Return the URL where curtin on the node can download its installer."""
    osystem = node.get_osystem()
    series = node.get_distro_series()
    arch, subarch = node.architecture.split('/')
    # XXX rvb(?): The path shouldn't be hardcoded like this, but rather synced
    # somehow with the content of contrib/maas-cluster-http.conf.
    # Per etc/services cluster is opening port 5248 to serve images via HTTP
    image = get_curtin_image(node)
    if image['xinstall_type'] == 'squashfs':
        # XXX: roaksoax LP: #1739761 - Since the switch to squashfs (and drop
        # of iscsi), precise is no longer deployable. To address a squashfs
        # image is made available allowing it to be deployed in the
        # commissioning ephemeral environment.
        if series == 'precise':
            url_prepend = "fsimage:"
        else:
            return 'cp:///media/root-ro'
    elif image['xinstall_type'] == 'tgz':
        url_prepend = ''
    else:
        url_prepend = '%s:' % image['xinstall_type']
    dyn_uri = '/'.join([
        osystem,
        arch,
        subarch,
        series,
        image['label'],
        image['xinstall_path'],
    ])
    url = compose_URL('http://:5248/images/%s' % dyn_uri,
                      str(node.boot_cluster_ip))
    return url_prepend + url
コード例 #10
0
 def test__preserves_query(self):
     ip = factory.make_ipv4_address()
     key = factory.make_name("key")
     value = factory.make_name("value")
     self.assertEqual(
         "https://%s?%s=%s" % (ip, key, value),
         compose_URL("https://?%s=%s" % (key, value), ip),
     )
コード例 #11
0
 def test__inserts_bracketed_IPv6_unchanged(self):
     ip = factory.make_ipv6_address()
     hostname = "[%s]" % ip
     path = self.make_path()
     self.assertEqual(
         "http://%s/%s" % (hostname, path),
         compose_URL("http:///%s" % path, hostname),
     )
コード例 #12
0
ファイル: compose_preseed.py プロジェクト: ddstreet/maas
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
コード例 #13
0
ファイル: __init__.py プロジェクト: sydneypdx/maas
def absolute_reverse(
    view_name,
    default_region_ip=None,
    query=None,
    base_url=None,
    *args,
    **kwargs,
):
    """Return the absolute URL (i.e. including the URL scheme specifier and
    the network location of the MAAS server).  Internally this method simply
    calls Django's 'reverse' method and prefixes the result of that call with
    the configured MAAS URL.

    Consult the 'maas-region local_config_set --default-url' command for
    details on how to set the MAAS URL.

    :param view_name: Django's view function name/reference or URL pattern
        name for which to compute the absolute URL.
    :param default_region_ip: The default source IP address that should be
        used for the region controller.
    :param query: Optional query argument which will be passed down to
        urllib.urlencode.  The result of that call will be appended to the
        resulting url.
    :param base_url: Optional url used as base.  If None is provided, then
        configured MAAS URL will be used.
    :param args: Positional arguments for Django's 'reverse' method.
    :param kwargs: Named arguments for Django's 'reverse' method.
    """
    if not base_url:
        with RegionConfiguration.open() as config:
            base_url = config.maas_url
        if default_region_ip is not None:
            base_url = compose_URL(base_url, default_region_ip)
    if not base_url.endswith("/"):
        # Add trailing '/' to get urljoin to behave.
        base_url = base_url + "/"
    if view_name.startswith("/"):
        reverse_link = view_name
    else:
        reverse_link = reverse(view_name, *args, **kwargs)
    if reverse_link.startswith("/"):
        # Drop the leading '/'.
        reverse_link = reverse_link[1:]
    script_name = settings.FORCE_SCRIPT_NAME.lstrip("/")
    if base_url.endswith(script_name) and reverse_link.startswith(script_name):
        # This would double up the SCRIPT_NAME we only need one so remove the
        # prefix from the reverse_link.
        reverse_link = reverse_link[len(script_name):]
    url = urljoin(base_url, reverse_link)
    if query is not None:
        url += "?%s" % urlencode(query, doseq=True)
    return url
コード例 #14
0
def get_seamicro15k_api(version, ip, username, password):
    """Gets the api client depending on the version.
    Supports v0.9 and v2.0.

    :return: api for version, None if version not supported
    """
    if version == 'v0.9':
        api = SeaMicroAPIV09(compose_URL('http:///v0.9/', ip))
        try:
            api.login(username, password)
        except urllib.error.URLError:
            # Cannot reach using v0.9, might not be supported
            return None
        return api
    elif version == 'v2.0':
        url = compose_URL('http:///v2.0', ip)
        try:
            api = seamicro_client.Client(
                auth_url=url, username=username, password=password)
        except seamicro_exceptions.ConnectionRefused:
            # Cannot reach using v2.0, might no be supported
            return None
        return api
コード例 #15
0
def get_apt_proxy(rack_controller=None):
    """Return the APT proxy for the `rack_controller`."""
    if Config.objects.get_config("enable_http_proxy"):
        http_proxy = Config.objects.get_config("http_proxy")
        if http_proxy is not None:
            http_proxy = http_proxy.strip()
        use_peer_proxy = Config.objects.get_config("use_peer_proxy")
        if http_proxy and not use_peer_proxy:
            return http_proxy
        else:
            return compose_URL(
                "http://:8000/", get_maas_facing_server_host(rack_controller))
    else:
        return None
コード例 #16
0
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
コード例 #17
0
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
コード例 #18
0
 def test__preserves_port_with_IPv4(self):
     ip = factory.make_ipv4_address()
     port = factory.pick_port()
     self.assertEqual('https://%s:%s/' % (ip, port),
                      compose_URL('https://:%s/' % port, ip))
コード例 #19
0
 def test__preserves_query(self):
     ip = factory.make_ipv4_address()
     key = factory.make_name('key')
     value = factory.make_name('value')
     self.assertEqual('https://%s?%s=%s' % (ip, key, value),
                      compose_URL('https://?%s=%s' % (key, value), ip))
コード例 #20
0
 def test__inserts_hostname(self):
     hostname = factory.make_name('host')
     path = self.make_path()
     self.assertEqual('http://%s/%s' % (hostname, path),
                      compose_URL('http:///%s' % path, hostname))
コード例 #21
0
 def test__inserts_IPv6_with_brackets(self):
     ip = factory.make_ipv6_address()
     path = self.make_path()
     self.assertEqual('http://[%s]/%s' % (ip, path),
                      compose_URL('http:///%s' % path, ip))
コード例 #22
0
 def test__inserts_IPv4(self):
     ip = factory.make_ipv4_address()
     path = self.make_path()
     self.assertEqual('http://%s/%s' % (ip, path),
                      compose_URL('http:///%s' % path, ip))