add zte-virtul3 and zte-virtul4 to Doctor CI pods
[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 # Assigning Variables
20 command=$1
21 url=$2
22
23 function check() {
24
25     # Verify hosted
26     sleep 5
27     cmd=`curl -s --head  --request GET ${url} | grep '200 OK' > /dev/null`
28     rc=$?
29     echo $rc
30
31     if [[ $rc == 0 ]]
32     then
33         return 0
34     else
35         return 1
36     fi
37
38 }
39
40 echo "Getting contianer Id of the currently running one"
41 contId=$(sudo docker ps | grep "opnfv/testapi:latest" | awk '{print $1}')
42
43 echo "Pulling the latest image"
44 sudo docker pull opnfv/testapi:latest
45
46 echo "Deleting old containers of opnfv/testapi:old"
47 sudo docker ps -a | grep "opnfv/testapi" | grep "old" | awk '{print $1}' | xargs -r sudo docker rm -f
48
49 echo "Deleting old images of opnfv/testapi:latest"
50 sudo docker images | grep "opnfv/testapi" | grep "old" | awk '{print $3}' | xargs -r sudo docker rmi -f
51
52
53 if [[ -z "$contId" ]]
54 then
55     echo "No running testapi container"
56
57     echo "Removing stopped testapi containers in the previous iterations"
58     sudo docker ps -f status=exited | grep "opnfv_testapi" | awk '{print $1}' | xargs -r sudo docker rm -f
59 else
60     echo $contId
61
62     echo "Get the image id of the currently running conatiner"
63     currImgId=$(sudo docker ps | grep "$contId" | awk '{print $2}')
64     echo $currImgId
65
66     if [[ -z "$currImgId" ]]
67     then
68         echo "No image id found for the container id"
69         exit 1
70     fi
71
72     echo "Changing current image tag to old"
73     sudo docker tag "$currImgId" opnfv/testapi:old
74
75     echo "Removing stopped testapi containers in the previous iteration"
76     sudo docker ps -f status=exited | grep "opnfv_testapi" | awk '{print $1}' | xargs -r sudo docker rm -f
77
78     echo "Renaming the running container name to opnfv_testapi as to identify it."
79     sudo docker rename $contId opnfv_testapi
80
81     echo "Stop the currently running container"
82     sudo docker stop $contId
83 fi
84
85 echo "Running a container with the new image"
86 $command:latest
87
88 if check; then
89     echo "TestResults Hosted."
90 else
91     echo "TestResults Hosting Failed"
92     if [[ $(sudo docker images | grep "opnfv/testapi" | grep "old" | awk '{print $3}') ]]; then
93         echo "Running old Image"
94         $command:old
95         exit 1
96     fi
97 fi
98
99 # Echo Images and Containers
100 sudo docker images
101 sudo docker ps -a