Improve setup-server

The order of parameters is now changed to make it easier when deploying
to a lot of servers.

Also, it introduces the concept of a friendly name, which is useful when
DNS is not available for a specific host. Instead of being forced to
have the alias be the same as the IP address, it can now be named.

Signed-off-by: Jacob Kiers <jacob@jacobkiers.net>
This commit is contained in:
Jacob Kiers 2015-04-10 13:51:06 +00:00
parent 1415dec58b
commit 3cbff4da78

View File

@ -8,40 +8,59 @@ function red() { echo -e "$RED$*$NORMAL"; }
function green() { echo -e "$GREEN$*$NORMAL"; } function green() { echo -e "$GREEN$*$NORMAL"; }
function yellow() { echo -e "$YELLOW$*$NORMAL"; } function yellow() { echo -e "$YELLOW$*$NORMAL"; }
if [ ! $# == 2 ]; then if [ $# -lt 1 ]; then
echo "Usage: $0 <user> <host>" echo "Usage: $0 <host|IP> [friendly-name] [user]"
echo "Example: $0 127.0.0.1 server-name $USER"
echo
echo "Afterwards you can login with: ssh server-name"
exit 1; exit 1;
fi fi
USER=$1 HOST=$1
HOST=$2 NAME=$2
USERNAME=$3
IP=`dig +short $HOST` IP=`dig +short $HOST`
if [ -z "$USERNAME" ]; then
USERNAME=$USER
fi
if [ -z "$IP" ]; then if [ -z "$IP" ]; then
IP=$HOST IP=$HOST
fi fi
grep "Host $HOST" ~/.ssh/config 2>&1 > /dev/null if [ -z "$NAME" ]; then
NAME=$HOST
fi
yellow "Setting up server $USERNAME@$HOST with name '$NAME'"
grep "Host $NAME" ~/.ssh/config 2>&1 > /dev/null
if [ 0 -eq $? ]; then if [ 0 -eq $? ]; then
echo $HOST is already in .ssh/config echo $NAME is already in .ssh/config
else else
echo Adding server to .ssh/config... echo Adding server to .ssh/config...
cat >> ~/.ssh/config <<END_OF_HOST cat >> ~/.ssh/config <<END_OF_HOST
Host $HOST Host $NAME
HostName $IP HostName $IP
User $USER User $USERNAME
END_OF_HOST END_OF_HOST
fi fi
echo Copying .ssh directory to $USER@$HOST... echo Copying .ssh directory...
scp -qr $HOME/.ssh/ $HOST: scp -qr $HOME/.ssh/ $NAME:
echo Installing dotfiles... echo Installing dotfiles...
ssh $HOST 'find . -maxdepth 1 -type l -exec unlink {} \;; rm $HOME/.bash*; curl -s https://dot.jacobkiers.net | bash' ssh $NAME 'find . -maxdepth 1 -type l -exec unlink {} \;; rm $HOME/.bash*; curl -s https://dot.jacobkiers.net | bash'
echo echo
green Done installing server. You will now be logged in.
ssh $HOST green "Done installing server. You can now log in with: ssh $NAME"
green "As a convenience, you will be logged in immediately."
ssh $NAME