-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordpress.py
More file actions
executable file
·141 lines (126 loc) · 4.8 KB
/
wordpress.py
File metadata and controls
executable file
·141 lines (126 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python3
import os
import sys
import subprocess
import shutil
import argparse
import time
import re
BASE_DIR = os.path.expanduser("~/.local/share/wordpress-lab/sites")
COMPOSE_TEMPLATE = """
services:
db:
image: mariadb:10.6
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-proot"]
timeout: 5s
retries: 10
wordpress:
image: wordpress:latest
depends_on:
db:
condition: service_healthy
ports:
- "{port}:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
- ./app:/var/www/html
"""
def check_dependencies():
for tool in ["docker", "curl"]:
if shutil.which(tool) is None:
print(f"❌ Error: '{tool}' is not installed.")
sys.exit(1)
try:
subprocess.run(["docker", "compose", "version"], capture_output=True, check=True)
except:
print("❌ Error: 'docker compose' (V2) is not available.")
sys.exit(1)
def create_site(name, port):
path = os.path.join(BASE_DIR, name)
if os.path.exists(path):
print(f"❌ Error: Site '{name}' already exists.")
return
print(f"🚀 Initializing '{name}' on port {port}...")
try:
os.makedirs(os.path.join(path, "app"), exist_ok=True)
with open(os.path.join(path, "docker-compose.yml"), "w") as f:
f.write(COMPOSE_TEMPLATE.format(port=port))
subprocess.run(["docker", "compose", "-p", name, "up", "-d"], cwd=path, check=True)
print("⏳ Waiting for setup (30s)...")
time.sleep(20)
container_name = f"{name}-wordpress-1"
setup_script = f"""
docker exec -u 0 {container_name} curl -s -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar && \
docker exec -u 0 {container_name} chmod +x wp-cli.phar && \
docker exec -u 0 {container_name} mv wp-cli.phar /usr/local/bin/wp && \
docker exec {container_name} wp core install --url=http://localhost:{port} --title='{name.capitalize()}' --admin_user=admin --admin_password=admin --admin_email=admin@example.com --allow-root
"""
subprocess.run(setup_script, shell=True, check=True)
print(f"\n✨ SUCCESS! http://localhost:{port} (admin/admin)")
except Exception as e:
print(f"❌ Failed: {e}")
def delete_site(name):
path = os.path.join(BASE_DIR, name)
if not os.path.exists(path):
print(f"❌ Error: Site '{name}' not found.")
return
if input(f"⚠️ Confirm delete '{name}'? (y/N): ").lower() != 'y': return
try:
subprocess.run(["docker", "compose", "-p", name, "down", "-v"], cwd=path, check=True)
subprocess.run(["sudo", "rm", "-rf", path], check=True)
print(f"✅ Deleted '{name}'.")
except Exception as e:
print(f"❌ Error: {e}")
def get_site_port(path):
compose_path = os.path.join(path, "docker-compose.yml")
if not os.path.exists(compose_path): return "???"
try:
with open(compose_path, "r") as f:
content = f.read()
match = re.search(r'["\'](\d+):80["\']', content)
return match.group(1) if match else "???"
except: return "???"
def list_sites():
if not os.path.exists(BASE_DIR): return
sites = sorted(os.listdir(BASE_DIR))
if not sites:
print("📭 No sites found.")
return
print(f"{'SITE NAME':<20} {'PORT':<10} {'STATUS'}")
print("-" * 45)
for name in sites:
path = os.path.join(BASE_DIR, name)
port = get_site_port(path)
status = "🔴 Stopped"
try:
res = subprocess.run(["docker", "compose", "-p", name, "ps", "--format", "json"],
cwd=path, capture_output=True, text=True)
if "running" in res.stdout.lower():
status = "🟢 Running"
except: pass
print(f"{name:<20} {port:<10} {status}")
def main():
check_dependencies()
parser = argparse.ArgumentParser(description="Wordpress Ultimate Manager")
subparsers = parser.add_subparsers(dest="command")
c = subparsers.add_parser("create"); c.add_argument("name"); c.add_argument("port", type=int)
d = subparsers.add_parser("delete"); d.add_argument("name")
subparsers.add_parser("list")
args = parser.parse_args()
if args.command == "create": create_site(args.name, args.port)
elif args.command == "delete": delete_site(args.name)
elif args.command == "list": list_sites()
else: parser.print_help()
if __name__ == "__main__": main()