2011-01-28 15:55:26 +00:00
#!/bin/bash
2011-03-04 13:45:40 +00:00
# Update domain next-gen by fufroma
2011-01-28 15:55:26 +00:00
for CONFIG_FILE in \
/etc/alternc/local.sh \
/usr/lib/alternc/functions.sh \
2011-01-30 12:45:53 +00:00
/usr/lib/alternc/functions_hosting.sh \
/usr/lib/alternc/functions_dns.sh
2011-01-28 15:55:26 +00:00
do
if [ ! -r " $CONFIG_FILE " ] ; then
echo " Can't access $CONFIG_FILE . "
exit 1
fi
. " $CONFIG_FILE "
done
2006-04-26 12:28:53 +00:00
2011-01-28 15:55:26 +00:00
# Some vars
2006-04-26 12:28:53 +00:00
umask 022
2011-01-28 15:55:26 +00:00
LOCK_FILE = " $ALTERNC_LOC /bureau/cron.lock "
2011-02-01 22:44:18 +00:00
OLDIFS = " $IFS "
NEWIFS = " "
2012-08-21 18:25:56 +00:00
LOGFORMAT_FILE = "/etc/alternc/apache_logformat.conf"
2011-06-07 09:24:35 +00:00
RELOAD_ZONES = " $( mktemp /tmp/alternc_reload_zones.XXXX) "
RELOAD_WEB = " $( mktemp /tmp/alternc_reload_web.XXXX) "
2011-10-18 18:25:47 +00:00
DNS_DO_RESTART = " /tmp/alternc.do_do_restart. $$ "
2011-02-01 22:44:18 +00:00
B = "µµ§§" # Strange letters to make split in query
2006-04-26 12:28:53 +00:00
2011-06-07 09:24:35 +00:00
echo "" > " $RELOAD_ZONES "
echo "" > " $RELOAD_WEB "
2011-01-28 15:55:26 +00:00
# Somes check before start operations
2006-04-26 12:28:53 +00:00
if [ ` id -u` -ne 0 ] ; then
2011-01-28 15:55:26 +00:00
log_error "must be launched as root"
elif [ -z " $DEFAULT_MX " -o -z " $PUBLIC_IP " ] ; then
log_error "Bad configuration. Please use: dpkg-reconfigure alternc"
elif [ -f " $LOCK_FILE " ] ; then
2011-03-07 20:12:09 +00:00
process = $( ps f -p ` cat " $LOCK_FILE " | tail -1` | tail -1| awk '{print $NF;}' )
if [ " $( basename $process ) " = " $( basename " $0 " ) " ] ; then
log_error " last cron unfinished or stale lock file ( $LOCK_FILE ). "
else
rm " $LOCK_FILE "
fi
2006-04-26 12:28:53 +00:00
fi
2009-07-27 21:21:26 +00:00
# backward compatibility: single-server setup
if [ -z " $ALTERNC_SLAVES " ] ; then
ALTERNC_SLAVES = "localhost"
fi
2011-01-28 15:55:26 +00:00
# We lock the application
2011-03-07 20:12:09 +00:00
echo $$ > " $LOCK_FILE "
2006-04-26 12:28:53 +00:00
2011-01-28 15:55:26 +00:00
# For domains we want to delete completely, make sure all the tags are all right
# set sub_domaines.web_action = delete where domaines.dns_action = DELETE
2011-02-01 22:44:18 +00:00
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';"
2006-04-26 12:28:53 +00:00
2011-02-01 17:17:58 +00:00
# Sub_domaines we want to delete
2011-01-28 15:55:26 +00:00
# sub_domaines.web_action = delete
2011-02-01 22:44:18 +00:00
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
host_delete ${ sub / $B / }
2011-02-03 12:41:58 +00:00
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'; "
2011-06-07 09:24:35 +00:00
echo 1 > " $RELOAD_WEB "
2011-01-28 15:55:26 +00:00
done
2006-04-26 12:28:53 +00:00
2011-02-01 17:17:58 +00:00
# Sub domaines we want to update
2011-01-28 15:55:26 +00:00
# sub_domaines.web_action = update and sub_domains.only_dns = false
2011-02-01 22:44:18 +00:00
IFS = " $NEWIFS "
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.web_action = 'UPDATE'
; " | while read type domain valeur ; do
host_create " $type " " $domain " " $valeur "
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 '; "
2011-06-07 09:24:35 +00:00
echo 1 > " $RELOAD_WEB "
2011-01-29 15:12:26 +00:00
done
2011-01-29 15:46:33 +00:00
# Domaine to enable
2011-02-01 22:44:18 +00:00
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
host_enable " $type " " $domain " " $valeur "
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 '; "
2011-06-07 09:24:35 +00:00
echo 1 > " $RELOAD_WEB "
2011-01-29 15:46:33 +00:00
done
# Domains to disable
2011-02-01 22:44:18 +00:00
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
host_disable " $type " " $domain " " $valeur "
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 '; "
2011-06-07 09:24:35 +00:00
echo 1 > " $RELOAD_WEB "
2011-01-29 15:46:33 +00:00
done
2006-04-26 12:28:53 +00:00
2011-01-28 15:55:26 +00:00
# Domains we do not want to be the DNS serveur anymore :
# domaines.dns_action = UPDATE and domaines.gesdns = 0
2011-03-27 17:34:02 +00:00
for dom in ` mysql_query "select domaine from domaines where dns_action = 'UPDATE' and gesdns = 0;" | tr '\n' ' ' `
do
2011-01-28 15:55:26 +00:00
dns_delete $dom
2011-02-01 22:44:18 +00:00
mysql_query " update domaines set dns_action = 'OK', dns_result = ' $? ' where domaine = ' $dom ' "
2011-06-07 09:24:35 +00:00
echo -n " $dom " >> " $RELOAD_ZONES "
2011-01-28 15:55:26 +00:00
done
2006-04-26 12:28:53 +00:00
2011-01-28 15:55:26 +00:00
# Domains we have to update the dns :
# domaines.dns_action = UPDATE
2011-03-27 17:34:02 +00:00
for dom in ` mysql_query "select domaine from domaines where dns_action = 'UPDATE';" | tr '\n' ' ' `
do
echo " dns_regenerate : domain=/ $dom / "
2011-01-28 15:55:26 +00:00
dns_regenerate $dom
2011-02-01 22:44:18 +00:00
mysql_query " update domaines set dns_action = 'OK', dns_result = ' $? ' where domaine = ' $dom ' "
2011-06-07 09:24:35 +00:00
echo -n " $dom " >> " $RELOAD_ZONES "
2011-01-28 15:55:26 +00:00
done
2006-04-26 12:28:53 +00:00
2011-01-28 15:55:26 +00:00
# Domains we want to delete completely, now we do it
# domaines.dns_action = DELETE
2011-03-27 17:34:02 +00:00
for dom in ` mysql_query "select domaine from domaines where dns_action = 'DELETE';" | tr '\n' ' ' `
do
2011-01-28 15:55:26 +00:00
dns_delete $dom
2011-02-01 17:17:58 +00:00
# Web configurations have already bean cleaned previously
2011-03-06 18:06:27 +00:00
mysql_query " delete from sub_domaines where domaine=' $dom '; delete from domaines where domaine=' $dom '; "
2011-06-07 09:24:35 +00:00
echo -n " $dom " >> " $RELOAD_ZONES "
2011-01-28 15:55:26 +00:00
done
2006-04-26 12:28:53 +00:00
2011-06-07 09:24:35 +00:00
if [ ! -z " $( cat " $RELOAD_WEB " ) " ] ; then
echo " apache " >> " $RELOAD_ZONES "
2011-03-07 10:15:38 +00:00
# Concat the apaches files
tempo = $( mktemp " $VHOST_FILE .XXXXX " )
2012-08-21 18:25:56 +00:00
(
echo "###BEGIN OF ALTERNC AUTO-GENERATED FILE - DO NOT EDIT MANUALLY###"
# If exists and readable, include conf file "apache_logformat.conf"
# contain LogFormat and CustomLog directives for our Vhosts)
echo "## LogFormat informations"
if [ ! -r " $LOGFORMAT_FILE " ] ; then
echo " ## Warning : Cannot read $LOGFORMAT_FILE "
else
echo " Include \" $LOGFORMAT_FILE \" "
fi
find " $VHOST_DIR " -mindepth 2 -type f -iname "*.conf" -print0 | xargs -0 cat
echo "###END OF ALTERNC AUTO-GENERATED FILE - DO NOT EDIT MANUALLY###"
) > " $tempo "
2011-03-07 10:15:38 +00:00
if [ $? -ne 0 ] ; then
log_error " web file concatenation failed"
fi
touch " $VHOST_FILE "
if [ ! -w " $VHOST_FILE " ] ; then
log_error " cannot write on $VHOST_FILE "
fi
mv " $tempo " " $VHOST_FILE "
2011-03-07 10:09:20 +00:00
fi
2011-10-18 18:25:47 +00:00
# What do we reload ?
lst_zones = $( cat " $RELOAD_ZONES " | tr '\n' ' ' )
if [ -e " $DNS_DO_RESTART " ] ; then
lst_zones = " dns_daemon $lst_zones "
fi
2011-03-07 10:15:38 +00:00
# we assume we run apache and bind on the master
2011-10-18 18:25:47 +00:00
/usr/bin/alternc_reload $lst_zones || true
2011-03-04 13:45:40 +00:00
for slave in $ALTERNC_SLAVES ; do
if [ " $slave " != "localhost" ] ; then
2011-10-18 18:25:47 +00:00
ssh alternc@$slave alternc_reload $lst_zones || true
2011-03-04 13:45:40 +00:00
fi
done
2009-07-27 21:21:26 +00:00
2011-10-18 18:25:47 +00:00
rm -f " $LOCK_FILE " " $RELOAD_ZONES " " $RELOAD_WEB " " $DNS_DO_RESTART "
2006-04-26 12:28:53 +00:00
2011-01-28 15:55:26 +00:00
exit 0
2006-04-26 12:28:53 +00:00