diff --git a/services/base-image/Dockerfile b/services/base-image/Dockerfile index 8d64dbb69..e0726cdbd 100644 --- a/services/base-image/Dockerfile +++ b/services/base-image/Dockerfile @@ -1,13 +1,13 @@ FROM alpine:3.14 ENV SHELL=/bin/bash WORKDIR / -COPY services/base-image/contents /transferred +COPY contents /transferred -RUN apk update &&\ - apk add bash cmake git build-base autoconf libtool pkgconfig boost-dev fmt &&\ - /transferred/install_grpc.sh +RUN apk update +RUN apk add bash cmake git build-base autoconf libtool pkgconfig boost-dev fmt +RUN /transferred/install_grpc.sh CMD /bin/bash diff --git a/services/base-image/config.sh b/services/base-image/config.sh deleted file mode 100755 index 823a2fca9..000000000 --- a/services/base-image/config.sh +++ /dev/null @@ -1,6 +0,0 @@ -#/bin/bash - -set -e - -IMAGE_NAME="commapp/services-base" -VERSION="1.0" diff --git a/services/docker-compose.yml b/services/docker-compose.yml new file mode 100644 index 000000000..49ea08041 --- /dev/null +++ b/services/docker-compose.yml @@ -0,0 +1,33 @@ +version: "3.9" +services: + base: + build: base-image + image: commapp/services-base:1.0 + tunnelbroker-base: + build: + dockerfile: services/tunnelbroker/docker-base/Dockerfile + context: ../ + image: commapp/tunnelbroker-base:1.0 + tunnelbroker-server: + build: + dockerfile: services/tunnelbroker/docker-server/Dockerfile + context: ../ + image: commapp/tunnelbroker-server:1.0 + container_name: tunnelbroker-server + ports: + - "${COMM_SERVICES_PORT_TUNNELBROKER}:50051" + backup-base: + build: + dockerfile: services/backup/docker-base/Dockerfile + context: ../ + image: commapp/backup-base:1.0 + backup-server: + build: + dockerfile: services/backup/docker-server/Dockerfile + context: ../ + image: commapp/backup-server:1.0 + container_name: backup-server + ports: + - "${COMM_SERVICES_PORT_BACKUP}:50051" + volumes: + - $HOME/.aws/credentials:/root/.aws/credentials:ro diff --git a/services/package.json b/services/package.json index 35dee6c40..ff1973612 100644 --- a/services/package.json +++ b/services/package.json @@ -1,13 +1,14 @@ { "name": "services", "version": "1.0.0", "private": true, "license": "BSD-3-Clause", "scripts": { - "build-base-image": "cd .. && ./services/scripts/build_base_image.sh ./services/base-image", - "build-tunnelbroker-base": "cd .. && ./services/scripts/build_base_image.sh ./services/tunnelbroker/docker-base", - "run-tunnelbroker-server": "cd .. && ./services/scripts/run_server_image.sh ./services/tunnelbroker/docker-server", - "build-backup-base": "cd .. && ./services/scripts/build_base_image.sh ./services/backup/docker-base", - "run-backup-server": "cd .. && ./services/scripts/run_server_image.sh ./services/backup/docker-server" + "build-all": "docker-compose build", + "build-base-image": "docker-compose build base", + "build-tunnelbroker-base": "docker-compose build tunnelbroker-base", + "run-tunnelbroker-server": "./scripts/run_server_image.sh tunnelbroker", + "build-backup-base": "docker-compose build backup-base", + "run-backup-server": "./scripts/run_server_image.sh backup" } } diff --git a/services/scripts/build_base_image.sh b/services/scripts/build_base_image.sh deleted file mode 100755 index 5b4a2f833..000000000 --- a/services/scripts/build_base_image.sh +++ /dev/null @@ -1,47 +0,0 @@ -#/bin/bash - -set -e - -# this script should be run from the comm's root directory - -if [ "$#" -ne 1 ]; then - echo "Illegal number of parameters, expected one argument with a path to a directory containing Dockerfile and a config file" - exit 1; -fi - -GIVEN_PATH=$1 -CONFIG_PATH=$GIVEN_PATH/config.sh -DOCKERFILE_PATH=$GIVEN_PATH/Dockerfile - -if [ ! -f $CONFIG_PATH ]; then - echo "Config file not found in $GIVEN_PATH, it has to be named \`config.sh\`" - exit 1; -fi - -if [ ! -f $DOCKERFILE_PATH ]; then - echo "Dockerfile not found in $GIVEN_PATH" - exit 1; -fi - -echo "reading configuration from $GIVEN_PATH" -source $CONFIG_PATH - -IMAGES_IDS=$(docker images -f "reference=$IMAGE_NAME" -q) -NIMAGES=$(docker images -f "reference=$IMAGE_NAME" -q | wc -l | sed 's/ //g') - -if [[ $NIMAGES -gt 1 ]] -then - echo "There's more than one image '$IMAGE_NAME'. Due to possible large sizes of images creating a new one will be aborted now. Please remove images manually and run this script again." - echo "You can run:" - echo "docker rmi $(echo $IMAGES_IDS)" - exit 1; -elif [[ $NIMAGES -eq 1 ]] -then - echo "An image $IMAGE_NAME already exists. A new one will not be created, the current one will be used." - echo "If you encounter errors, please remove this image manually and re-run this script." - echo "To remove this image, you can run:" - echo "docker rmi $(echo $IMAGES_IDS)" -else - echo "Creating a new image $IMAGE_NAME" - docker build -t $IMAGE_NAME:$VERSION -f $DOCKERFILE_PATH . -fi diff --git a/services/scripts/port_config.sh b/services/scripts/port_config.sh new file mode 100755 index 000000000..ad963e162 --- /dev/null +++ b/services/scripts/port_config.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +set -e + +export COMM_SERVICES_PORT_TUNNELBROKER=50051 +export COMM_SERVICES_PORT_BACKUP=50052 diff --git a/services/scripts/run_server_image.sh b/services/scripts/run_server_image.sh index ddee1176e..18beef68e 100755 --- a/services/scripts/run_server_image.sh +++ b/services/scripts/run_server_image.sh @@ -1,65 +1,36 @@ -#/bin/bash +#!/bin/bash set -e -# this script should be run from the comm's root directory - if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then echo "Illegal number of parameters, expected:" - echo "- one argument with a path to a directory containing Dockerfile and a config file" - echo "- one optional argument `port`" - echo "- example: `./services/scripts/run_server_image.sh ./services/tunnelbroker/docker-server 12345`" - exit 1; -fi - -PORT="50051" -if [ ! -z "$2" ]; then - PORT=$2 -fi - -re='^[0-9]+$' -if [[ ! "$PORT" =~ $re ]]; then - echo "given port value [$PORT] is not a number, aborting" - exit 1; -fi - -GIVEN_PATH=$1 -CONFIG_PATH=$GIVEN_PATH/config.sh -DOCKERFILE_PATH=$GIVEN_PATH/Dockerfile - -if [ ! -f $CONFIG_PATH ]; then - echo "Config file not found in $GIVEN_PATH, it has to be named \`config.sh\`" + echo "- one argument with a name of the service, currently available services:" + ls | grep -vE 'base-image|docker.compose.yml|package.json|scripts' + echo "- one optional argument with port" + echo "- example: ./services/scripts/run_server_image.sh tunnelbroker 12345" exit 1; fi -if [ ! -f $DOCKERFILE_PATH ]; then - echo "Dockerfile not found in $GIVEN_PATH" - exit 1; +. ./scripts/port_config.sh + +SERVICE=$1 +PORT=0 +if [ "$1" == "tunnelbroker" ]; then + if [ ! -z "$2" ]; then + export COMM_SERVICES_PORT_TUNNELBROKER=$2 + fi + PORT=$COMM_SERVICES_PORT_TUNNELBROKER +elif [ "$1" == "backup" ]; then + if [ ! -z "$2" ]; then + export COMM_SERVICES_PORT_BACKUP=$2 + fi + PORT=$COMM_SERVICES_PORT_BACKUP +else + echo "No such service ${SERVICE}, aborting" + exit 1 fi -echo "reading configuration from $GIVEN_PATH" -source $CONFIG_PATH +echo "${SERVICE} service will run at port ${PORT}" -CONTAINERS_IDS=$(docker ps -aq -f ancestor=$IMAGE_NAME:$VERSION) -NCONTAINERS=$(docker ps -aq -f ancestor=$IMAGE_NAME:$VERSION | wc -l | sed 's/ //g') - -if [[ $NCONTAINERS -gt 1 ]] -then - echo "There's more than one container of '$IMAGE_NAME' image. Due to possible large sizes of containers creating a new one will be aborted now. Please remove containers manually and run this script again." - echo "You can run:" - echo "docker stop $(echo $CONTAINERS_IDS) && docker rm $(echo $CONTAINERS_IDS)" - exit 0; -elif [[ $NCONTAINERS -eq 1 ]] -then - echo "A container of image $IMAGE_NAME already exists(container id: $CONTAINERS_IDS)." - echo "Overwriting an existing image" - docker stop $(echo $CONTAINERS_IDS) && docker rm $(echo $CONTAINERS_IDS) - docker rmi $IMAGE_NAME:$VERSION - - docker build -t $IMAGE_NAME:$VERSION -f $DOCKERFILE_PATH . - docker run -it -p $PORT:50051 $EXTRA_ARGS $IMAGE_NAME:$VERSION -else - echo "Creating a new container for image $IMAGE_NAME" - docker build -t $IMAGE_NAME:$VERSION -f $DOCKERFILE_PATH . - docker run -it -p $PORT:50051 $EXTRA_ARGS $IMAGE_NAME:$VERSION -fi +docker-compose build $SERVICE-server +docker-compose up $SERVICE-server