~ubuntu-branches/ubuntu/precise/lxc/precise-updates

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/bash

# (C) Copyright Canonical 2011,2012

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

usage() {
    echo "Usage: lxc-wait --name=NAME --state=STATE"
}

help() {
    usage
    echo
    echo "lxc-wait waits for NAME container state to reach STATE"
    echo
    echo "Options :"
    echo "   -n, --name=NAME   NAME for name of the container"
    echo "   -s, --state=STATE ORed states to wait for"
    echo "                     STOPPED, STARTING, RUNNING, STOPPING,"
    echo "                     ABORTING, FREEZING, FROZEN"
}

set -e

valid_states=("STOPPED" "STARTING" "RUNNING" "STOPPING"
"ABORTING" "FREEZING" "FROZEN" "THAWED" "MAX_STATE")

container_exists() {
    local c=$1
    local list=`lxc-ls -1`
    local name

    for name in $list; do
        if [ "$c" = "$name" ]; then
            echo "yes"
        fi
        echo "no"
    done
}

verify_state() {
    local s="$1"
    local i

    for((i=0;i<${#valid_states[@]};i++)); do
        if [ "$s" = "${valid_states[$i]}" ]; then
            echo "ok"; return;
        fi
    done
    echo "bad"; return;
}
        
badstate=""
verify_states() {
    local states=$1
    local s
    local v

    for s in $states; do
        echo "verifying $s"
        v=$(verify_state "$s")
        if [ $v = "bad" ]; then
            echo "bad";
            badstate="$s"
            return;
        fi
    done
    echo "ok";
}

state_is_in() {
    local state=$1
    local states=$2
    local s

    for s in $states; do
        if [ "$state" = "$s" ]; then
            echo "yes"; return;
        fi
    done
    echo "no"
}

shortoptions='hn:s:'
longoptions='help,name:,states:'

getopt=$(getopt -o $shortoptions --longoptions  $longoptions -- "$@")
if [ $? != 0 ]; then
    usage
    exit 1;
fi

eval set -- "$getopt"

states="RUNNING"

while true; do
    case "$1" in
    -h|--help)
        help
        exit 1
        ;;
    -n|--name)
        shift
        lxc_name=$1
        shift
        ;;
    -s|--states)
        shift
        states=$1
        shift
        ;;
    --)
        shift
        break;;
    *)
        echo $1
        usage
        exit 1
        ;;
    esac
done

if [ -z "$lxc_name" ]; then
    echo "no container name specified"
    usage
    exit 1
fi

if [ "$(id -u)" != "0" ]; then
   echo "This command has to be run as root"
   exit 1
fi

type lxc-info > /dev/null || { echo "lxc-info not found."; exit 1; }

v=$(container_exists $lxc_name)
if [ "$v" = "no" ]; then
    echo "Container $lxc_name does not exist"
    exit 1
fi
states2="`echo $states | sed -e 's/|/ /g'`"
v=$(verify_states "${states2}")
if [ "$v" = "bad" ]; then
   echo "invalid state $badstate in provided set"
   exit 1
fi
	

while [ 1 ]; do
    o=`lxc-info -s -n $lxc_name`
    s=`echo $o | awk '{ print $2 }'`
    if [ $(state_is_in "$s" "${states2}") = "yes" ]; then
        break;
    fi
    sleep 1
done

exit 0