| 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/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" |
|---|
| 15 | done |
|---|
| 16 | |
|---|
| 17 | # Some vars |
|---|
| 18 | umask 022 |
|---|
| 19 | LOCK_FILE="$ALTERNC_LOC/bureau/cron.lock" |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | # Somes check before start operations |
|---|
| 23 | if [ `id -u` -ne 0 ]; then |
|---|
| 24 | log_error "must be launched as root" |
|---|
| 25 | elif [ -z "$DEFAULT_MX" -o -z "$PUBLIC_IP" ]; then |
|---|
| 26 | log_error "Bad configuration. Please use: dpkg-reconfigure alternc" |
|---|
| 27 | elif [ -f "$LOCK_FILE" ]; then |
|---|
| 28 | log_error "last cron unfinished or stale lock file ($LOCK_FILE)." |
|---|
| 29 | fi |
|---|
| 30 | |
|---|
| 31 | # backward compatibility: single-server setup |
|---|
| 32 | if [ -z "$ALTERNC_SLAVES" ] ; then |
|---|
| 33 | ALTERNC_SLAVES="localhost" |
|---|
| 34 | fi |
|---|
| 35 | |
|---|
| 36 | # We lock the application |
|---|
| 37 | touch "$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 |
|---|
| 45 | for 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 |
|---|
| 48 | done |
|---|
| 49 | |
|---|
| 50 | # Sub domaines we want to update |
|---|
| 51 | # sub_domaines.web_action = update and sub_domains.only_dns = false |
|---|
| 52 | params=$( $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 | ;") |
|---|
| 57 | for 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'" |
|---|
| 60 | done |
|---|
| 61 | |
|---|
| 62 | # Domaine to enable |
|---|
| 63 | for 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';" |
|---|
| 66 | done |
|---|
| 67 | |
|---|
| 68 | # Domains to disable |
|---|
| 69 | for 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';" |
|---|
| 72 | done |
|---|
| 73 | |
|---|
| 74 | # Domains we do not want to be the DNS serveur anymore : |
|---|
| 75 | # domaines.dns_action = UPDATE and domaines.gesdns = 0 |
|---|
| 76 | for 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'" |
|---|
| 79 | done |
|---|
| 80 | |
|---|
| 81 | # Domains we have to update the dns : |
|---|
| 82 | # domaines.dns_action = UPDATE |
|---|
| 83 | for 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'" |
|---|
| 86 | done |
|---|
| 87 | |
|---|
| 88 | # Domains we want to delete completely, now we do it |
|---|
| 89 | # domaines.dns_action = DELETE |
|---|
| 90 | for 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';" |
|---|
| 94 | done |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | # Concat the apaches files |
|---|
| 98 | tempo=$(mktemp /tmp/alternc-vhost.XXXXX) |
|---|
| 99 | find "$VHOST_DIR" -mindepth 2 -type f -iname "*.conf" -exec cat '{}' > "$tempo" \; |
|---|
| 100 | if [ $? -ne 0 ] ; then |
|---|
| 101 | log_error " web file concatenation failed" |
|---|
| 102 | fi |
|---|
| 103 | touch "$VHOST_FILE" |
|---|
| 104 | if [ ! -w "$VHOST_FILE" ] ; then |
|---|
| 105 | log_error "cannot write on $VHOST_FILE" |
|---|
| 106 | fi |
|---|
| 107 | |
|---|
| 108 | mv "$tempo" "$VHOST_FILE" |
|---|
| 109 | |
|---|
| 110 | echo Exitbefore reload everything, we are testing, FUCK |
|---|
| 111 | rm "$LOCK_FILE" |
|---|
| 112 | exit 1 |
|---|
| 113 | |
|---|
| 114 | # Reload web and dns |
|---|
| 115 | alternc_reload all |
|---|
| 116 | |
|---|
| 117 | # TODO reload slaves |
|---|
| 118 | |
|---|
| 119 | rm "$LOCK_FILE" |
|---|
| 120 | |
|---|
| 121 | exit 0 |
|---|
| 122 | |
|---|
| 123 | |
|---|