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
18 # The hosts file to which an entry for the new vserver should be added. Can be
19 # left empty to not record the new vserver anywhere.
20 HOSTS_FILE=$VSERVERS_CONF/$HOSTS_VSERVER/vdir/etc/hosts
21 # Any extra commands to execute after creating the vserver
22 POST_EXEC="sudo vserver $HOSTS_VSERVER exec /usr/bin/killall -HUP dnsmasq"
27 echo "Creates a new vserver with the specified name, which is a clone"
28 echo "from the vserver \`\`$TEMPLATE'' with some post-processing done."
33 IP_FILES="$VSERVERS_CONF/*/interfaces/*/ip"
36 if ! grep ^$IP$ $IP_FILES &>/dev/null; then
44 if [ ! "$#" -eq 1 ]; then
51 if ! echo $NAME | grep "^[a-zA-Z0-9-]*$" &>/dev/null; then
52 echo "Name can only contain alphanumerics and dashes"
56 HOST="$NAME/`hostname --fqdn`"
60 echo "No available ip address found, aborting"
64 echo "Creating vserver $NAME with address $IP..."
65 sudo vserver $NAME build -m clone --hostname $HOST --interface $INTERFACE:$IP/$NETMASK -- --source $TEMPLATE --exclude-from $EXCLUDE_FILE
67 echo "Vserver created, configuring..."
68 sudo vserver $NAME start
70 sudo vserver $NAME exec dpkg-reconfigure openssh-server
71 # Setup git - Add a branch for this vserver
72 sudo vserver $NAME exec git branch $NAME origin/$TEMPLATE
73 sudo vserver $NAME exec git checkout $NAME
74 # Remove the template branch, to prevent pushing it back later on
75 sudo vserver $NAME exec git branch -d $TEMPLATE
76 # Push our new branch upstream so it can be pushed with "git push" afterwards.
77 sudo vserver $NAME exec git push origin $NAME:$NAME
78 # Enable vhashify/vunify
79 sudo mkdir $VSERVER_CONF/$NAME/apps/vunify
80 sudo touch $VSERVER_CONF/$NAME/apps/vunify/dummy # For git
82 # Remove trailing slashes from the confdir. git only works when we're using
83 # relative paths and the cwd is / for some reason...
84 CONF_FOR_GIT=`echo $VSERVERS_CONF/$NAME | sed "s#^/*##"`
85 # Commit the configuration
86 (cd /; git add $CONF_FOR_GIT)
87 (cd /; git commit $CONF_FOR_GIT --edit --message "vserver: Add $NAME vserver configuration.")
89 if [ -n "$HOSTS_FILE" ]; then
90 sudo sh -c "echo '$IP $NAME' >> '$HOSTS_FILE'"
93 if [ -n "$POST_EXEC" ]; then