| 1 | #!/bin/bash |
|---|
| 2 | # Update domain next-gen by fufrom |
|---|
| 3 | |
|---|
| 4 | for 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" |
|---|
| 15 | done |
|---|
| 16 | |
|---|
| 17 | # Some vars |
|---|
| 18 | umask 022 |
|---|
| 19 | LOCK_FILE="$ALTERNC_LOC/bureau/cron.lock" |
|---|
| 20 | OLDIFS="$IFS" |
|---|
| 21 | NEWIFS=" " |
|---|
| 22 | B="µµ§§" # Strange letters to make split in query |
|---|
| 23 | |
|---|
| 24 | # Somes check before start operations |
|---|
| 25 | if [ `id -u` -ne 0 ]; then |
|---|
| 26 | log_error "must be launched as root" |
|---|
| 27 | elif [ -z "$DEFAULT_MX" -o -z "$PUBLIC_IP" ]; then |
|---|
| 28 | log_error "Bad configuration. Please use: dpkg-reconfigure alternc" |
|---|
| 29 | elif [ -f "$LOCK_FILE" ]; then |
|---|
| 30 | log_error "last cron unfinished or stale lock file ($LOCK_FILE)." |
|---|
| 31 | fi |
|---|
| 32 | |
|---|
| 33 | # backward compatibility: single-server setup |
|---|
| 34 | if [ -z "$ALTERNC_SLAVES" ] ; then |
|---|
| 35 | ALTERNC_SLAVES="localhost" |
|---|
| 36 | fi |
|---|
| 37 | |
|---|
| 38 | # We lock the application |
|---|
| 39 | touch "$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 |
|---|
| 43 | mysql_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 |
|---|
| 47 | for 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';" |
|---|
| 50 | done |
|---|
| 51 | |
|---|
| 52 | # Sub domaines we want to update |
|---|
| 53 | # sub_domaines.web_action = update and sub_domains.only_dns = false |
|---|
| 54 | IFS="$NEWIFS" |
|---|
| 55 | mysql_query " |
|---|
| 56 | select concat_ws('$IFS',lower(sd.type), if(length(sd.sub)>0,concat_ws('.',sd.sub,sd.domaine),sd.domaine), sd.valeur ) |
|---|
| 57 | from sub_domaines sd |
|---|
| 58 | where 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'; " |
|---|
| 62 | done |
|---|
| 63 | |
|---|
| 64 | # Domaine to enable |
|---|
| 65 | mysql_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';" |
|---|
| 68 | done |
|---|
| 69 | |
|---|
| 70 | # Domains to disable |
|---|
| 71 | mysql_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';" |
|---|
| 74 | done |
|---|
| 75 | |
|---|
| 76 | # Domains we do not want to be the DNS serveur anymore : |
|---|
| 77 | # domaines.dns_action = UPDATE and domaines.gesdns = 0 |
|---|
| 78 | for 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'" |
|---|
| 81 | done |
|---|
| 82 | |
|---|
| 83 | # Domains we have to update the dns : |
|---|
| 84 | # domaines.dns_action = UPDATE |
|---|
| 85 | for 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'" |
|---|
| 88 | done |
|---|
| 89 | |
|---|
| 90 | # Domains we want to delete completely, now we do it |
|---|
| 91 | # domaines.dns_action = DELETE |
|---|
| 92 | for 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';" |
|---|
| 96 | done |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | # Concat the apaches files |
|---|
| 100 | tempo=$(mktemp "$VHOST_FILE.XXXXX") |
|---|
| 101 | find "$VHOST_DIR" -mindepth 2 -type f -iname "*.conf" -exec cat '{}' > "$tempo" \; |
|---|
| 102 | if [ $? -ne 0 ] ; then |
|---|
| 103 | log_error " web file concatenation failed" |
|---|
| 104 | fi |
|---|
| 105 | touch "$VHOST_FILE" |
|---|
| 106 | if [ ! -w "$VHOST_FILE" ] ; then |
|---|
| 107 | log_error "cannot write on $VHOST_FILE" |
|---|
| 108 | fi |
|---|
| 109 | |
|---|
| 110 | mv "$tempo" "$VHOST_FILE" |
|---|
| 111 | |
|---|
| 112 | # Reload web and dns |
|---|
| 113 | /usr/bin/alternc_reload all |
|---|
| 114 | |
|---|
| 115 | # TODO reload slaves |
|---|
| 116 | |
|---|
| 117 | rm "$LOCK_FILE" |
|---|
| 118 | |
|---|
| 119 | exit 0 |
|---|
| 120 | |
|---|
| 121 | |
|---|