mirror of
https://github.com/DerTyp7/docker_minecraft_server_auto_starter.git
synced 2025-10-29 20:52:09 +01:00
Update Docker configuration and Nginx setup
This commit is contained in:
30
app/app.py
30
app/app.py
@@ -1,4 +1,5 @@
|
||||
import logging
|
||||
|
||||
from requestHandler import RequestHandler
|
||||
from utils import docker_container_mapping
|
||||
from dockerHandler import DockerHandler
|
||||
@@ -9,24 +10,45 @@ logging.basicConfig(level=logging.INFO)
|
||||
|
||||
def main():
|
||||
try:
|
||||
port_ip_map = docker_container_mapping()
|
||||
port_map = docker_container_mapping()
|
||||
|
||||
# Create a DockerHandler instance
|
||||
docker_handler = DockerHandler(
|
||||
'unix://var/run/docker.sock', port_ip_map)
|
||||
'unix://var/run/docker.sock', port_map)
|
||||
|
||||
# Create an NginxHandler instance
|
||||
nginx_handler = NginxHandler('/etc/nginx/nginx.conf')
|
||||
print(1)
|
||||
docker_handler.get_ip_by_dns_name(
|
||||
docker_handler.get_current_container_name())
|
||||
print(2)
|
||||
|
||||
nginx_handler.setup_config_file(
|
||||
docker_container_mapping(), docker_handler.get_current_container_ip())
|
||||
port_map, docker_handler.get_ip_by_dns_name(docker_handler.get_current_container_name()), docker_handler)
|
||||
print(3)
|
||||
|
||||
nginx_handler.print_config()
|
||||
|
||||
# Create a RequestHandler instance for each port
|
||||
for port in port_ip_map.keys():
|
||||
for port in port_map.keys():
|
||||
logging.info(f'Creating request handler for port {port}')
|
||||
request_handler = RequestHandler(int(port), docker_handler)
|
||||
request_handler.start()
|
||||
|
||||
# DEBUG
|
||||
logging.info(
|
||||
'-----------------------------DEBUG--------------------------------')
|
||||
logging.info(
|
||||
f'Current container: {docker_handler.get_current_container_name()}')
|
||||
logging.info(
|
||||
f'Current container ip: {docker_handler.get_ip_by_dns_name(docker_handler.get_current_container_name())}')
|
||||
logging.info(
|
||||
f'Current network: {docker_handler.get_current_network()}')
|
||||
|
||||
docker_handler.print_all_container_names()
|
||||
logging.info(
|
||||
'-----------------------------DEBUG END--------------------------------')
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f'An error occurred: {e}')
|
||||
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
from math import log
|
||||
import socket
|
||||
import docker
|
||||
import os
|
||||
import logging
|
||||
|
||||
|
||||
class DockerHandler:
|
||||
def __init__(self, base_url, port_ip_map):
|
||||
def __init__(self, base_url, port_map):
|
||||
logging.info(
|
||||
f'Initializing docker handler with base url {base_url} and port ip map: {port_ip_map}')
|
||||
f'Initializing docker handler with base url {base_url} and port ip map: {port_map}')
|
||||
self.base_url = base_url
|
||||
self.client = docker.DockerClient(base_url=base_url)
|
||||
self.port_ip_map = port_ip_map
|
||||
self.port_map = port_map
|
||||
self.current_network = self.get_current_network()
|
||||
logging.info('Docker handler initialized')
|
||||
logging.info(f'Current network: {self.current_network}')
|
||||
|
||||
def get_current_container(self):
|
||||
current_container_name = os.environ.get('HOSTNAME')
|
||||
@@ -20,13 +24,15 @@ class DockerHandler:
|
||||
logging.error(f'Container {current_container_name} not found')
|
||||
return None
|
||||
|
||||
def get_current_container_ip(self):
|
||||
# Get IP of current container
|
||||
def get_current_container_name(self):
|
||||
# Get DNS name of current container
|
||||
current_container = self.get_current_container()
|
||||
if current_container:
|
||||
networks = current_container.attrs['NetworkSettings']['Networks']
|
||||
current_network = list(networks.keys())[0]
|
||||
return networks[current_network]['IPAddress']
|
||||
if 'Aliases' in networks[current_network]:
|
||||
dns_name = networks[current_network]['Aliases'][0]
|
||||
return dns_name
|
||||
return None
|
||||
|
||||
def get_current_network(self):
|
||||
@@ -54,7 +60,67 @@ class DockerHandler:
|
||||
f'No docker container found with ip {ip} in network {self.current_network}')
|
||||
return None
|
||||
|
||||
def get_container_by_name(self, name):
|
||||
try:
|
||||
return self.client.containers.get(name)
|
||||
except docker.errors.NotFound:
|
||||
logging.error(f'Container {name} not found')
|
||||
return None
|
||||
|
||||
def is_container_starting(self, container):
|
||||
if container:
|
||||
return container.attrs['State']['Health']['Status'] == 'starting'
|
||||
return False
|
||||
|
||||
def print_all_container_names(self):
|
||||
try:
|
||||
containers = self.client.containers.list(
|
||||
all=True, filters={"network": self.current_network})
|
||||
|
||||
if containers is None:
|
||||
logging.info('No containers found')
|
||||
return None
|
||||
|
||||
for container in containers:
|
||||
logging.info(f'Container name: {container.name}')
|
||||
# get docker compose dns name
|
||||
networks = container.attrs['NetworkSettings']['Networks']
|
||||
if self.current_network in networks:
|
||||
logging.info(
|
||||
f'Container ip: {networks[self.current_network]["IPAMConfig"]["IPv4Address"]}')
|
||||
else:
|
||||
logging.info(f'Container ip: None')
|
||||
|
||||
except docker.errors.APIError as e:
|
||||
logging.error(f'Error getting container list: {e}')
|
||||
return None
|
||||
|
||||
def get_ip_by_dns_name(self, dns_name):
|
||||
# dEBUG Print all containers with their network ip and dns name
|
||||
for container in self.client.containers.list(all=True):
|
||||
networks = container.attrs['NetworkSettings']['Networks']
|
||||
if self.current_network in networks:
|
||||
logging.info(
|
||||
f'Container {container.name} ip: {networks[self.current_network]["IPAMConfig"]["IPv4Address"]}, dns name: {networks[self.current_network]["Aliases"]}')
|
||||
|
||||
try:
|
||||
containers = self.client.containers.list(
|
||||
all=True, filters={"network": self.current_network})
|
||||
|
||||
if containers is None:
|
||||
logging.info('No containers found')
|
||||
return None
|
||||
|
||||
for container in containers:
|
||||
networks = container.attrs['NetworkSettings']['Networks']
|
||||
if self.current_network in networks and dns_name in networks[self.current_network]['Aliases']:
|
||||
logging.info(
|
||||
f'Found container {container.name} with dns name {dns_name} in network {self.current_network}')
|
||||
return networks[self.current_network]['IPAMConfig']['IPv4Address']
|
||||
logging.info(
|
||||
f'No docker container found with dns name {dns_name} in network {self.current_network}')
|
||||
return None
|
||||
|
||||
except docker.errors.APIError as e:
|
||||
logging.error(f'Error getting container list: {e}')
|
||||
return None
|
||||
|
||||
@@ -24,11 +24,16 @@ class NginxHandler:
|
||||
with open(self.config_path, 'r') as f:
|
||||
logging.info(f.read())
|
||||
|
||||
def setup_config_file(self, port_ip_map, current_container_ip):
|
||||
def setup_config_file(self, port_map, current_container_ip, docker_handler):
|
||||
print(f'-------------------> {port_map}')
|
||||
if port_map is None:
|
||||
logging.error('port_map is None')
|
||||
return
|
||||
|
||||
proxy_timeout = "5s"
|
||||
self.stop()
|
||||
logging.info('Setting up NGINX config file...')
|
||||
logging.info('port_ip_map: {}'.format(port_ip_map))
|
||||
logging.info('port_map: {}'.format(port_map))
|
||||
nginx_conf = open(self.config_path, 'w+')
|
||||
nginx_conf.truncate()
|
||||
nginx_conf.write('worker_processes 5;\n')
|
||||
@@ -40,22 +45,30 @@ class NginxHandler:
|
||||
|
||||
# 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(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')
|
||||
if isinstance(port_map, dict):
|
||||
for port in port_map:
|
||||
print(f'-------------------> {port_map}')
|
||||
ip = docker_handler.get_ip_by_dns_name(port_map[port])
|
||||
print(f'-------------------> {port_map[port]}, {ip}')
|
||||
|
||||
nginx_conf.write(' server {\n')
|
||||
nginx_conf.write(
|
||||
f' listen {current_container_ip}:{port};\n')
|
||||
nginx_conf.write(f' upstream upstream_{port} {{\n')
|
||||
nginx_conf.write(f' server {ip}:25565;\n')
|
||||
nginx_conf.write(f' server 127.0.0.1:{port} backup;\n')
|
||||
nginx_conf.write(' }\n')
|
||||
|
||||
nginx_conf.write(
|
||||
f' proxy_connect_timeout {proxy_timeout};\n')
|
||||
nginx_conf.write(f' proxy_timeout {proxy_timeout};\n')
|
||||
nginx_conf.write(' server {\n')
|
||||
nginx_conf.write(
|
||||
f' listen {current_container_ip}:{port};\n')
|
||||
|
||||
nginx_conf.write(
|
||||
f' proxy_connect_timeout {proxy_timeout};\n')
|
||||
nginx_conf.write(f' proxy_timeout {proxy_timeout};\n')
|
||||
|
||||
nginx_conf.write(f' proxy_pass upstream_{port};\n')
|
||||
nginx_conf.write(' }\n')
|
||||
else:
|
||||
logging.error('port_map is not a dictionary')
|
||||
|
||||
nginx_conf.write(f' proxy_pass upstream_{port};\n')
|
||||
nginx_conf.write(' }\n')
|
||||
nginx_conf.write('}\n')
|
||||
nginx_conf.close()
|
||||
logging.info('NGINX config file setup complete')
|
||||
|
||||
@@ -47,7 +47,8 @@ class RequestHandler(threading.Thread):
|
||||
|
||||
def handle_request(self):
|
||||
logging.info(f'Handling request on port {self.port}')
|
||||
container_ip = docker_container_mapping().get(str(self.port))
|
||||
container_ip = self.docker_handler.get_ip_by_dns_name(
|
||||
docker_container_mapping().get(str(self.port)))
|
||||
if container_ip:
|
||||
container = self.docker_handler.get_container_by_ip(
|
||||
container_ip)
|
||||
@@ -85,11 +86,13 @@ class RequestHandler(threading.Thread):
|
||||
def forward_request_to_placeholder(self, request, isStarting=False):
|
||||
logging.info('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')
|
||||
ip = self.docker_handler.get_ip_by_dns_name(
|
||||
os.environ.get('PLACEHOLDER_SERVER_SLEEPING_SERVICE'))
|
||||
if isStarting:
|
||||
logging.info(
|
||||
'Container is starting. Using starting placeholder IP')
|
||||
ip = os.environ.get('PLACEHOLDER_SERVER_STARTING_IP')
|
||||
ip = self.docker_handler.get_ip_by_dns_name(
|
||||
os.environ.get('PLACEHOLDER_SERVER_STARTING_SERVICE'))
|
||||
|
||||
if not ip:
|
||||
logging.info('No placeholder server IP found')
|
||||
|
||||
13
app/utils.py
13
app/utils.py
@@ -1,8 +1,11 @@
|
||||
import logging
|
||||
import os
|
||||
import socket
|
||||
import socket
|
||||
|
||||
|
||||
def docker_container_mapping():
|
||||
port_ip_map_str = os.environ.get('PORT_IP_MAP')
|
||||
port_ip_map_str = os.environ.get('PORT_MAP')
|
||||
# Convert the environment variable to a Python dictionary
|
||||
port_ip_map = {}
|
||||
for line in port_ip_map_str.split('\n'):
|
||||
@@ -11,3 +14,11 @@ def docker_container_mapping():
|
||||
port_ip_map[port.strip()] = ip.strip()
|
||||
|
||||
return port_ip_map
|
||||
|
||||
|
||||
def get_ip_by_dns_name(dns_name):
|
||||
try:
|
||||
return socket.gethostbyname(dns_name, resolver='127.0.0.1')
|
||||
except socket.gaierror:
|
||||
logging.error(f'Could not resolve dns name {dns_name}')
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user