using THISDIR instead of WORKDIR
[feisty_meow.git] / scripts / system / add_domain.sh
1 #!/bin/bash
2
3 # this set of functions serve the main purpose of adding new domains or
4 # subdomains to the bind9 DNS server on the current host.  it is currently
5 # highly specific to running a bunch of domains on a linux VM, where the VM
6 # has one IP address.  note that the bind 'named' must already be configured.
7 # also, it is assumed that, if a subdomain is being added, then the containing
8 # domain has already been configured and is configured in a file similar to
9 # "blah.com.conf" in /etc/bind.
10 #
11 # Author: Chris Koeritz
12
13 export THISDIR="$( \cd "$(\dirname "$0")" && \pwd )"  # obtain the script's working directory.
14 export FEISTY_MEOW_APEX="$( \cd "$THISDIR/../.." && \pwd )"
15
16 source "$FEISTY_MEOW_APEX/scripts/core/launch_feisty_meow.sh"
17 source "$FEISTY_MEOW_SCRIPTS/system/common_sysadmin.sh"
18
19 # some defaults that are convenient for current purposes.
20 # existing values will be respected over our defaults.
21
22 if [ -z "$IP_ADDRESS" ]; then
23   # in our scheme, the single IP address that all our domains map to.
24   IP_ADDRESS="10.28.42.20"
25 fi
26 if [ -z "$SERVER_ADMIN" ]; then
27   # the email address (where first dot is replaced by @) for the administrator of the domain.
28   SERVER_ADMIN="developer.cakelampvm.com"
29 fi
30 if [ -z "$MAIN_NAME_SERVER" ]; then
31   # the name of the name server for the new domains (should already be configured).
32   MAIN_NAME_SERVER="ns.cakelampvm.com"
33 fi
34 if [ -z "$MAIL_SERVER" ]; then
35   # the name of the mail server for a new domain (should already be configured).
36   MAIL_SERVER="mail.cakelampvm.com"
37 fi
38 if [ -z "$DISTRO" ]; then
39   # the distribution name to be listed in info for the new domain or subdomain.
40   DISTRO="ubuntu"
41 fi
42
43 # main body of script.
44
45 if [[ $EUID != 0 ]]; then
46   echo "This script must be run as root or sudo."
47   exit 1
48 fi
49
50 new_domain="$1"; shift
51
52 if [ -z "$new_domain" ]; then
53   echo "This script needs a domain name to add to DNS." 
54   exit 1
55 fi
56
57 # if domain name has three or more components, then add a subdomain.
58 # otherwise, add a full new domain.
59 if [[ $new_domain == *"."*"."* ]]; then
60   # add a subdomain to the containing domain.
61   add_new_subdomain "$new_domain"
62   restart_bind
63 else
64   # create a totally new domain in DNS.
65   write_new_domain_file "$new_domain"
66   add_zone_for_new_domain "$new_domain"
67   restart_bind
68 fi
69
70