Created
July 21, 2014 02:03
-
-
Save luginbash/7387e27b4e4eaacc5ba2 to your computer and use it in GitHub Desktop.
Functions I used to deploy a Debian server.
This file contains hidden or 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 | |
# | |
# debian deployment script base, this script does nothing itself. | |
# | |
########################################################### | |
# System | |
########################################################### | |
function updateSystem { | |
apt-get update | |
apt-get -y install aptitude | |
aptitude -y full-upgrade | |
} | |
function get_rdns_primary_ip { | |
echo $(get_rdns $(system_primary_ip)) | |
} | |
function addRepo { | |
# add properties common | |
aptitude -y install software-properties-common | |
# MariaDB | |
apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db | |
add-apt-repository 'deb http://ftp.osuosl.org/pub/mariadb/repo/10.0/debian wheezy main' | |
# Nginx | |
curl http://nginx.org/keys/nginx_signing.key | apt-key add - | |
echo "deb http://nginx.org/packages/mainline/debian/ wheezy nginx" | tee -a /etc/apt/sources.list | |
echo "deb-src http://nginx.org/packages/mainline/debian/ wheezy nginx" | tee -a /etc/apt/sources.list | |
# Varnish cache | |
curl http://repo.varnish-cache.org/debian/GPG-key.txt | apt-key add - | |
echo "deb http://repo.varnish-cache.org/ubuntu/ wheezy varnish-3.0" | tee -a /etc/apt/sources.list | |
# Update the apt catalogue | |
aptitude update | |
} | |
function configure_user { | |
# $1 - username | |
# $2 - password | |
# $3 - sshkey | |
#configure ssh/sudo | |
useradd -m -s /bin/bash $1 #add user account | |
groupadd sudo | |
usermod -a -G sudo $1 | |
echo "$1:$2" | chpasswd #setpassword | |
#add user to sudoers | |
echo "$1 ALL=NOPASSWD: ALL" >> /etc/sudoers | |
mkdir -p /home/$1/.ssh | |
echo "$3" >> /home/$1/.ssh/authorized_keys | |
chown -R $1:$1 /home/$1/.ssh | |
chmod 700 /home/$1/.ssh | |
chmod 600 /home/$1/.ssh/authorized_keys | |
} | |
function configure_systemSecurity { | |
# NOPASSWD sudo by default | |
# shut ports except tcp 22 | |
# lock out root | |
sed -i 's/PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config | |
aptitude -y install fail2ban ufw | |
passwd -l root | |
printf "y\ny\ny\n" | ufw reset | |
ufw default deny | |
ufw allow ssh | |
ufw logging on | |
printf "y\ny\ny\n" | ufw enable | |
} | |
########################################################### | |
# mariadb-server | |
########################################################### | |
function install_mariadb { | |
# $1 - the mysql root password | |
if [ ! -n "$1" ]; then | |
echo "install_mariadb() requires the root pass as its first argument" | |
return 1; | |
fi | |
echo "mariadb-server-10.0 mariadb-server/root_password password $1" | debconf-set-selections | |
echo "mariadb-server-10.0 mariadb-server/root_password_again password $1" | debconf-set-selections | |
aptitude -y install mariadb-server | |
echo "Sleeping while MySQL starts up for the first time..." | |
sleep 5 | |
} | |
function mysql_create_database { | |
# $1 - the mysql root password | |
# $2 - the db name to create | |
if [ ! -n "$1" ]; then | |
echo "mysql_create_database() requires the root pass as its first argument" | |
return 1; | |
fi | |
if [ ! -n "$2" ]; then | |
echo "mysql_create_database() requires the name of the database as the second argument" | |
return 1; | |
fi | |
echo "CREATE DATABASE $2;" | mysql -u root -p$1 | |
} | |
# mysql commands are compatible with mariadb. | |
function mysql_create_user { | |
# $1 - the mysql root password | |
# $2 - the user to create | |
# $3 - their password | |
if [ ! -n "$1" ]; then | |
echo "mysql_create_user() requires the root pass as its first argument" | |
return 1; | |
fi | |
if [ ! -n "$2" ]; then | |
echo "mysql_create_user() requires username as the second argument" | |
return 1; | |
fi | |
if [ ! -n "$3" ]; then | |
echo "mysql_create_user() requires a password as the third argument" | |
return 1; | |
fi | |
echo "CREATE USER '$2'@'localhost' IDENTIFIED BY '$3';" | mysql -u root -p$1 | |
} | |
function mysql_grant_user { | |
# $1 - the mysql root password | |
# $2 - the user to bestow privileges | |
# $3 - the database | |
if [ ! -n "$1" ]; then | |
echo "mysql_create_user() requires the root pass as its first argument" | |
return 1; | |
fi | |
if [ ! -n "$2" ]; then | |
echo "mysql_create_user() requires username as the second argument" | |
return 1; | |
fi | |
if [ ! -n "$3" ]; then | |
echo "mysql_create_user() requires a database as the third argument" | |
return 1; | |
fi | |
echo "GRANT ALL PRIVILEGES ON $3.* TO '$2'@'localhost';" | mysql -u root -p$1 | |
echo "FLUSH PRIVILEGES;" | mysql -u root -p$1 | |
} | |
########################################################### | |
# Other niceties! | |
########################################################### | |
function goodstuff { | |
# Installs the REAL vim, wget, less, and enables color root prompt and the "ll" list long alias | |
aptitude -y install wget vim less axel tmux subversion git zsh mercurial | |
sed -i -e "s/^#alias ll='ls -l'/alias ll='ls -al'/" /root/.bashrc # enable ll list long alias <3 | |
} | |
########################################################### | |
# utility functions | |
########################################################### | |
function restartServices { | |
# restarts services that have a file in /tmp/needs-restart/ | |
for service in $(ls /tmp/restart-* | cut -d- -f2-10); do | |
/etc/init.d/$service restart | |
rm -f /tmp/restart-$service | |
done | |
} | |
function randomString { | |
if [ ! -n "$1" ]; | |
then LEN=20 | |
else LEN="$1" | |
fi | |
echo $(</dev/urandom tr -dc A-Za-z0-9 | head -c $LEN) # generate a random string | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment