#!/bin/bash #set -x # Parse command line options using getopt options=$(getopt -o f:l:o:ah --long first:,last:,only:,all,help -- "$@") eval set -- "$options" LIST="" # get node definitions NODES="0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 50 51 52 53 54 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84" #all default values are according to the testbed first_mendatory=true last_mendatory=true start_node=0 # default stop_node=24 # default current=0 path="/home/apu/testbed_files/apu-tb-opt/" # default remote="/opt/" # default login="root" host_ip="" all=false # Function to resolve th ip of a given host function resolveip(){ local host="$1" if [ -z "$host" ];then host_ip="resolving failed" else local ip=$( getent hosts "$host" | awk '{print $1}' ) if [ -z "$ip" ];then ip=$( dig +short "$host" ) if [ -z "$ip" ];then # echo "unable to resolve '$host'" >&2 host_ip="resolving failed" else # echo "$ip" host_ip=$ip fi else # echo "$ip" host_ip=$ip fi fi } # Function to display usage information function display_usage { echo "Usage: $0 [--first <[0..84]>] [--last <[0..84]>] [--only <[0..84]>] [--help]" echo "Example: $0 -f 2 -l 10" } # Function to display help information function display_help { echo "This is script can be used, to deploy the /opt folder to the apu testbed nodes" echo display_usage echo echo "Options:" echo " --first (-f) Set the start node (0)" echo " --last (-l) Set the stop node (24)" echo " --only (-o) Only one node" echo " --help (-h) Displays help" } # argument parsing while true; do case "$1" in -f|--first) start_node="$2" shift 2 ;; -l|--last) stop_node="$2" shift 2 ;; -o|--only) start_node="$2" stop_node="$2" shift 2 ;; -h|--help) display_help exit 0 ;; --) shift break ;; *) display_usage exit 1 ;; esac done # arranging node list LIST=$(seq $start_node 1 $stop_node) # check ranges if [[ $start_node -gt 84 || $start_node -lt 0 || $stop_node -gt 84 || $stop_node -lt 0 ]];then echo "Nodes out of range" echo "" display_help exit 1 fi echo "Copying $path to $remote of nodes apu$start_node to apu$stop_node" echo "" # interate list for i in $LIST ; do found=false # search for node in defined node list (array) for j in "${NODES[@]}"; do for k in $j; do #echo $j if [[ $i -eq $k ]]; then found=true fi done done if [[ $i -lt 10 ]];then current=0$i else current=$i fi if [ "$found" = true ]; then # delete node from known-host list to avoid problems when switching between deb and tc ssh-keygen -f "/home/apu/.ssh/known_hosts" -R "apu$current" > /dev/null 2>&1 # get node ip resolveip "apu$current" echo "Copying $path to apu$current with the IP $host_ip..." sudo rsync -ahx --stats --info=progress2 -e 'ssh -o StrictHostKeyChecking=no' $path root@"apu$current":$remote else # only inform the user about missing node defines when defining ranges by him-/herself if [ "$all" = false ]; then echo "Node apu$current not defined" fi fi done #set +x