vserver: Add script for adding and setting up new vservers.
authorMatthijs Kooijman <matthijs@stdin.nl>
Thu, 16 Oct 2008 14:10:07 +0000 (16:10 +0200)
committerMatthijs Kooijman <matthijs@stdin.nl>
Thu, 16 Oct 2008 14:10:07 +0000 (16:10 +0200)
usr/local/bin/vserver-create [new file with mode: 0755]

diff --git a/usr/local/bin/vserver-create b/usr/local/bin/vserver-create
new file mode 100755 (executable)
index 0000000..b73f6a2
--- /dev/null
@@ -0,0 +1,70 @@
+#!/bin/sh
+
+TEMPLATE=template
+VSERVERS_CONF=/etc/vservers
+EXCLUDE_FILE="$VSERVERS_CONF/clone-exclude"
+IP_RANGE=10.42.0.
+INTERFACE=dummy0
+NETMASK=24
+
+function usage()
+{
+       echo "Usage: $0 name"
+       echo "Creates a new vserver with the specified name, which is a clone"
+       echo "from the vserver \`\`$TEMPLATE'' with some post-processing done."
+}
+
+function find_ip()
+{
+       IP_FILES="$VSERVERS_CONF/*/interfaces/*/ip"
+       for i in {2..53}; do
+               IP=${IP_RANGE}$i
+               if ! grep ^$IP$ $IP_FILES &>/dev/null; then
+                       return 0
+               fi
+       done
+       return 1
+}
+
+# Check arguments
+if [ ! "$#" -eq 1 ]; then
+       usage
+       exit 1
+fi
+
+NAME=$1
+
+if ! echo $NAME | grep "^[a-zA-Z0-9-]*$" &>/dev/null; then
+       echo "Name can only contain alphanumerics and dashes"
+       exit 1
+fi
+
+HOST="$NAME/`hostname --fqdn`"
+
+# Set the IP var
+if ! find_ip; then
+       echo "No available ip address found, aborting"
+       exit 1
+fi;
+
+sudo vserver $NAME build -m clone --hostname $HOST --interface $INTERFACE:$IP/$NETMASK -- --source $TEMPLATE --exclude-from $EXCLUDE_FILE
+sudo vserver $NAME start
+# Regen ssh keys
+sudo vserver $NAME exec dpkg-reconfigure openssh-server
+# Setup git - Add a branch for this vserver
+sudo vserver $NAME exec git branch $NAME origin/$TEMPLATE
+sudo vserver $NAME exec git checkout $NAME
+# Remove the template branch, to prevent pushing it back later on 
+sudo vserver $NAME exec git branch -d $TEMPLATE
+# Push our new branch upstream so it can be pushed with "git push" afterwards.
+sudo vserver $NAME exec git push origin $NAME:$NAME
+# Enable vhashify/vunify
+sudo mkdir $VSERVER_CONF/$NAME/apps/vunify
+sudo touch $VSERVER_CONF/$NAME/apps/vunify/dummy # For git
+
+# Remove trailing slashes from the confdir. git only works when we're using
+# relative paths and the cwd is / for some reason...
+CONF_FOR_GIT=`echo $VSERVERS_CONF/$NAME | sed "s#^/*##"`
+# Commit the configuration
+(cd /; git add $CONF_FOR_GIT)
+(cd /; git commit $CONF_FOR_GIT --edit --message "vserver: Add $NAME vserver configuration.")