File size: 3,670 Bytes
4d9ee25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from typing import Iterable, Literal

import grpc

from . import beaker_pb2 as pb2
from ._service_client import RpcMethod, ServiceClient
from .exceptions import *
from .types import *


class ClusterClient(ServiceClient):
    """
    Methods for interacting with Beaker `Clusters <https://beaker-docs.apps.allenai.org/concept/clusters.html>`_.
    Accessed via the :data:`Beaker.cluster <beaker.Beaker.cluster>` property.

    .. warning::
        Do not instantiate this class directly! The :class:`~beaker.Beaker` client will create
        one automatically which you can access through the corresponding property.
    """

    def get(self, cluster: str, *, include_cluster_occupancy: bool = False) -> pb2.Cluster:
        """
        :examples:

        >>> with Beaker.from_env() as beaker:
        ...     cluster = beaker.cluster.get(cluster_name)

        :returns: A :class:`~beaker.types.BeakerCluster`.

        :raises ~beaker.exceptions.BeakerClusterNotFound: If the cluster doesn't exist.
        """
        return self.rpc_request(
            RpcMethod[pb2.GetClusterResponse](self.service.GetCluster),
            pb2.GetClusterRequest(
                cluster_id=self.resolve_cluster_id(cluster),
                include_cluster_occupancy=include_cluster_occupancy,
            ),
            exceptions_for_status={grpc.StatusCode.NOT_FOUND: BeakerClusterNotFound(cluster)},
        ).cluster

    def list(
        self,
        *,
        org: pb2.Organization | None = None,
        sort_order: BeakerSortOrder | None = None,
        sort_field: Literal[
            "created", "name", "running_jobs", "total_nodes", "total_gpus", "free_gpus"
        ] = "name",
        include_cluster_occupancy: bool = False,
        limit: int | None = None,
    ) -> Iterable[pb2.Cluster]:
        """
        List clusters.

        :returns: An iterator over :class:`~beaker.types.BeakerCluster` protobuf objects.
        """
        if limit is not None and limit <= 0:
            raise ValueError("'limit' must be a positive integer")

        count = 0
        for response in self.rpc_paged_request(
            RpcMethod[pb2.ListClustersResponse](self.service.ListClusters),
            pb2.ListClustersRequest(
                options=pb2.ListClustersRequest.Opts(
                    sort_clause=pb2.ListClustersRequest.Opts.SortClause(
                        sort_order=None if sort_order is None else sort_order.as_pb2(),
                        created={} if sort_field == "created" else None,
                        name={} if sort_field == "name" else None,
                        running_jobs={} if sort_field == "running_jobs" else None,
                        total_nodes={} if sort_field == "total_nodes" else None,
                        total_gpus={} if sort_field == "total_gpus" else None,
                        free_gpus={} if sort_field == "free_gpus" else None,
                    ),
                    organization_id=self.resolve_org_id(org),
                    include_cluster_occupancy=include_cluster_occupancy,
                    page_size=self.MAX_PAGE_SIZE
                    if limit is None
                    else min(self.MAX_PAGE_SIZE, limit),
                )
            ),
        ):
            for cluster in response.clusters:
                count += 1
                yield cluster
                if limit is not None and count >= limit:
                    return

    def url(self, cluster: pb2.Cluster) -> str:
        """
        Get the URL to the cluster on the Beaker dashboard.
        """
        return f"{self.config.agent_address}/orgs/{self.beaker.org_name}/clusters/{cluster.name}"