blob: d785713cf3bbc603cacab7796902415a3e6a3639 [file] [log] [blame]
piclosec4e65032017-02-21 14:52:45 +01001#!/bin/bash
2# Deployment script for MSO lab
3# ===================================================
4# Available parameters :
5#
6# env DOCKER_HOST (optional)
7# | sets the docker host to be used if not local unix socket
8#
9# env MSO_CONFIG_UPDATES (optional)
10# | json structure that matches volumes/mso/chef-config/mso-docker.json
11# | elements whose value needs to be updated before the deployment
12# | phase.
13#
14# env MSO_DOCKER_IMAGE_VERSION
15# | json structure that matches volumes/mso/chef-config/mso-docker.json
16# | elements whose value needs to be updated before the deployment
17# | phase.
18################################### Functions definition ################################
19
Determe, Sebastien (sd378r)7f036c82017-03-09 09:42:46 -080020NEXUS=1
Alexis de Talhouëtc1570952017-03-08 11:40:51 -050021if [ "$#" = 0 ]; then
Determe, Sebastien (sd378r)7f036c82017-03-09 09:42:46 -080022 echo "Deploying with local images, not pulling them from Nexus."
23 NEXUS=0
Alexis de Talhouëtc1570952017-03-08 11:40:51 -050024fi
Determe, Sebastien (sd378r)7f036c82017-03-09 09:42:46 -080025
26if [ "$#" -ne 6 ] && [ $NEXUS -eq 1 ]; then
27 echo "Usage: deploy.sh <NEXUS_HOST_MSO:NEXUS_PORT_MSO> <NEXUS_LOGIN_MSO> <NEXUS_PASSWORD_MSO> <NEXUS_HOST_MARIADB:NEXUS_PORT_MARIADB> <NEXUS_LOGIN_MARIADB> <NEXUS_PASSWORD_MARIADB>
28 - env DOCKER_HOST (optional)
29 sets the docker host to be used if not local unix socket
30
31 - env MSO_DOCKER_IMAGE_VERSION (required)
32 sets the mso docker image version
33
34 - env MSO_CONFIG_UPDATES (optional)
35 json structure that matches volumes/mso/chef-config/mso-docker.json
36 elements whose value needs to be updated before the deployment
37 phase."
38
39 exit 1
piclosec4e65032017-02-21 14:52:45 +010040fi
Determe, Sebastien (sd378r)7f036c82017-03-09 09:42:46 -080041
42if [ -z "$MSO_DOCKER_IMAGE_VERSION" ] && [ $NEXUS -eq 1 ]; then
43 echo "Env variable MSO_DOCKER_IMAGE_VERSION must be SET to a version before running this script"
44 exit 1
piclosec4e65032017-02-21 14:52:45 +010045fi
46
47NEXUS_DOCKER_REPO_MSO=$1
48NEXUS_USERNAME_MSO=$2
49NEXUS_PASSWD_MSO=$3
50NEXUS_DOCKER_REPO_MARIADB=$4
51NEXUS_USERNAME_MARIADB=$5
52NEXUS_PASSWD_MARIADB=$6
53
54
55
56function init_docker_command() {
Determe, Sebastien (sd378r)7f036c82017-03-09 09:42:46 -080057 if [ -z ${DOCKER_HOST+x} ];
58 then
59 DOCKER_CMD="docker"
60 LBL_DOCKER_HOST="local docker using unix socket"
61 else
62 DOCKER_CMD="docker -H ${DOCKER_HOST}"
63 LBL_DOCKER_HOST="(remote) docker using ${DOCKER_HOST}"
64 fi
piclosec4e65032017-02-21 14:52:45 +010065
Determe, Sebastien (sd378r)7f036c82017-03-09 09:42:46 -080066 if [ -f "/opt/docker/docker-compose" ];
67 then
68 DOCKER_COMPOSE_CMD="/opt/docker/docker-compose"
69 else
70 DOCKER_COMPOSE_CMD="docker-compose"
71 fi
piclosec4e65032017-02-21 14:52:45 +010072
Determe, Sebastien (sd378r)7f036c82017-03-09 09:42:46 -080073 echo "docker command: ${LBL_DOCKER_HOST}"
piclosec4e65032017-02-21 14:52:45 +010074}
75
76function container_name() {
77 SERVICE=$1
78 BASE=$(echo $(basename `pwd`) | sed "s/[^a-z0-9]//i" | tr [:upper:] [:lower:])
79 echo ${BASE}_${SERVICE}_1
80}
81
82function update_json_config() {
Determe, Sebastien (sd378r)7f036c82017-03-09 09:42:46 -080083 if [ -n "$MSO_CONFIG_UPDATES" ];
84 then
85 chmod u+x $SCRIPT_DIR/json_updater.py
86 echo $MSO_CONFIG_UPDATES | $SCRIPT_DIR/json_updater.py $SCRIPT_DIR/volumes/mso/chef-config/mso-docker.json
87 echo "MSO docker JSON updated"
88 fi
89
piclosec4e65032017-02-21 14:52:45 +010090}
91
92function pull_docker_images() {
Determe, Sebastien (sd378r)7f036c82017-03-09 09:42:46 -080093 echo "Using Nexus for MSO: $NEXUS_DOCKER_REPO_MSO (user "$NEXUS_USERNAME_MSO")"
94 # login to nexus
95 $DOCKER_CMD login -u $NEXUS_USERNAME_MSO -p $NEXUS_PASSWD_MSO $NEXUS_DOCKER_REPO_MSO
96 $DOCKER_CMD login -u $NEXUS_USERNAME_MARIADB -p $NEXUS_PASSWD_MARIADB $NEXUS_DOCKER_REPO_MARIADB
97
98 # get images
99 $DOCKER_CMD pull $NEXUS_DOCKER_REPO_MSO/openecomp/mso:$MSO_DOCKER_IMAGE_VERSION
100 $DOCKER_CMD tag $NEXUS_DOCKER_REPO_MSO/openecomp/mso:$MSO_DOCKER_IMAGE_VERSION openecomp/mso:latest
101 $DOCKER_CMD rmi $NEXUS_DOCKER_REPO_MSO/openecomp/mso:$MSO_DOCKER_IMAGE_VERSION
102
103 echo "Using Nexus for MARIADB: $NEXUS_DOCKER_REPO_MARIADB (user "$NEXUS_USERNAME_MARIADB")"
104 $DOCKER_CMD pull $NEXUS_DOCKER_REPO_MARIADB/mariadb:10.1.11
105 $DOCKER_CMD tag $NEXUS_DOCKER_REPO_MARIADB/mariadb:10.1.11 mariadb:10.1.11
106 $DOCKER_CMD rmi $NEXUS_DOCKER_REPO_MARIADB/mariadb:10.1.11
piclosec4e65032017-02-21 14:52:45 +0100107
108}
109
110function wait_for_mariadb() {
111 CONTAINER_NAME=$1
112
Kanagaraj Manickam k00365106801e3762017-06-13 19:47:56 +0530113 TIMEOUT=600
piclosec4e65032017-02-21 14:52:45 +0100114
115 # wait for the real startup
116 AMOUNT_STARTUP=$($DOCKER_CMD logs ${CONTAINER_NAME} 2>&1 | grep 'mysqld: ready for connections.' | wc -l)
Seshu-Kumar-M8d73a692017-10-17 18:06:26 +0530117 while [[ ${AMOUNT_STARTUP} -lt 1 ]];
piclosec4e65032017-02-21 14:52:45 +0100118 do
Determe, Sebastien (sd378r)7f036c82017-03-09 09:42:46 -0800119 echo "Waiting for '$CONTAINER_NAME' deployment to finish ..."
120 AMOUNT_STARTUP=$($DOCKER_CMD logs ${CONTAINER_NAME} 2>&1 | grep 'mysqld: ready for connections.' | wc -l)
121 if [ "$TIMEOUT" = "0" ];
122 then
123 echo "ERROR: Mariadb deployment failed."
124 exit 1
125 fi
Kanagaraj Manickam k00365106801e3762017-06-13 19:47:56 +0530126 let TIMEOUT-=5
127 sleep 5
piclosec4e65032017-02-21 14:52:45 +0100128 done
129}
130
131################################### Script entry - Starting CODE ################################
132SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
133
134init_docker_command
135update_json_config
Determe, Sebastien (sd378r)7f036c82017-03-09 09:42:46 -0800136if [ $NEXUS -eq 1 ]; then
137 pull_docker_images
Alexis de Talhouëtc1570952017-03-08 11:40:51 -0500138fi
piclosec4e65032017-02-21 14:52:45 +0100139
140# don't remove the containers,no cleanup
141#$DOCKER_COMPOSE_CMD stop
142#$DOCKER_COMPOSE_CMD rm -f -v
143
144# deploy
145#Running docker-compose up -d starts the containers in the background and leaves them running.
146#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.
147#If you want to force Compose to stop and recreate all containers, use the --force-recreate flag.
148$DOCKER_COMPOSE_CMD up -d --no-recreate mariadb
149CONTAINER_NAME=$(container_name mariadb)
150wait_for_mariadb $CONTAINER_NAME
Determe, Sebastien (sd378r)7f036c82017-03-09 09:42:46 -0800151$DOCKER_COMPOSE_CMD up -d mso