Skip to content

Commit

Permalink
Merge pull request #48 from CanDIG/lilyyang/docker-healthcheck-fix
Browse files Browse the repository at this point in the history
 DIG-1409: Fix docker health checks
  • Loading branch information
lilyyangyi301 authored Feb 1, 2024
2 parents 0fd7d79 + e55140e commit 35bfecc
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions candig_federation/healthcheck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import json
import os
import re
import sys
import uuid
from http import HTTPStatus
from pathlib import Path
import datetime
import requests

# Read Docker secrets
with open("/run/secrets/client_secret") as f:
client_secret = f.read().strip()

with open("/run/secrets/password") as f:
password = f.read().strip()

client_id = "local_candig"
username = "user2"

def get_token(username=None, password=None, client_id=None, client_secret=None):
payload = {
"client_id": client_id,
"client_secret": client_secret,
"grant_type": "password",
"username": username,
"password": password,
"scope": "openid",
}
response = requests.post(
"http://candig.docker.internal:8080/auth/realms/candig/protocol/openid-connect/token",
data=payload,
)
if response.status_code == 200:
return response.json()["access_token"]

def perform_healthcheck():
auth_token = get_token(username="user2", password=password, client_id="local_candig", client_secret=client_secret)
print(f"Authentication Token: {auth_token}")

headers = {"Authorization": f"Bearer {auth_token}"}
url = "http://candig.docker.internal:5080/federation/v1/service-info"

try:
response = requests.get(url, headers=headers)
response.raise_for_status()
print("Health check passed!")
return True
except requests.exceptions.RequestException as e:
print(f"Health check failed: {e}")
return False

if __name__ == "__main__":
perform_healthcheck()

0 comments on commit 35bfecc

Please sign in to comment.