context-hub/update_stack.py

50 lines
1.7 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}/stacks", headers={'Authorization': f'Bearer {token}'})
try:
with urllib.request.urlopen(req) as response:
stacks = json.loads(response.read().decode())
stack_id = None
for s in stacks:
if s['Name'] == 'context-hub':
stack_id = s['Id']
endpoint_id = s['EndpointId']
env = s.get('Env', [])
break
print("Stack ID:", stack_id)
except Exception as e:
print("Stacks failed:", e)
exit(1)
if stack_id:
# We will just read the current docker-compose.yml
with open('docker-compose.yml', 'r') as f:
compose_file = f.read()
update_data = {
"StackFileContent": compose_file,
"Env": env,
"Prune": False
}
req = urllib.request.Request(f"{base_url}/stacks/{stack_id}?endpointId={endpoint_id}", method="PUT", data=json.dumps(update_data).encode('utf-8'), headers={'Authorization': f'Bearer {token}', 'Content-Type': 'application/json'})
try:
with urllib.request.urlopen(req) as response:
print("Stack updated:", response.status)
except Exception as e:
print("Update failed:", e)
if hasattr(e, 'read'):
print(e.read().decode())
exit(1)