diff --git a/repo-template/node-based-repo/d/actions-base.Dockerfile b/repo-template/node-based-repo/fab/d/actions-base.Dockerfile similarity index 100% rename from repo-template/node-based-repo/d/actions-base.Dockerfile rename to repo-template/node-based-repo/fab/d/actions-base.Dockerfile diff --git a/repo-template/node-based-repo/d/actions-build.Dockerfile b/repo-template/node-based-repo/fab/d/actions-build.Dockerfile similarity index 100% rename from repo-template/node-based-repo/d/actions-build.Dockerfile rename to repo-template/node-based-repo/fab/d/actions-build.Dockerfile diff --git a/repo-template/node-based-repo/fab/d/docker-compose.yaml b/repo-template/node-based-repo/fab/d/docker-compose.yaml new file mode 100644 index 0000000..073f40a --- /dev/null +++ b/repo-template/node-based-repo/fab/d/docker-compose.yaml @@ -0,0 +1,24 @@ +services: + dt-api: + image: repo2.hub.gmetri.io/gmetrivr/basin:node-22-dev-v2 + container_name: dt_dt-api + ports: + - "4205:4205" + volumes: + - .:/src + command: ["sleep", "inf"] + privileged: true + environment: + #Common Env Vars, PUBLIC_ vars may be exposed to the window also + - PUBLIC_IS_LOCAL=true #Used to check if running in developer machine + - PUBLIC_BUILD_VERSION=local #Used for unique CDN paths on evey build + - PUBLIC_NAMESPACE=local #Used to check environment (is "prod" in production) + - PUBLIC_REPO=dt-api #Repo name + #Repo Specific Env Vars + #If using dev environment + - PORT=4205 + +networks: + default: + name: dt + external: true diff --git a/repo-template/node-based-repo/fab/entryPoint.sh b/repo-template/node-based-repo/fab/entryPoint.sh new file mode 100755 index 0000000..f8935f7 --- /dev/null +++ b/repo-template/node-based-repo/fab/entryPoint.sh @@ -0,0 +1,6 @@ +#!/bin/sh +echo "Image ENTRYPOINT executing as user `whoami` in directory `pwd`" + +set -x +#Run command passed by docker exec/run as arguments (else the default CMD gets executed) +sh -c "$*" diff --git a/repo-template/node-based-repo/fab/sh/compose_down.sh b/repo-template/node-based-repo/fab/sh/compose_down.sh new file mode 100755 index 0000000..7e111c3 --- /dev/null +++ b/repo-template/node-based-repo/fab/sh/compose_down.sh @@ -0,0 +1,9 @@ +#!/bin/sh +. ./fab/sh/constants.sh + +set -x +docker compose \ + -f fab/d/docker-compose.yaml \ + --project-name ${REPO_NAME} \ + --project-directory ${REPO_FOLDER} \ + down diff --git a/repo-template/node-based-repo/fab/sh/compose_up.sh b/repo-template/node-based-repo/fab/sh/compose_up.sh new file mode 100755 index 0000000..37ddb44 --- /dev/null +++ b/repo-template/node-based-repo/fab/sh/compose_up.sh @@ -0,0 +1,41 @@ +#!/bin/sh + +if [ -z $1 ] +then + COMMAND="bash" +else + COMMAND="${@}" +fi + +. ./fab/sh/constants.sh +. ./fab/sh/docker_network_create.sh + +#Make more verbose now +set -x +docker compose \ + -f fab/d/docker-compose.yaml \ + -p ${REPO_NAME} \ + --project-directory ${REPO_FOLDER} \ + ps --services --filter status=running | grep $REPO_NAME > /dev/null +#$? is 0 if already running, 1 if not (0=no error) +ALREADY_RUNNING=$? +#Make less verbose now +set +x + +if [ "$ALREADY_RUNNING" -eq 0 ]; +then + echo "Service already running, only opening shell" +else + docker compose \ + -f fab/d/docker-compose.yaml \ + --project-name ${REPO_NAME} \ + --project-directory ${REPO_FOLDER} \ + up -d +fi + +echo "Connecting to docker shell and running command $COMMAND..." +docker compose \ + -f fab/d/docker-compose.yaml \ + --project-name ${REPO_NAME} \ + --project-directory ${REPO_FOLDER} \ + exec $REPO_NAME $COMMAND \ No newline at end of file diff --git a/repo-template/node-based-repo/fab/sh/constants.sh b/repo-template/node-based-repo/fab/sh/constants.sh new file mode 100644 index 0000000..8fb4075 --- /dev/null +++ b/repo-template/node-based-repo/fab/sh/constants.sh @@ -0,0 +1,8 @@ +#!/bin/sh +export REPO_BASE=gmetrivr +export REPO_NAME=dt-api +export PARENT_PROJECT=dt #Controls which projects close together +export REPO_FOLDER=`git rev-parse --show-toplevel` +export SHORT_REF=`git rev-parse --short HEAD` +export BUILD_VERSION="b-$SHORT_REF" +export TAG_BASE=base-v2 diff --git a/repo-template/node-based-repo/fab/sh/docker_build_base.sh b/repo-template/node-based-repo/fab/sh/docker_build_base.sh new file mode 100755 index 0000000..3803037 --- /dev/null +++ b/repo-template/node-based-repo/fab/sh/docker_build_base.sh @@ -0,0 +1,11 @@ +#!/bin/sh +#Build and push the dev (or build) image for the project use both for local development +# and to build the project remotely +#The prod image (alpine based) is usually different from the dev/build image (ubuntu based). +. ./fab/sh/constants.sh + +cd ${REPO_FOLDER} +docker build --tag ${GMETRI_DREPO}/${REPO_BASE}/${REPO_NAME}:${TAG_BASE} \ + -f fab/d/actions-base.Dockerfile ./fab/context/ + +docker push ${GMETRI_DREPO}/${REPO_BASE}/${REPO_NAME}:${TAG_BASE} diff --git a/repo-template/node-based-repo/fab/sh/docker_network_create.sh b/repo-template/node-based-repo/fab/sh/docker_network_create.sh new file mode 100755 index 0000000..685bfda --- /dev/null +++ b/repo-template/node-based-repo/fab/sh/docker_network_create.sh @@ -0,0 +1,18 @@ +#!/bin/sh +#Externalizing the network creation step allows multiple docker composes to independently connect +#to the same network without worrying about start order + +. ./fab/sh/constants.sh + +NETWORK_EXISTS=0 +#This is necessary instead of using $? (previous command exit code) as we are set -e mode, +#which exists the script on any error +docker network ls | grep ${PARENT_PROJECT} || NETWORK_EXISTS=1 +#0 if already exists, 1 if doesn't exist (0=no error) + +if [ "$NETWORK_EXISTS" -eq 0 ]; +then + echo "Network exists" +else + docker network create --attachable ${PARENT_PROJECT} || true +fi diff --git a/repo-template/node-based-repo/fab/sh/wait-for-it.sh b/repo-template/node-based-repo/fab/sh/wait-for-it.sh new file mode 100755 index 0000000..071c2be --- /dev/null +++ b/repo-template/node-based-repo/fab/sh/wait-for-it.sh @@ -0,0 +1,178 @@ +#!/usr/bin/env bash +# Use this script to test if a given TCP host/port are available + +WAITFORIT_cmdname=${0##*/} + +echoerr() { if [[ $WAITFORIT_QUIET -ne 1 ]]; then echo "$@" 1>&2; fi } + +usage() +{ + cat << USAGE >&2 +Usage: + $WAITFORIT_cmdname host:port [-s] [-t timeout] [-- command args] + -h HOST | --host=HOST Host or IP under test + -p PORT | --port=PORT TCP port under test + Alternatively, you specify the host and port as host:port + -s | --strict Only execute subcommand if the test succeeds + -q | --quiet Don't output any status messages + -t TIMEOUT | --timeout=TIMEOUT + Timeout in seconds, zero for no timeout + -- COMMAND ARGS Execute command with args after the test finishes +USAGE + exit 1 +} + +wait_for() +{ + if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then + echoerr "$WAITFORIT_cmdname: waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT" + else + echoerr "$WAITFORIT_cmdname: waiting for $WAITFORIT_HOST:$WAITFORIT_PORT without a timeout" + fi + WAITFORIT_start_ts=$(date +%s) + while : + do + if [[ $WAITFORIT_ISBUSY -eq 1 ]]; then + nc -z $WAITFORIT_HOST $WAITFORIT_PORT + WAITFORIT_result=$? + else + (echo > /dev/tcp/$WAITFORIT_HOST/$WAITFORIT_PORT) >/dev/null 2>&1 + WAITFORIT_result=$? + fi + if [[ $WAITFORIT_result -eq 0 ]]; then + WAITFORIT_end_ts=$(date +%s) + echoerr "$WAITFORIT_cmdname: $WAITFORIT_HOST:$WAITFORIT_PORT is available after $((WAITFORIT_end_ts - WAITFORIT_start_ts)) seconds" + break + fi + sleep 1 + done + return $WAITFORIT_result +} + +wait_for_wrapper() +{ + # In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692 + if [[ $WAITFORIT_QUIET -eq 1 ]]; then + timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --quiet --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT & + else + timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT & + fi + WAITFORIT_PID=$! + trap "kill -INT -$WAITFORIT_PID" INT + wait $WAITFORIT_PID + WAITFORIT_RESULT=$? + if [[ $WAITFORIT_RESULT -ne 0 ]]; then + echoerr "$WAITFORIT_cmdname: timeout occurred after waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT" + fi + return $WAITFORIT_RESULT +} + +# process arguments +while [[ $# -gt 0 ]] +do + case "$1" in + *:* ) + WAITFORIT_hostport=(${1//:/ }) + WAITFORIT_HOST=${WAITFORIT_hostport[0]} + WAITFORIT_PORT=${WAITFORIT_hostport[1]} + shift 1 + ;; + --child) + WAITFORIT_CHILD=1 + shift 1 + ;; + -q | --quiet) + WAITFORIT_QUIET=1 + shift 1 + ;; + -s | --strict) + WAITFORIT_STRICT=1 + shift 1 + ;; + -h) + WAITFORIT_HOST="$2" + if [[ $WAITFORIT_HOST == "" ]]; then break; fi + shift 2 + ;; + --host=*) + WAITFORIT_HOST="${1#*=}" + shift 1 + ;; + -p) + WAITFORIT_PORT="$2" + if [[ $WAITFORIT_PORT == "" ]]; then break; fi + shift 2 + ;; + --port=*) + WAITFORIT_PORT="${1#*=}" + shift 1 + ;; + -t) + WAITFORIT_TIMEOUT="$2" + if [[ $WAITFORIT_TIMEOUT == "" ]]; then break; fi + shift 2 + ;; + --timeout=*) + WAITFORIT_TIMEOUT="${1#*=}" + shift 1 + ;; + --) + shift + WAITFORIT_CLI=("$@") + break + ;; + --help) + usage + ;; + *) + echoerr "Unknown argument: $1" + usage + ;; + esac +done + +if [[ "$WAITFORIT_HOST" == "" || "$WAITFORIT_PORT" == "" ]]; then + echoerr "Error: you need to provide a host and port to test." + usage +fi + +WAITFORIT_TIMEOUT=${WAITFORIT_TIMEOUT:-15} +WAITFORIT_STRICT=${WAITFORIT_STRICT:-0} +WAITFORIT_CHILD=${WAITFORIT_CHILD:-0} +WAITFORIT_QUIET=${WAITFORIT_QUIET:-0} + +# check to see if timeout is from busybox? +WAITFORIT_TIMEOUT_PATH=$(type -p timeout) +WAITFORIT_TIMEOUT_PATH=$(realpath $WAITFORIT_TIMEOUT_PATH 2>/dev/null || readlink -f $WAITFORIT_TIMEOUT_PATH) +if [[ $WAITFORIT_TIMEOUT_PATH =~ "busybox" ]]; then + WAITFORIT_ISBUSY=1 + WAITFORIT_BUSYTIMEFLAG="-t" + +else + WAITFORIT_ISBUSY=0 + WAITFORIT_BUSYTIMEFLAG="" +fi + +if [[ $WAITFORIT_CHILD -gt 0 ]]; then + wait_for + WAITFORIT_RESULT=$? + exit $WAITFORIT_RESULT +else + if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then + wait_for_wrapper + WAITFORIT_RESULT=$? + else + wait_for + WAITFORIT_RESULT=$? + fi +fi + +if [[ $WAITFORIT_CLI != "" ]]; then + if [[ $WAITFORIT_RESULT -ne 0 && $WAITFORIT_STRICT -eq 1 ]]; then + echoerr "$WAITFORIT_cmdname: strict mode, refusing to execute subprocess" + exit $WAITFORIT_RESULT + fi + exec "${WAITFORIT_CLI[@]}" +else + exit $WAITFORIT_RESULT +fi