unified logname uses into fm_username
[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="$(get_ip_addresses | head)"
25   echo "** defaulting IP address to $IP_ADDRESS"
26 fi
27 if [ -z "$SERVER_ADMIN" ]; then
28   # the email address (where first dot is replaced by @) for the administrator of the domain.
29   SERVER_ADMIN="$(fm_username).localhost"
30   echo "** defaulting server admin to $SERVER_ADMIN"
31 fi
32 if [ -z "$MAIN_NAME_SERVER" ]; then
33   # the name of the name server for the new domains (should already be configured).
34   MAIN_NAME_SERVER="ns.localhost"
35   echo "** defaulting main name server to $MAIN_NAME_SERVER"
36 fi
37 if [ -z "$MAIL_SERVER" ]; then
38   # the name of the mail server for a new domain (should already be configured).
39   MAIL_SERVER="mail.localhost"
40   echo "** defaulting mail server to $MAIL_SERVER"
41 fi
42 if [ -z "$DISTRO" ]; then
43   # the distribution name to be listed in info for the new domain or subdomain.
44   DISTRO="ubuntu"
45   echo "** defaulting distro to $DISTRO"
46 fi
47
48 # main body of script.
49
50 if [[ $EUID != 0 ]]; then
51   echo "This script must be run as root or sudo."
52   exit 1
53 fi
54
55 new_domain="$1"; shift
56
57 if [ -z "$new_domain" ]; then
58   echo "This script needs a domain name to add to DNS." 
59   exit 1
60 fi
61
62 # if domain name has three or more components, then add a subdomain.
63 # otherwise, add a full new domain.
64 if [[ $new_domain == *"."*"."* ]]; then
65   # add a subdomain to the containing domain.
66   add_new_subdomain "$new_domain"
67   restart_bind
68 else
69   # create a totally new domain in DNS.
70   write_new_domain_file "$new_domain"
71   add_zone_for_new_domain "$new_domain"
72   restart_bind
73 fi
74
75