-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconfig.py
More file actions
646 lines (487 loc) · 21.5 KB
/
config.py
File metadata and controls
646 lines (487 loc) · 21.5 KB
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
import os
import sys
import json
import base64
__all__ = [
"Config",
"BuildTimeVariableAccessException",
"NoCredentialFormatterFoundException",
"NotValidPlatformException"
]
class Config:
"""Reads Platform.sh configuration from environment variables.
See: https://docs.platform.sh/development/variables.html
The following are 'magic' properties that may exist on a Config object. Before accessing a property, check its
existence with hasattr(config, variableName). Attempting to access a nonexistent variable will throw an exception.
Attributes:
(The following properties are available at build time and run time.)
project (string):
The project ID.
applicationName (string):
The name of the application, as defined in its configuration.
treeID (string):
An ID identifying the application tree before it was built: a unique hash is generated based on the contents
of the application's files in the repository.
appDir (string):
The absolute path to the application.
projectEntropy (string):
A random string generated for each project, useful for generating hash keys.
(The following properties are only available at runtime.)
branch (string):
The Git branch name.
environment (string):
The environment ID (usually the Git branch plus a hash).
documentRoot (string):
The absolute path to the web root of the application.
smtpHost (string):
The hostname of the Platform.sh default SMTP server (an empty string if emails are disabled on the
environment.
port (string):
The TCP port number the application should listen to for incoming requests.
socket (string):
The Unix socket the application should listen to for incoming requests.
.. Platform.sh Environment Variables
https://docs.platform.sh/development/variables.html
"""
"""
Local index of the variables that can be accessed as direct properties (build and
runtime). The key is the property that will be read. The value is the environment variables, minus the variable prefix,
that contains the value to look up.
"""
_directVariables = {
"project": "PROJECT",
"appDir": "APP_DIR",
"applicationName": 'APPLICATION_NAME',
"treeID": "TREE_ID",
"projectEntropy": "PROJECT_ENTROPY"
}
"""
Local index of the variables that can be accessed as direct properties
(runtime only). The key is the property that will be read. The value is the environment variables, minus
prefix, that contains the value to look up.
"""
_directVariablesRuntime = {
"branch": "BRANCH",
"environment": "ENVIRONMENT",
"documentRoot": "DOCUMENT_ROOT",
"smtpHost": "SMTP_HOST"
}
"""
Local index of variables available at runtime that have no prefix.
"""
_unPrefixedVariablesRuntime = {
"port": "PORT",
"socket": "SOCKET"
}
"""
A local copy of all environment variables as of when the object was initialized.
"""
_environmentVariables = []
"""
The vendor prefix for all environment variables we care about.
"""
_varPrefix = ''
"""
The routes definition dict. Only available at runtime.
"""
_routesDef = {}
"""
The relationships definition dict. Only available at runtime.
"""
_relationshipsDef = {}
"""
The variables definition dict. Available in both build and runtime, although possibly with different
values.
"""
_variablesDef = {}
"""
The application definition dict. This is, approximately, the .platform.app.yaml file in nested dictionary form.
"""
_applicationDef = {}
"""
A map of the registered credential formatters. The key is the name, the value is a function.
"""
_credentialFormatters = {}
def __init__(self, environment_variables=None, var_prefix='PLATFORM_'):
"""Constructs a ConfigReader object.
Args:
environment_variables (dict):
The environment variables to read. Defaults to the current environment. Defaults to None.
var_prefix (string):
The prefix for environment variables. Defaults to 'PLATFORM_'.
"""
self._environmentVariables = os.environ if environment_variables is None else environment_variables
self._varPrefix = var_prefix
if self['ROUTES']:
routes = self['ROUTES']
self._routesDef = self.decode(routes)
if self['RELATIONSHIPS']:
relationships = self['RELATIONSHIPS']
self._relationshipsDef = self.decode(relationships)
self.register_formatter('pymongo', pymongo_formatter)
self.register_formatter('pysolr', pysolr_formatter)
self.register_formatter('postgresql_dsn', posgresql_dsn_formatter)
if self['VARIABLES']:
variables = self['VARIABLES']
self._variablesDef = self.decode(variables)
if self['APPLICATION']:
application = self['APPLICATION']
self._applicationDef = self.decode(application)
def is_valid_platform(self):
"""Checks whether the code is running on a platform with valid environment variables.
Returns:
bool:
True if configuration can be used, False otherwise.
"""
return 'APPLICATION_NAME' in self
def in_build(self):
"""Checks whether the code is running in a build environment.
Returns:
bool: True if running in build environment, False otherwise.
"""
return self.is_valid_platform() and not self['ENVIRONMENT']
def in_runtime(self):
"""Checks whether the code is running in a runtime environment.
Returns:
bool: True if in a runtime environment, False otherwise.
"""
return self.is_valid_platform() and self['ENVIRONMENT']
def credentials(self, relationship, index=0):
"""Retrieves the credentials for accessing a relationship.
Args:
relationship (string):
The relationship name as defined in .platform.app.yaml
index (int):
The index within the relationship to access. This is always 0, but reserved for future extension.
Returns:
The credentials dict for the service pointed to by the relationship.
Raises:
RuntimeError:
Thrown if called in a context that has no relationships (eg, in build).
KeyError:
Thrown if the relationship/index pair requested does not exist.
"""
if not self._relationshipsDef:
if self.in_build():
raise BuildTimeVariableAccessException(
'Relationships are not available during the build phase.'
)
raise NotValidPlatformException(
"""No relationships are defined. Are you sure you are on Platform.sh?
If you're running on your local system you may need to create a tunnel
to access your environment services. See https://docs.platform.sh/gettingstarted/local/tethered.html"""
)
if not self.has_relationship(relationship):
raise KeyError(
'No relationship defined: {}. Check your .platform.app.yaml file.'
.format(relationship))
if index >= len(self._relationshipsDef):
raise KeyError('No index {} defined for relationship: {}. '
'Check your .platform.app.yaml file.'.format(
index, relationship))
return self._relationshipsDef[relationship][index]
def variable(self, name, default=None):
"""Returns a variable from the VARIABLES dict.
Note:
Variables prefixed with `env`: can be accessed as normal environment variables. This method will return
such a variable by the name with the variable prefix still included. Generally it's better to access those variables
directly.
Args:
name (string):
The name of the variable to retrieve.
default (mixed):
The default value to return if the variable is not defined. Defaults to None.
Returns:
The value of the variable, or the specified default. This may be a string or a dict.
"""
if not self._variablesDef:
return default
return self._variablesDef.get(name, default)
def variables(self):
"""Returns the full variables dict.
If you're looking for a specific variable, the variable() method is a more robust option.
This method is for classes where you want to scan the whole variables list looking for a pattern.
It's valid for there to be no variables defined at all, so there's no guard for missing values.
Returns:
The full variables dict.
"""
return self._variablesDef
def routes(self):
"""Return the routes definition.
Returns:
The routes dict.
Raises:
RuntimeError:
If the routes are not accessible due to being in the wrong environment.
"""
if self.in_build():
raise BuildTimeVariableAccessException(
'Routes are not available during the build phase.'
)
if not self._routesDef:
raise NotValidPlatformException(
'No routes are defined. Are you sure you are running on Platform.sh?'
)
return self._routesDef
def get_primary_route(self):
"""Returns the primary route.
The primary route is the one marked primary in `routes.yaml`, or else
the first non-direct route in that file if none are marked.
Returns:
The route definition. The generated URL of the route is added as a "url" key.
"""
for (url, route) in self.routes().items():
if route["primary"]:
return route
raise KeyError("No primary route found. This isn't supposed to happen.")
def get_upstream_routes(self, app_name=None):
"""Returns just those routes that point to a valid upstream.
The method is similar to routes(), but filters out redirect routes that are rarely
useful for app configuration. If desired it can also filter to just those routes
whose upstream is a given application name. To retrieve routes that point to the
current application where the code is being run, use:
routes = config.get_upstream_routes(config._applicationName)
Args:
app_name (string|None):
The name of the upstream app on which to filter, if any.
Returns:
A dictionary of route definitions.
"""
if app_name:
# On Dedicated, the upstream name sometimes is `app:http` instead of just `app`.
# If no name is specified then don't bother checking.
return {
url: route
for url, route in self.routes().items()
if route["type"] == "upstream" and app_name == route["upstream"].split(":")[0]
}
else:
return {
url: route
for url, route in self.routes().items()
if route["type"] == "upstream"
}
def get_route(self, route_id):
"""Get route definition by route ID.
Args:
route_id (string):
The ID of the route to load.
Returns:
The route definition. The generated URL of the route is added as a 'url' key.
Raises:
KeyError:
If there is no route by that ID, an exception is thrown.
"""
if not self._routesDef:
raise NotValidPlatformException(
'No routes are defined. Are you sure you are running on Platform.sh?'
)
for (url, route) in self.routes().items():
if route['id'] == route_id:
route['url'] = url
return route
raise KeyError('No such route id found: {}'.format(route_id))
def application(self):
"""Returns the application definition dict.
This is, approximately, the .platform.app.yaml file as a nested dict. However, it also has other information
added by Platform.sh as part of the build and deploy process.
Returns:
The application definition dict.
"""
if not self._applicationDef:
raise NotValidPlatformException(
'No application definition is available. Are you sure you are running on Platform.sh?'
)
return self._applicationDef
def on_dedicated(self):
"""Determines if the current environment is a Platform.sh Dedicated environment.
Returns:
bool:
True on an Dedicated environment, False otherwise.
"""
return self.is_valid_platform() and self['MODE'] == 'enterprise'
def on_enterprise(self):
"""Determines if the current environment is a Platform.sh Dedicated environment.
@deprecated
The Platform.sh "Enterprise" will soon be referred to exclusively as "Dedicated". the `on_enterprise` method remains available for now, but it will be removed in a future version of this library.
It is recommended that you update your projects to use `on_dedicated` as soon as possible.
Returns:
bool:
True on a Dedicated environment, False otherwise.
"""
return self.on_dedicated()
def on_production(self):
"""Determines if the current environment is a production environment.
Note:
There may be a few edge cases where this is not entirely correct on Enterprise, if the production branch is
not named `production`. In that case you'll need to use your own logic.
Returns:
bool:
True if the environment is a production environment, False otherwise. It will also return False if not
running on Platform.sh or in the build phase.
"""
if not self.is_valid_platform() and not self.in_build():
return False
prod_branch = 'production' if self.on_dedicated() else 'master'
return self['BRANCH'] == prod_branch
def register_formatter(self, name, formatter):
"""Adds a credential formatter to the configuration.
A credential formatter is responsible for formatting the credentials for a relationship in a way expected
by a particular client library. For instance, it can take the credentials from Platform.sh for a MongoDB
database and format them into a URL string expected by pymongo. Use the formatted credentials() method to
get the formatted version of a particular relationship.
Args:
name (string):
The name of the formatter. This may be an arbitrary alphanumeric string.
formatter (callable):
A callback function that will format relationship credentials for a specific client library.
Returns:
Config. The called object, for chaining.
"""
self._credentialFormatters[name] = formatter
return self
def formatted_credentials(self, relationship, formatter):
"""Returns credentials for the specified relationship as formatted by the specified formatter.
Args:
relationship (string):
formatter (string):
Returns:
The credentials formatted with the given formatter.
Raises:
NoCredentialFormatterFoundException
"""
if formatter not in self._credentialFormatters:
raise NoCredentialFormatterFoundException(
'There is no credential formatter named {0} registered. Did you remember to call register_formatter()?'
.format(formatter)
)
return self._credentialFormatters[formatter](self.credentials(relationship))
def has_relationship(self, relationship):
"""Determines if a relationship is defined, and thus has credentials available.
Args:
relationship (string):
The name of the relationship to check.
Returns:
bool:
True if the relationship is defined, False otherwise.
"""
return relationship in self._relationshipsDef
def __getitem__(self, item):
"""Reads an environment variable, taking the variable prefix into account.
Args:
item (string):
The variable to read.
"""
check_name = self._varPrefix + item.upper()
return self._environmentVariables.get(check_name)
@staticmethod
def decode(variable):
"""Decodes a Platform.sh environment variable.
Args:
variable (string):
Base64-encoded JSON (the content of an environment variable).
Returns:
An dict (if representing a JSON object), or a scalar type.
Raises:
JSON decoding error.
"""
try:
if sys.version_info[1] > 5:
return json.loads(base64.b64decode(variable))
else:
return json.loads(base64.b64decode(variable).decode('utf-8'))
except json.decoder.JSONDecodeError:
print('Error decoding JSON, code %d', json.decoder.JSONDecodeError)
def __contains__(self, item):
"""Defines environment variable membership in Config.
Args:
item (string):
variable string to be judged.
Returns:
bool:
Returns True if Config contains the variable, False otherwise.
"""
if self[item]:
return True
return False
def __getattr__(self, config_property):
"""Gets a configuration property.
Args:
config_property (string):
A (magic) property name. The properties are documented in the DocBlock for this class.
Returns:
The return types are documented in the DocBlock for this class.
Raises:
RuntimeError:
If not running on Platform.sh, and the variable is not found.
AttributeError:
If a variable is not found, or if decoding fails.
"""
is_build_var = config_property in self._directVariables.keys()
is_runtime_var = config_property in self._directVariablesRuntime.keys()
# For now, all unprefixed variables are also runtime variables. If that ever changes this logic will change
# with it.
is_unprefixed_var = config_property in self._unPrefixedVariablesRuntime.keys()
if is_build_var:
value = self[self._directVariables[config_property]]
elif is_runtime_var:
value = self[self._directVariablesRuntime[config_property]]
elif is_unprefixed_var:
value = self._environmentVariables.get(self._unPrefixedVariablesRuntime[config_property])
else:
raise AttributeError('No such variable defined: {}'.format(config_property))
if not value:
if self.in_build() and (is_runtime_var or is_unprefixed_var):
raise BuildTimeVariableAccessException(
'The {} variable is not available during build time.'.format(config_property)
)
raise NotValidPlatformException(
'The {} variable is not defined. Are you sure you\'re running on Platform.sh?'.format(config_property)
)
return value
def pymongo_formatter(credentials):
"""Returns a DSN for a pymongo-MongoDB connection.
Note that the username and password will still be needed separately in the constructor.
Args:
credentials (dict):
The credentials dictionary from the relationships.
Returns:
(string) A formatted pymongo DSN.
"""
return '{0}:{1}/{2}'.format(
credentials['host'],
credentials['port'],
credentials['path']
)
def pysolr_formatter(credentials):
"""
Returns formatted Solr credentials for a pysolr-Solr connection.
Args:
credentials (dict):
The credentials dictionary from the relationships.
Returns:
(string) A formatted pysolr credential.
"""
return "http://{0}:{1}/{2}".format(credentials['ip'],
credentials['port'],
credentials['path'])
def posgresql_dsn_formatter(credentials):
"""
Returns formatted Posgresql credentials as DSN.
Args:
credentials (dict):
The credentials dictionary from the relationships.
Returns:
(string) A formatted postgresql DSN.
"""
return "postgresql://{0}:{1}@{2}:{3}/{4}".format(credentials["username"],
credentials["password"],
credentials["host"],
credentials["port"],
credentials["path"])
class BuildTimeVariableAccessException(RuntimeError):
pass
class NoCredentialFormatterFoundException(ValueError):
pass
class NotValidPlatformException(RuntimeError):
pass