-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment.py
85 lines (63 loc) · 2.44 KB
/
environment.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
import subprocess
import os
import logging
logger = logging.getLogger("environment")
logging.basicConfig(level=logging.INFO, format='%(message)s')
class TestEnvironment:
def __init__(self, engine, target):
self.engine = engine
self.target = target
self.cfg = engine.generate_launch_config(target)
self.target_dir = self.engine.target_run_dir
def __enter__(self):
logger.debug("Create environment...")
self.wipe()
self.setup()
return self
def __exit__(self, *args):
logger.debug("Destroy environment...")
self.wipe()
def wipe(self):
proc = subprocess.run( [ "rm", "-rf", self.target_dir, ] )
if proc.returncode != 0:
raise RuntimeError("Clearout failed")
def create_directory(self):
os.mkdir(self.target_dir)
self.chcon(self.target_dir)
def chcon(self, path):
proc = subprocess.run([
# -R for recursive?
"chcon", "-t", "svirt_sandbox_file_t", path
])
if proc.returncode != 0:
raise RuntimeError("Clearout failed")
def generate_deploy_package(self):
path = f"{self.target_dir}/launch.yaml"
with open(path, "w") as f:
f.write(self.cfg)
self.chcon(path)
logger.debug(f"Generated launch.yaml.")
def configure_extras(self):
os.mkdir(self.target_dir + "/prometheus/")
self.chcon(self.target_dir + "/prometheus/")
os.mkdir(self.target_dir + "/grafana/")
self.chcon(self.target_dir + "/grafana/")
os.mkdir(self.target_dir + "/grafana/dashboards/")
self.chcon(self.target_dir + "/grafana/dashboards/")
os.mkdir(self.target_dir + "/grafana/provisioning/")
self.chcon(self.target_dir + "/grafana/provisioning/")
for file in [
"prometheus/prometheus.yml",
"grafana/dashboards/dashboard.json",
"grafana/provisioning/dashboard.yml",
"grafana/provisioning/datasource.yml",
]:
with open(f"{self.engine.trustgraph_dir}/{file}", "r") as src:
with open(f"{self.target_dir}/{file}", "w") as dest:
dest.write(src.read())
logger.debug(f"Wrote {file}.")
self.chcon(self.target_dir)
def setup(self):
self.create_directory()
self.generate_deploy_package()
self.configure_extras()