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