forked from SchedMD/slurm-gcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
252 lines (215 loc) · 7.65 KB
/
util.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
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
#!/usr/bin/env python3
# Copyright 2019 SchedMD LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import logging.config
import os
import shlex
import subprocess
import sys
import time
from pathlib import Path
from contextlib import contextmanager
from collections import OrderedDict
import requests
import yaml
log = logging.getLogger(__name__)
def config_root_logger(level='DEBUG', util_level=None, file=None):
if not util_level:
util_level = level
handler = 'file_handler' if file else 'stdout_handler'
config = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'standard': {
'format': '',
},
'stamp': {
'format': '%(asctime)s %(name)s %(levelname)s: %(message)s',
},
},
'handlers': {
'stdout_handler': {
'level': 'DEBUG',
'formatter': 'standard',
'class': 'logging.StreamHandler',
'stream': sys.stdout,
},
},
'loggers': {
'': {
'handlers': [handler],
'level': level,
},
__name__: { # enable util.py logging
'level': util_level,
}
},
}
if file:
config['handlers']['file_handler'] = {
'level': 'DEBUG',
'formatter': 'stamp',
'class': 'logging.handlers.WatchedFileHandler',
'filename': file,
}
logging.config.dictConfig(config)
def get_metadata(path):
""" Get metadata relative to metadata/computeMetadata/v1/instance/ """
URL = 'http://metadata.google.internal/computeMetadata/v1/instance/'
HEADERS = {'Metadata-Flavor': 'Google'}
full_path = URL + path
try:
resp = requests.get(full_path, headers=HEADERS)
resp.raise_for_status()
except requests.exceptions.RequestException:
log.exception(f"Error while getting metadata from {full_path}")
return None
return resp.text
def run(cmd, wait=0, quiet=False, get_stdout=False,
shell=False, universal_newlines=True, **kwargs):
""" run in subprocess. Optional wait after return. """
if not quiet:
log.debug(f"run: {cmd}")
if get_stdout:
kwargs['stdout'] = subprocess.PIPE
args = cmd if shell else shlex.split(cmd)
ret = subprocess.run(args, shell=shell,
universal_newlines=universal_newlines,
**kwargs)
if wait:
time.sleep(wait)
return ret
def spawn(cmd, quiet=False, shell=False, **kwargs):
""" nonblocking spawn of subprocess """
if not quiet:
log.debug(f"spawn: {cmd}")
args = cmd if shell else shlex.split(cmd)
return subprocess.Popen(args, shell=shell, **kwargs)
def get_pid(node_name):
"""Convert <prefix>-<pid>-<nid>"""
return int(node_name.split('-')[-2])
@contextmanager
def cd(path):
""" Change working directory for context """
prev = Path.cwd()
os.chdir(path)
try:
yield
finally:
os.chdir(prev)
def static_vars(**kwargs):
"""
Add variables to the function namespace.
@static_vars(var=init): var must be referenced func.var
"""
def decorate(func):
for k in kwargs:
setattr(func, k, kwargs[k])
return func
return decorate
class cached_property:
"""
Descriptor for creating a property that is computed once and cached
"""
def __init__(self, factory):
self._attr_name = factory.__name__
self._factory = factory
def __get__(self, instance, owner=None):
if instance is None: # only if invoked from class
return self
attr = self._factory(instance)
setattr(instance, self._attr_name, attr)
return attr
class Config(OrderedDict):
""" Loads config from yaml and holds values in nested namespaces """
TYPES = set(('compute', 'login', 'controller'))
# PROPERTIES defines which properties in slurm.jinja.schema are included
# in the config file. SAVED_PROPS are saved to file via save_config.
SAVED_PROPS = ('project',
'zone',
'cluster_name',
'external_compute_ips',
'shared_vpc_host_project',
'compute_node_prefix',
'compute_node_service_account',
'compute_node_scopes',
'slurm_cmd_path',
'log_dir',
'google_app_cred_path',
'update_node_addrs',
'partitions',
)
PROPERTIES = (*SAVED_PROPS,
'munge_key',
'external_compute_ips',
'nfs_home_server',
'nfs_home_dir',
'nfs_apps_server',
'nfs_apps_dir',
'ompi_version',
'controller_secondary_disk',
'slurm_version',
'suspend_time',
'network_storage',
'login_network_storage',
'login_node_count',
'cloudsql',
)
def __init__(self, *args, **kwargs):
def from_nested(value):
""" If value is dict, convert to Config. Also recurse lists. """
if isinstance(value, dict):
return Config({k: from_nested(v) for k, v in value.items()})
elif isinstance(value, list):
return [from_nested(v) for v in value]
else:
return value
super(Config, self).__init__(*args, **kwargs)
self.__dict__ = self # all properties are member attributes
# Convert nested dicts to Configs
for k, v in self.items():
self[k] = from_nested(v)
@classmethod
def new_config(cls, properties):
# If k is ever not found, None will be inserted as the value
return cls({k: properties.setdefault(k, None) for k in cls.PROPERTIES})
@classmethod
def load_config(cls, path):
config = yaml.safe_load(Path(path).read_text())
return cls(config)
def save_config(self, path):
save_dict = Config([(k, self[k]) for k in self.SAVED_PROPS])
Path(path).write_text(yaml.dump(save_dict, Dumper=self.Dumper))
@cached_property
def instance_type(self):
# get tags, intersect with possible types, get the first or none
tags = yaml.safe_load(get_metadata('tags'))
# TODO what to default to if no match found.
return next(iter(set(tags) & self.TYPES), None)
@property
def region(self):
return self.zone and '-'.join(self.zone.split('-')[:-1])
def __getattr__(self, item):
""" only called if item is not found in self """
return None
class Dumper(yaml.SafeDumper):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.add_representer(Config, self.represent_config)
@staticmethod
def represent_config(dumper, data):
return dumper.represent_mapping('tag:yaml.org,2002:map',
data.items())