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 *
10 # http://www.apache.org/licenses/LICENSE-2.0 *
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. *
25 latest_image=$REPO/$module:latest
26 old_image=$REPO/$module:old
27 latest_container_name=$module
28 old_container_name=$module"_old"
34 echo `date "+%Y-%m-%d %H:%M:%S.%N"` ": $1"
37 function check_connectivity() {
38 # check update status via test the connectivity of provide url
40 cmd=`curl -s --head --request GET ${url} | grep '200 OK' > /dev/null`
43 if [[ $rc == 0 ]]; then
51 function pull_latest_image() {
52 DEBUG "pull latest image $latest_image"
53 docker pull $latest_image
56 function get_latest_running_container() {
57 latest_container_id=`docker ps -q --filter name=^/$latest_container_name$`
60 function get_old_running_container() {
61 old_container_id=`docker ps -q --filter name=^/$old_container_name$`
64 function delete_old_image() {
65 DEBUG "delete old image: $old_image"
66 docker rmi -f $old_image
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
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
79 function delete_latest_image() {
80 DEBUG "delete latest image: $REPO/$module:latest"
81 docker rmi -f $latest_image
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
90 function mark_latest_container_2_old() {
91 DEBUG "mark latest container to be old"
92 docker rename "$latest_container_name" "$old_container_name"
95 function stop_old_container() {
96 DEBUG "stop old container"
97 docker stop "$old_container_name"
100 function run_latest_image() {
101 new_start_container=`$command`
102 DEBUG "run latest image: $new_start_container"
105 get_latest_running_container
106 get_old_running_container
108 if [[ ! -z $latest_container_id ]]; then
109 DEBUG "latest container is running: $latest_container_id"
112 change_image_tag_2_old
113 mark_latest_container_2_old
118 elif [[ ! -z $old_container_id ]]; then
119 DEBUG "old container is running: $old_container_id"
120 delete_latest_container
126 DEBUG "no container is running"
129 delete_latest_container
135 if check_connectivity; then
136 DEBUG "CONGRATS: $module update successfully"
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
145 if ! check_connectivity; then
146 DEBUG "BIG ISSUE: no container is running normally"