-
Notifications
You must be signed in to change notification settings - Fork 5
/
combined_agent.py
80 lines (72 loc) · 2.7 KB
/
combined_agent.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
import subprocess
import waiting
from swarmexecutor import SwarmExecutor
from assisted_swarm_client.assisted_swarm import SwarmApi
DEFAULT_AGENT_IMAGE = "quay.io/oamizur/assisted-swarm:latest"
DEFAULT_STDOUT_FILE = "/tmp/combined-agent.stdout"
DEFAULT_STDERR_FILE = "/tmp/combined-agent.stderr"
class CombinedAgent:
def __init__(self, executor: SwarmExecutor, swarm_client: SwarmApi):
self.executor = executor
self.swarm_client = swarm_client
def spawn(self, agent_image=DEFAULT_AGENT_IMAGE, stdout_fname=DEFAULT_STDOUT_FILE,
stderr_fname=DEFAULT_STDERR_FILE):
command = ["podman",
"run",
"--rm",
"--net=host",
"--privileged",
"-v",
"/var/log:/var/log",
"-v",
"/run/udev:/run/udev",
"-v",
"/dev/disk:/dev/disk",
"-v",
"/run/systemd/journal/socket:/run/systemd/journal/socket",
"-v",
"/var/log:/host/var/log:ro",
"-v",
"/proc/meminfo:/host/proc/meminfo:ro",
"-v",
"/sys/kernel/mm/hugepages:/host/sys/kernel/mm/hugepages:ro",
"-v",
"/proc/cpuinfo:/host/proc/cpuinfo:ro",
"-v",
"/etc/mtab:/host/etc/mtab:ro",
"-v",
"/sys/block:/host/sys/block:ro",
"-v",
"/sys/devices:/host/sys/devices:ro",
"-v",
"/sys/bus:/host/sys/bus:ro",
"-v",
"/sys/class:/host/sys/class:ro",
"-v",
"/run/udev:/host/run/udev:ro",
"-v",
"/dev/disk:/host/dev/disk:ro",
"-v",
"/opt:/opt:rw",
agent_image]
with open(stdout_fname, "ab") as stdout_file:
with open(stderr_fname, "ab") as stderr_file:
self.executor.Popen(
command,
stdin=subprocess.DEVNULL,
stdout=stdout_file,
stderr=stderr_file
)
self.wait_for_health()
def wait_for_health(self):
def get_health_status():
try:
self.swarm_client.health()
return True
except Exception:
return False
waiting.wait(get_health_status,
sleep_seconds=1,
timeout_seconds=300)
def stop(self):
self.swarm_client.exit()