File size: 10,723 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
from __future__ import annotations

import logging
import os
import threading
import time
from contextlib import contextmanager
from functools import cached_property
from typing import ClassVar, Generator, TypeVar

import grpc
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

from . import beaker_pb2_grpc
from ._cluster import ClusterClient
from ._dataset import DatasetClient
from ._experiment import ExperimentClient
from ._group import GroupClient
from ._image import ImageClient
from ._job import JobClient
from ._node import NodeClient
from ._organization import OrganizationClient
from ._queue import QueueClient
from ._secret import SecretClient
from ._user import UserClient
from ._workload import WorkloadClient
from ._workspace import WorkspaceClient
from .config import Config, InternalConfig
from .exceptions import *
from .version import VERSION

__all__ = ["Beaker"]


_LATEST_VERSION_CHECKED = False
T = TypeVar("T")


class Beaker:
    """
    A client for interacting with `Beaker <https://beaker.org>`_. This should be used as a context
    manager to ensure connections are properly closed on exit.

    .. tip::
        Use :meth:`from_env()` to create a client instance.

    :param config: The Beaker :class:`Config`.
    :param check_for_upgrades: Automatically check that beaker-py is up-to-date. You'll see
        a warning if it isn't.
    :param user_agent: Override the "User-Agent" header used in requests to the Beaker server.
    """

    API_VERSION: ClassVar[str] = "v3"
    CLIENT_VERSION: ClassVar[str] = VERSION
    VERSION_CHECK_INTERVAL: ClassVar[int] = 12 * 3600  # 12 hours

    RPC_MAX_SEND_MESSAGE_LENGTH: ClassVar[int] = 64 * 1024 * 1024  # 64MiB

    RECOVERABLE_SERVER_ERROR_CODES: ClassVar[tuple[int, ...]] = (429, 500, 502, 503, 504)
    MAX_RETRIES: ClassVar[int] = 5
    BACKOFF_FACTOR: ClassVar[int] = 1
    BACKOFF_MAX: ClassVar[int] = 120
    TIMEOUT: ClassVar[float] = 5.0
    POOL_MAXSIZE: ClassVar[int] = min(100, (os.cpu_count() or 16) * 6)

    logger = logging.getLogger("beaker")

    def __init__(
        self,
        config: Config,
        check_for_upgrades: bool = True,
        user_agent: str = f"beaker-py v{VERSION}",
    ):
        self.user_agent = user_agent
        self._config = config
        self._channel: grpc.Channel | None = None
        self._service: beaker_pb2_grpc.BeakerStub | None = None
        self._thread_local = threading.local()
        self._thread_local.http_session = None  # requests.Session not thread safe

        # See if there's a newer version, and if so, suggest that the user upgrades.
        if check_for_upgrades:
            self._check_for_upgrades()

    def _get_latest_version(self) -> str:
        response = requests.get(
            "https://pypi.org/simple/beaker-py",
            headers={"Accept": "application/vnd.pypi.simple.v1+json"},
            timeout=2,
        )
        response.raise_for_status()
        return response.json()["versions"][-1]

    def _check_for_upgrades(self, force: bool = False) -> Exception | bool | None:
        global _LATEST_VERSION_CHECKED

        if not force and _LATEST_VERSION_CHECKED:
            return None

        import warnings

        import packaging.version

        try:
            config = InternalConfig.load()
            if (
                not force
                and config is not None
                and config.version_checked is not None
                and (time.time() - config.version_checked <= self.VERSION_CHECK_INTERVAL)
            ):
                return None

            should_upgrade: bool | None = None
            latest_version = packaging.version.parse(self._get_latest_version())
            current_version = packaging.version.parse(self.CLIENT_VERSION)
            if latest_version > current_version and (
                not latest_version.is_prerelease or current_version.is_prerelease
            ):
                warnings.warn(
                    f"You're using beaker-py v{current_version}, "
                    f"but a newer version (v{latest_version}) is available.\n\n"
                    f"Please upgrade with `pip install --upgrade beaker-py`.",
                    UserWarning,
                )
                should_upgrade = True
            else:
                should_upgrade = False

            _LATEST_VERSION_CHECKED = True
            if config is not None:
                config.version_checked = time.time()
                config.save()

            return should_upgrade
        except Exception as e:
            return e

    @classmethod
    def from_env(
        cls,
        check_for_upgrades: bool = True,
        user_agent: str = f"beaker-py v{VERSION}",
        **overrides,
    ) -> Beaker:
        """
        Initialize client from a config file and/or environment variables.

        :examples:

        >>> with Beaker.from_env(default_workspace="ai2/my-workspace") as beaker:
        ...     print(beaker.user_name)

        :param check_for_upgrades: Automatically check that beaker-py is up-to-date. You'll see
            a warning if it isn't.
        :param user_agent: Override the "User-Agent" header used in requests to the Beaker server.
        :param overrides: Fields in the :class:`Config` to override.

        .. note::
            This will use the same config file that the Beaker command-line client
            creates and uses, which is usually located at ``$HOME/.beaker/config.yml``.

            If you haven't configured the command-line client, then you can alternately just
            set the environment variable ``BEAKER_TOKEN`` to your Beaker `user token <https://beaker.org/user>`_.

        """
        return cls(
            Config.from_env(**overrides),
            check_for_upgrades=check_for_upgrades,
            user_agent=user_agent,
        )

    @property
    def service(self) -> beaker_pb2_grpc.BeakerStub:
        if self._service is None:
            self._channel = grpc.secure_channel(
                self.config.rpc_address,
                grpc.ssl_channel_credentials(),
                options=[
                    ("grpc.max_send_message_length", self.RPC_MAX_SEND_MESSAGE_LENGTH),
                    #  ("grpc.keepalive_time_ms", 10_000),
                ],
            )
            self._service = beaker_pb2_grpc.BeakerStub(self._channel)
        return self._service

    @property
    def config(self) -> Config:
        """
        The client's :class:`Config`.
        """
        return self._config

    @cached_property
    def user_name(self) -> str:
        return self.user.get().name

    @cached_property
    def org_name(self) -> str:
        return self.organization.get().name

    @cached_property
    def organization(self) -> OrganizationClient:
        """
        Manage organizations.
        """
        return OrganizationClient(self)

    @cached_property
    def user(self) -> UserClient:
        """
        Manage users.
        """
        return UserClient(self)

    @cached_property
    def workspace(self) -> WorkspaceClient:
        """
        Manage workspaces.
        """
        return WorkspaceClient(self)

    @cached_property
    def cluster(self) -> ClusterClient:
        """
        Manage clusters.
        """
        return ClusterClient(self)

    @cached_property
    def node(self) -> NodeClient:
        """
        Manage nodes.
        """
        return NodeClient(self)

    @cached_property
    def dataset(self) -> DatasetClient:
        """
        Manage datasets.
        """
        return DatasetClient(self)

    @cached_property
    def image(self) -> ImageClient:
        """
        Manage images.
        """
        return ImageClient(self)

    @cached_property
    def job(self) -> JobClient:
        """
        Manage jobs.
        """
        return JobClient(self)

    @cached_property
    def experiment(self) -> ExperimentClient:
        """
        Manage experiments.
        """
        return ExperimentClient(self)

    @cached_property
    def workload(self) -> WorkloadClient:
        """
        Manage workloads.
        """
        return WorkloadClient(self)

    @cached_property
    def secret(self) -> SecretClient:
        """
        Manage secrets.
        """
        return SecretClient(self)

    @cached_property
    def group(self) -> GroupClient:
        """
        Manage groups.
        """
        return GroupClient(self)

    @cached_property
    def queue(self) -> QueueClient:
        """
        Manage queues.
        """
        return QueueClient(self)

    @contextmanager
    def http_session(self) -> Generator[requests.Session, None, None]:
        if (
            not hasattr(self._thread_local, "http_session")
            or self._thread_local.http_session is None
        ):
            self._thread_local.http_session = self._init_http_session()
            try:
                yield self._thread_local.http_session
            finally:
                self._thread_local.http_session.close()
                self._thread_local.http_session = None
        else:
            yield self._thread_local.http_session

    def _init_http_session(self):
        session = requests.Session()
        retries = Retry(
            total=self.MAX_RETRIES * 2,
            connect=self.MAX_RETRIES,
            status=self.MAX_RETRIES,
            backoff_factor=self.BACKOFF_FACTOR,
            status_forcelist=self.RECOVERABLE_SERVER_ERROR_CODES,
        )
        session.mount("https://", HTTPAdapter(max_retries=retries, pool_maxsize=self.POOL_MAXSIZE))
        return session

    def __enter__(self) -> "Beaker":
        if (
            not hasattr(self._thread_local, "http_session")
            or self._thread_local.http_session is None
        ):
            self._thread_local.http_session = self._init_http_session()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        del exc_type, exc_val, exc_tb
        self.close()
        return False

    def close(self):
        """
        Close down RPC channels and HTTP sessions. This will be called automatically when using
        the client as a context manager.
        """
        # Close RPC channel.
        if self._channel is not None:
            self._channel.close()
        self._channel = None
        self._service = None

        # Close HTTP session.
        if (
            hasattr(self._thread_local, "http_session")
            and self._thread_local.http_session is not None
        ):
            self._thread_local.http_session.close()
        self._thread_local.http_session = None

    def __del__(self):
        self.close()