aboutsummaryrefslogtreecommitdiffstats
path: root/deploy.sh
blob: a7d7c37edd4046115daeb2af6acec395c7606d80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/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 ################################


if [ "$#" -ne 6 ]; then
	    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>
	          - 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" ]; 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=120
    
    # 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-=1
	sleep 1       
    done
}

################################### Script entry - Starting CODE ################################
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

init_docker_command
update_json_config
pull_docker_images

# 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