-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
213 lines (182 loc) · 6.44 KB
/
main.py
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
"""
Zelt: Orchestrate Locust deployments in Kubernetes.
Usage:
zelt from-har <har-files>... -m <manifests>
[-w <pods>]
[--storage <method>]
[--s3-bucket <name> --s3-key <name>]
[-p <plugin-name>]...
[--clean]
[--logging <level>]
zelt from-har <har-files>... --local
[-p <plugin-name>]...
[--logging <level>]
zelt from-har --config <file>
[--local]
[--clean]
[--logging <level>]
zelt from-locustfile <locustfile> -m <manifests>
[-w <pods>]
[--storage <method>]
[--s3-bucket <name> --s3-key <name>]
[--clean]
[--logging <level>]
zelt from-locustfile <locustfile> --local
[--logging <level>]
zelt from-locustfile --config <file>
[--local]
[--clean]
[--logging <level>]
zelt rescale <required-pods> -m <manifests>
[--logging <level>]
zelt rescale <required-pods> --config <file>
[--logging <level>]
zelt delete -m <manifests> [--storage <method>]
[--s3-bucket <name> --s3-key <name>]
[--logging <level>]
zelt delete --config <file>
[--logging <level>]
zelt --help
zelt --version
Options:
-h, --help Show this screen.
-v, --version Show version.
-p, --transformer-plugins=<plugin-name> Module name of Transformer plugin (repeatable).
-m, --manifests=<manifests> Path to manifest files.
-w, --worker-pods=<pods> Number of worker pods to deploy [default: 1].
-s, --storage=<method> Remote locustfile storage method (S3 or ConfigMap)
[default: ConfigMap].
--s3-bucket=<name> Name of S3 bucket for remote locustfile storage.
--s3-key=<name> Name of S3 key for remote locustfile storage.
-c, --clean Delete and redeploy remote resources.
-l, --local Run Locust locally.
--logging=<level> Set logging level (INFO, DEBUG, or ERROR) [default: INFO].
--config=<file> Optional configuration file specifying options.
"""
import logging
import os
import pkg_resources
import sys
import yaml
from docopt import docopt
from pathlib import Path
from typing import NamedTuple, Sequence
import zelt
from zelt.zelt import StorageMethod
class Config(NamedTuple):
from_har: bool
from_locustfile: bool
rescale: bool
delete: bool
har_files: Sequence[os.PathLike]
locustfile: os.PathLike
transformer_plugins: Sequence[str]
manifests: os.PathLike
worker_pods: int
required_pods: int
storage: str
s3_bucket: str
s3_key: str
clean: bool
local: bool
logging: str
def cli():
"""
Entrypoint for Zelt.
"""
# Disable deprecation warning coming from Kubernetes client's YAML loading.
# See https://github.com/yaml/pyyaml/wiki/PyYAML-yaml.load(input)-Deprecation
yaml.warnings({"YAMLLoadWarning": False})
config = _load_config(docopt(__doc__, version=_version()))
logging.basicConfig(level=config.logging)
if config.from_har:
config = config._replace(
locustfile=zelt.invoke_transformer(
paths=config.har_files, plugin_names=config.transformer_plugins
)
)
_deploy(config)
if config.from_locustfile:
_deploy(config)
if config.rescale:
_rescale(config)
if config.delete:
_delete(config)
def _version() -> str:
return pkg_resources.get_distribution("zelt").version
def _deploy(config: Config) -> None:
"""
Deploys Locust.
"""
try:
zelt.deploy(
config.locustfile,
int(config.worker_pods),
config.manifests,
config.clean,
StorageMethod.from_storage_arg(config.storage),
config.local,
config.s3_bucket,
config.s3_key,
)
except Exception as e:
logging.fatal("Error: %s", e)
exit(1)
def _rescale(config: Config) -> None:
"""
Rescales a worker deployment.
"""
try:
zelt.rescale(config.manifests, int(config.required_pods))
except Exception as e:
logging.fatal("Error: %s", e)
exit(1)
def _delete(config: Config) -> None:
"""
Deletes a deployment.
"""
try:
zelt.delete(
config.manifests,
StorageMethod.from_storage_arg(config.storage),
config.s3_bucket,
config.s3_key,
)
except Exception as e:
logging.fatal("Error: %s", e)
exit(1)
def _load_config(config: dict) -> Config:
"""
Loads config from command-line or file.
"""
config = _normalise_config(config)
if config["config"]:
config = {**config, **yaml.safe_load(Path(config["config"]).read_text())}
return Config(
from_har=config["from-har"],
from_locustfile=config["from-locustfile"],
rescale=config["rescale"],
delete=config["delete"],
har_files=config.get("har-files", []),
locustfile=config["locustfile"],
transformer_plugins=config.get("transformer-plugins", []),
manifests=config["manifests"],
worker_pods=config["worker-pods"],
required_pods=config["required-pods"],
storage=config["storage"],
s3_bucket=config["s3-bucket"],
s3_key=config["s3-key"],
clean=config["clean"],
local=config["local"],
logging=config["logging"],
)
def _normalise_config(config: dict) -> dict:
"""
Removes special characters from config keys.
"""
normalised_config = {}
for k in config:
normalised_config[
k.replace("--", "").replace("<", "").replace(">", "")
] = config[k]
return normalised_config