This repository has been archived by the owner on Jun 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenges.py
131 lines (113 loc) · 4.11 KB
/
challenges.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
import json
import random
import time
from dataclasses import dataclass
from pathlib import Path
import docker
from docker.errors import BuildError
from docker.models.containers import Container
from redis import Redis
import settings
client = docker.from_env()
redis = Redis(host=settings.REDIS['ip'], port=settings.REDIS['port'], password=settings.REDIS['password'],
db=settings.REDIS['db'], charset='utf-8', decode_responses=True)
@dataclass
class Instance:
challenge: str
container: Container
port: int
started: int
users: list
user_limit: int
container_id: str
def __str__(self):
return json.dumps({
"challenge": self.challenge,
"port": self.port,
"started": self.started,
"users": self.users,
"user_limit": self.user_limit,
"container_id": self.container_id,
})
def to_json(self):
return {
"challenge": self.challenge,
"port": self.port,
"started": self.started,
"users": self.users,
"user_limit": self.user_limit,
"container_id": self.container_id,
}
@classmethod
def from_string(cls, string):
if string is not None:
data = json.loads(string)
return Instance(
challenge=data['challenge'],
container=client.containers.get(data['container_id']),
port=data['port'],
started=data['started'],
users=data['users'],
user_limit=data['user_limit'],
container_id=data['container_id']
)
return None
@classmethod
def get(cls, container_id):
return Instance.from_string(redis.get(container_id))
def save(self):
with redis.pipeline() as pipeline:
pipeline.set(self.container_id, str(self))
pipeline.sadd('ports', self.port)
pipeline.sadd(self.challenge, self.container_id)
pipeline.sadd('instance_set', self.container_id)
pipeline.execute()
def stop(self):
with redis.pipeline() as pipeline:
pipeline.set(self.container_id, None)
pipeline.srem('ports', self.port)
pipeline.srem(self.challenge, self.container_id)
pipeline.srem('instance_set', self.container_id)
pipeline.execute()
self.container.stop(timeout=5)
def start_instance(challenge, port=None):
print(f'Starting {challenge}...')
used_ports = redis.smembers('ports')
if port is None:
while True:
port = random.randrange(10000, 65535)
if port not in used_ports:
break
mem_limit = challenge_data[challenge]['mem_limit'] * (1024 ** 2)
try:
client.images.get(challenge)
except:
build_image(challenge)
container = client.containers.run(challenge, detach=True, ports={ports[challenge]: port},
mem_limit=mem_limit, memswap_limit=mem_limit)
instance = Instance(challenge=challenge, container=container, port=port, started=int(time.time()),
users=[],
user_limit=challenge_data[challenge]['user_limit'], container_id=container.id)
instance.save()
redis.incr('instances')
if redis.sismember('new_instace_queue', instance.challenge):
redis.srem('new_instance_queue', instance.challenge)
return instance
def build_image(challenge_name):
print(f'Building {challenge_name}...')
try:
client.images.build(path=f'challenges/{challenge_name}/', tag=challenge_name)
ports[challenge_name] = challenge_data[challenge_name]['port']
start_instance(challenge_name)
except BuildError as e:
print(f"Error building image for {challenge_name}: {str(e)}")
bad_challenges.append(challenge_name)
ports = {}
challenge_data = {}
bad_challenges = []
for file in Path('challenges').glob('*/challenge.json'):
with file.open() as file:
data = json.load(file)
name = data['name']
ports[name] = data['port']
challenge_data[name] = data