-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordpress
More file actions
executable file
·421 lines (368 loc) · 15 KB
/
Copy pathwordpress
File metadata and controls
executable file
·421 lines (368 loc) · 15 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#!/usr/bin/python3
import os
import sys
import subprocess
import shutil
import argparse
import time
import re
import json
from datetime import datetime
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
volumes:
- db_data:/var/lib/mysql
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
WORDPRESS_CONFIG_EXTRA: |
define('WP_DEBUG', false);
define('DISALLOW_FILE_EDIT', true);
define('WP_AUTO_UPDATE_CORE', false);
define('FS_METHOD', 'direct');
volumes:
- ./app:/var/www/html
- ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
volumes:
db_data:
"""
UPLOADS_INI = """upload_max_filesize = 256M
post_max_size = 256M
max_execution_time = 300
max_input_time = 300
memory_limit = 512M
"""
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 get_container_name(name, service="wordpress"):
res = subprocess.run(
["docker", "compose", "-p", name, "ps", "--format", "json"],
cwd=os.path.join(BASE_DIR, name), capture_output=True, text=True
)
for line in res.stdout.strip().splitlines():
try:
data = json.loads(line)
if data.get("Service") == service:
return data.get("Name")
except: pass
return f"{name}-{service}-1"
def site_path(name):
return os.path.join(BASE_DIR, name)
def site_exists(name):
return os.path.exists(site_path(name))
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 get_site_status(name):
path = site_path(name)
if not os.path.exists(path): return "Not found"
try:
res = subprocess.run(["docker", "compose", "-p", name, "ps", "--format", "json"],
cwd=path, capture_output=True, text=True)
if "running" in res.stdout.lower():
return "Running"
if res.stdout.strip():
return "Stopped"
return "Not created"
except: return "Unknown"
# === COMMANDS ===
def cmd_create(name, port, force_pull=False):
path = site_path(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))
with open(os.path.join(path, "uploads.ini"), "w") as f:
f.write(UPLOADS_INI)
subprocess.run(["sudo", "chown", "33:33", os.path.join(path, "app")], check=True)
if force_pull:
print("Pulling fresh images...")
subprocess.run(["docker", "compose", "-p", name, "pull"], cwd=path, check=True)
subprocess.run(["docker", "compose", "-p", name, "up", "-d", "--pull", "missing"], cwd=path, check=True)
print("Waiting for setup (30s)...")
time.sleep(20)
container = get_container_name(name)
setup = "; ".join([
f"docker exec -u 0 {container} curl -s -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar",
f"docker exec -u 0 {container} chmod +x wp-cli.phar",
f"docker exec -u 0 {container} mv wp-cli.phar /usr/local/bin/wp",
f"docker exec {container} wp core install --url=http://localhost:{port} --title='{name.capitalize()}' --admin_user=admin --admin_password=admin --admin_email=admin@example.com --allow-root",
f"docker exec -u 0 {container} chown -R www-data:www-data /var/www/html"
])
subprocess.run(setup, shell=True, check=True)
print(f"\nSUCCESS! http://localhost:{port} (admin / admin)")
except Exception as e:
print(f"Failed: {e}")
def cmd_start(name):
if not site_exists(name):
print(f"Error: Site '{name}' not found.")
return
try:
subprocess.run(["docker", "compose", "-p", name, "up", "-d", "--pull", "missing"], cwd=site_path(name), check=True)
print(f"Started '{name}'.")
except Exception as e:
print(f"Error: {e}")
def cmd_stop(name):
if not site_exists(name):
print(f"Error: Site '{name}' not found.")
return
try:
subprocess.run(["docker", "compose", "-p", name, "down"], cwd=site_path(name), check=True)
print(f"Stopped '{name}'.")
except Exception as e:
print(f"Error: {e}")
def cmd_restart(name):
cmd_stop(name)
cmd_start(name)
def cmd_delete(name):
if not site_exists(name):
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=site_path(name), check=True)
subprocess.run(["sudo", "rm", "-rf", site_path(name)], check=True)
print(f"Deleted '{name}'.")
except Exception as e:
print(f"Error: {e}")
def cmd_list():
if not os.path.exists(BASE_DIR):
print("No sites found.")
return
sites = sorted(os.listdir(BASE_DIR))
if not sites:
print("No sites found.")
return
print(f"{'SITE':<20} {'PORT':<10} {'STATUS'}")
print("-" * 42)
for name in sites:
path = site_path(name)
if not os.path.isdir(path): continue
port = get_site_port(path)
status = get_site_status(name)
print(f"{name:<20} {port:<10} {status}")
def cmd_info(name):
if not site_exists(name):
print(f"Error: Site '{name}' not found.")
return
path = site_path(name)
port = get_site_port(path)
status = get_site_status(name)
print(f"Name: {name}")
print(f"Port: {port}")
print(f"URL: http://localhost:{port}")
print(f"Status: {status}")
print(f"Path: {path}")
print(f"App: {path}/app")
if status == "Running":
wc = get_container_name(name, "wordpress")
dc = get_container_name(name, "db")
try:
wp_id = subprocess.run(["docker", "inspect", "-f", "{{.ID}}", wc], capture_output=True, text=True).stdout.strip()[:12]
db_id = subprocess.run(["docker", "inspect", "-f", "{{.ID}}", dc], capture_output=True, text=True).stdout.strip()[:12]
print(f"WP: {wp_id} ({wc})")
print(f"DB: {db_id} ({dc})")
except: pass
print(f"Admin: admin / admin")
def cmd_shell(name, service="wordpress"):
if not site_exists(name):
print(f"Error: Site '{name}' not found.")
return
container = get_container_name(name, service)
print(f"Opening shell in '{container}'...")
subprocess.run(["docker", "exec", "-it", container, "bash"], check=False)
def cmd_logs(name, service="wordpress", follow=False, tail=50):
if not site_exists(name):
print(f"Error: Site '{name}' not found.")
return
container = get_container_name(name, service)
cmd = ["docker", "logs"]
if follow: cmd.append("--follow")
cmd.extend(["--tail", str(tail), container])
subprocess.run(cmd, check=False)
def cmd_wp(name, args_str):
if not site_exists(name):
print(f"Error: Site '{name}' not found.")
return
container = get_container_name(name, "wordpress")
subprocess.run(["docker", "exec", container, "wp"] + args_str, check=False)
def cmd_db_export(name):
if not site_exists(name):
print(f"Error: Site '{name}' not found.")
return
container = get_container_name(name, "db")
backup_dir = site_path(name)
filename = f"backup-{datetime.now().strftime('%Y%m%d-%H%M%S')}.sql"
outpath = os.path.join(backup_dir, filename)
with open(outpath, "w") as f:
subprocess.run(
["docker", "exec", container, "mariadb-dump", "-uwordpress", "-pwordpress", "wordpress"],
stdout=f, check=True
)
subprocess.run(["sudo", "chown", "33:33", outpath], check=False)
print(f"Exported to {outpath}")
def cmd_db_import(name, filepath):
if not site_exists(name):
print(f"Error: Site '{name}' not found.")
return
if not os.path.exists(filepath):
print(f"Error: File '{filepath}' not found.")
return
container = get_container_name(name, "db")
with open(filepath, "r") as f:
subprocess.run(
["docker", "exec", "-i", container, "mariadb", "-uwordpress", "-pwordpress", "wordpress"],
stdin=f, check=True
)
print(f"Imported from {filepath}")
def cmd_backup(name):
if not site_exists(name):
print(f"Error: Site '{name}' not found.")
return
path = site_path(name)
backup_dir = os.path.join(path, "backups")
os.makedirs(backup_dir, exist_ok=True)
ts = datetime.now().strftime("%Y%m%d-%H%M%S")
db_file = os.path.join(backup_dir, f"db-{ts}.sql")
dc = get_container_name(name, "db")
with open(db_file, "w") as f:
subprocess.run(
["docker", "exec", dc, "mariadb-dump", "-uwordpress", "-pwordpress", "wordpress"],
stdout=f, check=True
)
site_file = os.path.join(backup_dir, f"site-{ts}.tar.gz")
subprocess.run(["sudo", "tar", "-czf", site_file, "-C", path, "app"], check=True)
subprocess.run(["sudo", "chown", "-R", f"{os.getuid()}:{os.getgid()}", backup_dir], check=True)
print(f"Backup saved: {backup_dir}")
def cmd_ps(name):
if not site_exists(name):
print(f"Error: Site '{name}' not found.")
return
path = site_path(name)
subprocess.run(["docker", "compose", "-p", name, "ps"], cwd=path, check=False)
def cmd_port(name, new_port):
if not site_exists(name):
print(f"Error: Site '{name}' not found.")
return
path = site_path(name)
compose_file = os.path.join(path, "docker-compose.yml")
if not os.path.exists(compose_file):
print("Error: docker-compose.yml not found.")
return
try:
with open(compose_file, "r") as f:
content = f.read()
content = re.sub(r'["\'](\d+):80["\']', f'"{new_port}:80"', content)
with open(compose_file, "w") as f:
f.write(content)
print(f"Port changed to {new_port}. Restart the site for changes to take effect.")
except Exception as e:
print(f"Error: {e}")
def main():
check_dependencies()
parser = argparse.ArgumentParser(description="WordPress CLI Manager")
subparsers = parser.add_subparsers(dest="command")
p_create = subparsers.add_parser("create", help="Create a new WordPress site")
p_create.add_argument("name", help="Site name")
p_create.add_argument("port", type=int, help="Port number")
p_create.add_argument("-f", "--force-pull", action="store_true", help="Force pull fresh images")
p_start = subparsers.add_parser("start", help="Start a site")
p_start.add_argument("name")
p_stop = subparsers.add_parser("stop", help="Stop a site")
p_stop.add_argument("name")
p_restart = subparsers.add_parser("restart", help="Restart a site")
p_restart.add_argument("name")
p_delete = subparsers.add_parser("delete", help="Delete a site")
p_delete.add_argument("name")
p_list = subparsers.add_parser("list", help="List all sites")
p_info = subparsers.add_parser("info", help="Show site details")
p_info.add_argument("name")
p_shell = subparsers.add_parser("shell", help="Open shell in a container")
p_shell.add_argument("name")
p_shell.add_argument("-s", "--service", default="wordpress", choices=["wordpress", "db"], help="Container service")
p_logs = subparsers.add_parser("logs", help="View container logs")
p_logs.add_argument("name")
p_logs.add_argument("-s", "--service", default="wordpress", choices=["wordpress", "db"])
p_logs.add_argument("-f", "--follow", action="store_true", help="Follow logs")
p_logs.add_argument("-n", "--lines", type=int, default=50, help="Number of lines")
p_wp = subparsers.add_parser("wp", help="Run wp-cli command")
p_wp.add_argument("name")
p_wp.add_argument("args", nargs=argparse.REMAINDER, help="wp-cli arguments")
p_db = subparsers.add_parser("db", help="Database operations")
p_db_sub = p_db.add_subparsers(dest="db_command")
p_db_export = p_db_sub.add_parser("export", help="Export database")
p_db_export.add_argument("name")
p_db_import = p_db_sub.add_parser("import", help="Import database")
p_db_import.add_argument("name")
p_db_import.add_argument("file", help="SQL file path")
p_backup = subparsers.add_parser("backup", help="Backup a site (files + db)")
p_backup.add_argument("name")
p_ps = subparsers.add_parser("ps", help="Show containers for a site")
p_ps.add_argument("name")
p_port = subparsers.add_parser("port", help="Change site port")
p_port.add_argument("name")
p_port.add_argument("port", type=int, help="New port number")
args = parser.parse_args()
if args.command == "create": cmd_create(args.name, args.port, args.force_pull)
elif args.command == "start": cmd_start(args.name)
elif args.command == "stop": cmd_stop(args.name)
elif args.command == "restart": cmd_restart(args.name)
elif args.command == "delete": cmd_delete(args.name)
elif args.command == "list": cmd_list()
elif args.command == "info": cmd_info(args.name)
elif args.command == "shell": cmd_shell(args.name, args.service)
elif args.command == "logs": cmd_logs(args.name, args.service, args.follow, args.lines)
elif args.command == "wp": cmd_wp(args.name, args.args)
elif args.command == "db":
if not hasattr(args, "db_command") or not args.db_command:
print("Usage: wordpress db {export|import} ...")
return
if args.db_command == "export": cmd_db_export(args.name)
elif args.db_command == "import": cmd_db_import(args.name, args.file)
elif args.command == "backup": cmd_backup(args.name)
elif args.command == "ps": cmd_ps(args.name)
elif args.command == "port": cmd_port(args.name, args.port)
else: parser.print_help()
if __name__ == "__main__":
main()