-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy path__init__.py
More file actions
188 lines (172 loc) · 5.83 KB
/
__init__.py
File metadata and controls
188 lines (172 loc) · 5.83 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
import argparse
import logging.config
import tomlkit
import tomlkit.items
import tomlkit.exceptions
from pathlib import Path
from typing import List, Optional, Union
VERBOSE = 15
logging.addLevelName(15, "VERBOSE")
logging.config.fileConfig(Path(__file__).parent / "logging.conf")
logger = logging.getLogger("ceph-devstack")
PROJECT_ROOT = Path(__file__).parent
DEFAULT_CONFIG_PATH = Path("~/.config/ceph-devstack/config.toml")
def parse_args(args: List[str]) -> argparse.Namespace:
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
"--dry-run",
action="store_true",
default=False,
help="Instead of running commands, print them",
)
parser.add_argument(
"-v",
"--verbose",
action="store_true",
default=False,
help="Be more verbose",
)
parser.add_argument(
"-c",
"--config-file",
type=Path,
default=DEFAULT_CONFIG_PATH,
help="Path to the ceph-devstack config file",
)
subparsers = parser.add_subparsers(dest="command")
parser_config = subparsers.add_parser("config", help="Get or set config items")
subparsers_config = parser_config.add_subparsers(dest="config_op")
subparsers_config.add_parser("dump", help="show the configuration")
parser_config_get = subparsers_config.add_parser("get")
parser_config_get.add_argument("name")
parser_config_set = subparsers_config.add_parser("set")
parser_config_set.add_argument("name")
parser_config_set.add_argument("value")
parser_doc = subparsers.add_parser(
"doctor", help="Check that the system meets requirements"
)
parser_doc.add_argument(
"--fix",
action="store_true",
default=False,
help="Apply suggested fixes for issues found",
)
parser_pull = subparsers.add_parser("pull", help="Pull container images")
parser_pull.add_argument(
"image",
nargs="*",
help="Specific image(s) to pull",
)
parser_build = subparsers.add_parser("build", help="Build container images")
parser_build.add_argument(
"image",
nargs="*",
help="Specific image(s) to build",
)
parser_create = subparsers.add_parser(
"create",
help="Create the cluster",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser_create.add_argument(
"-b",
"--build",
action="store_true",
default=False,
help="Build images before creating",
)
parser_create.add_argument(
"-w",
"--wait",
action="store_true",
default=False,
help="Leave the cluster running - and don't auto-schedule anything",
)
subparsers.add_parser("remove", help="Destroy the cluster")
subparsers.add_parser("start", help="Start the cluster")
subparsers.add_parser("stop", help="Stop the cluster")
subparsers.add_parser(
"watch", help="Monitor the cluster, recreating containers as necessary"
)
parser_wait = subparsers.add_parser(
"wait",
help="Wait for the specified container to exit. Exit with its exit code.",
)
parser_wait.add_argument(
"container",
help="The container to wait for",
)
parser_logs=subparsers.add_parser(
"logs", help="Display teuthology.log logs of the latest run"
)
parser_logs.add_argument(
"--log-file",
action="store_true",
default=False,
help="Display teuthology.log file path of the latest run with flag set"
)
return parser.parse_args(args)
def deep_merge(*maps):
result = {}
for map in maps:
for k, v in map.items():
if isinstance(v, dict):
v = deep_merge(result.get(k, {}), v)
result[k] = v
return result
class Config(dict):
__slots__ = ["user_obj", "user_path"]
def load(self, config_path: Optional[Path] = None):
parsed = tomlkit.parse((Path(__file__).parent / "config.toml").read_text())
self.update(parsed)
if config_path:
self.user_path = config_path.expanduser()
if self.user_path.exists():
self.user_obj: dict = tomlkit.parse(self.user_path.read_text()) or {}
self.update(deep_merge(config, self.user_obj))
elif self.user_path != DEFAULT_CONFIG_PATH.expanduser():
raise OSError(f"Config file at {self.user_path} not found!")
else:
self.user_obj = {}
def dump(self):
return tomlkit.dumps(self)
def get_value(self, name: str) -> str:
path = name.split(".")
obj = config
i = 0
while i < len(path):
sub_path = path[i]
try:
obj = obj[sub_path]
except KeyError:
logger.error(f"{name} not found in config")
raise
i += 1
if isinstance(obj, (str, int, bool)):
return str(obj)
return tomlkit.dumps(obj).strip()
def set_value(self, name: str, value: str):
path = name.split(".")
obj = self.user_obj
i = 0
last_index = len(path) - 1
item: Union[tomlkit.items.Item, str] = value
try:
item = tomlkit.value(item)
except tomlkit.exceptions.UnexpectedCharError:
pass
except tomlkit.exceptions.InternalParserError:
pass
while i <= last_index:
if i < last_index:
obj = obj.setdefault(path[i], {})
elif i == last_index:
obj[path[i]] = item
self.update(self.user_obj)
self.user_path.parent.mkdir(exist_ok=True)
self.user_path.write_text(tomlkit.dumps(self.user_obj).strip())
i += 1
config = Config()
config.load()