From 212c211d22b3769ea048603a41c77b6926c6bfa6 Mon Sep 17 00:00:00 2001 From: david Date: Sun, 7 Jul 2013 19:56:31 +0200 Subject: [PATCH] added deployment feature --- sshbackup | 71 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 39 insertions(+), 32 deletions(-) diff --git a/sshbackup b/sshbackup index 0268559..2754f1c 100755 --- a/sshbackup +++ b/sshbackup @@ -31,7 +31,7 @@ usage() echo " -s, --sshkey alternate sshkey [~/.ssh/id_rsa]" echo " -b, --bandwidth bandwidth limit in kbit/s" echo - echo " -d, --deploy deploy settings to remote host" + echo " -d, --deploy deploy settings to remote host" echo " -n, --no-root run without root privileges" echo } @@ -44,21 +44,6 @@ version() echo } -pipewrap() -{ - echo $1 - local lockfile=$2; - while true; do - if [ ! -e $lockfile ]; then - return 0 - fi - read -t 1 line - if [ $? -eq 0 ]; then - echo $line - fi - done -} - interactive() { tty -s @@ -91,17 +76,38 @@ findhome() echo $home } +#this function is needed to pass on the +#password to the remote sudo command +pipewrap() +{ + echo $1 + local lockfile=$2; + while true; do + if [ ! -e $lockfile ]; then + return 0 + fi + read -t 1 line + if [ $? -eq 0 ]; then + echo $line + fi + done +} + deploy() { - local machine=$1 #user@machine.example.com - local user=${machine%@*} - local machine=${machine#*@} + local machine=$1 #user@machine.example.com + local user=${machine%@*} #user + if [ -z $user ]; then + user=$USER #set $USER if none is specified + fi + local machine=${machine#*@} #machine.example.com local username="" local password="" local script="/tmp/sshbackup_deploy`date +%s`" local pubkey=$(cat $2) - #create temporary deployment script + #create script which is executed on remote host (as root) + #TODO: check dependencies on remote system as well echo '#!/bin/bash user="'$user'" pubkeyfile="'$pubkeyfile'" @@ -122,12 +128,16 @@ deploy() sshgroups=${sshgroups#AllowGroups } usermod -a -G ${sshgroups// /,} '$user' echo "'$user' ALL=(root)NOPASSWD: /usr/bin/rsync" >> /etc/sudoers - echo "aborting mission. useradd returned a non-zero value." + echo "remote settings deployed. hopefully :)" fi' > $script - echo -e "please enter your [sudo] username for $machine: \c" + echo "i will now attempt to create the user $user and apply all needed" + echo "settings on following remote host: $machine" + echo "in order to do so i need a sudo enabled username and" + echo "password on the remote host." + echo -e "please enter your [sudo] username: \c" read username - echo -e "please enter your [sudo] password for $machine: \c" + echo -e "please enter your [sudo] password: \c" read -s password sshpass -p "$password" scp -q "$script" "$username@$machine:'$script'" @@ -139,7 +149,6 @@ deploy() eval pipewrap '$password' '$lockfile' | (sshpass -p "$password" ssh -q "$username@$machine" "sudo -S '$script'"; rm "$lockfile") sshpass -p $password ssh -q "$username@$machine" rm $script rm $script - exit 1 } preflight() @@ -196,7 +205,9 @@ preflight() if [ -r $privkeyfile ]; then #ssh key found - : + if ( interactive ) && [ $deploy -eq 1 ]; then + deploy ${sourcepath%:*} $pubkeyfile + fi else if ( interactive ); then echo -e "no ssh key found. do you want to create a new key pair? [y/n] \c" @@ -211,8 +222,9 @@ preflight() echo "aborting mission. error occured while creating ssh key pair" return 1 fi - #TODO: key should also be deployed to remote side - return 1 #for now i'll break up here + #deploy key to remote system + deploy ${sourcepath%:*} $pubkeyfile + return 0 else echo "aborting mission. no ssh key found." return 1 @@ -222,7 +234,6 @@ preflight() return 1 fi fi - deploy ${sourcepath%:*} $pubkeyfile fi return 0 @@ -373,10 +384,6 @@ for option in $options; do echo "aborting mission. cannot read listfile. [$option]" exit 1 fi - elif [ $deploy -eq 1 ]; then - #TODO: do nothing for now - : - deploy=0 else if [[ $option =~ ^-.* ]]; then echo "aborting mission. unknown option given. [$option]"