ignore regular containers - like e.g. testmaster in build
[tests.git] / system / lxc-driver.sh
1 #!/bin/bash
2
3 path=/vservers
4
5 #################### work on all containers - only deal with the ones that have a timestamp
6 function sense_all () {
7     virsh -c lxc:/// list 2> /dev/null | grep running | while read line; do
8         pid=$(echo $line | cut -d' ' -f1)
9         lxc_name=$(echo $line | cut -d' ' -f2)
10         # ignore regular vservers like testmaster and the like
11         timestamp_file=$path/$lxc_name/$lxc_name.timestamp
12         [ -f $timestamp_file ] || continue
13         timestamp=$(cat $timestamp_file 2> /dev/null)
14         echo "$lxc_name;$pid;$timestamp" 
15     done  
16 }
17
18 function start_all () {
19     virsh -c lxc:/// list --inactive | grep " - "| while read line; do
20         lxc_name=$(echo $line | cut -d' ' -f2)
21         # ignore regular vservers like testmaster and the like
22         timestamp_file=$path/$lxc_name/$lxc_name.timestamp
23         [ -f $timestamp_file ] || continue
24         virsh -c lxc:/// start $lxc_name
25     done    
26 }
27
28 function stop_all () {
29     virsh -c lxc:/// list | grep running | while read line; do
30         lxc_name=$(echo $line | cut -d' ' -f2)
31         # ignore regular vservers like testmaster and the like
32         timestamp_file=$path/$lxc_name/$lxc_name.timestamp
33         [ -f $timestamp_file ] || continue
34         virsh -c lxc:/// destroy $lxc_name
35     done   
36 }
37
38 function destroy_all () {
39     
40     stop_all
41     virsh -c lxc:/// list --all | while read line; do
42         lxc_name=$(echo $line | cut -d' ' -f2)
43         # ignore regular vservers like testmaster and the like
44         timestamp_file=$path/$lxc_name/$lxc_name.timestamp
45         [ -f $timestamp_file ] || continue
46         virsh -c lxc:/// undefine $lxc_name
47         rm -fr $path/$lxc_name 
48     done
49 }
50
51 function restart_all () {
52
53     stop_all 
54     start_all
55 }
56
57 #################### deal with one user-specified container
58 function sense_lxc () {
59
60     lxc_name=$1; shift
61     if [ "$(virsh -c lxc:/// dominfo $lxc_name | grep State| cut -d' ' -f11)" == "running" ] ; then
62        pid=$(virsh -c lxc:/// dominfo $lxc_name| grep Id | cut -d' ' -f14)
63         timestamp_file=$path/$lxc_name/$lxc_name.timestamp
64        timestamp=$(cat $timestamp_file)
65        echo "$lxc_name;$pid;$timestamp"
66     fi
67 }
68
69 function start_lxc () {
70
71     lxc_name=$1; shift
72     if [ "$(virsh -c lxc:/// dominfo $lxc_name | grep State| cut -d' ' -f11)" != "running" ] ; then
73        virsh -c lxc:/// start $lxc_name
74     fi
75 }
76
77 function stop_lxc () {
78
79     lxc_name=$1; shift
80     if [ "$(virsh -c lxc:/// dominfo $lxc_name | grep State| cut -d' ' -f11)" != "shut off" ] ; then
81        virsh -c lxc:/// destroy $lxc_name
82     fi
83 }
84
85 function restart_lxc () {
86
87     lxc_name=$1; shift
88     stop_lxc $lxc_name
89     start_lxc $lxc_name
90 }
91
92 function destroy_lxc () {
93
94     lxc_name=$1; shift
95     stop_lxc $lxc_name
96     virsh -c lxc:/// undefine $lxc_name
97     rm -fr $path/$lxc_name
98 }
99
100 ####################
101 function usage () {
102     echo "Usage: lxc-driver.sh [options]"
103     echo "Description:"
104     echo "   This command is used to manage and retreive information on existing lxc containers "
105     echo "lxc-driver.sh -c <COMMAND>_all"
106     echo "lxc-driver.sh -c <COMMAND>_lxc -n <LXCNAME>"
107     echo "<COMMAND> in {sense,start,stop,restart,destroy}"
108
109 }
110
111 function main () {
112
113     #set -x
114
115     while getopts "c:n:" opt ; do
116         case $opt in
117             c) command=$OPTARG;;
118             n) lxc=$OPTARG;;
119             *) usage && exit 1;;
120         esac
121     done
122
123     
124     case $command in
125         sense_all|start_all|stop_all|restart_all|destroy_all|sense_lxc|start_lxc|stop_lxc|restart_lxc|destroy_lxc) $command ;;
126         *) usage
127     esac
128
129
130 }
131
132 main "$@"
133