source: alternc/trunk/src/update_domains.sh @ 2836

Revision 2836, 4.6 KB checked in by fufroma, 2 years ago (diff)

Bug delete

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