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
|
#!/usr/bin/env python3
"""
Rocky SSH Container Launcher
Manual build command:
podman build -f docker_build/Dockerfile -t rocky_dev:latest .
Usage:
python3 podman_launch_devenv.py # Build and launch container
python3 podman_launch_devenv.py --list # List running rocky-dev containers
python3 podman_launch_devenv.py --cleanup # Stop and remove all containers
"""
import subprocess, argparse, os, glob
def run(cmd): return subprocess.run(cmd, shell=True, capture_output=True, text=True)
def build():
if not glob.glob("docker_build/ssh-keys/*.pub"): os.makedirs("docker_build/ssh-keys", exist_ok=True); open("docker_build/ssh-keys/dummy.pub", "w").write("# dummy")
result = run("podman build -f docker_build/Dockerfile -t rocky_dev:latest .")
if os.path.exists("docker_build/ssh-keys/dummy.pub"): os.remove("docker_build/ssh-keys/dummy.pub")
return result.returncode == 0
def launch():
port = str(args.port) if args.port else run("shuf -i 10000-65000 -n 1").stdout.strip()
result = run(f"podman run -d -p {port}:22 --privileged --name rocky_dev-{port} rocky_dev:latest")
if result.returncode == 0:
ip = run("hostname -I | awk '{print $1}'").stdout.strip() or "localhost"
print(f"🐳 SSH: ssh root@{ip} -p {port}")
print(f"🐚 Shell: podman exec -it rocky_dev-{port} /bin/bash")
print(f"💡 Tip: For direct shell without port forwarding, use: podman run -it rocky_dev:latest /bin/bash")
return result.returncode == 0
parser = argparse.ArgumentParser(epilog="""
Manual build commands:
Build: podman build -f docker_build/Dockerfile -t rocky_dev:latest .
Rebuild: podman rmi rocky_dev:latest && podman build -f docker_build/Dockerfile -t rocky_dev:latest .
""", formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("command", nargs="?", choices=["run", "list", "cleanup"], help="Command to execute")
parser.add_argument("-p", "--port", type=int)
args = parser.parse_args()
if args.command == "list": print(run("podman ps --filter name=rocky_dev").stdout or "No containers")
elif args.command == "cleanup": [run(f"podman stop {c} && podman rm {c}") for c in run("podman ps -a --filter name=rocky_dev --format '{{.Names}}'").stdout.split()]
elif args.command == "run":
if run("podman images -q rocky_dev").stdout:
print("found rocky_dev container! starting with a random public port to ssh... ")
launch()
else:
print("❌ Image rocky_dev:latest not found")
else:
print("Usage: python3 podman_launch_devenv.py {run|list|cleanup} [-p PORT]")
print("🐚 Shell: podman exec -it rocky_dev-<port> /bin/bash")
print("💡 Tip: For direct shell without port forwarding, use: podman run -it rocky_dev:latest /bin/bash")
|