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

Revision 2969, 5.4 KB checked in by xals, 2 years ago (diff)

Merge changeset 2968.

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