-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrun.py
executable file
·117 lines (88 loc) · 3.17 KB
/
run.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
#!/usr/bin/python3
import argparse
import docker
from docker.client import DockerClient
from docker.models.containers import Container
from docker.models.images import Image
from docker.types import Mount
import os
import os.path
from typing import Optional
CONTAINER = 'udrop-server'
IMAGE = 'debian:bookworm-slim'
def server_kill(client: DockerClient, args) -> None:
try:
container = client.containers.get(CONTAINER)
print('Killing container…')
container.kill()
except docker.errors.NotFound:
print('Container not running!')
def server_create(client: DockerClient) -> None:
try:
image = client.images.get(IMAGE)
except docker.errors.APIError:
image = client.images.pull(IMAGE)
print('Creating container…')
return client.containers.create(
image.id,
command='/bin/sh',
auto_remove=True,
environment=[
"RUST_BACKTRACE=full",
],
name=CONTAINER,
mounts=[
Mount(
'/src',
type='bind',
source=os.path.abspath(os.path.dirname(__file__)),
propagation='rslave',
),
],
tty=True,
working_dir='/src',
)
def server_run(client: DockerClient, args) -> None:
try:
container = client.containers.get(CONTAINER)
if container.status == 'running':
print('Container already running!')
return
except docker.errors.NotFound:
container = server_create(client)
print('Starting container…')
container.start()
cmd = ['target/debug/examples/udrop', '-l', '0.0.0.0', '--output', '/root']
res = container.exec_run(cmd, tty=True, stream=True)
try:
for message in res.output:
print(message.decode('utf-8'), end='')
except KeyboardInterrupt:
server_kill(client, args)
def server_get_ip(client: DockerClient) -> str:
container = client.containers.get(CONTAINER)
return container.attrs['NetworkSettings']['Networks']['bridge']['IPAddress']
def server_restart(client: DockerClient, args) -> None:
try:
container = client.containers.get(CONTAINER)
print('Restarting container…')
container.restart(timeout=3)
except docker.errors.NotFound:
print('Container not running!')
def server_print_ip(client: DockerClient, args) -> None:
try:
print(server_get_ip(client))
except docker.errors.NotFound:
print('Container not running!')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(title='subcommands')
server_parser = subparsers.add_parser('server')
server_subparsers = server_parser.add_subparsers(title='subcommands',
required=True)
server_subparsers.add_parser('run').set_defaults(func=server_run)
server_subparsers.add_parser('kill').set_defaults(func=server_kill)
server_subparsers.add_parser('ip').set_defaults(func=server_print_ip)
server_subparsers.add_parser('restart').set_defaults(func=server_restart)
args = parser.parse_args()
args.func(docker.from_env(), args)