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(project=project, prefix=prefix, |
58 streamserver_uri=env.get(cls._ENV_STREAM_SERVER_PATH)) | 59 streamserver_uri=env.get(cls._ENV_STREAM_SERVER_PATH), |
60 coordinator_host=env.get(cls._ENV_COORDINATOR_HOST)) | |
59 | 61 |
60 def stream_client(self): | 62 def stream_client(self, reg=None): |
61 """Returns: (StreamClient) stream client for the bootstrap streamserver URI. | 63 """Returns: (StreamClient) stream client for the bootstrap streamserver URI. |
62 | 64 |
63 If the Butler accepts external stream connections, it will export a | 65 If the Butler accepts external stream connections, it will export a |
64 streamserver URI in the environment. This will create a StreamClient | 66 streamserver URI in the environment. This will create a StreamClient |
65 instance to operate on the streamserver if one is defined. | 67 instance to operate on the streamserver if one is defined. |
66 | 68 |
69 Args: | |
70 reg (stream.StreamProtocolRegistry or None): The stream protocol registry | |
71 to use to create the stream. If None, the default global registry will | |
72 be used (recommended). | |
73 | |
67 Raises: | 74 Raises: |
68 ValueError: If no streamserver URI is present in the environment. | 75 ValueError: If no streamserver URI is present in the environment. |
69 """ | 76 """ |
70 if not self.streamserver_uri: | 77 if not self.streamserver_uri: |
71 raise ValueError('No streamserver in bootstrap environment.') | 78 raise ValueError('No streamserver in bootstrap environment.') |
72 return stream.create(self.streamserver_uri) | 79 reg = reg or stream._default_registry |
jbudorick
2016/10/27 13:24:06
nit: can this just be `reg or stream` given your c
dnj
2016/10/27 22:42:44
It could, but I'd like the types to be consistent
jbudorick
2016/10/27 23:42:50
Fair enough.
| |
80 return reg.create( | |
81 self.streamserver_uri, | |
82 prefix=self.prefix, | |
83 coordinator_host=self.coordinator_host) | |
OLD | NEW |