-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathhosts.py
78 lines (59 loc) · 2.3 KB
/
hosts.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
#!/usr/bin/env python3
import os
from pathlib import Path
from wxflow import YAMLFile
__all__ = ['Host']
class Host:
"""
Gather Host specific information.
"""
SUPPORTED_HOSTS = ['HERA', 'ORION', 'JET',
'WCOSS2', 'S4', 'CONTAINER', 'AWSPW']
def __init__(self, host=None):
detected_host = self.detect()
if host is not None and host != detected_host:
raise ValueError(
f'detected host: "{detected_host}" does not match host: "{host}"')
self.machine = detected_host
self.info = self._get_info
self.scheduler = self.info['SCHEDULER']
@classmethod
def detect(cls):
machine = 'NOTFOUND'
container = os.getenv('SINGULARITY_NAME', None)
pw_csp = os.getenv('PW_CSP', None)
if os.path.exists('/scratch1/NCEPDEV'):
machine = 'HERA'
elif os.path.exists('/work/noaa'):
machine = 'ORION'
elif os.path.exists('/lfs4/HFIP'):
machine = 'JET'
elif os.path.exists('/lfs/f1'):
machine = 'WCOSS2'
elif os.path.exists('/data/prod'):
machine = 'S4'
elif container is not None:
machine = 'CONTAINER'
elif pw_csp is not None:
if pw_csp.lower() not in ['azure', 'aws', 'gcp']:
raise ValueError(
f'NOAA cloud service provider "{pw_csp}" is not supported.')
machine = f"{pw_csp.upper()}PW"
if machine not in Host.SUPPORTED_HOSTS:
raise NotImplementedError(f'This machine is not a supported host.\n' +
'Currently supported hosts are:\n' +
f'{" | ".join(Host.SUPPORTED_HOSTS)}')
return machine
@property
def _get_info(self) -> dict:
hostfile = Path(os.path.join(os.path.dirname(__file__),
f'hosts/{self.machine.lower()}.yaml'))
try:
info = YAMLFile(path=hostfile)
except FileNotFoundError:
raise FileNotFoundError(f'{hostfile} does not exist!')
except IOError:
raise IOError(f'Unable to read from {hostfile}')
except Exception:
raise Exception(f'unable to get information for {self.machine}')
return info