Skip to content

Instantly share code, notes, and snippets.

@mm53bar
Last active December 15, 2015 07:49
Show Gist options
  • Save mm53bar/5226101 to your computer and use it in GitHub Desktop.
Save mm53bar/5226101 to your computer and use it in GitHub Desktop.

Simpler bash recipes

Goal

Build install scripts that are stand-alone bash

Secondary goal

Support "dry runs", where the guard statements can be executed without actually running the installation statements

Notes

With this code, a $DRY_RUN environment variable needs to be set to enable dry run functionality. If it is set, the installation statements won't execute and a message will be displayed instead.

# Install deploy user
#
# Environment variables required:
#
# DEPLOYER # should be same as $APP_NAME
# DRY_RUN # if set, don't execute install
function check_deployer() {
test -f /home/$1/.ssh/authorized_keys
}
function create_deployer() {
useradd --create-home --shell /bin/bash --user-group --groups admin $1
mkdir -p /home/$1/.ssh
cp files/id_rsa.pub /home/$1/.ssh/authorized_keys
chmod 700 /home/$1/.ssh
chmod 600 /home/$1/.ssh/authorized_keys
chown -R $1:$1 /home/$1/.ssh
}
if ! check_deployer "$DEPLOYER"; then
echo "Creating user '$DEPLOYER'"
[[ $DRY_RUN ]] || create_deployer "$DEPLOYER"
fi
# Install deploy user
#
# $1 - deploy_user
# $2 - ssh_key_file
useradd --create-home --shell /bin/bash --user-group --groups admin $1
if test -f /home/$1/.ssh/authorized_keys ; then
echo 'authorized_keys already created'
else
mkdir -p /home/$1/.ssh
cp files/$2 /home/$1/.ssh/authorized_keys
chmod 700 /home/$1/.ssh
chmod 600 /home/$1/.ssh/authorized_keys
chown -R $1:$1 /home/$1/.ssh
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment