vserver: Make the vserver-create script update the dns verserver's hosts file.
[matthijs/servers/drsnuggles.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 HOSTS_VSERVER=dns
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
22 function usage()
23 {
24         echo "Usage: $0 name"
25         echo "Creates a new vserver with the specified name, which is a clone"
26         echo "from the vserver \`\`$TEMPLATE'' with some post-processing done."
27 }
28
29 function find_ip()
30 {
31         IP_FILES="$VSERVERS_CONF/*/interfaces/*/ip"
32         for i in {2..53}; do
33                 IP=${IP_RANGE}$i
34                 if ! grep ^$IP$ $IP_FILES &>/dev/null; then
35                         return 0
36                 fi
37         done
38         return 1
39 }
40
41 # Check arguments
42 if [ ! "$#" -eq 1 ]; then
43         usage
44         exit 1
45 fi
46
47 NAME=$1
48
49 if ! echo $NAME | grep "^[a-zA-Z0-9-]*$" &>/dev/null; then
50         echo "Name can only contain alphanumerics and dashes"
51         exit 1
52 fi
53
54 HOST="$NAME/`hostname --fqdn`"
55
56 # Set the IP var
57 if ! find_ip; then
58         echo "No available ip address found, aborting"
59         exit 1
60 fi;
61
62 echo "Creating vserver $NAME with address $IP..."
63 sudo vserver $NAME build -m clone --hostname $HOST --interface $INTERFACE:$IP/$NETMASK -- --source $TEMPLATE --exclude-from $EXCLUDE_FILE
64
65 echo "Vserver created, configuring..."
66 sudo vserver $NAME start
67 # Regen ssh keys
68 sudo vserver $NAME exec dpkg-reconfigure openssh-server
69 # Setup git - Add a branch for this vserver
70 sudo vserver $NAME exec git branch $NAME origin/$TEMPLATE
71 sudo vserver $NAME exec git checkout $NAME
72 # Remove the template branch, to prevent pushing it back later on 
73 sudo vserver $NAME exec git branch -d $TEMPLATE
74 # Push our new branch upstream so it can be pushed with "git push" afterwards.
75 sudo vserver $NAME exec git push origin $NAME:$NAME
76 # Enable vhashify/vunify
77 sudo mkdir $VSERVER_CONF/$NAME/apps/vunify
78 sudo touch $VSERVER_CONF/$NAME/apps/vunify/dummy # For git
79
80 # Remove trailing slashes from the confdir. git only works when we're using
81 # relative paths and the cwd is / for some reason...
82 CONF_FOR_GIT=`echo $VSERVERS_CONF/$NAME | sed "s#^/*##"`
83 # Commit the configuration
84 (cd /; git add $CONF_FOR_GIT)
85 (cd /; git commit $CONF_FOR_GIT --edit --message "vserver: Add $NAME vserver configuration.")
86
87 if [ -n "$HOSTS_FILE" ]; then
88         sudo sh -c "echo '$IP   $NAME' >> '$HOSTS_FILE'"
89 fi