89db8bab5411b60d1a23a3e2f3955c22c5f2ce68
[osm/devops.git] / jenkins / common / container
1 # This file is meant to be SOURCED
2 #
3 #   Copyright 2016 RIFT.IO Inc
4 #
5 #   Licensed under the Apache License, Version 2.0 (the "License");
6 #   you may not use this file except in compliance with the License.
7 #   You may obtain a copy of the License at
8 #
9 #       http://www.apache.org/licenses/LICENSE-2.0
10 #
11 #   Unless required by applicable law or agreed to in writing, software
12 #   distributed under the License is distributed on an "AS IS" BASIS,
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 #   See the License for the specific language governing permissions and
15 #   limitations under the License.
16 #
17 # container_funcs
18 # 24 June 2016 -- Jeremy Mordkoff -- Genesis
19
20 container_exists() { 
21         if [ $# -ne 1 ]; then
22                 FATAL "arg is container name"
23         fi
24         lxc config show $1 2>&1 >/dev/null
25         if [ $? -eq 0 ]; then
26                 DEBUG "container $1 exists"
27                 return 0
28         else
29                 DEBUG "container $1 not found"
30                 return 1
31         fi
32 }
33
34 create_container() { 
35         if [ $# -ne 2 ]; then
36                 FATAL "args are image container"
37         fi
38         INFO "creating container $2 using image $1"
39         DEBUG "lxc launch $1 $2"
40         sudo lxc launch "$1" "$2"
41 }
42
43 container_exec() { 
44         container="$1"
45         shift
46         DEBUG "exec in $1 \"$*\""
47         lxc exec "$container" -- $*
48 }
49
50 wait_container_up() {
51         [ $# -eq 1 ] || FATAL "arg is container name got $# args - $*"
52         ct=0
53         RE="1 received"
54         while [ $ct -lt 60 ]; do
55                 let ct=ct+1
56                 output=$(container_exec "$1" ping -c 1 google.com)
57                 if [[ $output =~ $RE ]]; then   
58                         DEBUG "$1 is up"
59                         return
60                 fi
61                 INFO "waiting for container $1 to start"
62                 DEBUG "expected '$RE' in $output"
63                 sleep 1
64         done
65         FATAL "container $1 did not start"
66 }