mirror of
https://github.com/DerTyp7/docker_minecraft_server_auto_starter.git
synced 2025-10-29 12:42:09 +01:00
Add placeholder server sleeping IP environment
variable
This commit is contained in:
@@ -3,6 +3,7 @@ FROM nginx:1.25.3
|
|||||||
|
|
||||||
ENV PORT_IP_MAP ""
|
ENV PORT_IP_MAP ""
|
||||||
ENV PYTHONUNBUFFERED=1
|
ENV PYTHONUNBUFFERED=1
|
||||||
|
ENV PLACEHOLDER_SERVER_SLEEPING_IP ""
|
||||||
|
|
||||||
# Install Python and pip
|
# Install Python and pip
|
||||||
RUN apt-get update && apt-get install -y python3 python3-pip python3-venv
|
RUN apt-get update && apt-get install -y python3 python3-pip python3-venv
|
||||||
|
|||||||
58
app/app.py
58
app/app.py
@@ -1,68 +1,20 @@
|
|||||||
import socket
|
from requestHandler import RequestHandler
|
||||||
import threading
|
from utils import docker_container_mapping
|
||||||
import os
|
|
||||||
from dockerHandler import DockerHandler
|
from dockerHandler import DockerHandler
|
||||||
from nginxHandler import NginxHandler
|
from nginxHandler import NginxHandler
|
||||||
|
|
||||||
|
|
||||||
def docker_container_mapping():
|
|
||||||
port_ip_map_str = os.environ.get('PORT_IP_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()
|
|
||||||
|
|
||||||
return port_ip_map
|
|
||||||
|
|
||||||
|
|
||||||
class RequestHandler(threading.Thread):
|
|
||||||
def __init__(self, port, docker_handler):
|
|
||||||
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))
|
|
||||||
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))
|
|
||||||
self.connection, self.client_address = self.sock.accept()
|
|
||||||
try:
|
|
||||||
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))
|
|
||||||
# Get the Docker container name for this port
|
|
||||||
container_ip = docker_container_mapping().get(str(self.port))
|
|
||||||
if container_ip:
|
|
||||||
# Start the Docker container
|
|
||||||
print('starting docker container {}'.format(container_ip))
|
|
||||||
self.docker_handler.get_container_by_ip(container_ip).start()
|
|
||||||
# Send a response
|
|
||||||
self.connection.sendall("Server starting...".encode('utf-8'))
|
|
||||||
else:
|
|
||||||
print('no docker container mapped to this port')
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
# Create a DockerHandler instance
|
||||||
docker_handler = DockerHandler(
|
docker_handler = DockerHandler(
|
||||||
'unix://var/run/docker.sock', docker_container_mapping())
|
'unix://var/run/docker.sock', docker_container_mapping())
|
||||||
|
|
||||||
|
# Create a RequestHandler instance for each port
|
||||||
for port in range(25560, 25571):
|
for port in range(25560, 25571):
|
||||||
request_handler = RequestHandler(port, docker_handler)
|
request_handler = RequestHandler(port, docker_handler)
|
||||||
request_handler.start()
|
request_handler.start()
|
||||||
|
|
||||||
|
# Create an NginxHandler instance
|
||||||
nginx_handler = NginxHandler('/etc/nginx/nginx.conf')
|
nginx_handler = NginxHandler('/etc/nginx/nginx.conf')
|
||||||
nginx_handler.setup_config_file(
|
nginx_handler.setup_config_file(
|
||||||
docker_container_mapping(), docker_handler.get_current_container_ip())
|
docker_container_mapping(), docker_handler.get_current_container_ip())
|
||||||
|
|||||||
@@ -32,13 +32,6 @@ class DockerHandler:
|
|||||||
containers = self.client.containers.list(all=True)
|
containers = self.client.containers.list(all=True)
|
||||||
|
|
||||||
for container in containers:
|
for container in containers:
|
||||||
print('current network: {}'.format(self.current_network))
|
|
||||||
print('checking container {}'.format(container.name))
|
|
||||||
print('container networks: {}'.format(
|
|
||||||
container.attrs['NetworkSettings']['Networks']))
|
|
||||||
print('container ip: {}'.format(
|
|
||||||
container.attrs['NetworkSettings']['Networks'][self.current_network]['IPAMConfig']['IPv4Address']))
|
|
||||||
|
|
||||||
networks = container.attrs['NetworkSettings']['Networks']
|
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]['IPAMConfig']['IPv4Address'] == ip:
|
||||||
print('found docker container {} with ip {} in network {}'.format(
|
print('found docker container {} with ip {} in network {}'.format(
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import os
|
import os
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
class NginxHandler:
|
class NginxHandler:
|
||||||
@@ -24,7 +25,8 @@ class NginxHandler:
|
|||||||
print(f.read())
|
print(f.read())
|
||||||
|
|
||||||
def setup_config_file(self, port_ip_map, current_container_ip):
|
def setup_config_file(self, port_ip_map, current_container_ip):
|
||||||
proxy_timeout = "300ms"
|
placeholder_ip = os.environ.get('PLACEHOLDER_SERVER_SLEEPING_IP')
|
||||||
|
proxy_timeout = "5s"
|
||||||
self.stop()
|
self.stop()
|
||||||
print('Setting up NGINX config file...')
|
print('Setting up NGINX config file...')
|
||||||
print('port_ip_map: {}'.format(port_ip_map))
|
print('port_ip_map: {}'.format(port_ip_map))
|
||||||
@@ -39,6 +41,10 @@ class NginxHandler:
|
|||||||
port_ip_map[port]))
|
port_ip_map[port]))
|
||||||
nginx_conf.write(
|
nginx_conf.write(
|
||||||
' server 127.0.0.1:{} backup;\n'.format(port))
|
' server 127.0.0.1:{} backup;\n'.format(port))
|
||||||
|
|
||||||
|
nginx_conf.write(
|
||||||
|
' server {}:25565 backup;\n'.format(placeholder_ip))
|
||||||
|
|
||||||
nginx_conf.write(' }\n')
|
nginx_conf.write(' }\n')
|
||||||
|
|
||||||
nginx_conf.write(' server {\n')
|
nginx_conf.write(' server {\n')
|
||||||
|
|||||||
58
app/requestHandler.py
Normal file
58
app/requestHandler.py
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
import json
|
||||||
|
import socket
|
||||||
|
import threading
|
||||||
|
from utils import docker_container_mapping
|
||||||
|
|
||||||
|
|
||||||
|
class RequestHandler(threading.Thread):
|
||||||
|
def __init__(self, port, docker_handler):
|
||||||
|
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))
|
||||||
|
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))
|
||||||
|
self.connection, self.client_address = self.sock.accept()
|
||||||
|
try:
|
||||||
|
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))
|
||||||
|
container_ip = docker_container_mapping().get(str(self.port))
|
||||||
|
if container_ip:
|
||||||
|
request = self.connection.recv(1024)
|
||||||
|
print('---------------------------')
|
||||||
|
print(request)
|
||||||
|
print('---------------------------')
|
||||||
|
|
||||||
|
if request[0] == 0x10:
|
||||||
|
if b'\x01' in request:
|
||||||
|
print('ping')
|
||||||
|
self.redirect_to_placeholder()
|
||||||
|
elif b'\x02' in request:
|
||||||
|
print('join')
|
||||||
|
print('starting docker container {}'.format(container_ip))
|
||||||
|
self.docker_handler.get_container_by_ip(
|
||||||
|
container_ip).start()
|
||||||
|
|
||||||
|
elif request[0] == 0xFE:
|
||||||
|
print('legacy server list ping')
|
||||||
|
self.redirect_to_placeholder()
|
||||||
|
|
||||||
|
else:
|
||||||
|
print('no docker container mapped to this port')
|
||||||
|
|
||||||
|
def redirect_to_placeholder(self):
|
||||||
|
print('redirecting to placeholder')
|
||||||
13
app/utils.py
Normal file
13
app/utils.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
def docker_container_mapping():
|
||||||
|
port_ip_map_str = os.environ.get('PORT_IP_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()
|
||||||
|
|
||||||
|
return port_ip_map
|
||||||
@@ -10,32 +10,51 @@ networks:
|
|||||||
|
|
||||||
services:
|
services:
|
||||||
auto_starter:
|
auto_starter:
|
||||||
|
container_name: mc_auto_starter
|
||||||
build: .
|
build: .
|
||||||
ports:
|
ports:
|
||||||
- 25565:25565
|
- 25565:25565
|
||||||
- 25566:25566
|
- 25566:25566
|
||||||
environment:
|
environment:
|
||||||
|
PLACEHOLDER_SERVER_SLEEPING_IP: "172.20.0.3"
|
||||||
PORT_IP_MAP: |
|
PORT_IP_MAP: |
|
||||||
25565: 172.20.0.3
|
25565: 172.20.0.5
|
||||||
25566: 172.20.0.4
|
25566: 172.20.0.6
|
||||||
networks:
|
networks:
|
||||||
mc_network:
|
mc_network:
|
||||||
ipv4_address: 172.20.0.2
|
ipv4_address: 172.20.0.2
|
||||||
volumes:
|
volumes:
|
||||||
- /var/run/docker.sock:/var/run/docker.sock
|
- /var/run/docker.sock:/var/run/docker.sock
|
||||||
|
|
||||||
|
mc_placeholder_server:
|
||||||
|
container_name: mc_placeholder_server
|
||||||
|
image: itzg/minecraft-server
|
||||||
|
environment:
|
||||||
|
type: "PAPER"
|
||||||
|
EULA: "TRUE"
|
||||||
|
MOTD: "Sleeping | Join & Wait to wake up"
|
||||||
|
MAX_PLAYERS: "0"
|
||||||
|
MAX_MEMORY: "500M"
|
||||||
|
INIT_MEMORY: "100M"
|
||||||
|
networks:
|
||||||
|
mc_network:
|
||||||
|
ipv4_address: 172.20.0.3
|
||||||
|
|
||||||
mc:
|
mc:
|
||||||
|
container_name: example_mc_server_1
|
||||||
image: itzg/minecraft-server
|
image: itzg/minecraft-server
|
||||||
environment:
|
environment:
|
||||||
type: "PAPER"
|
type: "PAPER"
|
||||||
EULA: "TRUE"
|
EULA: "TRUE"
|
||||||
MOTD: "TEST1"
|
MOTD: "TEST1"
|
||||||
|
MAX_PLAYERS: "0"
|
||||||
#! Dont change SERVER_PORT. Use PORT_IP_MAP in auto_starter instead.
|
#! Dont change SERVER_PORT. Use PORT_IP_MAP in auto_starter instead.
|
||||||
#! SERVER_PORT default is "25565"
|
#! SERVER_PORT default is "25565"
|
||||||
networks:
|
networks:
|
||||||
mc_network:
|
mc_network:
|
||||||
ipv4_address: 172.20.0.3
|
ipv4_address: 172.20.0.5
|
||||||
mc2:
|
mc2:
|
||||||
|
container_name: example_mc_server_2
|
||||||
image: itzg/minecraft-server
|
image: itzg/minecraft-server
|
||||||
environment:
|
environment:
|
||||||
type: "PAPER"
|
type: "PAPER"
|
||||||
@@ -45,4 +64,4 @@ services:
|
|||||||
#! SERVER_PORT default is "25565"
|
#! SERVER_PORT default is "25565"
|
||||||
networks:
|
networks:
|
||||||
mc_network:
|
mc_network:
|
||||||
ipv4_address: 172.20.0.4
|
ipv4_address: 172.20.0.6
|
||||||
|
|||||||
42
nginx.conf
42
nginx.conf
@@ -1,19 +1,25 @@
|
|||||||
events { }
|
events { }
|
||||||
stream {
|
stream {
|
||||||
upstream upstream_25565 {
|
upstream upstream_25565 {
|
||||||
server 172.20.0.3:25565;
|
server 172.20.0.5:25565;
|
||||||
server 127.0.0.1:25565 backup;
|
server 127.0.0.1:25565 backup;
|
||||||
}
|
server 172.20.0.3:25565 backup;
|
||||||
server {
|
}
|
||||||
listen 172.20.0.2:25565;
|
server {
|
||||||
proxy_pass upstream_25565;
|
listen 172.20.0.2:25565;
|
||||||
}
|
proxy_connect_timeout 5s;
|
||||||
upstream upstream_25566 {
|
proxy_timeout 5s;
|
||||||
server 172.20.0.4:25565;
|
proxy_pass upstream_25565;
|
||||||
server 127.0.0.1:25566 backup;
|
}
|
||||||
}
|
upstream upstream_25566 {
|
||||||
server {
|
server 172.20.0.6:25565;
|
||||||
listen 172.20.0.2:25566;
|
server 127.0.0.1:25566 backup;
|
||||||
proxy_pass upstream_25566;
|
server 172.20.0.3:25565 backup;
|
||||||
}
|
}
|
||||||
}
|
server {
|
||||||
|
listen 172.20.0.2:25566;
|
||||||
|
proxy_connect_timeout 5s;
|
||||||
|
proxy_timeout 5s;
|
||||||
|
proxy_pass upstream_25566;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user