#!/bin/bash # Deployment script for MSO lab # =================================================== # Available parameters : # # env DOCKER_HOST (optional) # | sets the docker host to be used if not local unix socket # # env MSO_CONFIG_UPDATES (optional) # | json structure that matches volumes/mso/chef-config/mso-docker.json # | elements whose value needs to be updated before the deployment # | phase. # # env MSO_DOCKER_IMAGE_VERSION # | json structure that matches volumes/mso/chef-config/mso-docker.json # | elements whose value needs to be updated before the deployment # | phase. ################################### Functions definition ################################ NEXUS=1 if [ "$#" = 0 ]; then echo "Deploying with local images, not pulling them from Nexus." NEXUS=0 fi if [ "$#" -ne 6 ] && [ $NEXUS -eq 1 ]; then echo "Usage: deploy.sh - env DOCKER_HOST (optional) sets the docker host to be used if not local unix socket - env MSO_DOCKER_IMAGE_VERSION (required) sets the mso docker image version - env MSO_CONFIG_UPDATES (optional) json structure that matches volumes/mso/chef-config/mso-docker.json elements whose value needs to be updated before the deployment phase." exit 1 fi if [ -z "$MSO_DOCKER_IMAGE_VERSION" ] && [ $NEXUS -eq 1 ]; then echo "Env variable MSO_DOCKER_IMAGE_VERSION must be SET to a version before running this script" exit 1 fi NEXUS_DOCKER_REPO_MSO=$1 NEXUS_USERNAME_MSO=$2 NEXUS_PASSWD_MSO=$3 NEXUS_DOCKER_REPO_MARIADB=$4 NEXUS_USERNAME_MARIADB=$5 NEXUS_PASSWD_MARIADB=$6 function init_docker_command() { if [ -z ${DOCKER_HOST+x} ]; then DOCKER_CMD="docker" LBL_DOCKER_HOST="local docker using unix socket" else DOCKER_CMD="docker -H ${DOCKER_HOST}" LBL_DOCKER_HOST="(remote) docker using ${DOCKER_HOST}" fi if [ -f "/opt/docker/docker-compose" ]; then DOCKER_COMPOSE_CMD="/opt/docker/docker-compose" else DOCKER_COMPOSE_CMD="docker-compose" fi echo "docker command: ${LBL_DOCKER_HOST}" } function container_name() { SERVICE=$1 BASE=$(echo $(basename `pwd`) | sed "s/[^a-z0-9]//i" | tr [:upper:] [:lower:]) echo ${BASE}_${SERVICE}_1 } function update_json_config() { if [ -n "$MSO_CONFIG_UPDATES" ]; then chmod u+x $SCRIPT_DIR/json_updater.py echo $MSO_CONFIG_UPDATES | $SCRIPT_DIR/json_updater.py $SCRIPT_DIR/volumes/mso/chef-config/mso-docker.json echo "MSO docker JSON updated" fi } function pull_docker_images() { echo "Using Nexus for MSO: $NEXUS_DOCKER_REPO_MSO (user "$NEXUS_USERNAME_MSO")" # login to nexus $DOCKER_CMD login -u $NEXUS_USERNAME_MSO -p $NEXUS_PASSWD_MSO $NEXUS_DOCKER_REPO_MSO $DOCKER_CMD login -u $NEXUS_USERNAME_MARIADB -p $NEXUS_PASSWD_MARIADB $NEXUS_DOCKER_REPO_MARIADB # get images $DOCKER_CMD pull $NEXUS_DOCKER_REPO_MSO/openecomp/mso:$MSO_DOCKER_IMAGE_VERSION $DOCKER_CMD tag $NEXUS_DOCKER_REPO_MSO/openecomp/mso:$MSO_DOCKER_IMAGE_VERSION openecomp/mso:latest $DOCKER_CMD rmi $NEXUS_DOCKER_REPO_MSO/openecomp/mso:$MSO_DOCKER_IMAGE_VERSION echo "Using Nexus for MARIADB: $NEXUS_DOCKER_REPO_MARIADB (user "$NEXUS_USERNAME_MARIADB")" $DOCKER_CMD pull $NEXUS_DOCKER_REPO_MARIADB/mariadb:10.1.11 $DOCKER_CMD tag $NEXUS_DOCKER_REPO_MARIADB/mariadb:10.1.11 mariadb:10.1.11 $DOCKER_CMD rmi $NEXUS_DOCKER_REPO_MARIADB/mariadb:10.1.11 } function wait_for_mariadb() { CONTAINER_NAME=$1 TIMEOUT=600 # wait for the real startup AMOUNT_STARTUP=$($DOCKER_CMD logs ${CONTAINER_NAME} 2>&1 | grep 'mysqld: ready for connections.' | wc -l) while [[ ${AMOUNT_STARTUP} -lt 2 ]]; do echo "Waiting for '$CONTAINER_NAME' deployment to finish ..." AMOUNT_STARTUP=$($DOCKER_CMD logs ${CONTAINER_NAME} 2>&1 | grep 'mysqld: ready for connections.' | wc -l) if [ "$TIMEOUT" = "0" ]; then echo "ERROR: Mariadb deployment failed." exit 1 fi let TIMEOUT-=5 sleep 5 done } ################################### Script entry - Starting CODE ################################ SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" init_docker_command update_json_config if [ $NEXUS -eq 1 ]; then pull_docker_images fi # don't remove the containers,no cleanup #$DOCKER_COMPOSE_CMD stop #$DOCKER_COMPOSE_CMD rm -f -v # deploy #Running docker-compose up -d starts the containers in the background and leaves them running. #If there are existing containers for a service, and the service’s configuration or image was changed after the container’s creation, docker-compose up picks up the changes by stopping and recreating the containers (preserving mounted volumes). To prevent Compose from picking up changes, use the --no-recreate flag. #If you want to force Compose to stop and recreate all containers, use the --force-recreate flag. $DOCKER_COMPOSE_CMD up -d --no-recreate mariadb CONTAINER_NAME=$(container_name mariadb) wait_for_mariadb $CONTAINER_NAME $DOCKER_COMPOSE_CMD up -d mso