You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.9 KiB
60 lines
1.9 KiB
# -*- coding: utf-8 -*-
|
|
import subprocess
|
|
import multiprocessing
|
|
import os
|
|
import json
|
|
|
|
def load_config():
|
|
with open("config.json", "r") as f:
|
|
return json.load(f)
|
|
|
|
config = load_config()
|
|
PREFIX = "clash-multi-"
|
|
START = 1
|
|
END = config["total_container"]
|
|
START_PORT = config["start_port"]
|
|
START_API_PORT = config["start_api_port"]
|
|
START_WEB_PORT = config["start_web_port"]
|
|
|
|
def get_docker_run_command(container_id):
|
|
container_name = f"{PREFIX}{container_id}"
|
|
port = START_PORT + container_id
|
|
api_port = START_API_PORT + container_id
|
|
web_port = START_WEB_PORT + container_id
|
|
|
|
script_dir = os.path.dirname(os.path.abspath(__file__))
|
|
config_file_path = os.path.join(script_dir, "config", f"config{container_id}.yaml")
|
|
|
|
return [
|
|
"docker", "run", "-d",
|
|
"--name", container_name,
|
|
"--restart=always",
|
|
"--log-opt", "max-size=1m",
|
|
"-v", f"{config_file_path}:/root/.config/clash/config.yaml",
|
|
"-p", f"{web_port}:8080",
|
|
"-p", f"{port}:7890",
|
|
"-p", f"{api_port}:9090",
|
|
"laoyutang/clash-and-dashboard:latest"
|
|
]
|
|
|
|
|
|
def start_container(container_id):
|
|
container_name = f"{PREFIX}{container_id}"
|
|
print(f"Starting container: {container_name}")
|
|
try:
|
|
cmd = get_docker_run_command(container_id)
|
|
# print(f"Running Docker command: {' '.join(cmd)}")
|
|
subprocess.run(cmd, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
|
|
print(f"Successfully started container: {container_name}")
|
|
except subprocess.CalledProcessError as e:
|
|
error_message = e.stderr.decode().strip() if e.stderr else "Unknown error"
|
|
print(f"Error starting container {container_name}: {error_message}")
|
|
|
|
|
|
def main():
|
|
with multiprocessing.Pool(processes=os.cpu_count() or 8) as pool:
|
|
pool.map(start_container, range(START, END + 1))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|