X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=system%2Flxc-driver.sh;h=cec69fbf7c30f48661145bc7565519e024535f87;hb=9b733a4719b75382285799e42b245108f986aaa1;hp=36d4f98a4d695ded583712c0797703001a3c5912;hpb=076a4169b9bc4f3b182a06a548405147463c4cfa;p=tests.git diff --git a/system/lxc-driver.sh b/system/lxc-driver.sh index 36d4f98..cec69fb 100755 --- a/system/lxc-driver.sh +++ b/system/lxc-driver.sh @@ -2,35 +2,66 @@ path=/vservers +#################### work on all containers - only deal with the ones that have a timestamp function sense_all () { - virsh -c lxc:// list | grep running | while read line; do + virsh -c lxc:/// list 2> /dev/null | grep running | while read line; do pid=$(echo $line | cut -d' ' -f1) lxc_name=$(echo $line | cut -d' ' -f2) - timestamp=$(cat $path/$lxc_name/$lxc_name.timestamp) + # ignore regular vservers like testmaster and the like + timestamp_file=$path/$lxc_name/$lxc_name.timestamp + [ -f $timestamp_file ] || continue + timestamp=$(cat $timestamp_file 2> /dev/null) echo "$lxc_name;$pid;$timestamp" done } function start_all () { - virsh -c lxc:// list --inactive | grep " - "| while read line; do + virsh -c lxc:/// list --inactive | grep " - "| while read line; do lxc_name=$(echo $line | cut -d' ' -f2) - virsh -c lxc:// start $lxc_name + # ignore regular vservers like testmaster and the like + timestamp_file=$path/$lxc_name/$lxc_name.timestamp + [ -f $timestamp_file ] || continue + virsh -c lxc:/// start $lxc_name done } function stop_all () { - virsh -c lxc:// list | grep running | while read line; do + virsh -c lxc:/// list | grep running | while read line; do lxc_name=$(echo $line | cut -d' ' -f2) - virsh -c lxc:// destroy $lxc_name + # ignore regular vservers like testmaster and the like + timestamp_file=$path/$lxc_name/$lxc_name.timestamp + [ -f $timestamp_file ] || continue + virsh -c lxc:/// destroy $lxc_name done } +function destroy_all () { + + stop_all + virsh -c lxc:/// list --all | while read line; do + lxc_name=$(echo $line | cut -d' ' -f2) + # ignore regular vservers like testmaster and the like + timestamp_file=$path/$lxc_name/$lxc_name.timestamp + [ -f $timestamp_file ] || continue + virsh -c lxc:/// undefine $lxc_name + rm -fr $path/$lxc_name + done +} + +function restart_all () { + + stop_all + start_all +} + +#################### deal with one user-specified container function sense_lxc () { lxc_name=$1; shift - if [ "$(virsh -c lxc:// dominfo $lxc_name | grep State| cut -d' ' -f11)" == "running" ] ; then - pid=$(virsh -c lxc:// dominfo $lxc_name| grep Id | cut -d' ' -f14) - timestamp=$(cat $path/$lxc_name/$lxc_name.timestamp) + if [ "$(virsh -c lxc:/// dominfo $lxc_name | grep State| cut -d' ' -f11)" == "running" ] ; then + pid=$(virsh -c lxc:/// dominfo $lxc_name| grep Id | cut -d' ' -f14) + timestamp_file=$path/$lxc_name/$lxc_name.timestamp + timestamp=$(cat $timestamp_file) echo "$lxc_name;$pid;$timestamp" fi } @@ -38,25 +69,19 @@ function sense_lxc () { function start_lxc () { lxc_name=$1; shift - if [ "$(virsh -c lxc:// dominfo $lxc_name | grep State| cut -d' ' -f11)" != "running" ] ; then - virsh -c lxc:// start $lxc_name + if [ "$(virsh -c lxc:/// dominfo $lxc_name | grep State| cut -d' ' -f11)" != "running" ] ; then + virsh -c lxc:/// start $lxc_name fi } function stop_lxc () { lxc_name=$1; shift - if [ "$(virsh -c lxc:// dominfo $lxc_name | grep State| cut -d' ' -f11)" != "shut off" ] ; then - virsh -c lxc:// destroy $lxc_name + if [ "$(virsh -c lxc:/// dominfo $lxc_name | grep State| cut -d' ' -f11)" != "shut off" ] ; then + virsh -c lxc:/// destroy $lxc_name fi } -function restart_all () { - - stop_all - start_all -} - function restart_lxc () { lxc_name=$1; shift @@ -64,31 +89,24 @@ function restart_lxc () { start_lxc $lxc_name } -function destroy_all () { - - stop_all - virsh -c lxc:// list --all | while read line; do - lxc_name=$(echo $line | cut -d' ' -f2) - virsh -c lxc:// undefine $lxc_name - rm -fr $path/$lxc_name - done -} - function destroy_lxc () { lxc_name=$1; shift stop_lxc $lxc_name - virsh -c lxc:// undefine $lxc_name + virsh -c lxc:/// undefine $lxc_name rm -fr $path/$lxc_name } +#################### +commands="sense_all|start_all|stop_all|restart_all|destroy_all|sense_lxc|start_lxc|stop_lxc|restart_lxc|destroy_lxc" + function usage () { echo "Usage: lxc-driver.sh [options]" echo "Description:" echo " This command is used to manage and retreive information on existing lxc containers " echo "lxc-driver.sh -c _all" echo "lxc-driver.sh -c _lxc -n " - echo " in {sense,start,stop,restart,destroy}" + echo " in {$commands}" } @@ -106,17 +124,8 @@ function main () { case $command in - sense_all) sense_all ;; - start_all) start_all ;; - stop_all) stop_all ;; - restart_all) restart_all ;; - destroy_all) destroy_all ;; - sense_lxc) sense_lxc $lxc;; - start_lxc) start_lxc $lxc;; - stop_lxc) stop_lxc $lxc;; - restart_lxc) restart_lxc $lxc;; - destroy_lxc) destroy_lxc $lxc;; - *) usage + sense_all|start_all|stop_all|restart_all|destroy_all|sense_lxc|start_lxc|stop_lxc|restart_lxc|destroy_lxc) $command ;; + *) usage esac