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