45 lines
2.0 KiB
Python
45 lines
2.0 KiB
Python
import urllib.request
|
|
import json
|
|
import time
|
|
|
|
base_url = "http://192.168.100.33:9000/api"
|
|
data = json.dumps({"Username":"bestof","Password":"2L2u519wportainer"}).encode('utf-8')
|
|
req = urllib.request.Request(f"{base_url}/auth", data=data, headers={'Content-Type': 'application/json'})
|
|
token = json.loads(urllib.request.urlopen(req).read().decode())['jwt']
|
|
|
|
req = urllib.request.Request(f"{base_url}/endpoints", headers={'Authorization': f'Bearer {token}'})
|
|
endpoints = json.loads(urllib.request.urlopen(req).read().decode())
|
|
endpoint_id = endpoints[0]['Id']
|
|
|
|
create_data = {
|
|
"Image": "python:3.12-alpine",
|
|
"Cmd": ["sh", "-c", "apk add --no-cache docker-cli docker-cli-compose && cd /workspace && docker compose up -d --build"],
|
|
"HostConfig": {
|
|
"Binds": [
|
|
"/var/run/docker.sock:/var/run/docker.sock",
|
|
"/volume1/docker/context-hub:/workspace"
|
|
]
|
|
}
|
|
}
|
|
req = urllib.request.Request(f"{base_url}/endpoints/{endpoint_id}/docker/containers/create", method="POST", data=json.dumps(create_data).encode('utf-8'), headers={'Authorization': f'Bearer {token}', 'Content-Type': 'application/json'})
|
|
try:
|
|
response = urllib.request.urlopen(req)
|
|
container_id = json.loads(response.read().decode())['Id']
|
|
print("Created container:", container_id)
|
|
except Exception as e:
|
|
print("Create failed:", e.read().decode() if hasattr(e, 'read') else str(e))
|
|
exit(1)
|
|
|
|
req = urllib.request.Request(f"{base_url}/endpoints/{endpoint_id}/docker/containers/{container_id}/start", method="POST", headers={'Authorization': f'Bearer {token}'})
|
|
urllib.request.urlopen(req)
|
|
print("Started container.")
|
|
|
|
time.sleep(15)
|
|
|
|
# Optional: check if context-hub is running
|
|
req = urllib.request.Request(f"{base_url}/endpoints/{endpoint_id}/docker/containers/json?all=1", headers={'Authorization': f'Bearer {token}'})
|
|
containers = json.loads(urllib.request.urlopen(req).read().decode())
|
|
for c in containers:
|
|
if "/context-hub" in c.get('Names', []):
|
|
print("context-hub status:", c['State'])
|