mirror of
https://github.com/DerTyp7/docker_minecraft_server_auto_starter.git
synced 2025-10-29 04:42:07 +01:00
Fix DockerHandler and NginxHandler bugs
This commit is contained in:
@@ -18,14 +18,11 @@ def main():
|
||||
|
||||
# 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(
|
||||
port_map, docker_handler.get_ip_by_dns_name(docker_handler.get_current_container_name()), docker_handler)
|
||||
print(3)
|
||||
|
||||
nginx_handler.print_config()
|
||||
|
||||
|
||||
@@ -19,15 +19,13 @@ class DockerHandler:
|
||||
def get_current_container(self):
|
||||
hostname = self.get_current_container_name()
|
||||
if hostname:
|
||||
return self.get_container_by_name(hostname)
|
||||
return self.client.containers.get(hostname)
|
||||
return None
|
||||
|
||||
def get_current_container_name(self):
|
||||
# Get container name from environment variable
|
||||
return os.environ.get('HOSTNAME')
|
||||
|
||||
def get_current_network(self):
|
||||
# Get network of current container
|
||||
current_container = self.get_current_container()
|
||||
if current_container:
|
||||
networks = current_container.attrs['NetworkSettings']['Networks']
|
||||
@@ -43,7 +41,7 @@ class DockerHandler:
|
||||
|
||||
for container in containers:
|
||||
networks = container.attrs['NetworkSettings']['Networks']
|
||||
if self.current_network in networks and networks[self.current_network]['IPAMConfig']['IPv4Address'] == ip:
|
||||
if self.current_network in networks and networks[self.current_network]['IPAddress'] == ip:
|
||||
logging.info(
|
||||
f'Found container {container.name} with ip {ip} in network {self.current_network}')
|
||||
return container
|
||||
@@ -51,13 +49,6 @@ 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'
|
||||
@@ -87,12 +78,6 @@ class DockerHandler:
|
||||
return None
|
||||
|
||||
def get_ip_by_dns_name(self, 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]["IPAddress"]}, dns name: {networks[self.current_network]["Aliases"]}')
|
||||
|
||||
try:
|
||||
containers = self.client.containers.list(
|
||||
all=True, filters={"network": self.current_network})
|
||||
@@ -100,13 +85,13 @@ class DockerHandler:
|
||||
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']:
|
||||
ip = networks[self.current_network]['IPAddress']
|
||||
logging.info(
|
||||
f'Found container {container.name} with dns name {dns_name} in network {self.current_network}')
|
||||
return networks[self.current_network]['IPAddress']
|
||||
f'Found container {container.name} with dns name {dns_name} with ip {ip} in network {self.current_network}')
|
||||
return ip
|
||||
logging.info(
|
||||
f'No docker container found with dns name {dns_name} in network {self.current_network}')
|
||||
return None
|
||||
|
||||
@@ -25,7 +25,6 @@ class NginxHandler:
|
||||
logging.info(f.read())
|
||||
|
||||
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
|
||||
@@ -47,9 +46,7 @@ class NginxHandler:
|
||||
# Example for the nginx-example.conf file is in the repo root directory
|
||||
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(f' upstream upstream_{port} {{\n')
|
||||
nginx_conf.write(f' server {ip}:25565;\n')
|
||||
|
||||
@@ -29,7 +29,7 @@ class RequestHandler(threading.Thread):
|
||||
logging.info(f'Waiting for a connection on port {self.port}')
|
||||
self.connection, self.client_address = self.sock.accept()
|
||||
try:
|
||||
logging.info('Connection from', self.client_address)
|
||||
logging.info(f'Connection from {self.client_address}')
|
||||
self.handle_request()
|
||||
except Exception as e:
|
||||
logging.info(
|
||||
@@ -47,8 +47,11 @@ class RequestHandler(threading.Thread):
|
||||
|
||||
def handle_request(self):
|
||||
logging.info(f'Handling request on port {self.port}')
|
||||
print(docker_container_mapping().get(str(self.port)))
|
||||
container_ip = self.docker_handler.get_ip_by_dns_name(
|
||||
docker_container_mapping().get(str(self.port)))
|
||||
print(f"----Container IP: {container_ip}")
|
||||
|
||||
if container_ip:
|
||||
container = self.docker_handler.get_container_by_ip(
|
||||
container_ip)
|
||||
|
||||
22
app/utils.py
22
app/utils.py
@@ -1,19 +1,23 @@
|
||||
import logging
|
||||
import os
|
||||
import socket
|
||||
import socket
|
||||
|
||||
|
||||
def docker_container_mapping():
|
||||
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'):
|
||||
if line: # ignore empty lines
|
||||
port, ip = line.split(':')
|
||||
port_ip_map[port.strip()] = ip.strip()
|
||||
port_map_str = os.environ.get('PORT_MAP')
|
||||
|
||||
return port_ip_map
|
||||
port_map = {}
|
||||
for line in port_map_str.split('\n'):
|
||||
if line:
|
||||
port, name = line.split(':')
|
||||
port_map[port.strip()] = name.strip().replace(
|
||||
"'", "").replace('"', "").strip()
|
||||
|
||||
# print port map for debugging
|
||||
logging.info('PORT_MAP:')
|
||||
for port in port_map:
|
||||
logging.info(f'{port} -> {port_map[port]}')
|
||||
return port_map
|
||||
|
||||
|
||||
def get_ip_by_dns_name(dns_name):
|
||||
|
||||
@@ -54,6 +54,7 @@ services:
|
||||
restart: always
|
||||
image: itzg/minecraft-server:java8
|
||||
environment:
|
||||
VERSION: "1.12.2"
|
||||
EULA: "TRUE"
|
||||
MOTD: "Starting, please wait..."
|
||||
|
||||
|
||||
Reference in New Issue
Block a user