source: alternc/branches/stable-1.0/src/update_domains.sh @ 3061

Revision 3061, 5.6 KB checked in by benjamin, 2 years ago (diff)

applying [3051] to stable-1.0

Line 
1#!/bin/bash
2# Update domain next-gen by fufroma
3
4for CONFIG_FILE in \
5      /etc/alternc/local.sh \
6      /usr/lib/alternc/functions.sh \
7      /usr/lib/alternc/functions_hosting.sh \
8      /usr/lib/alternc/functions_dns.sh
9  do
10    if [ ! -r "$CONFIG_FILE" ]; then
11        echo "Can't access $CONFIG_FILE."
12        exit 1
13    fi
14    . "$CONFIG_FILE"
15done
16
17# Some vars
18umask 022
19LOCK_FILE="$ALTERNC_LOC/bureau/cron.lock"
20OLDIFS="$IFS"
21NEWIFS=" "
22RELOAD_ZONES="$(mktemp /tmp/alternc_reload_zones.XXXX)"
23RELOAD_WEB="$(mktemp /tmp/alternc_reload_web.XXXX)"
24B="µµ§§" # Strange letters to make split in query
25
26echo "" > "$RELOAD_ZONES"
27echo "" > "$RELOAD_WEB"
28
29# Somes check before start operations
30if [ `id -u` -ne 0 ]; then
31    log_error "must be launched as root"
32elif [ -z "$DEFAULT_MX" -o -z "$PUBLIC_IP" ]; then
33    log_error "Bad configuration. Please use: dpkg-reconfigure alternc"
34elif [ -f "$LOCK_FILE" ]; then
35    process=$(ps f -p `cat "$LOCK_FILE"|tail -1`|tail -1|awk '{print $NF;}')
36    if [ "$(basename $process)" = "$(basename "$0")" ] ; then
37      log_error "last cron unfinished or stale lock file ($LOCK_FILE)."
38    else
39      rm "$LOCK_FILE"
40    fi
41fi
42
43# backward compatibility: single-server setup
44if [ -z "$ALTERNC_SLAVES" ] ; then
45    ALTERNC_SLAVES="localhost"
46fi
47
48# We lock the application
49echo $$ > "$LOCK_FILE"
50
51# For domains we want to delete completely, make sure all the tags are all right
52# set sub_domaines.web_action = delete where domaines.dns_action = DELETE
53mysql_query "update sub_domaines sd, domaines d set sd.web_action = 'DELETE' where sd.domaine = d.domaine and sd.compte=d.compte and d.dns_action = 'DELETE';"
54
55# Sub_domaines we want to delete
56# sub_domaines.web_action = delete
57for sub in $( mysql_query "select concat_ws('$B',lower(sd.type), if(length(sd.sub)>0,concat_ws('.',sd.sub,sd.domaine),sd.domaine)) from sub_domaines sd where web_action ='DELETE';") ; do
58    host_delete ${sub/$B/ }
59    mysql_query "delete from sub_domaines where concat_ws('$B',lower(type), if(length(sub)>0,concat_ws('.',sub,domaine),domaine)) = '$sub' and web_action ='DELETE';"
60    echo 1 > "$RELOAD_WEB"
61done
62
63# Sub domaines we want to update
64# sub_domaines.web_action = update and sub_domains.only_dns = false
65IFS="$NEWIFS"
66mysql_query "
67select concat_ws('$IFS',lower(sd.type), if(length(sd.sub)>0,concat_ws('.',sd.sub,sd.domaine),sd.domaine), sd.valeur )
68from sub_domaines sd
69where sd.web_action ='UPDATE'
70;" | while read type domain valeur ; do
71    host_create "$type" "$domain" "$valeur"
72    mysql_query "update sub_domaines sd set web_action='OK',web_result='$?' where lower(sd.type)='$type' and if(length(sd.sub)>0,concat_ws('.',sd.sub,sd.domaine),sd.domaine)='$domain' and sd.valeur='$valeur'; "
73    echo 1 > "$RELOAD_WEB"
74done
75
76# Domaine to enable
77mysql_query "select concat_ws('$IFS',lower(sd.type),if(length(sd.sub)>0,concat_ws('.',sd.sub,sd.domaine),sd.domaine),sd.valeur) from sub_domaines sd where sd.enable ='ENABLE' ;"|while read type domain valeur ; do
78    host_enable "$type" "$domain" "$valeur"
79    mysql_query "update sub_domaines sd set enable='ENABLED' where lower(sd.type)='$type' and if(length(sd.sub)>0,concat_ws('.',sd.sub,sd.domaine),sd.domaine)='$domain' and sd.valeur='$valeur';"
80    echo 1 > "$RELOAD_WEB"
81done
82
83# Domains to disable
84mysql_query "select concat_ws('$IFS',lower(sd.type),if(length(sd.sub)>0,concat_ws('.',sd.sub,sd.domaine),sd.domaine),sd.valeur) from sub_domaines sd where sd.enable ='DISABLE' ;"|while read type domain valeur ; do
85    host_disable "$type" "$domain" "$valeur"
86    mysql_query "update sub_domaines sd set enable='DISABLED' where lower(sd.type)='$type' and if(length(sd.sub)>0,concat_ws('.',sd.sub,sd.domaine),sd.domaine)='$domain' and sd.valeur='$valeur';"
87    echo 1 > "$RELOAD_WEB"
88done
89
90# Domains we do not want to be the DNS serveur anymore :
91# domaines.dns_action = UPDATE and domaines.gesdns = 0
92for dom in `mysql_query "select domaine from domaines where dns_action = 'UPDATE' and gesdns = 0;"| tr '\n' ' '`
93do
94    dns_delete $dom
95    mysql_query "update domaines set dns_action = 'OK', dns_result = '$?' where domaine = '$dom'"
96    echo -n " $dom " >> "$RELOAD_ZONES"
97done
98
99# Domains we have to update the dns :
100# domaines.dns_action = UPDATE
101for dom in `mysql_query "select domaine from domaines where dns_action = 'UPDATE';" | tr '\n' ' '`
102do
103    echo "dns_regenerate : domain=/$dom/"
104    dns_regenerate $dom
105    mysql_query "update domaines set dns_action = 'OK', dns_result = '$?' where domaine = '$dom'"
106    echo -n " $dom " >> "$RELOAD_ZONES"
107done
108
109# Domains we want to delete completely, now we do it
110# domaines.dns_action = DELETE
111for dom in `mysql_query "select domaine from domaines where dns_action = 'DELETE';" | tr '\n' ' '`
112do
113    dns_delete $dom
114    # Web configurations have already bean cleaned previously
115    mysql_query "delete from sub_domaines where domaine='$dom'; delete from domaines where domaine='$dom';"
116    echo -n " $dom " >> "$RELOAD_ZONES"
117done
118
119
120if [ ! -z "$(cat "$RELOAD_WEB")" ] ; then
121  echo " apache " >> "$RELOAD_ZONES"
122
123  # Concat the apaches files
124  tempo=$(mktemp "$VHOST_FILE.XXXXX")
125  find "$VHOST_DIR" -mindepth 2 -type f -iname "*.conf" -exec cat '{}' > "$tempo" \;
126  if [ $? -ne 0 ] ; then
127    log_error " web file concatenation failed"
128  fi
129  touch "$VHOST_FILE"
130  if [ ! -w "$VHOST_FILE" ] ; then
131    log_error "cannot write on $VHOST_FILE"
132  fi
133  mv "$tempo" "$VHOST_FILE"
134
135fi
136
137# we assume we run apache and bind on the master
138/usr/bin/alternc_reload $( cat "$RELOAD_ZONES") || true
139for slave in $ALTERNC_SLAVES; do
140    if [ "$slave" != "localhost" ]; then
141        ssh alternc@$slave alternc_reload $(cat "$RELOAD_ZONES") || true
142    fi
143done
144
145rm -f "$LOCK_FILE" "$RELOAD_ZONES" "$RELOAD_WEB"
146
147exit 0
148
149
Note: See TracBrowser for help on using the repository browser.