49 lines
1.8 KiB
Python
49 lines
1.8 KiB
Python
import urllib.request
|
|
import json
|
|
|
|
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'})
|
|
try:
|
|
with urllib.request.urlopen(req) as response:
|
|
token = json.loads(response.read().decode())['jwt']
|
|
print("Got token")
|
|
except Exception as e:
|
|
print("Auth failed:", e)
|
|
exit(1)
|
|
|
|
req = urllib.request.Request(f"{base_url}/endpoints", headers={'Authorization': f'Bearer {token}'})
|
|
try:
|
|
with urllib.request.urlopen(req) as response:
|
|
endpoints = json.loads(response.read().decode())
|
|
endpoint_id = endpoints[0]['Id']
|
|
print("Endpoint ID:", endpoint_id)
|
|
except Exception as e:
|
|
print("Endpoints failed:", e)
|
|
exit(1)
|
|
|
|
req = urllib.request.Request(f"{base_url}/endpoints/{endpoint_id}/docker/containers/json?all=1", headers={'Authorization': f'Bearer {token}'})
|
|
try:
|
|
with urllib.request.urlopen(req) as response:
|
|
containers = json.loads(response.read().decode())
|
|
container_id = None
|
|
for c in containers:
|
|
if "/context-hub" in c.get('Names', []):
|
|
container_id = c['Id']
|
|
break
|
|
print("Container ID:", container_id)
|
|
except Exception as e:
|
|
print("Containers failed:", e)
|
|
exit(1)
|
|
|
|
if container_id:
|
|
req = urllib.request.Request(f"{base_url}/endpoints/{endpoint_id}/docker/containers/{container_id}/restart", method="POST", headers={'Authorization': f'Bearer {token}'})
|
|
try:
|
|
with urllib.request.urlopen(req) as response:
|
|
print("Restarted:", response.status)
|
|
except Exception as e:
|
|
print("Restart failed:", e)
|
|
exit(1)
|
|
else:
|
|
print("Container not found")
|