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