Created
April 28, 2016 19:44
-
-
Save Dude4Linux/fe0ae9c5095011b8e0813608e42e3b78 to your computer and use it in GitHub Desktop.
Apply my personal customizations to a freshly install TurnKey appliance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# --------------------------------------------------------------------------- | |
# turnkey-setup - Setup and configure a freshly installed TurnKey appliance | |
# Install ssh key for automated remote login | |
# Install vim-nox | |
# Setup additional bash aliases | |
# Copyright 2013, John Carver <dude4linux@gmail.com> | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License at (http://www.gnu.org/licenses/) for | |
# more details. | |
# Usage: turnkey-setup [-h|--help] [-v|--version] | |
# Revision history: | |
# 2013-12-12 Released ver. 1.0, John Carver <dude4linux@gmail.com> | |
# 2013-12-04 Created by new_script ver. 3.0, William Shotts <bshotts@users.sourceforge.net> | |
# --------------------------------------------------------------------------- | |
PROGNAME=${0##*/} | |
VERSION="1.0" | |
clean_up() { # Perform pre-exit housekeeping | |
return | |
} | |
error_exit() { | |
echo -e "${PROGNAME}: Error: ${1:-"Unknown Error"}" >&2 | |
clean_up | |
exit 1 | |
} | |
graceful_exit() { | |
clean_up | |
exit | |
} | |
signal_exit() { # Handle trapped signals | |
case $1 in | |
INT) error_exit "Program interrupted by user" ;; | |
TERM) echo -e "\n$PROGNAME: Program terminated" >&2 ; graceful_exit ;; | |
*) error_exit "$PROGNAME: Terminating on unknown signal" ;; | |
esac | |
} | |
usage() { | |
echo -e "Usage: $PROGNAME [-h|--help] [-v|--version] hostname\n" | |
} | |
version() { | |
echo -e "$PROGNAME ver. $VERSION" | |
} | |
help_message() { | |
cat << _EOF_ | |
$(version) | |
Setup and configure a freshly installed TurnKey appliance | |
1) Install ssh key for automated remote login | |
2) Install vim-nox | |
3) Setup additional bash aliases | |
$(usage) | |
Options: | |
-h, --help Display this help message and exit. | |
-v, --version Display version and exit. | |
_EOF_ | |
return | |
} | |
# Trap signals | |
trap "signal_exit TERM" TERM HUP | |
trap "signal_exit INT" INT | |
# Parse command-line | |
while [[ -n $1 ]]; do | |
case $1 in | |
-h | --help) help_message; graceful_exit ;; | |
-v | --version) version; graceful_exit ;; | |
-* | --*) usage; error_exit "Unknown option $1" ;; | |
*) break ;; | |
esac | |
shift | |
done | |
## Main logic | |
HOST=$1 | |
IP=$(host $HOST | awk '/has address/ { print $4 }') | |
[[ $HOST == "" ]] && usage && error_exit "Must specify hostname on command line" | |
[[ $IP == "" ]] && error_exit "Host $HOST not found" | |
# Remove HOST and IP from known_hosts | |
ssh-keygen -q -R $HOST | |
ssh-keygen -q -R $IP | |
# Setup remote login credentials | |
ssh-copy-id -i ~/.ssh/id_rsa root@$HOST | |
# Execute remote script | |
ssh root@$HOST << 'ENDSSH' | |
# Initialize Vars for remote host | |
ALIAS=~/.bashrc.d/add_alias | |
KEYS=~/.ssh/authorized_keys | |
# Remove duplicate keys, if any due to bug in ssh-copy-id | |
sort -u -o $KEYS $KEYS | |
if [[ -f /etc/turnkey_version ]]; then | |
# Update appliance packages | |
apt-get -q update | |
apt-get -q -y upgrade | |
apt-get -q -y autoremove | |
# Setup Vim | |
echo -e "Installing Vim-nox and Plugins" | |
apt-get -q -y install vim-nox | |
# Setup aliases | |
if [[ ! -f $ALIAS ]]; then | |
echo -e "Setting additional aliases" | |
cat > $ALIAS << '_EOF_' | |
if [ "$TERM" != "dumb" ]; then | |
alias ll="ls -alh --color=auto" | |
alias la="ls -A --color=auto" | |
alias l="ls -C --color=auto" | |
fi | |
# Simplify SourceForge downloads | |
alias wget="wget --trust-server-names" | |
_EOF_ | |
chmod a+x $ALIAS | |
fi | |
echo -e "Finished TurnKey Setup on $(hostname)" | |
else | |
echo -e "Host $(hostname) is not a TurnKey appliance" | |
fi | |
ENDSSH | |
graceful_exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment