vserver: Add script for adding and setting up new vservers.
[matthijs/servers/tika.git] / usr / local / bin / vserver-create
1 #!/bin/sh
2
3 # The dir containing vserver configuration
4 VSERVERS_CONF=/etc/vservers
5 # The vserver to use as a template
6 TEMPLATE=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
11 # another vserver..
12 IP_RANGE=10.42.0.
13 # The interface on which to create the vserver
14 INTERFACE=dummy0
15 # The netmask for the address
16 NETMASK=24
17 # The DNS vserver gets a new entry in its /etc/hosts
18 DNS_VSERVER=dns
19
20 function usage()
21 {
22         echo "Usage: $0 name"
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."
25 }
26
27 function find_ip()
28 {
29         IP_FILES="$VSERVERS_CONF/*/interfaces/*/ip"
30         for i in {2..53}; do
31                 IP=${IP_RANGE}$i
32                 if ! grep ^$IP$ $IP_FILES &>/dev/null; then
33                         return 0
34                 fi
35         done
36         return 1
37 }
38
39 if [ "`id -u`" -eq 0 ]; then
40         echo "$0 should not be run as root, it will use sudo where appropriate"
41         exit 1
42 fi
43
44 # Check arguments
45 if [ ! "$#" -eq 1 ]; then
46         usage
47         exit 1
48 fi
49
50 NAME=$1
51
52 if ! echo $NAME | grep "^[a-zA-Z0-9-]*$" &>/dev/null; then
53         echo "Name can only contain alphanumerics and dashes"
54         exit 1
55 fi
56
57 HOST="$NAME.`hostname --fqdn`"
58
59 # Set the IP var
60 if ! find_ip; then
61         echo "No available ip address found, aborting"
62         exit 1
63 fi;
64
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
67
68 echo "Vserver created, configuring..."
69 sudo vserver $NAME start
70 # Regen ssh keys
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
82
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.")
89
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'"
93 done
94
95 # Restart dnsmasq in the dns vserver
96 sudo vserver $DNS_VSERVER exec /usr/bin/killall -HUP dnsmasq"