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