Change deploy.sh permission to be executable
[so/docker-config.git] / deploy.sh
1 #!/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
20
21 if [ "$#" -ne 6 ]; then
22             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>
23                   - env DOCKER_HOST (optional) 
24                      sets the docker host to be used if not local unix socket 
25          
26                           - env MSO_DOCKER_IMAGE_VERSION (required)
27                      sets the mso docker image version
28          
29                   - env MSO_CONFIG_UPDATES (optional) 
30                     json structure that matches volumes/mso/chef-config/mso-docker.json 
31                     elements whose value needs to be updated before the deployment 
32                     phase."
33             
34             exit 1
35 fi
36 if [ -z "$MSO_DOCKER_IMAGE_VERSION" ]; then   
37             echo "Env variable MSO_DOCKER_IMAGE_VERSION must be SET to a version before running this script" 
38             exit 1
39 fi
40
41 NEXUS_DOCKER_REPO_MSO=$1
42 NEXUS_USERNAME_MSO=$2
43 NEXUS_PASSWD_MSO=$3
44 NEXUS_DOCKER_REPO_MARIADB=$4
45 NEXUS_USERNAME_MARIADB=$5
46 NEXUS_PASSWD_MARIADB=$6
47
48
49
50 function init_docker_command() {
51         if [ -z ${DOCKER_HOST+x} ];
52         then
53             DOCKER_CMD="docker"
54             LBL_DOCKER_HOST="local docker using unix socket"
55         else
56             DOCKER_CMD="docker -H ${DOCKER_HOST}"
57             LBL_DOCKER_HOST="(remote) docker using ${DOCKER_HOST}"  
58         fi
59
60         if [ -f "/opt/docker/docker-compose" ];
61         then
62         DOCKER_COMPOSE_CMD="/opt/docker/docker-compose"
63         else
64             DOCKER_COMPOSE_CMD="docker-compose"
65         fi
66
67         echo "docker command: ${LBL_DOCKER_HOST}"
68 }
69
70 function container_name() {
71     SERVICE=$1
72     BASE=$(echo $(basename `pwd`) | sed "s/[^a-z0-9]//i" | tr [:upper:] [:lower:])
73     echo ${BASE}_${SERVICE}_1
74 }
75
76 function update_json_config() {
77         if [ -n "$MSO_CONFIG_UPDATES" ];
78         then   
79             chmod u+x $SCRIPT_DIR/json_updater.py
80             echo $MSO_CONFIG_UPDATES | $SCRIPT_DIR/json_updater.py $SCRIPT_DIR/volumes/mso/chef-config/mso-docker.json
81             echo "MSO docker JSON updated"
82         fi
83         
84 }
85
86 function pull_docker_images() {
87         echo "Using Nexus for MSO: $NEXUS_DOCKER_REPO_MSO (user "$NEXUS_USERNAME_MSO")"
88         # login to nexus
89         $DOCKER_CMD login -u $NEXUS_USERNAME_MSO -p $NEXUS_PASSWD_MSO $NEXUS_DOCKER_REPO_MSO
90         $DOCKER_CMD login -u $NEXUS_USERNAME_MARIADB -p $NEXUS_PASSWD_MARIADB $NEXUS_DOCKER_REPO_MARIADB
91         
92         # get images
93         $DOCKER_CMD pull $NEXUS_DOCKER_REPO_MSO/openecomp/mso:$MSO_DOCKER_IMAGE_VERSION
94         $DOCKER_CMD tag $NEXUS_DOCKER_REPO_MSO/openecomp/mso:$MSO_DOCKER_IMAGE_VERSION openecomp/mso:latest
95         $DOCKER_CMD rmi $NEXUS_DOCKER_REPO_MSO/openecomp/mso:$MSO_DOCKER_IMAGE_VERSION
96         
97         echo "Using Nexus for MARIADB: $NEXUS_DOCKER_REPO_MARIADB (user "$NEXUS_USERNAME_MARIADB")"
98         $DOCKER_CMD pull $NEXUS_DOCKER_REPO_MARIADB/mariadb:10.1.11
99         $DOCKER_CMD tag  $NEXUS_DOCKER_REPO_MARIADB/mariadb:10.1.11  mariadb:10.1.11
100         $DOCKER_CMD rmi $NEXUS_DOCKER_REPO_MARIADB/mariadb:10.1.11
101
102 }
103
104 function wait_for_mariadb() {
105     CONTAINER_NAME=$1
106    
107     TIMEOUT=120
108     
109     # wait for the real startup
110     AMOUNT_STARTUP=$($DOCKER_CMD logs ${CONTAINER_NAME} 2>&1 | grep 'mysqld: ready for connections.' | wc -l)
111     while [[ ${AMOUNT_STARTUP} -lt 2 ]];
112     do
113         echo "Waiting for '$CONTAINER_NAME' deployment to finish ..."
114         AMOUNT_STARTUP=$($DOCKER_CMD logs ${CONTAINER_NAME} 2>&1 | grep 'mysqld: ready for connections.' | wc -l)
115         if [ "$TIMEOUT" = "0" ];
116         then
117             echo "ERROR: Mariadb deployment failed."
118             exit 1
119         fi
120         let TIMEOUT-=1
121         sleep 1       
122     done
123 }
124
125 ################################### Script entry - Starting CODE ################################
126 SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
127
128 init_docker_command
129 update_json_config
130 pull_docker_images
131
132 # don't remove the containers,no cleanup
133 #$DOCKER_COMPOSE_CMD stop 
134 #$DOCKER_COMPOSE_CMD rm -f -v
135
136 # deploy
137 #Running docker-compose up -d starts the containers in the background and leaves them running.
138 #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.
139 #If you want to force Compose to stop and recreate all containers, use the --force-recreate flag.
140 $DOCKER_COMPOSE_CMD up -d --no-recreate mariadb 
141 CONTAINER_NAME=$(container_name mariadb)
142 wait_for_mariadb $CONTAINER_NAME
143 $DOCKER_COMPOSE_CMD up -d mso