3 # The dir containing vserver configuration
4 VSERVERS_CONF=/etc/vservers
5 # The vserver to use as a template
7 # A file containing files (or masks) to not copy from the template vserver.
8 EXCLUDE_FILE="$VSERVERS_CONF/clone-exclude"
9 # The ip range to use for creating a new vserver. A number between 2 and 253
10 # will be appended to this, until an address is found that is not used by
13 # The interface on which to create the vserver
15 # The netmask for the address
17 # The DNS vserver gets a new entry in its /etc/hosts
23 echo "Creates a new vserver with the specified name, which is a clone"
24 echo "from the vserver \`\`$TEMPLATE'' with some post-processing done."
29 IP_FILES="$VSERVERS_CONF/*/interfaces/*/ip"
32 if ! grep ^$IP$ $IP_FILES &>/dev/null; then
39 if [ "`id -u`" -eq 0 ]; then
40 echo "$0 should not be run as root, it will use sudo where appropriate"
45 if [ ! "$#" -eq 1 ]; then
52 if ! echo $NAME | grep "^[a-zA-Z0-9-]*$" &>/dev/null; then
53 echo "Name can only contain alphanumerics and dashes"
57 HOST="$NAME.`hostname --fqdn`"
61 echo "No available ip address found, aborting"
65 echo "Creating vserver $NAME with address $IP..."
66 sudo vserver $NAME build -m clone --hostname $HOST --interface $INTERFACE:$IP/$NETMASK -- --source $TEMPLATE --exclude-from $EXCLUDE_FILE
68 echo "Vserver created, configuring..."
69 sudo vserver $NAME start
71 sudo vserver $NAME exec dpkg-reconfigure openssh-server
72 # Setup git - Add a branch for this vserver
73 sudo vserver $NAME exec git branch $NAME origin/$TEMPLATE
74 sudo vserver $NAME exec git checkout $NAME
75 # Remove the template branch, to prevent pushing it back later on
76 sudo vserver $NAME exec git branch -d $TEMPLATE
77 # Push our new branch upstream so it can be pushed with "git push" afterwards.
78 sudo vserver $NAME exec git push origin $NAME:$NAME
79 # Enable vhashify/vunify
80 sudo mkdir $VSERVER_CONF/$NAME/apps/vunify
81 sudo touch $VSERVER_CONF/$NAME/apps/vunify/dummy # For git
83 # Remove trailing slashes from the confdir. git only works when we're using
84 # relative paths and the cwd is / for some reason...
85 CONF_FOR_GIT=`echo $VSERVERS_CONF/$NAME | sed "s#^/*##"`
86 # Commit the configuration
87 (cd /; git add $CONF_FOR_GIT)
88 (cd /; git commit $CONF_FOR_GIT --edit --message "vserver: Add $NAME vserver configuration.")
90 # Add this new vserver to the hosts file in the dns vserver and in the host
91 for file in /etc/hosts "$VSERVERS_CONF/$DNS_VSERVER/vdir/etc/hosts"; do
92 sudo sh -c "echo '$IP $NAME' >> '$file'"
95 # Restart dnsmasq in the dns vserver
96 sudo vserver $DNS_VSERVER exec /usr/bin/killall -HUP dnsmasq"