| Jeremy Mordkoff | 50c2e86 | 2016-06-24 15:26:41 -0400 | [diff] [blame] | 1 | # This file is meant to be SOURCED |
| 2 | # |
| 3 | # Copyright 2016 RIFT.IO Inc |
| garciadeblas | ee04b45 | 2016-09-19 18:07:40 +0200 | [diff] [blame] | 4 | # Copyright 2016 Telefónica Investigación y Desarrollo S.A.U. |
| Jeremy Mordkoff | 50c2e86 | 2016-06-24 15:26:41 -0400 | [diff] [blame] | 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # 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, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # |
| 18 | # container_funcs |
| 19 | # 24 June 2016 -- Jeremy Mordkoff -- Genesis |
| garciadeblas | ee04b45 | 2016-09-19 18:07:40 +0200 | [diff] [blame] | 20 | # -- Gerardo García |
| Jeremy Mordkoff | 50c2e86 | 2016-06-24 15:26:41 -0400 | [diff] [blame] | 21 | |
| 22 | container_exists() { |
| 23 | if [ $# -ne 1 ]; then |
| 24 | FATAL "arg is container name" |
| 25 | fi |
| Jeremy Mordkoff | a281369 | 2016-08-04 19:46:55 -0400 | [diff] [blame] | 26 | lxc config show $1 >/dev/null 2>&1 |
| Jeremy Mordkoff | 50c2e86 | 2016-06-24 15:26:41 -0400 | [diff] [blame] | 27 | if [ $? -eq 0 ]; then |
| 28 | DEBUG "container $1 exists" |
| 29 | return 0 |
| 30 | else |
| 31 | DEBUG "container $1 not found" |
| 32 | return 1 |
| 33 | fi |
| 34 | } |
| 35 | |
| 36 | create_container() { |
| garciadeblas | ee04b45 | 2016-09-19 18:07:40 +0200 | [diff] [blame] | 37 | if [ $# -lt 2 ]; then |
| 38 | FATAL "args are image container [options]" |
| Jeremy Mordkoff | 50c2e86 | 2016-06-24 15:26:41 -0400 | [diff] [blame] | 39 | fi |
| 40 | INFO "creating container $2 using image $1" |
| garciadeblas | ee04b45 | 2016-09-19 18:07:40 +0200 | [diff] [blame] | 41 | image=$1 |
| 42 | container=$2 |
| 43 | shift 2 |
| 44 | DEBUG "lxc launch $image $container $*" |
| garciadeblas | db26f3c | 2016-09-20 16:46:16 +0200 | [diff] [blame] | 45 | lxc launch "$image" "$container" $* |
| Jeremy Mordkoff | 50c2e86 | 2016-06-24 15:26:41 -0400 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | container_exec() { |
| 49 | container="$1" |
| 50 | shift |
| garciadeblas | f937a8b | 2016-07-21 14:53:32 +0200 | [diff] [blame] | 51 | DEBUG "exec in $container \"$*\"" |
| Jeremy Mordkoff | 50c2e86 | 2016-06-24 15:26:41 -0400 | [diff] [blame] | 52 | lxc exec "$container" -- $* |
| 53 | } |
| garciadeblas | db26f3c | 2016-09-20 16:46:16 +0200 | [diff] [blame] | 54 | |
| Jeremy Mordkoff | a281369 | 2016-08-04 19:46:55 -0400 | [diff] [blame] | 55 | container_exec_stderr() { |
| 56 | container="$1" |
| 57 | shift |
| 58 | DEBUG "exec in $container \"$*\"" |
| 59 | lxc exec "$container" -- $* 2>&1 |
| 60 | } |
| Jeremy Mordkoff | 50c2e86 | 2016-06-24 15:26:41 -0400 | [diff] [blame] | 61 | |
| 62 | wait_container_up() { |
| prithiv | 5afdf8d | 2017-02-03 12:12:12 +0100 | [diff] [blame] | 63 | [ $# -eq 1 ] || FATAL "arg is container name got $# args - $*" |
| 64 | RE="200" |
| 65 | ct=0 |
| 66 | while [ $ct -lt 10 ]; do |
| 67 | let ct=ct+1 |
| 68 | output=$(container_exec_stderr "$1" curl -sL -w "%{http_code}\\n" "http://www.google.com/" -o /dev/null) |
| 69 | if [[ $output =~ $RE ]]; then |
| 70 | DEBUG "$1 is up" |
| 71 | return |
| 72 | fi |
| 73 | INFO "waiting for container $1 to start" |
| 74 | DEBUG "expected '$RE' in $output" |
| 75 | sleep 5 |
| 76 | done |
| 77 | FATAL "container $1 did not start" |
| Jeremy Mordkoff | 50c2e86 | 2016-06-24 15:26:41 -0400 | [diff] [blame] | 78 | } |
| Jeremy Mordkoff | 5cf3216 | 2016-07-21 16:32:25 -0400 | [diff] [blame] | 79 | container_push_tree() { |
| 80 | # create a tarball locally, pipe it into the container and unpack it there |
| 81 | [ $# -eq 3 ] || FATAL "args are container dir_from dir_to (dir_to MUST exist)" |
| 82 | tar -C "$2" -c . -f - | container_exec $1 tar -C "$3" -x -f - |
| 83 | } |
| 84 | |
| 85 | container_push_devops() { |
| 86 | [ $# -eq 1 ] || FATAL "arg is container name got $# args - $*" |
| 87 | container_exec "$1" mkdir -p /root/devops |
| 88 | container_push_tree "$1" "$(dirname $OSM_JENKINS)" "/root/devops" |
| 89 | } |