4dbb30d9230324fed95b36f7fb394d427012fc1b
[tests.git] / system / lxc-driver.sh
1 #!/bin/bash
2
3 function sense_all () {
4     virsh -c lxc:// list | grep running | while read line; do
5         pid=$(echo $line | cut -d' ' -f1)
6         lxc_name=$(echo $line | cut -d' ' -f2)
7         timestamp=$(cat /var/lib/lxc/$lxc_name/$lxc_name.timestamp)
8         echo "$lxc_name;$pid;$timestamp" 
9     done  
10 }
11
12 function start_all () {
13     virsh -c lxc:// list --inactive | grep " - "| while read line; do
14         lxc_name=$(echo $line | cut -d' ' -f2)
15         virsh -c lxc:// start $lxc_name
16     done    
17 }
18
19 function stop_all () {
20     virsh -c lxc:// list | grep running | while read line; do
21         lxc_name=$(echo $line | cut -d' ' -f2)
22         virsh -c lxc:// destroy $lxc_name
23     done   
24 }
25
26 function sense_lxc () {
27
28     lxc_name=$1; shift
29     if [ "$(virsh -c lxc:// dominfo $lxc_name | grep State| cut -d' ' -f11)" == "running" ] ; then
30        pid=$(virsh -c lxc:// dominfo $lxc_name| grep Id | cut -d' ' -f14)
31        timestamp=$(cat /var/lib/lxc/$lxc_name/$lxc_name.timestamp)
32        echo "$lxc_name;$pid;$timestamp"
33     fi
34 }
35
36 function start_lxc () {
37
38     lxc_name=$1; shift
39     if [ "$(virsh -c lxc:// dominfo $lxc_name | grep State| cut -d' ' -f11)" != "running" ] ; then
40        virsh -c lxc:// start $lxc_name
41     fi
42 }
43
44 function stop_lxc () {
45
46     lxc_name=$1; shift
47     if [ "$(virsh -c lxc:// dominfo $lxc_name | grep State| cut -d' ' -f11)" != "shut off" ] ; then
48        virsh -c lxc:// destroy $lxc_name
49     fi
50 }
51
52 function restart_all () {
53
54     stop_all 
55     start_all
56 }
57
58 function restart_lxc () {
59
60     lxc_name=$1; shift
61     stop_lxc $lxc_name
62     start_lxc $lxc_name
63 }
64
65 function destroy_all () {
66     
67     stop_all
68     virsh -c lxc:// list --all | while read line; do
69         lxc_name=$(echo $line | cut -d' ' -f2)
70         virsh -c lxc:// undefine $lxc_name
71         rm -fr /var/lib/lxc/$lxc_name 
72     done
73 }
74
75 function destroy_lxc () {
76
77     lxc_name=$1; shift
78     stop_lxc $lxc_name
79     virsh -c lxc:// undefine $lxc_name
80     rm -fr /var/lib/lxc/$lxc_name
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 -n <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