| OLD | NEW |
| (Empty) | |
| 1 # Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 # Use of this source code is governed by the Apache v2.0 license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import collections |
| 6 import os |
| 7 |
| 8 from libs.logdog import stream, streamname |
| 9 |
| 10 |
| 11 class NotBootstrappedError(RuntimeError): |
| 12 """Raised when the current environment is missing Butler bootstrap variables. |
| 13 """ |
| 14 |
| 15 |
| 16 _ButlerBootstrapBase = collections.namedtuple('_ButlerBootstrapBase', |
| 17 ('project', 'prefix', 'streamserver_uri')) |
| 18 |
| 19 |
| 20 class ButlerBootstrap(_ButlerBootstrapBase): |
| 21 """Loads LogDog Butler bootstrap parameters from the environment. |
| 22 |
| 23 LogDog Butler adds variables describing the LogDog stream parameters to the |
| 24 environment when it bootstraps an application. This class probes the |
| 25 environment and identifies those parameters. |
| 26 """ |
| 27 |
| 28 _ENV_PROJECT = 'LOGDOG_STREAM_PROJECT' |
| 29 _ENV_PREFIX = 'LOGDOG_STREAM_PREFIX' |
| 30 _ENV_STREAM_SERVER_PATH = 'LOGDOG_STREAM_SERVER_PATH' |
| 31 |
| 32 @classmethod |
| 33 def probe(cls, env=None): |
| 34 """Returns (ButlerBootstrap): The probed bootstrap environment. |
| 35 |
| 36 Args: |
| 37 env (dict): The environment to probe. If None, `os.getenv` will be used. |
| 38 |
| 39 Raises: |
| 40 NotBootstrappedError if the current environment is not boostrapped. |
| 41 """ |
| 42 if env is None: |
| 43 env = os.environ |
| 44 project = env.get(cls._ENV_PROJECT) |
| 45 prefix = env.get(cls._ENV_PREFIX) |
| 46 |
| 47 if not project: |
| 48 raise NotBootstrappedError('Missing project [%s]' % (cls._ENV_PROJECT,)) |
| 49 |
| 50 if not prefix: |
| 51 raise NotBootstrappedError('Missing prefix [%s]' % (cls._ENV_PREFIX,)) |
| 52 try: |
| 53 streamname.validate_stream_name(prefix) |
| 54 except ValueError as e: |
| 55 raise NotBootstrappedError('Prefix (%s) is invalid: %s' % (prefix, e)) |
| 56 |
| 57 return cls(project=project, prefix=prefix, |
| 58 streamserver_uri=env.get(cls._ENV_STREAM_SERVER_PATH)) |
| 59 |
| 60 def stream_client(self): |
| 61 """Returns: (StreamClient) stream client for the bootstrap streamserver URI. |
| 62 |
| 63 If the Butler accepts external stream connections, it will export a |
| 64 streamserver URI in the environment. This will create a StreamClient |
| 65 instance to operate on the streamserver if one is defined. |
| 66 |
| 67 Raises: |
| 68 ValueError: If no streamserver URI is present in the environment. |
| 69 """ |
| 70 if not self.streamserver_uri: |
| 71 raise ValueError('No streamserver in bootstrap environment.') |
| 72 return stream.StreamClient.create(self.streamserver_uri) |
| OLD | NEW |