reverse symbolic links

c’est bien plus beau quand c’est inutile :

j’installe un apache , donc dans /var/www /usr/share/apache2 /var/log/httpd /var/run/httpd.pid pour schématiser

mais j’ai un DSI maniaque obsessionnel, il veut que tout soit bien rangé dans /data /softs /logs/ ;

qu’à cela ne tienne, embobinons le avec des liens symboliques

 

/softs/apache/share -> /usr/share/apache2
/softs/apache/bin/httpd -> /usr/bin/httpd
/data/apache/data -> /var/www
/data/apache/config -> /etc/apache2.d
/data/run/httpd.pid -> /var/run/httpd.pid
/logs/apache -> /var/log/httpd

Voilà c’est joli, il n’est pas au centre et pourtant il tourne, comme dirait Galilée.

Mais le DSI est technobsessionnel, il veut que ça aille dans les bon filesystems,
le plus simple est de ne rien renommer mais d’inverser les liens symboliques

-> devient <- , ben voyons…

 

#!/bin/sh
act=$1
apps=$2
 
 
 
DRH_softs="redhat"
DRH_logs="redhat/redhat"
DRH_appli="redhat/redhat"
 
# build apps list
if [ "$apps" = "" ];then
 apps=""
 for rdd in `ls -d /softs/*/redhat /appli/*/redhat/redhat /logs/*/redhat/redhat`;do
 app=`echo $rdd|sed -e "s:/redhat.*$::" -e "s:^.*/::"`
 apps="$apps $app"
 done
 apps=`echo "$apps"|tr " " "\n"|sort -u|tr "\n" " "`
fi
 
echo "$0 [link,tar,unlink]:$act [apps]:$apps"
if [ "$act" = "" ];then exit 10;fi
 
 
for app in $apps;do
 
#count links
 cntl=5
 cntf=0
 for s in appli softs logs;do
 cnt=`find /$s/$app/redhat/ -type l |grep -v /tools/|grep -v /conf|wc -l`
 cntl=`expr $cntl + $cnt`
 cnt=`find /$s/$app/redhat/ -type f |grep -v /tools/|grep -v /conf|wc -l`
 cntf=`expr $cntf + $cnt`
 done
 if [ "$cntl" -ge "$cntf" ];then isapplnk=1;else isapplnk=2;fi
 echo "$app $cntl links, $cntf files : "
 if [ "$isapplnk" = "1" ];then echo "ok to tar or link";fi
 if [ "$isapplnk" = "2" ];then echo "ok to unlink";fi
 isok=0
 if [ $act = "link" ];then if [ "$isapplnk" = "1" ];then isok=1;fi;fi
 if [ $act = "tar" ];then if [ "$isapplnk" = "1" ];then isok=1;fi;fi
 if [ $act = "unlink" ];then if [ "$isapplnk" = "2" ];then isok=1;fi;fi
 if [ $isok = "0" ];then echo "ERR: $act not permitted";exit 5;fi
 
#do tar untar if ok
 ru=/appli/$app/redhat
 rur=/appli/$app/redhat/tar
 mkdir -p $ru;
 cd $ru
 if [ $act != "unlink" ];then #create tar
 rm -f $ru/lnks_$app.tgz
 tar cfz $ru/lnks_$app.tgz /softs/$app/redhat/ /appli/$app/redhat/redhat/ /logs/$app/redhat/redhat/
 echo "OK : $ru/lnks_$app.tgz";
 fi
 #always untar
 if [ ! -f lnks_$app.tgz ];then echo "ERR: lnks_$app.tgz not found";exit 1;fi
 rm -rf $rur;mkdir -p $rur;cd $rur;tar xfz ../lnks_$app.tgz;cd -
 
#do link unlink
 for LNKK in softs appli logs;do
 eval "LNKR=\$LNK_${LNKK}"
 eval "LNKS=/\$LNKK/\$app/\$DRH_${LNKK}"
 eval "DIR_${LNKK}=$LNKS"
 dd="$LNKS"
 rdd="$rur$LNKS"
 #for rdd in `ls -d $r/logs/$app/redhat $r/appli/$app/redhat/redhat $r/softs/$app/redhat/redhat`;do
 #dd=`echo $rdd|sed -e "s:^$r::"`
 ###app=`echo $dd|sed -e "s:/$dr/::" -e "s:/.*$::"`
 #filtre si app=papp ou papp=""
 if [ -d "$rdd" ];then
 for rl in `find $rdd -type l`;do
 l=`echo $rl|sed -e "s:^$rur::"`
 ld=`ls -ld $rl|sed -e "s/^.* -> //"`
 if [ $act = "link" ];then
 if [ -L $l ];then
 islocal=0
 if [ "`echo $ld|sed -e 's:^\.::'`" != "$ld" ];then islocal=1;fi
 if [ "`echo $ld|sed -e 's:/redhat/::'`" != "$ld" ];then islocal=1;fi
 if [ "$islocal" = "1" ];then 
 echo "INFO $l -> $ld local link unchanged"
 else
 if [ -e $ld ];then
 rm -f $l
 mv $ld $l
 ln -s $l $ld
 else
 echo "ERR $ld should exists"
 fi
 fi
 else
 echo "ERR $l should be a link"
 fi
 fi
 if [ $act = "unlink" ];then
 if [ -L $ld ];then
 if [ -e $l ];then
 rm -f $ld
 mv $l $ld
 ln -s $ld $l
 else
 echo "ERR $l should exists"
 fi
 else
 echo "ERR $ld should be a link"
 fi
 fi
 done
 fi
 done
 #clean untared
 #if [ "`basename $rur`" != "tar" ];then exit 99;fi;#just checking
 rm -rf $rur
done

en gros :
j’ai une action link, et unlink pour remettre comme avant, et tar
je fais un tar pour me rappeler comment c’était les links avant
je compte le nombre de fichiers et de liens pour savoir si j’ai déjà fait un link
(mais je pourrais plutôt comparer au tar)
je boucle pour faire un rm B, mv A B;ln -s A B.

je relance, c’est moche mais ça marche.
le rpm ne retrouve pas ses fichiers (à tester, ça se trouve c’est bon ?)
apparmor ou selinux sont un peu perdu (binaire pas en place, ou data déplacées)

un peu flippant.