Chef and RBENV

by customerio
57 deployments · 20 still active · last rev. 4 years ago

Compatible with: No distros currently supported
						#!/bin/bash
# stackscript: RVM and Chef Solo
# Made by: http://customer.io
# Derived from: 2438, 2253, 1635, 1950

# <UDF name="deploy_user" Label="Name of deployment user" default="deploy" />
# <UDF name="deploy_password" Label="Password for deployment user" />
# <UDF name="new_hostname" Label="Server's hostname" default="appserver" />
# <UDF name="key_url" label="Location of SSH public Key" />

source <ssinclude StackScriptID="1">  # common bash functions
source <ssinclude StackScriptID=123>  # Awesome ubuntu utils script

# Setting up functions

function log {
  echo "### $1 -- `date '+%D %T'`"
}

function create_deployment_user {
  system_add_user $DEPLOY_USER $DEPLOY_PASSWORD "users,sudo"
  system_update_locale_en_US_UTF_8
  cp ~/.gemrc /home/$DEPLOY_USER/
  chown $DEPLOY_USER:$DEPLOY_USER /home/$DEPLOY_USER/.gemrc
}

function system_install_logrotate {
  apt-get -y install logrotate
}

function install_essentials {
  aptitude -y install build-essential libpcre3-dev libssl-dev libcurl4-openssl-dev libreadline5-dev libxml2-dev libxslt1-dev libmysqlclient-dev openssh-server git-core
  good_stuff
}

# Start Installing

log "Updating system..."
system_update
postfix_install_loopback_only
goodstuff

log "Installing essentials"
install_essentials

log "Setting basic security settings"
system_security_fail2ban
system_security_ufw_install
system_security_ufw_configure_basic
system_sshd_permitrootlogin No
system_sshd_passwordauthentication No
system_sshd_pubkeyauthentication Yes
/etc/init.d/ssh restart

log "installing log_rotate"
system_install_logrotate

log "Setting hostname to $NEW_HOSTNAME"
system_update_hostname $NEW_HOSTNAME

log "Creating deployment user $DEPLOY_USER"
create_deployment_user

log "Adding $DEPLOY_USER to sudoers"
cat >> /etc/sudoers <<EOF
Defaults !secure_path
$DEPLOY_USER ALL=(ALL) NOPASSWD: ALL
EOF

log "Adding authorized keys"

#Fetch the key
wget $KEY_URL -O ssh_key
mkdir /home/$DEPLOY_USER/.ssh
mv ssh_key /home/$DEPLOY_USER/.ssh/authorized_keys

#Be very blunt about permissions
chown -R $DEPLOY_USER:$DEPLOY_USER /home/$DEPLOY_USER/.ssh/
chmod 640 /home/$DEPLOY_USER/.ssh/authorized_keys 
chmod 750 /home/$DEPLOY_USER/.ssh/

log "Installing Ruby dependencies"
apt-get -y install curl git-core bzip2 build-essential zlib1g-dev libssl-dev

log "Installing rbenv as deploy user"
git clone git://github.com/sstephenson/rbenv.git /home/$DEPLOY_USER/.rbenv
mkdir -p /home/$DEPLOY_USER/.rbenv/plugins
(cd /home/$DEPLOY_USER/.rbenv/plugins && git clone git://github.com/sstephenson/ruby-build.git)
echo 'export PATH="/home/'$DEPLOY_USER'/.rbenv/bin:$PATH"' >> /home/$DEPLOY_USER/.profile
echo 'eval "$(rbenv init -)"' >> /home/$DEPLOY_USER/.profile
chown -R $DEPLOY_USER:$DEPLOY_USER /home/$DEPLOY_USER/.rbenv/
su deploy -c "source ~/.profile && rbenv install 1.9.3-p194"
su deploy -c "source ~/.profile && rbenv rehash"
su deploy -c "source ~/.profile && rbenv global 1.9.3-p194"

log "Updating Ruby gems"
cat > /home/$DEPLOY_USER/.gemrc << EOF
verbose: true
bulk_treshold: 1000
install: --no-ri --no-rdoc --env-shebang
benchmark: false
backtrace: false
update: --no-ri --no-rdoc --env-shebang
update_sources: true
EOF
chown -R $DEPLOY_USER:$DEPLOY_USER /home/$DEPLOY_USER/.gemrc

su deploy -c "source ~/.profile && gem update --system"

log "Install Bundler"
su deploy -c "source ~/.profile && gem install bundler"

log "Installing Chef"
su deploy -c "source ~/.profile && gem install chef"
log "Configuring Chef solo"
mkdir /etc/chef
cat >> /etc/chef/solo.rb <<EOF
file_cache_path "/tmp/chef"
cookbook_path "/tmp/chef/cookbooks"
role_path "/tmp/chef/roles"
EOF

restartServices