From d3aed99aadf88c0bb972712b6c01fbca08d4f551 Mon Sep 17 00:00:00 2001 From: briskt <3172830+briskt@users.noreply.github.com> Date: Thu, 5 Dec 2024 19:08:00 +0800 Subject: [PATCH] replace whenavail with healthcheck --- Makefile | 10 ++--- actions-services.yml | 40 ++++++++++++++++--- application/run-tests-api.sh | 3 +- application/run-tests.sh | 3 +- docker-compose.yml | 67 +++++++++++++++++++++++++------- dockerbuild/broker/run-broker.sh | 2 +- dockerbuild/ldap/load_ldap.sh | 18 --------- email.common.env | 1 - 8 files changed, 95 insertions(+), 49 deletions(-) diff --git a/Makefile b/Makefile index df770175..2e55fdeb 100644 --- a/Makefile +++ b/Makefile @@ -42,19 +42,19 @@ upDb: docker compose up -d db yiimigrate: - docker compose run --rm cli whenavail db 3306 100 ./yii migrate --interactive=0 + docker compose run --rm cli ./yii migrate --interactive=0 yiimigratelocal: - docker compose run --rm cli whenavail db 3306 100 ./yii migrate --migrationPath=console/migrations-local/ --interactive=0 + docker compose run --rm cli ./yii migrate --migrationPath=console/migrations-local/ --interactive=0 basemodels: - docker compose run --rm cli whenavail db 3306 100 ./rebuildbasemodels.sh + docker compose run --rm cli ./rebuildbasemodels.sh yiimigratetestDb: - docker compose run --rm cli bash -c 'MYSQL_HOST=testDb MYSQL_DATABASE=test whenavail testDb 3306 100 ./yii migrate --interactive=0' + docker compose run --rm cli bash -c 'MYSQL_HOST=testDb MYSQL_DATABASE=test ./yii migrate --interactive=0' yiimigratetestDblocal: - docker compose run --rm cli bash -c 'MYSQL_HOST=testDb MYSQL_DATABASE=test whenavail testDb 3306 100 ./yii migrate --migrationPath=console/migrations-test/ --interactive=0' + docker compose run --rm cli bash -c 'MYSQL_HOST=testDb MYSQL_DATABASE=test ./yii migrate --migrationPath=console/migrations-test/ --interactive=0' rmTestDb: docker compose kill testDb diff --git a/actions-services.yml b/actions-services.yml index cb68b576..0df9d1de 100644 --- a/actions-services.yml +++ b/actions-services.yml @@ -1,3 +1,11 @@ +x-db_healthcheck: &db_healthcheck + healthcheck: + test: [ "CMD", "healthcheck.sh", "--connect", "--innodb_initialized" ] + start_period: 10s + interval: 10s + timeout: 5s + retries: 3 + services: db: image: mariadb:10 @@ -6,6 +14,7 @@ services: MYSQL_DATABASE: test MYSQL_USER: idpmgmt MYSQL_PASSWORD: idpmgmt + <<: *db_healthcheck zxcvbn: image: wcjr/zxcvbn-api:1.1.0 @@ -15,10 +24,14 @@ services: app: build: . depends_on: - - db - - zxcvbn - - broker - # - ldapload + db: + condition: service_healthy + zxcvbn: + condition: service_started + broker: + condition: service_healthy + # ldapload: + # condition: service_started environment: MYSQL_HOST: db MYSQL_DATABASE: test @@ -55,11 +68,13 @@ services: MYSQL_DATABASE: app MYSQL_USER: user MYSQL_PASSWORD: pass + <<: *db_healthcheck broker: image: silintl/idp-id-broker:6 depends_on: - - brokerDb + brokerDb: + condition: service_healthy volumes: - ./dockerbuild/broker/run-broker.sh:/data/run-broker.sh - ./dockerbuild/broker/m381901_235959_insert_test_data.php:/data/console/migrations/m381901_235959_insert_test_data.php @@ -90,15 +105,28 @@ services: MFA_WEBAUTHN_apiKey: not_needed_here MFA_WEBAUTHN_apiSecret: not_needed_here command: ./run-broker.sh + healthcheck: + test: nc -vz localhost 80 + start_period: 1s + interval: 5s + timeout: 1s + retries: 30 ldap: build: ./dockerbuild/ldap environment: DEBUG_LEVEL: 320 + healthcheck: + test: nc -vz localhost 389 + start_period: 1s + interval: 5s + timeout: 1s + retries: 90 ldapload: build: ./dockerbuild/ldap depends_on: - - ldap + ldap: + condition: service_healthy working_dir: /data command: ./load_ldap.sh diff --git a/application/run-tests-api.sh b/application/run-tests-api.sh index 64afff32..86063693 100755 --- a/application/run-tests-api.sh +++ b/application/run-tests-api.sh @@ -8,13 +8,12 @@ composer install --prefer-dist --no-interaction --optimize-autoloader --no-progr mkdir -p /data/runtime/mail # Run database migrations -whenavail ${MYSQL_HOST} 3306 100 /data/yii migrate --interactive=0 +/data/yii migrate --interactive=0 # Start apache apache2ctl start # Run codeception tests -whenavail broker 80 100 echo "broker ready" /data/vendor/bin/codecept run api -d TESTRESULTS_API=$? diff --git a/application/run-tests.sh b/application/run-tests.sh index 5f98166b..d63eeefd 100755 --- a/application/run-tests.sh +++ b/application/run-tests.sh @@ -13,13 +13,12 @@ composer install --prefer-dist --no-interaction --optimize-autoloader --no-progr mkdir -p /data/runtime/mail # Run database migrations -whenavail ${MYSQL_HOST} 3306 100 /data/yii migrate --interactive=0 +/data/yii migrate --interactive=0 # Install and enable xdebug for code coverage apt-get update && apt-get install -y php-xdebug # Run codeception tests -whenavail broker 80 100 echo "broker ready, running unit tests..." ./vendor/bin/codecept run unit # Run local behat tests diff --git a/docker-compose.yml b/docker-compose.yml index db94bd92..278f8f77 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,3 +1,11 @@ +x-db_healthcheck: &db_healthcheck + healthcheck: + test: [ "CMD", "healthcheck.sh", "--connect", "--innodb_initialized" ] + start_period: 10s + interval: 10s + timeout: 5s + retries: 3 + services: db: @@ -7,6 +15,7 @@ services: MYSQL_DATABASE: pwmgr MYSQL_USER: idpmgmt MYSQL_PASSWORD: idpmgmt + <<: *db_healthcheck testDb: image: mariadb:10 @@ -15,6 +24,7 @@ services: MYSQL_DATABASE: test MYSQL_USER: idpmgmt MYSQL_PASSWORD: idpmgmt + <<: *db_healthcheck phpmyadmin: image: phpmyadmin/phpmyadmin:latest @@ -32,8 +42,10 @@ services: ports: - "51155:80" depends_on: - - db - - email + db: + condition: service_healthy + email: + condition: service_started env_file: - ./common.env - ./local.env @@ -43,9 +55,12 @@ services: volumes: - ./application:/data depends_on: - - testDb - - zxcvbn - - broker + testDb: + condition: service_healthy + zxcvbn: + condition: service_started + broker: + condition: service_healthy env_file: - ./common.env - ./test.env @@ -62,8 +77,10 @@ services: - ./application:/data - ./dockerbuild/dev-ldap.conf:/etc/ldap/ldap.conf depends_on: - - testDb - - zxcvbn + db: + condition: service_healthy + zxcvbn: + condition: service_started env_file: - ./common.env - ./test.env @@ -80,8 +97,10 @@ services: - ./application:/data - ./dockerbuild/dev-ldap.conf:/etc/ldap/ldap.conf depends_on: - - testDb - - zxcvbn + testDb: + condition: service_healthy + zxcvbn: + condition: service_started env_file: - ./common.env - ./test.env @@ -98,6 +117,9 @@ services: volumes: - ./application:/data - ${COMPOSER_CACHE_DIR}:/composer + depends_on: + testDb: + condition: service_healthy working_dir: /data user: "${DOCKER_UIDGID}" env_file: @@ -108,8 +130,12 @@ services: email: image: silintl/email-service:1 depends_on: - - emaildb - - emailcron + emaildb: + condition: service_healthy + emailcron: + condition: service_started + environment: + MYSQL_HOST: emaildb env_file: - ./email.common.env - ./email.local.env @@ -119,7 +145,10 @@ services: emailcron: image: silintl/email-service:1 depends_on: - - emaildb + emaildb: + condition: service_healthy + environment: + MYSQL_HOST: emaildb env_file: - ./email.common.env - ./email.local.env @@ -131,6 +160,7 @@ services: - ./email.common.env environment: MYSQL_ROOT_PASSWORD: not-a-secret + <<: *db_healthcheck emailpma: image: phpmyadmin/phpmyadmin:latest @@ -151,6 +181,7 @@ services: MYSQL_DATABASE: broker MYSQL_USER: broker MYSQL_PASSWORD: broker + <<: *db_healthcheck brokerpma: image: phpmyadmin/phpmyadmin:latest @@ -166,7 +197,8 @@ services: ports: - "51154:80" depends_on: - - brokerDb + brokerDb: + condition: service_healthy volumes: - ./dockerbuild/broker/run-broker.sh:/data/run-broker.sh - ./dockerbuild/broker/m381901_235959_insert_test_data.php:/data/console/migrations/m381901_235959_insert_test_data.php @@ -200,11 +232,18 @@ services: ldap: build: ./dockerbuild/ldap + healthcheck: + test: nc -vz localhost 389 + start_period: 1s + interval: 1s + timeout: 5s + retries: 90 ldapload: build: ./dockerbuild/ldap depends_on: - - ldap + ldap: + condition: service_healthy working_dir: /data command: ./load_ldap.sh diff --git a/dockerbuild/broker/run-broker.sh b/dockerbuild/broker/run-broker.sh index c619f1ac..7e7c5ac2 100755 --- a/dockerbuild/broker/run-broker.sh +++ b/dockerbuild/broker/run-broker.sh @@ -17,7 +17,7 @@ composer install --no-interaction --no-scripts --no-progress rc=$?; if [[ $rc != 0 ]]; then exit $rc; fi # Try to run database migrations -whenavail $MYSQL_HOST 3306 100 ./yii migrate --interactive=0 +./yii migrate --interactive=0 # If that failed, exit. rc=$?; if [[ $rc != 0 ]]; then exit $rc; fi diff --git a/dockerbuild/ldap/load_ldap.sh b/dockerbuild/ldap/load_ldap.sh index c1dfbe03..ebd6cf13 100755 --- a/dockerbuild/ldap/load_ldap.sh +++ b/dockerbuild/ldap/load_ldap.sh @@ -1,22 +1,4 @@ #!/bin/bash -# Simple replication of whenavail script -TIMEOUT=10; - -while true; do - if nc -z -v ldap 389; then - break - fi - - if [[ $TIMEOUT == 0 ]]; then - exit 42 - else - TIMEOUT=$(( $TIMEOUT-1 )) - fi - - sleep 1 -done - - ldapadd -h ldap -p 389 -f /root/fakerep.ldif -x -c -D 'cn=Manager,dc=acme,dc=org' -w admin ldapadd -h ldap -p 389 -f /root/fakepeople.ldif -x -c -D 'cn=Manager,dc=acme,dc=org' -w admin diff --git a/email.common.env b/email.common.env index 11158a0c..2dace06c 100644 --- a/email.common.env +++ b/email.common.env @@ -10,7 +10,6 @@ MAILER_HOST=dummy MAILER_USERNAME=dummy MAILER_PASSWORD=dummy -MYSQL_HOST=emaildb MYSQL_DATABASE=email MYSQL_USER=email MYSQL_PASSWORD=email