utility to create debian vservers
authorThierry Parmentelat <thierry.parmentelat@inria.fr>
Tue, 23 Oct 2012 13:26:08 +0000 (15:26 +0200)
committerThierry Parmentelat <thierry.parmentelat@inria.fr>
Tue, 23 Oct 2012 13:26:08 +0000 (15:26 +0200)
scripts/create-vm-debian.sh [new file with mode: 0755]

diff --git a/scripts/create-vm-debian.sh b/scripts/create-vm-debian.sh
new file mode 100755 (executable)
index 0000000..a26c4e7
--- /dev/null
@@ -0,0 +1,78 @@
+#!/bin/bash
+
+# WARNING: looks like specifying another arch/preso won't work
+
+COMMAND=$(basename $0)
+DIRNAME=$(dirname $0)
+
+#BUILD="${HOME}/git-build"
+DOMAIN=pl.sophia.inria.fr
+
+DEFAULT_DISTRO=wheezy
+DEFAULT_PERSO=linux64
+
+INTERACTIVE=
+DISTRO=$DEFAULT_DISTRO
+PERSO=$DEFAULT_PERSO
+function usage () {
+  message="$@" 
+  echo "usage : $COMMAND [-i] [-f distro] [-p perso] hostname"
+  echo "  default distro is $DEFAULT_DISTRO"
+  echo "  default perso is $DEFAULT_PERSO"
+  [ -n "$message" ] && echo $message
+  exit 1
+}
+
+while getopts "f:p:ih" flag; do
+    case $flag in
+       f) DISTRO=$OPTARG;;
+       p) PERSO=$OPTARG;;
+       i) INTERACTIVE=true ;;
+       ?|h) usage "" ;;
+    esac
+done
+shift $((OPTIND-1))
+[[ -z "$@" ]] && usage "no hostname provided"
+HOSTNAME="$1" ; shift
+[[ -n "$@" ]] && usage "extra arguments"  "$@" "(hostname=$HOSTNAME)"
+
+
+#[ -d "$BUILD" ] || usage "Could not find directory $BUILD"
+
+[ -d /vservers/$HOSTNAME ] && usage "vserver $HOSTNAME already exists in /vservers"
+# debian distros not found there - skip that
+#[ -d /etc/vservers/.distributions/$DISTRO ] || usage "distro $DISTRO not known in /etc/vservers/.distributions/"
+rpm -q debootstrap >& /dev/null || usage "You need to first run 'yum install debootstrap'"
+
+fqdn=$HOSTNAME.$DOMAIN
+## compute IP
+host $fqdn | grep -q 'has address' || usage "hostname $fqdn not known to DNS"
+ip=$(host $fqdn | grep 'has address' | awk '{print $4}')
+
+#cd $BUILD
+#git pull
+#cd -
+
+# single_ip needs to be turned off, vtest-init-vserver now takes care of that
+#command="$BUILD/vtest-init-vserver.sh -p $PERSO -f $DISTRO $HOSTNAME none -- --interface eth0:$ip --hostname $fqdn"
+
+case $PERSO in
+    linux32) arch=i386;;
+    linux64) arch=amd64;;
+esac
+
+command="$PERSO vserver ${HOSTNAME} build -m debootstrap --interface eth0:${ip}/21 --hostname ${fqdn} -- -d ${DISTRO} -m http://ftp2.fr.debian.org/debian/ -- --verbose --arch=${arch} "
+
+if [ -n "$INTERACTIVE" ] ; then
+    echo -n "Run $command OK ? "
+    read answer ; case $answer in [nN]*) exit 1 ;; esac
+fi
+
+$command
+
+### post processing for starting up
+for dir in run lock; do
+    [ -d /vservers/${HOSTNAME}/var/$dir ] || mkdir /vservers/${HOSTNAME}/var/$dir
+done
+
+vserver ${HOSTNAME} start