-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathconfiguration.py
More file actions
97 lines (87 loc) · 3.15 KB
/
configuration.py
File metadata and controls
97 lines (87 loc) · 3.15 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
# -*- coding: utf-8 -*-
import os
from typing import Dict
from logzero import logger
from chaoslib.exceptions import InvalidExperiment, ChaosException
from chaoslib.types import Configuration
from chaoslib.activity import run_activity
__all__ = ["load_configuration"]
def load_configuration(config_info: Dict[str, str]) -> Configuration:
"""
Load the configuration. The `config_info` parameter is a mapping from
key strings to value as strings or dictionaries. In the cert case, the
value is used as-is. In the other cases, if the dictionary has a key named
`type` with `env` value, it will take the `key` value from the env
variables. If `type` is `probe`, it will take the value from a probe
In the probe is of type `process`, the value will be taken from `stdout`
In the probe is of type `python`, the value will be taken from the
result as is
In the probe is of type `http`, the value will be taken from `body`
Here is a sample of what it looks like:
```
{
"cert": "/some/path/file.crt",
"token": {
"type": "env",
"key": "MY_TOKEN"
},
"date": {
"type": "probe",
"name": "Current date",
"provider": {
"type": "process",
"path": "date"
}
},
"words": {
"type": "probe",
"name": "Some capped words",
"provider": {
"type": "python",
"module": "string",
"func": "capwords",
"arguments": {
"s": "some words"
}
}
},
"valueFromServer": {
"type": "probe",
"name": "Some value taken from the network",
"provider": {
"type": "http",
"url": "http://my.config.server.com/value"
}
}
}
```
The `cert` configuration key is set to its string value whereas the `token`
configuration key is dynamically fetched from the `MY_TOKEN` environment
variable.
"""
logger.debug("Loading configuration...")
env = os.environ
conf = {}
for (key, value) in config_info.items():
if isinstance(value, dict) and "type" in value:
if value["type"] == "env":
env_key = value["key"]
if env_key not in env:
raise InvalidExperiment(
"Configuration makes reference to an environment key"
" that does not exist: {}".format(env_key))
conf[key] = env.get(env_key)
elif value["type"] == "probe":
result = run_activity(value, config_info, {})
if value["provider"]["type"] == "process":
conf[key] = result.get("stdout")
elif value["provider"]["type"] == "python":
conf[key] = result
elif value["provider"]["type"] == "body":
conf[key] = result
else:
raise ChaosException(
"Different provider than process not supported yet")
else:
conf[key] = value
return conf