blob: 400e38aa3ba2f35fdce1b846f89a9361b1e44343 [file] [log] [blame]
Jeremy Mordkoff50c2e862016-06-24 15:26:41 -04001# 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
20container_exists() {
21 if [ $# -ne 1 ]; then
22 FATAL "arg is container name"
23 fi
Jeremy Mordkoffa2813692016-08-04 19:46:55 -040024 lxc config show $1 >/dev/null 2>&1
Jeremy Mordkoff50c2e862016-06-24 15:26:41 -040025 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
34create_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"
garciadeblasf937a8b2016-07-21 14:53:32 +020040 lxc launch "$1" "$2"
41}
42
43create_privileged_container() {
44 if [ $# -ne 2 ]; then
45 FATAL "args are image container"
46 fi
47 INFO "creating container $2 using image $1"
48 DEBUG "lxc launch $1 $2"
49 lxc launch "$1" "$2" -c security.privileged=true
Jeremy Mordkoff50c2e862016-06-24 15:26:41 -040050}
51
52container_exec() {
53 container="$1"
54 shift
garciadeblasf937a8b2016-07-21 14:53:32 +020055 DEBUG "exec in $container \"$*\""
Jeremy Mordkoff50c2e862016-06-24 15:26:41 -040056 lxc exec "$container" -- $*
57}
Jeremy Mordkoffa2813692016-08-04 19:46:55 -040058container_exec_stderr() {
59 container="$1"
60 shift
61 DEBUG "exec in $container \"$*\""
62 lxc exec "$container" -- $* 2>&1
63}
Jeremy Mordkoff50c2e862016-06-24 15:26:41 -040064
65wait_container_up() {
66 [ $# -eq 1 ] || FATAL "arg is container name got $# args - $*"
67 ct=0
68 RE="1 received"
69 while [ $ct -lt 60 ]; do
70 let ct=ct+1
Jeremy Mordkoffa2813692016-08-04 19:46:55 -040071 output=$(container_exec_stderr "$1" ping -c 1 google.com)
Jeremy Mordkoff50c2e862016-06-24 15:26:41 -040072 if [[ $output =~ $RE ]]; then
73 DEBUG "$1 is up"
74 return
75 fi
76 INFO "waiting for container $1 to start"
77 DEBUG "expected '$RE' in $output"
78 sleep 1
79 done
80 FATAL "container $1 did not start"
81}
Jeremy Mordkoff5cf32162016-07-21 16:32:25 -040082container_push_tree() {
83 # create a tarball locally, pipe it into the container and unpack it there
84 [ $# -eq 3 ] || FATAL "args are container dir_from dir_to (dir_to MUST exist)"
85 tar -C "$2" -c . -f - | container_exec $1 tar -C "$3" -x -f -
86}
87
88container_push_devops() {
89 [ $# -eq 1 ] || FATAL "arg is container name got $# args - $*"
90 container_exec "$1" mkdir -p /root/devops
91 container_push_tree "$1" "$(dirname $OSM_JENKINS)" "/root/devops"
92}