From 604472ac60f3840d91cbc9fa9313e7448605cba7 Mon Sep 17 00:00:00 2001 From: Zoran Sinnema Date: Thu, 11 Dec 2025 08:54:44 +0100 Subject: [PATCH] feat: add KC ready check to create_api_user.sh --- .../accounts/scripts/create_api_user.sh | 38 +++++++++++-------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/applications/accounts/scripts/create_api_user.sh b/applications/accounts/scripts/create_api_user.sh index 43bcac20..e2d548be 100755 --- a/applications/accounts/scripts/create_api_user.sh +++ b/applications/accounts/scripts/create_api_user.sh @@ -5,9 +5,17 @@ export API_PASSWORD=$(cat /opt/cloudharness/resources/auth/api_user_password 2>/ export TMP_CLIENT="tmp_api_client" export TMP_CLIENT_SECRET="${KC_BOOTSTRAP_ADMIN_USERNAME}" -sleep 120 +check_kc_ready() { + if echo >/dev/tcp/127.0.0.1/${ACCOUNTS_SERVICE_PORT}; then + return 0 + fi 2> /dev/null + return 1 +} -echo "create_api_user: waiting for Keycloak to start..." +while ! check_kc_ready; do + echo "create_api_user: waiting for Keycloak to be ready..." + sleep 10 +done create_temporary_client() { /opt/keycloak/bin/kc.sh bootstrap-admin service --client-id=${TMP_CLIENT} --client-secret:env=TMP_CLIENT_SECRET --http-management-port 9876 @@ -21,7 +29,7 @@ delete_temporary_client() { } create_kc_config() { - /opt/keycloak/bin/kcadm.sh config credentials --server http://localhost:8080 --realm master --client ${TMP_CLIENT} --secret ${TMP_CLIENT_SECRET} + /opt/keycloak/bin/kcadm.sh config credentials --server http://localhost:${ACCOUNTS_SERVICE_PORT} --realm master --client ${TMP_CLIENT} --secret ${TMP_CLIENT_SECRET} } api_user_exists() { @@ -40,39 +48,39 @@ set_password_and_roles() { # Wait for Keycloak to be ready - just give it some time to start up -echo "Attempting authentication..." +echo "create_api_user: attempting authentication..." # First, try to authenticate as admin_api if [ -n "$API_PASSWORD" ] && /opt/keycloak/bin/kcadm.sh config credentials \ - --server http://localhost:8080 \ + --server http://localhost:${ACCOUNTS_SERVICE_PORT} \ --realm master \ --user "$API_USERNAME" \ --password "$API_PASSWORD" 2>/dev/null; then - echo "Successfully authenticated as $API_USERNAME" - echo "Startup scripts not needed (admin_api user already exists)" + echo "create_api_user: successfully authenticated as $API_USERNAME" + echo "create_api_user: sStartup scripts not needed (admin_api user already exists)" exit 0 fi -echo "admin_api user does not exist or authentication failed. Authenticating to create the user..." +echo "create_api_user: admin_api user does not exist or authentication failed. Authenticating to create the user..." set -e create_temporary_client create_kc_config -echo "Temporary credentials successfully created." +echo "create_api_user: temporary credentials successfully created." -echo "Checking if API user exists..." +echo "create_api_user: checking if API user exists..." # Check if user already exists if ! api_user_exists; then - echo "API user $API_USERNAME doesn't exists, creating..." + echo "create_api_user: API user $API_USERNAME doesn't exists, creating..." create_api_user - echo "API user created successfully" + echo "create_api_user: API user created successfully" else - echo "API user $API_USERNAME already exists." + echo "create_api_user: API user $API_USERNAME already exists." fi set +e -echo "Setting password and role." +echo "create_api_user: setting password and role." set_password_and_roles -echo "Cleaning up temporary client." +echo "create_api_user: cleaning up temporary client." delete_temporary_client