scripts/cluster/cluster-deploy.sh

77 lines
1.8 KiB
Bash
Raw Normal View History

2013-04-29 22:43:41 +02:00
#!/bin/sh
#
# cluster-deploy.sh, Copyright © 2013 Matteo Cypriani <mcy@lm7.fr>
#
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
#
# Deploy files on a number of remote hosts using Parallel SCP.
#set -x
2013-04-29 22:43:41 +02:00
print_usage()
{
echo "Usage:"
echo " $0 [-P] [-l login] <hosts_list> <file1> [file2 [...]]"
echo "-P: transfer files in parallel instead of one by one."
}
# Parse the optional arguments
while [ $# -gt 2 ] ; do
case $1 in
"-l")
LOGIN="$2"
shift
shift
;;
"-P")
PARALLEL=1
shift
;;
*)
break
;;
esac
done
# Do we still have at least a host list and a file?
# Note: if the program was wrongly called but still has 2 arguments, for
# example with "cluster-deploy -l root -P my_file", it will try to use
# "-P" as hosts' list and "my_file" as the file to be transfered.
if [ $# -lt 2 ] ; then
print_usage
2013-04-29 22:43:41 +02:00
exit 1
fi
# Check dependencies
2013-04-29 22:43:41 +02:00
PSCP=$(command -v parallel-scp || command -v pscp.pssh || command -v pscp)
if [ "$PSCP" = "" ] ; then
echo "Parallel SSH (pssh) is required for this script to work."
exit 4
2013-04-29 22:43:41 +02:00
fi
# Hosts' list file
2013-04-29 22:43:41 +02:00
HOSTS="$HOME/.config/cluster/$1.lst"
shift
echo "Using file '$HOSTS' as hosts' list."
# Login
if [ "$LOGIN" != "" ] ; then
echo "Login: $LOGIN"
LOGIN="-l $LOGIN"
fi
# Transfer the files in parallel...
if [ "$PARALLEL" = "1" ] ; then
exec $PSCP -r $LOGIN -h "$HOSTS" -- "$@" ~
fi
# ... or one by one
for FILE in "$@" ; do
2013-04-29 22:43:41 +02:00
echo "Deploying '$FILE'..."
$PSCP -r $LOGIN -h "$HOSTS" -- "$FILE" ~
2013-04-29 22:43:41 +02:00
done