Skip to content

Instantly share code, notes, and snippets.

@alanstevens
Created May 21, 2012 20:17
Show Gist options
  • Save alanstevens/2764397 to your computer and use it in GitHub Desktop.
Save alanstevens/2764397 to your computer and use it in GitHub Desktop.
Script to setup ruby and user accounts on a RPM box
#!/usr/bin/env bash
#
# execute this script with:
# curl https://raw.github.com/gist/2764397/gistfile1.sh | sudo bash
#
function add_user(){
local user_name=$1
local public_key=$2
echo -e "\nAdding user account: $user_name\n"
#
# create user account and home directory
#
sudo useradd -m -s /bin/bash $user_name
#
# add user to the rvm group to manage system rubies
#
sudo usermod -aG rvm $user_name
#
# add user to the web group to manage web sites
#
sudo usermod -a -G www-data $user_name
#
# write the user's public key to their authorized keys file
#
sudo mkdir -p /home/$user_name/.ssh
sudo curl $public_key | sudo tee /home/$user_name/.ssh/authorized_keys
#
# set ownership and permissions on authorized_keys
#
sudo chown -R $user_name:$user_name /home/$user_name/.ssh
sudo chmod -R 0751 /home/$user_name/.ssh
#
# add user to sudoers list with no password required (account has no password)
#
sudo grep $user_name /etc/sudoers
if [ $? -ne 0 ];then
(sudo cat /etc/sudoers;sudo echo "$user_name ALL=(ALL) NOPASSWD: ALL") | tee -a ~/tmp_sudoers
sudo chmod 0440 ~/tmp_sudoers
sudo visudo -q -c -s -f ~/tmp_sudoers
if [ $? -ne 0 ];then
echo -e "\nERROR: There is a problem with the sudoers configuration.\n Please review ~/tmp_sudoers.\n" && return 1
fi
sudo mv -f ~/tmp_sudoers /etc/sudoers
fi
}
sudo yum update
sudo yum install -y bash curl git libxslt libxslt-devel
sudo yum install -y gcc-c++ patch readline readline-devel zlib zlib-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison libxml2-devel
sudo yum install -y make bzip2
#sudo yum install -y iconv-devel
#
# create rvm group for managing system rubies
#
sudo mkdir -p /usr/local/rvm
sudo groupadd rvm
sudo chown -R root:rvm /usr/local/rvm
sudo chmod -R g+w /usr/local/rvm
#
# create alan and andrew's accounts
#
add_user 'alan' 'https://dl.dropbox.com/s/qfo16yktbn23q9j/id_rsa.pub?dl=1'
add_user 'andrew' 'https://dl.dropbox.com/s/2sld4rsbhl0o093/authorized_keys?dl=1'
#
# Ensure that /usr/local/bin is in the path
#
PATH=$(echo "/usr/local/bin:$PATH" | tr -s ':' '\n' | awk '!($0 in a){a[$0];print}' | tr -s '\n' ':' | sed 's#:$##')
#
# Configure system level gem settings.
#
echo -e "Disabling ri & rdoc system wide for gem installations and upgrades."
sudo echo "install: --no-rdoc --no-ri" >> /etc/gemrc
sudo echo "update: --no-rdoc --no-ri" >> /etc/gemrc
#
# Install rvm at the system level.
#
sudo bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
#
# rvm system level configuration.
#
sudo rm -f /etc/rvmrc
sudo echo "rvm_path=/usr/local/rvm" | sudo tee /etc/rvmrc
sudo echo "export rvm_gemset_create_on_use_flag=1" | sudo tee -a /etc/rvmrc
#
# rvm profile.d entry
#
sudo mkdir -p /etc/profile.d
sudo rm -f /etc/profile.d/rvm.sh
(
cat <<-File
# Load RVM if it is installed,
# first try to load user install
# then try to load root install, if user install is not there.
if [ -s "$HOME/.rvm/scripts/rvm" ] ; then
source "$HOME/.rvm/scripts/rvm"
elif [ -s "/usr/local/rvm/scripts/rvm" ] ; then
source "/usr/local/rvm/scripts/rvm"
fi
File
) | sudo tee /etc/profile.d/rvm.sh
#
# make sure root can use rvm
#
echo 'source /usr/local/rvm/scripts/rvm' | sudo tee /root/.bashrc
#
# source rvm in the current shell session
#
source /etc/profile.d/rvm.sh
#
# Install Ruby and set system defaults
#
rvm install 1.9.3-p194
rvm use 1.9.3-p194 --default
gem update --system
rvm use 1.9.3-p194@global
#
# set the hostname
#
if [ "$1" != "" ];then
hostName=$1
echo -e "\nSetting host name to \"$hostName\"\n"
echo "$hostName" > /etc/hostname
(echo "127.0.0.1 $hostName $hostName"; cat /etc/hosts) > ~/hosts
chmod 644 ~/hosts
mv -f ~/hosts /etc/hosts
hostname -F /etc/hostname
fi
#
# set timezone to Universal Coordinated Time
#
ln -sf /usr/share/zoneinfo/UTC /etc/localtime
#
# disable root login and password authentication over ssh
#
(cat /etc/ssh/sshd_config;echo "PermitRootLogin no") | sed 's/#PasswordAuthentication yes/PasswordAuthentication no/g' > ~/sshd_config
chmod 0644 ~/sshd_config
mv -f ~/sshd_config /etc/ssh/sshd_config
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment