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

Revision 2821, 4.4 KB checked in by fufroma, 2 years ago (diff)

Update domains, l'aventure continue

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