Merge "bugfix: OperationFailure: the limit must be positive"
[releng.git] / jjb / releng / docker-deploy.sh
1 #!/bin/bash
2 #  Licensed to the Apache Software Foundation (ASF) under one   *
3 #  or more contributor license agreements.  See the NOTICE file *
4 #  distributed with this work for additional information        *
5 #  regarding copyright ownership.  The ASF licenses this file   *
6 #  to you under the Apache License, Version 2.0 (the            *
7 #  "License"); you may not use this file except in compliance   *
8 #  with the License.  You may obtain a copy of the License at   *
9 #                                                               *
10 #    http://www.apache.org/licenses/LICENSE-2.0                 *
11 #                                                               *
12 #  Unless required by applicable law or agreed to in writing,   *
13 #  software distributed under the License is distributed on an  *
14 #  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15 #  KIND, either express or implied.  See the License for the    *
16 #  specific language governing permissions and limitations      *
17 #  under the License.                                           *
18
19
20 command=$1
21 url=$2
22 module=$3
23
24 REPO="opnfv"
25 latest_image=$REPO/$module:latest
26 old_image=$REPO/$module:old
27 latest_container_name=$module
28 old_container_name=$module"_old"
29 latest_container_id=
30 old_container_id=
31 new_start_container=
32
33 function DEBUG() {
34   echo `date "+%Y-%m-%d %H:%M:%S.%N"` ": $1"
35 }
36
37 function check_connectivity() {
38     # check update status via test the connectivity of provide url
39     sleep 5
40     cmd=`curl -s --head  --request GET ${url} | grep '200 OK' > /dev/null`
41     rc=$?
42     DEBUG $rc
43     if [[ $rc == 0 ]]; then
44         return 0
45     else
46         return 1
47     fi
48 }
49
50
51 function pull_latest_image() {
52     DEBUG "pull latest image $latest_image"
53     docker pull $latest_image
54 }
55
56 function get_latest_running_container() {
57     latest_container_id=`docker ps -q --filter name=^/$latest_container_name$`
58 }
59
60 function get_old_running_container() {
61     old_container_id=`docker ps -q --filter name=^/$old_container_name$`
62 }
63
64 function delete_old_image() {
65     DEBUG "delete old image: $old_image"
66     docker rmi -f $old_image
67 }
68
69 function delete_old_container() {
70     DEBUG "delete old container: $old_container_name"
71     docker ps -a -q --filter name=^/$old_container_name$ | xargs docker rm -f &>/dev/null
72 }
73
74 function delete_latest_container() {
75     DEBUG "delete latest container: $module"
76     docker ps -a -q --filter name=^/$latest_container_name$ | xargs docker rm -f &>/dev/null
77 }
78
79 function delete_latest_image() {
80     DEBUG "delete latest image: $REPO/$module:latest"
81     docker rmi -f $latest_image
82 }
83
84 function change_image_tag_2_old() {
85     DEBUG "change image tag 2 old"
86     docker tag $latest_image $old_image
87     docker rmi -f $latest_image
88 }
89
90 function mark_latest_container_2_old() {
91     DEBUG "mark latest container to be old"
92     docker rename "$latest_container_name" "$old_container_name"
93 }
94
95 function stop_old_container() {
96     DEBUG "stop old container"
97     docker stop "$old_container_name"
98 }
99
100 function run_latest_image() {
101     new_start_container=`$command`
102     DEBUG "run latest image: $new_start_container"
103 }
104
105 get_latest_running_container
106 get_old_running_container
107
108 if [[ ! -z $latest_container_id ]]; then
109     DEBUG "latest container is running: $latest_container_id"
110     delete_old_container
111     delete_old_image
112     change_image_tag_2_old
113     mark_latest_container_2_old
114     pull_latest_image
115     stop_old_container
116     run_latest_image
117
118 elif [[ ! -z $old_container_id ]]; then
119     DEBUG "old container is running: $old_container_id"
120     delete_latest_container
121     delete_latest_image
122     pull_latest_image
123     stop_old_container
124     run_latest_image
125 else
126     DEBUG "no container is running"
127     delete_old_container
128     delete_old_image
129     delete_latest_container
130     delete_latest_image
131     pull_latest_image
132     run_latest_image
133 fi
134
135 if check_connectivity; then
136     DEBUG "CONGRATS: $module update successfully"
137 else
138     DEBUG "ATTENTION: $module update failed"
139     id=`docker ps -a -q --filter name=^/$old_container_name$`
140     if [[ ! -z $id ]]; then
141         DEBUG "start old container instead"
142         docker stop $new_start_container
143         docker start $id
144     fi
145     if ! check_connectivity; then
146         DEBUG "BIG ISSUE: no container is running normally"
147     fi
148     exit 1
149 fi
150
151 docker images
152 docker ps -a