fa73cad44a62e056b75176ab167231451e751f2f
[tests.git] / system / lxc-driver.sh
1 #!/bin/bash
2
3 function sense_all () {
4
5     for i in $(lxc-ls -1|sort|uniq); do 
6         [ "$(lxc-info -n $i | grep state| awk '{print $2;}' )" == "RUNNING" ] && echo "$i;$(lxc-info -n $i | grep pid | awk '{print $2;}');$(cat /var/lib/lxc/$i/$i.timestamp)" || :
7     done    
8 }
9
10 function start_all () {
11
12     for i in $(lxc-ls -1|sort|uniq); do 
13         [ "$(lxc-info -n $i | grep state| awk '{print $2;}' )" != "RUNNING" ] && lxc-start -d -n $i || :
14         lxc-wait -n $i -s RUNNING
15     done
16    
17     #sense_all
18 }
19
20 function stop_all () {
21    
22     for i in $(lxc-ls -1|sort|uniq); do
23         [ "$(lxc-info -n $i | grep state| awk '{print $2;}' )" != "STOPPED" ] && lxc-stop -n $i
24         lxc-wait -n $i -s STOPPED
25     done
26     
27     sense_all
28 }
29
30 function sense_lxc () {
31
32     lxc=$1; shift
33     [ "$(lxc-info -n $lxc | grep state | awk '{print $2;}')" == "RUNNING" ] && echo "$lxc;$(lxc-info -n $lxc | grep pid | awk '{print $2;}');$(cat /var/lib/lxc/$lxc/$lxc.timestamp)" || :
34 }
35
36 function start_lxc () {
37
38     lxc=$1; shift
39     [ "$(lxc-info -n $lxc | grep state| awk '{print $2;}' )" != "RUNNING" ] && lxc-start -d -n $lxc ||:
40     lxc-wait -n $lxc -s RUNNING
41     
42     sense_lxc $lxc
43 }
44
45 function stop_lxc () {
46
47     lxc=$1; shift
48     [ "$(lxc-info -n $lxc | grep state| awk '{print $2;}' )" != "STOPPED" ] && lxc-stop -n $lxc
49     lxc-wait -n $lxc -s STOPPED
50
51     sense_lxc $lxc
52 }
53
54 function restart_all () {
55
56     stop_all 
57     start_all
58 }
59
60 function restart_lxc () {
61
62     lxc=$1; shift
63     stop_lxc $lxc
64     start_lxc $lxc
65 }
66
67 function destroy_all () {
68     
69     stop_all
70     for i in $(lxc-ls -1|sort|uniq); do
71         lxc-destroy -n $i
72     done
73
74 }
75
76 function destroy_lxc () {
77
78     lxc=$1; shift
79     stop_lxc $lxc
80     lxc-destroy -n $lxc
81 }
82
83 function usage () {
84     echo "Usage: lxc-driver.sh [options]"
85     echo "Description:"
86     echo "   This command is used to manage and retreive information on existing lxc containers "
87     echo "lxc-driver.sh -c <COMMAND>_all"
88     echo "lxc-driver.sh -c <COMMAND>_lxc -l <LXCNAME>"
89     echo "<COMMAND> in {sense,start,stop,restart,destroy}"
90
91 }
92
93 function main () {
94
95     #set -x
96
97     while getopts "c:n:" opt ; do
98         case $opt in
99             c) command=$OPTARG;;
100             n) lxc=$OPTARG;;
101             *) usage && exit 1;;
102         esac
103     done
104
105     
106     case $command in
107         sense_all) sense_all ;;
108         start_all) start_all ;;
109          stop_all) stop_all ;;
110       restart_all) restart_all ;;
111       destroy_all) destroy_all ;;
112         sense_lxc) sense_lxc $lxc;;
113         start_lxc) start_lxc $lxc;;
114          stop_lxc) stop_lxc $lxc;;
115       restart_lxc) restart_lxc $lxc;;
116       destroy_lxc) destroy_lxc $lxc;;
117                 *) usage
118     esac
119
120
121 }
122
123 main "$@"
124