context-hub/rebuild_check.py

33 lines
1.5 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 --build > /workspace/build.log 2>&1"],
"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']
except Exception as 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)