pcucontrol@master
[build.git] / create-vm.sh
1 #!/bin/bash
2
3 COMMAND=$(basename $0)
4 DIRNAME=$(dirname $0)
5
6 BUILD="${HOME}/git-build"
7
8 LOGS=$HOME/machines
9
10 [ -d $LOGS ] || { echo "Creating logs dir $LOGS" ; mkdir -p $LOGS; } 
11
12 DOMAIN=pl.sophia.inria.fr
13
14 DEFAULT_DISTRO=f31
15 DEFAULT_MEMORY=16384
16
17 CONFIRM=
18 function usage () {
19   message="$@" 
20   echo "usage : $COMMAND [-c] [-f distro] [-i image] [ -m memory ] [ -n hostname ] [-s] container"
21   echo " -c : confirm, will show the command and prompt for confirmation "
22   echo " -f : set distro, default is $DEFAULT_DISTRO"
23   echo " -i : if specified, image is rsynced into /vservers"
24   echo "      warning: we cannot use an image already in /vservers..."
25   echo " -m : memory size in Mb - default is $DEFAULT_MEMORY"
26   echo " -n : specify hostname if different from container"
27   echo " -s : do not start VM"
28   echo " container : used for /vservers/foo as well as the lxc/libvirt name"
29   echo "examples"
30   echo "  create-vm.sh sandbox"
31   echo "    Builds a brand new $DEFAULT_DISTRO 64bits VM named sandbox with hostname sandbox.pl.sophia.inria.fr"
32   echo "  create-vm.sh -i /vservers/migrating/testmaster -n testmaster testmaster.f14"
33   echo "    Create a container named testmaster.f14 from the specified image with hostname testmaster.pl.sophia.inria.fr"
34   [ -n "$message" ] && echo $message
35   exit 1
36 }
37
38 # using HOSTNAME won't work as this is already set in environment
39 while getopts "cf:i:m:n:sh" flag; do
40     case $flag in
41         c) CONFIRM=true ;;
42         f) DISTRO=$OPTARG ;;
43         i) IMAGE=$OPTARG ;;
44         m) MEMORY=$OPTARG ;;
45         n) VM_HOSTNAME=$OPTARG ;;
46         s) DO_NOT_START_VM=true ;;
47         ?|h) usage "" ;;
48     esac
49 done
50 # parse args
51 shift $((OPTIND-1))
52 [[ -z "$@" ]] && usage "no hostname provided"
53 container="$1" ; shift
54 [[ -n "$@" ]] && usage "extra arguments" "$@" "(container=$container)"
55
56 # sanity checks
57 [ -d "$BUILD" ] || usage "Could not find directory $BUILD"
58 [ -d /vservers/$container ] && usage "container $container already exists in /vservers"
59
60 echo "Updating $BUILD"
61 cd $BUILD
62 git pull
63 cd -
64
65 # compute all vars from args
66 [ -z "$DISTRO" ] && DISTRO="$DEFAULT_DISTRO"
67 [ -z "$MEMORY" ] && MEMORY="$DEFAULT_MEMORY"
68 [ -z "$VM_HOSTNAME" ] && VM_HOSTNAME="$container"
69 fqdn=$VM_HOSTNAME.$DOMAIN
70
71 # prepare initvm command
72 initvm="$BUILD/lbuild-initvm.sh"
73 [ -z "$IMAGE" ] && initvm="$initvm -f $DISTRO" || initvm="$initvm -i $IMAGE"
74 initvm="$initvm -n $fqdn"
75 [ -n "$DO_NOT_START_VM" ] && initvm="$initvm -s"
76 [ -n "$MEMORY" ] && initvm="$initvm -m $MEMORY" 
77 initvm="$initvm $container"
78
79 if [ -n "$CONFIRM" ] ; then
80     echo -n "Run $initvm OK ? "
81     read answer ; case $answer in [nN]*) exit 1 ;; esac
82 fi
83
84 echo "Running $initvm"
85 echo "Storing output in $LOGS/$container.log"
86 $initvm >& $LOGS/$container.log
87