diff --git a/.github/workflows/build-using-cnb-buildpack.yml b/.github/workflows/build-using-cnb-buildpack.yml index 88ee19c..2448e7d 100644 --- a/.github/workflows/build-using-cnb-buildpack.yml +++ b/.github/workflows/build-using-cnb-buildpack.yml @@ -9,11 +9,14 @@ on: - "v?[0-9]+.[0-9]+.[0-9]+*" env: CNB_IMAGE_NAME: cnb-app + SERVICE_NAME: 'mongodb' SERVICE_VERSION: '6.0' + SERVICE_IMAGE: "mongo:7.0.14" SERVICE_PORT: 27017 # Default value: 27017 SERVICE_USERNAME: '22032e25-4aba-417f-a394-8bbd78d920cd' SERVICE_PASSWORD: 'StbQ4EovUpwQjD0cT1Hr7PKBG' DATABASE_NAME: my-mongo-db + DEBUG: 0 jobs: package-app: @@ -40,65 +43,31 @@ jobs: - name: Setup pack for ${{ github.repository }} uses: buildpacks/github-actions/setup-pack@v5.0.0 - - - name: Start MongoDB testing server - uses: supercharge/mongodb-github-action@1.11.0 - with: - mongodb-version: ${{ env.SERVICE_VERSION }} - mongodb-port: ${{env.SERVICE_PORT}} - mongodb-username: ${{env.SERVICE_USERNAME}} - mongodb-password: ${{env.SERVICE_PASSWORD}} - mongodb-db: ${{env.DATABASE_NAME}} -# mongodb-container-name: mongodb-${{ env.SERVICE_VERSION }} +# - +# name: Start MongoDB testing server +# uses: supercharge/mongodb-github-action@1.11.0 +# with: +# mongodb-version: ${{ env.SERVICE_VERSION }} +# mongodb-port: ${{env.SERVICE_PORT}} +# mongodb-username: ${{env.SERVICE_USERNAME}} +# mongodb-password: ${{env.SERVICE_PASSWORD}} +# mongodb-db: ${{env.DATABASE_NAME}} +## mongodb-container-name: mongodb-${{ env.SERVICE_VERSION }} - name: Packaging app from ${{ github.repository }} pushed by ${{ github.actor }} shell: bash run: | echo "Pack Build ${{env.CNB_IMAGE_NAME}} (${{github.repository}})" - pack build ${{env.CNB_IMAGE_NAME}} --builder paketobuildpacks/builder-jammy-base + pack build ${{env.CNB_IMAGE_NAME}} echo "Pack Inspect ${{env.CNB_IMAGE_NAME}} (${{github.repository}})" pack inspect ${{env.CNB_IMAGE_NAME}} - name: Testing app shell: bash run: | - cat vcap-services-template.sh|envsubst >vcap-service.env - cat vcap-service.env - echo "Starting sample app" - container_id=$(docker run -d -e PORT=80 --env-file vcap-service.env -p 8080:80 --name "cnb-app-container" ${{env.CNB_IMAGE_NAME}}) - echo "Cnb app started (id: $container_id)" - docker inspect -f '{{.HostConfig.LogConfig.Type}}' $container_id - # docker run -e PORT=80 --env-file vcap-service.env -p 8080:80 --name "cnb-app-container" ${{env.CNB_IMAGE_NAME}} - docker logs -f cnb-app-container &> cnb-app-container.log & - docker logs mongodb - cat /etc/hosts - docker ps -a - env|sort - export APP="http://127.0.0.1:8080" - exit_status=0 - if nc -vz 127.0.0.1 8080;then echo "port 8080 available";else echo "port 8080 UNAVAILABLE";exit_status=1;fi - if nc -vz 127.0.0.1 ${SERVICE_PORT};then echo "port ${SERVICE_PORT} available";else echo "port ${SERVICE_PORT} UNAVAILABLE";exit_status=1;fi - if mongosh --version;then echo "mongosh available";else echo "mongosh UNAVAILABLE";fi - echo "Create using 'curl -X POST $APP/myCollection'" - if ! curl -vvv -X POST $APP/myCollection -H 'Content-Length: 0';then - echo "Create failed" - exit_status=1 - else - echo "Create successful" - fi - echo "Delete using 'curl -X DELETE $APP/myCollection'" - if ! curl -vvv -X DELETE $APP/myCollection;then - echo "Delete failed" - exit_status=1 - else - echo "Delete successful" - fi - cat cnb-app-container.log - echo "Dumping logs using docker logs cnb-app-container" - docker logs cnb-app-container - ls -lrt - exit $exit_status + ./setup-prerequisite.sh + ./run-tests.sh - name: Tag version ${{github.ref_name}} if: github.ref_type == 'tag' diff --git a/.gitignore b/.gitignore index edd5904..84bba53 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ /vendor /tmp .idea/ +*.log +*.iml +/vcap-service.env diff --git a/Gemfile.lock b/Gemfile.lock index 0843c91..23ce7ef 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -48,6 +48,7 @@ GEM nenv (~> 0.1) shellany (~> 0.0) openssl (3.0.2) + ostruct (0.6.0) pry (0.14.2) coderay (~> 1.1) method_source (~> 1.0) @@ -100,6 +101,7 @@ DEPENDENCIES guard guard-rspec mongo + ostruct (~> 0.6.0) rack-test rspec sinatra diff --git a/local.env b/local.env new file mode 100644 index 0000000..00a0c77 --- /dev/null +++ b/local.env @@ -0,0 +1,9 @@ +export CNB_IMAGE_NAME=mongodb-cnb-app +export SERVICE_NAME=mongodb +export SERVICE_HEALTH_CMD=mongo +export SERVICE_PORT=27001 +export DATABASE_NAME=myCollection +export SERVICE_IMAGE="mongo:7.0.14" +export SERVICE_HOST="dynamically_generated" +export SERVICE_PASSWORD=my-passwwwwwwwword15; +#export DEBUG=0 \ No newline at end of file diff --git a/project.toml b/project.toml new file mode 100644 index 0000000..df09a97 --- /dev/null +++ b/project.toml @@ -0,0 +1,13 @@ +[_] +schema-version = "0.2" +id = "com.orange-cloudfoundry.sample-apps.mongodb" +name = "MongoDB sample app" +#version = "1.0.0" + +[io.buildpacks] +builder = "paketobuildpacks/builder-jammy-base" +exclude = [ + ".github", + "renovate.json", + "README.md" +] diff --git a/run-tests.sh b/run-tests.sh new file mode 100755 index 0000000..be9f535 --- /dev/null +++ b/run-tests.sh @@ -0,0 +1,79 @@ +#!/usr/bin/env bash +set -e + +./vcap-services-template-reformat.sh +cat vcap-service.env +echo "Starting sample app" +container_name="cnb-app-container" +container_id=$(docker run -d --rm -e PORT=80 -p 8080:80 --env-file vcap-service.env --name "$container_name" ${CNB_IMAGE_NAME}) + +echo "Cnb app started (id: $container_id)" +echo "Waiting to ensure app is up and running" +while [ "$( docker container inspect -f '{{.State.Status}}' $container_name )" != "running" ]; do + echo "waiting for $container_name to be running current: $(docker inspect -f '{{.State.Status}}' $container_name)" + sleep 1 +done +sleep 3 # to ensure app is up and running +#docker inspect -f '{{.HostConfig.LogConfig.Type}}' $container_id +service_container_name="$(docker ps -f "ancestor=$SERVICE_IMAGE" --format "{{.Names}}")" +CONTAINER_APP_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $container_name) +echo "CONTAINER_APP_IP: $CONTAINER_APP_IP" + +if [ "$DEBUG" -eq 1 ]; then + echo "----------------- $container_name --------------------" + docker inspect -f '{{json .NetworkSettings}}' $container_name + + echo "----------------- $SERVICE_NAME: $service_container_name --------------------" + docker inspect -f '{{json .NetworkSettings}}' "$service_container_name" +fi +echo "=== Redirect logs to cnb-app-container.log ===" +docker logs -f cnb-app-container &> cnb-app-container.log & +docker ps -a + +echo "=== Check connectivity ===" +if nc -vz 127.0.0.1 8080;then echo "port 8080 available";else echo "port 8080 UNAVAILABLE";exit_status=1;fi +if nc -vz 127.0.0.1 "${SERVICE_PORT}";then echo "port ${SERVICE_PORT} available";else echo "port ${SERVICE_PORT} UNAVAILABLE";exit_status=1;fi +#echo "Checking redis server 127.0.0.1 : PING ==> $(redis-cli -a ${SERVICE_PASSWORD} -h 127.0.0.1 -p ${SERVICE_PORT} --no-auth-warning ping)" +#echo "Checking redis server $SERVICE_HOST : PING ==> $(redis-cli -a ${SERVICE_PASSWORD} -h ${SERVICE_HOST} -p ${SERVICE_PORT} --no-auth-warning ping)" + +function check_service() { + type="$1" + cmd="$2" + cmd_prefix="$3" + if [ -z "$cmd_prefix" ];then + cmd_prefix="curl" + fi + status=0 + echo "$type using > $cmd <" 1>&2 + if ! $cmd;then + echo "" 1>&2 + echo "$type failed: retry in verbose mode" 1>&2 + $cmd_prefix -vvv ${cmd##$cmd_prefix} + status=1 + else + echo "" 1>&2 + echo "$type successful" 1>&2 + fi + echo "check_service - status: $status" 1>&2 + return $status +} +export APP="http://127.0.0.1:8080" +set +e +exit_status=0 +create_service_output="$(check_service "Create" "curl -sSLf -X POST $APP/myCollection -H 'Content-Length: 0' )" +create_service=$? + +#get_service_output="$(check_service "Get" "curl -sSLf -X GET $APP/foo" )" +#get_service=$? + +delete_service_output="$(check_service "Delete" "curl -sSLf DELETE $APP/myCollection" )" +delete_service=$? +set -e + +exit_status=$(($create_service + $delete_service)) +echo "exit status: $exit_status" +echo "======================================================================================================" +echo "Dumping logs using docker logs cnb-app-container" +docker logs cnb-app-container 2>&1 +ls -lrt *.log +exit $exit_status diff --git a/setup-prerequisite.sh b/setup-prerequisite.sh new file mode 100755 index 0000000..9a2b3df --- /dev/null +++ b/setup-prerequisite.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +set -e + +service_container_id=$(docker run -d -p $SERVICE_PORT:$SERVICE_PORT \ + -e MONGO_INITDB_ROOT_USERNAME="$SERVICE_USERNAME" \ + -e MONGO_INITDB_ROOT_PASSWORD="$SERVICE_PASSWORD" \ + -e MONGO_INITDB_DATABASE="$DATABASE_NAME" \ + --name "$SERVICE_NAME-service" \ + --health-cmd "$SERVICE_HEALTH_CMD" \ + --health-interval 10s --health-timeout 5s --health-retries 5 \ + ${SERVICE_IMAGE}) +service_container_name="$(docker ps -f "ancestor=$SERVICE_IMAGE" --format "{{.Names}}")" +SERVICE_CONTAINER_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$service_container_name") +echo "$SERVICE_CONTAINER_IP: $SERVICE_CONTAINER_IP" +export SERVICE_HOST=$SERVICE_CONTAINER_IP +docker exec -i "$service_container_name" 'if mongosh --version;then echo "mongosh available";else echo "mongosh UNAVAILABLE";fi' +echo "SERVICE_HOST: $SERVICE_HOST" diff --git a/start-cnd-app-locally.sh b/start-cnd-app-locally.sh new file mode 100755 index 0000000..d5cb470 --- /dev/null +++ b/start-cnd-app-locally.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +set -e + +for c in cnb-app-container mongodb-service;do + if docker container stop $c;then + docker container rm -f $c + fi +done + +source local.env +source ./setup-prerequisite.sh +./run-tests.sh + + + +#./vcap-services-template-reformat.sh >vcap-service.env +#cat vcap-service.env +#echo "" +#echo "Starting app using ${CNB_IMAGE_NAME}" +##docker run -it --rm -e PORT=8081 --env-file vcap-service.env -p 8080:8081 --name "cnb-app" ${CNB_IMAGE_NAME} +#docker run --rm -e PORT=80 --env-file vcap-service.env -p 8080:80 --name "cnb-app" ${CNB_IMAGE_NAME} +##docker run -d --rm -e PORT=8081 --env-file vcap-service.env -p 8080:8081 --name "cnb-app" ${CNB_IMAGE_NAME} \ No newline at end of file diff --git a/vcap-services-template-reformat.sh b/vcap-services-template-reformat.sh new file mode 100755 index 0000000..143ec02 --- /dev/null +++ b/vcap-services-template-reformat.sh @@ -0,0 +1 @@ +echo VCAP_SERVICES="$(cat vcap-services-template.json|jq -c '.'|envsubst)" > vcap-service.env \ No newline at end of file diff --git a/vcap-services-template.sh b/vcap-services-template.json old mode 100755 new mode 100644 similarity index 81% rename from vcap-services-template.sh rename to vcap-services-template.json index 43487d5..9017f2e --- a/vcap-services-template.sh +++ b/vcap-services-template.json @@ -1,4 +1,3 @@ -VCAP_SERVICES=' { "mongodb-dedicated":[ { @@ -6,9 +5,9 @@ VCAP_SERVICES=' "binding_name":null, "credentials":{ "database":"${DATABASE_NAME}", - "hostname":"localhost:${SERVICE_PORT}", + "hostname":"${SERVICE_HOST}:${SERVICE_PORT}", "password":"${SERVICE_PASSWORD}", - "uri":"mongodb://${SERVICE_USERNAME}:${SERVICE_PASSWORD}@localhost:${SERVICE_PORT}/${DATABASE_NAME}?replicaSet=rs0", + "uri":"mongodb://${SERVICE_USERNAME}:${SERVICE_PASSWORD}@${SERVICE_HOST}:${SERVICE_PORT}/${DATABASE_NAME}?replicaSet=rs0", "username":"${SERVICE_USERNAME}" }, "instance_guid":"cc252ef5-2541-49fd-8a57-fe73957905a5", @@ -22,8 +21,7 @@ VCAP_SERVICES=' "mongodb", "document" ], - "volume_mounts":[] + "volume_mounts": [ ] } ] } -' \ No newline at end of file