Refactor DockerHandler and NginxHandler classes

This commit is contained in:
Janis
2023-11-24 22:27:07 +01:00
parent 3649c6b49c
commit 07ed97a6c3
3 changed files with 31 additions and 36 deletions

View File

@@ -4,6 +4,8 @@ import os
class DockerHandler:
def __init__(self, base_url, port_ip_map):
print(
f'initializing docker handler with base url {base_url} and port ip map {port_ip_map}')
self.base_url = base_url
self.client = docker.DockerClient(base_url=base_url)
self.port_ip_map = port_ip_map
@@ -27,20 +29,17 @@ class DockerHandler:
return current_network
def get_container_by_ip(self, ip):
print('getting docker container by ip {} in network {}'.format(
ip, self.current_network))
containers = self.client.containers.list(all=True)
for container in containers:
networks = container.attrs['NetworkSettings']['Networks']
if self.current_network in networks and networks[self.current_network]['IPAMConfig']['IPv4Address'] == ip:
print('found docker container {} with ip {} in network {}'.format(
container.name, ip, self.current_network))
print(
f'Found container {container.name} with ip {ip} in network {self.current_network}')
return container
print('no docker container found with ip {} in network {}'.format(
ip, self.current_network))
print(
f'No docker container found with ip {ip} in network {self.current_network}')
return None
def is_container_starting(self, container):
print(container.attrs['State']['Health']['Status'])
return container.attrs['State']['Health']['Status'] == 'starting'

View File

@@ -1,5 +1,4 @@
import os
import time
class NginxHandler:
@@ -34,27 +33,25 @@ class NginxHandler:
nginx_conf.write('events { }\n')
nginx_conf.write('stream {\n')
# This looks confusing, but the nginx.conf looks good when it's done
# Example for the nginx-example.conf file is in the repo root directory
for port in port_ip_map:
nginx_conf.write(' upstream upstream_{} {{\n'.format(port))
nginx_conf.write(' server {}:25565;\n'.format(
port_ip_map[port]))
nginx_conf.write(
' server 127.0.0.1:{} backup;\n'.format(port))
nginx_conf.write(f' upstream upstream_{port} {{\n')
nginx_conf.write(f' server {port_ip_map[port]}:25565;\n')
nginx_conf.write(f' server 127.0.0.1:{port} backup;\n')
nginx_conf.write(' }\n')
nginx_conf.write(' server {\n')
nginx_conf.write(' listen {}:{};\n'.format(
current_container_ip, port))
nginx_conf.write(
f' listen {current_container_ip}:{port};\n')
nginx_conf.write(' proxy_connect_timeout {};\n'.format(
proxy_timeout))
nginx_conf.write(' proxy_timeout {};\n'.format(
proxy_timeout))
nginx_conf.write(
f' proxy_connect_timeout {proxy_timeout};\n')
nginx_conf.write(f' proxy_timeout {proxy_timeout};\n')
nginx_conf.write(' proxy_pass upstream_{};\n'.format(port))
nginx_conf.write(f' proxy_pass upstream_{port};\n')
nginx_conf.write(' }\n')
nginx_conf.write('}\n')
nginx_conf.close()
print('NGINX config file setup')
print('NGINX config file setup complete')
self.start()

View File

@@ -9,27 +9,24 @@ class RequestHandler(threading.Thread):
super().__init__()
self.port = port
self.docker_handler = docker_handler
# Create a TCP/IP socket
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
server_address = ('localhost', self.port)
print('starting up on {} port {}'.format(*server_address))
print(f'Starting up on {server_address[0]} port {server_address[1]}')
self.sock.bind(server_address)
# Listen for incoming connections
self.sock.listen(1)
def run(self):
while True:
print('waiting for a connection on port {}'.format(self.port))
print(f'Waiting for a connection on port {self.port}')
self.connection, self.client_address = self.sock.accept()
try:
print('connection from', self.client_address)
print('Connection from', self.client_address)
self.handle_request()
finally:
self.connection.close()
def handle_request(self):
print('handling request on port {}'.format(self.port))
print(f'Handling request on port {self.port}')
container_ip = docker_container_mapping().get(str(self.port))
if container_ip:
container = self.docker_handler.get_container_by_ip(
@@ -38,29 +35,31 @@ class RequestHandler(threading.Thread):
request = self.connection.recv(1024)
if request[0] == 0x10:
if b'\x01' in request:
print('ping')
print(f'Detected ping request for {container_ip}')
self.forward_request_to_server(request, isStarting)
elif b'\x02' in request:
print('join')
print(f'Detected join/login request for {container_ip}')
if isStarting:
print('container is starting, waiting for 5 seconds')
print(
f'Container {container_ip} is already starting...')
self.forward_request_to_server(request, isStarting)
else:
print('starting docker container {}'.format(container_ip))
print(f'Starting container {container_ip}')
container.start()
elif request[0] == 0xFE:
print('legacy server list ping')
print(f'Detected legacy ping request for {container_ip}')
self.forward_request_to_server(request, isStarting)
else:
print('no docker container mapped to this port')
print(f'No container mapped to port {self.port}')
def forward_request_to_server(self, request, isStarting=False):
print('Forwarding request to placeholder server')
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
ip = os.environ.get('PLACEHOLDER_SERVER_SLEEPING_IP')
if isStarting:
print('----------------container is starting, waiting for 5 seconds')
print('Container is starting. Using starting placeholder IP')
ip = os.environ.get('PLACEHOLDER_SERVER_STARTING_IP')
server_socket.connect(