Skip to content

Commit ad54713

Browse files
authored
Merge pull request #829 from MetaCell/feat-add-check-kc-ready-to-init-api-user-script
feat: add KC ready check to create_api_user.sh
2 parents 9ecb4a9 + 604472a commit ad54713

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

applications/accounts/scripts/create_api_user.sh

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,17 @@ export API_PASSWORD=$(cat /opt/cloudharness/resources/auth/api_user_password 2>/
55
export TMP_CLIENT="tmp_api_client"
66
export TMP_CLIENT_SECRET="${KC_BOOTSTRAP_ADMIN_USERNAME}"
77

8-
sleep 120
8+
check_kc_ready() {
9+
if echo >/dev/tcp/127.0.0.1/${ACCOUNTS_SERVICE_PORT}; then
10+
return 0
11+
fi 2> /dev/null
12+
return 1
13+
}
914

10-
echo "create_api_user: waiting for Keycloak to start..."
15+
while ! check_kc_ready; do
16+
echo "create_api_user: waiting for Keycloak to be ready..."
17+
sleep 10
18+
done
1119

1220
create_temporary_client() {
1321
/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() {
2129
}
2230

2331
create_kc_config() {
24-
/opt/keycloak/bin/kcadm.sh config credentials --server http://localhost:8080 --realm master --client ${TMP_CLIENT} --secret ${TMP_CLIENT_SECRET}
32+
/opt/keycloak/bin/kcadm.sh config credentials --server http://localhost:${ACCOUNTS_SERVICE_PORT} --realm master --client ${TMP_CLIENT} --secret ${TMP_CLIENT_SECRET}
2533
}
2634

2735
api_user_exists() {
@@ -40,39 +48,39 @@ set_password_and_roles() {
4048
# Wait for Keycloak to be ready - just give it some time to start up
4149

4250

43-
echo "Attempting authentication..."
51+
echo "create_api_user: attempting authentication..."
4452

4553
# First, try to authenticate as admin_api
4654
if [ -n "$API_PASSWORD" ] && /opt/keycloak/bin/kcadm.sh config credentials \
47-
--server http://localhost:8080 \
55+
--server http://localhost:${ACCOUNTS_SERVICE_PORT} \
4856
--realm master \
4957
--user "$API_USERNAME" \
5058
--password "$API_PASSWORD" 2>/dev/null; then
51-
echo "Successfully authenticated as $API_USERNAME"
52-
echo "Startup scripts not needed (admin_api user already exists)"
59+
echo "create_api_user: successfully authenticated as $API_USERNAME"
60+
echo "create_api_user: sStartup scripts not needed (admin_api user already exists)"
5361
exit 0
5462
fi
5563

56-
echo "admin_api user does not exist or authentication failed. Authenticating to create the user..."
64+
echo "create_api_user: admin_api user does not exist or authentication failed. Authenticating to create the user..."
5765

5866
set -e
5967
create_temporary_client
6068
create_kc_config
61-
echo "Temporary credentials successfully created."
69+
echo "create_api_user: temporary credentials successfully created."
6270

63-
echo "Checking if API user exists..."
71+
echo "create_api_user: checking if API user exists..."
6472
# Check if user already exists
6573
if ! api_user_exists; then
66-
echo "API user $API_USERNAME doesn't exists, creating..."
74+
echo "create_api_user: API user $API_USERNAME doesn't exists, creating..."
6775
create_api_user
68-
echo "API user created successfully"
76+
echo "create_api_user: API user created successfully"
6977
else
70-
echo "API user $API_USERNAME already exists."
78+
echo "create_api_user: API user $API_USERNAME already exists."
7179
fi
7280
set +e
7381

74-
echo "Setting password and role."
82+
echo "create_api_user: setting password and role."
7583
set_password_and_roles
7684

77-
echo "Cleaning up temporary client."
85+
echo "create_api_user: cleaning up temporary client."
7886
delete_temporary_client

0 commit comments

Comments
 (0)