From 504883e55fb42cbcca61e8448177e78d57b62c69 Mon Sep 17 00:00:00 2001
From: David Beniamine <david.beniamine@tetras-libre.fr>
Date: Mon, 26 Aug 2019 10:40:04 +0200
Subject: [PATCH] Handle multiples IPs per clients #26

---
 registry/scripts/upgrade_all.sh | 81 +++++++++++++++++++++++++++------
 1 file changed, 68 insertions(+), 13 deletions(-)

diff --git a/registry/scripts/upgrade_all.sh b/registry/scripts/upgrade_all.sh
index 4da61b7..a3b8eb8 100755
--- a/registry/scripts/upgrade_all.sh
+++ b/registry/scripts/upgrade_all.sh
@@ -16,16 +16,23 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-getip(){
+mainlog="/var/log/`basename $0`_`date +%Y%m%d-%H%M`"
+
+getips(){
     awk '/^lease/{IP=$2} /hardware ethernet/{gsub(";","",$3);if($3 == "'$1'"){print IP}}' \
-        < /var/lib/dhcp/dhcpd.leases | tail -n 1
+        < /var/lib/dhcp/dhcpd.leases | sort -u
 }
 
 runcmd(){
     host=$1
+    logfile=/tmp/$$-$host.log
     shift
-    echo "Runnig $@ on $host"
-    ssh -o StrictHostKeychecking=no root@$host $@
+    echo "Runnig $@ on $host" | tee $logfile
+    ssh -o StrictHostKeychecking=no root@$host $@ | tee $logfile
+}
+
+log(){
+    echo $@ | tee -a $mainlog
 }
 
 # Switch to lametros repository
@@ -39,29 +46,77 @@ wait_time=120
 wait_step=5
 
 cmd="cd /opt/primtux-eole/; export HTTPS_PROXY='http://dnsproxy:3128'; git pull; ./upgrade-session.sh $1"
+declare -A HOSTS
+declare -A IPTOMACS
+declare -A DOWNS
 
 while read date mac name
 do
     # Boot machine
-    wakeonlan -i $broadcast "$mac"
-    MACS+=" $mac"
+    if [ ! -z "$date" ]
+    then
+        HOSTS[$mac]=$name
+        log "Trying to boot $name ($mac)"
+        for i in seq 1 3
+        do
+            wakeonlan -i $broadcast "$mac"
+        done
+        sleep 5
+        MACS+=" $mac"
+    fi
 done < $conf_file
 
 # Wait for boot
-echo ""
+log "Wakeonlan sent, waiting"
 while [ $wait_time -gt 0 ]
 do
-    echo -ne "Waiting for boot, remaining $wait_time seconds\r"
+    log -ne "Waiting for boot, remaining $wait_time seconds\r"
     sleep $wait_step
     wait_time=$(($wait_time - $wait_step))
 done
-echo -e "\nWaiting finished"
+log -e "\nWaiting finished"
 
+# Running command
 for mac in $MACS
 do
-    ip=$(getip $mac)
-    runcmd $ip $cmd &
-    PIDS="$PIDS $!"
+    for ip in $(getips $mac)
+    do
+        IPTOMACS[$ip]=$mac
+        runcmd $ip $cmd &
+        PIDS="$PIDS $!"
+    done
 done
 # wait all pids
-wait $PIDS
+for pid in $PIDS
+do
+    wait $pid
+done
+
+# Retrieving logs
+for f in /tmp/$$-*
+do
+    ip=$(echo $f | cut -d '-' -f 2- | sed 's/\.log$//')
+    mac=${IPTOMACS[$ip]}
+    name=${HOSTS[$mac]}
+    log "===== Begin host $name ($mac - $ip) ===="
+    cat $f >> $mainlog
+    log "===== End host $name ===="
+    if [ $(wc -l $f | awk '{print $1}') -gt 0 ]
+    then
+        DOWNS[$mac]=$name
+    fi
+    rm $f
+done
+
+# Listing down hosts
+if [ ${#DOWNS[@]} -eq 0 ]
+then
+    log "All ${#HOSTS[@]} hosts answered"
+else
+    log "On a total of ${#HOSTS[@]} hosts, ${#DOWNS[@]} are still down:"
+    for mac in ${!DOWNS[@]}
+    do
+        log -e "\t* ${DOWNS[$mac]} ($mac)"
+    done
+fi
+log "Output written to $mainlog"
-- 
GitLab