Ruby / nginx / PostgreSQL

by mhutter
27 deployments · 6 still active · last rev. 4 years ago

[Versions as of 2013-02-28!]

- latest stable PostgreSQL from ppa:pitti/postgresql [9.2]
- latest stable nginx from ppa:nginx/stable [1.2]
- Ruby via rbenv (any Version you like/is available via ruby-build)
- bundler

`tail -f /root/setup.log` to watch progress

Compatible with: No distros currently supported
						#!/bin/bash
#
# <udf name="username" label="User Name" default="deploy">
# <udf name="password" label="Password for user">
# <udf name="pubkey" label="Public Key for User" example="ssh-rsa AA... user@host">
# <udf name="host_name" label="Hostname" default="" example="leave empty to set the default hostname">
# <udf name="ruby_ver" label="Ruby Version" example="For available Versions see https://github.com/sstephenson/ruby-build/tree/master/share/ruby-build">
# <udf name="rr_env" label="Rails/Rack environment to run" default="production">

source <ssinclude StackScriptID=1>  # Common bash functions

function log {
  echo "$(date '+%D %T') $1" >> /root/setup.log
}
function as_user {
  su -l -c "$1" $USERNAME
}

cd /tmp

# Set up Hostname
if [ -z "${HOST_NAME}" ]; then
  HOST_NAME="$(get_rdns_primary_ip)"
fi
echo "$HOST_NAME" > /etc/hostname
hostname -F /etc/hostname

# Updating System, installing essentials
log "update system"
system_update
log "install essentials"
aptitude -y install \
  build-essential \
  zlib1g-dev \
  libssl-dev \
  libreadline5-dev \
  openssh-server \
  libyaml-dev \
  libcurl4-openssl-dev \
  libxslt-dev \
  libxml2-dev \
  python-software-properties \
  git-core
goodstuff
log "essentials installed!"

# setup Postfix
log "set up postfix"
postfix_install_loopback_only


# Installing PostgreSQL 9.2 and nginx
log "Installing PostgreSQL 9.2 and nginx"
case "`lsb_release -si`" in
  "Ubuntu")
    add-apt-repository -y ppa:pitti/postgresql
    add-apt-repository -y ppa:nginx/stable
  ;;
  "Debian")
    # PostgreSQL
    echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -sc`-pgdg main" > /etc/apt/sources.list.d/pgdg.list
    wget -q -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
    # nginx
    wget -q -O - http://nginx.org/keys/nginx_signing.key | apt-key add -
    cat <<EOT > /etc/apt/sources.list.d/nginx.list
deb http://nginx.org/packages/debian/ `lsb_release -sc` nginx
deb-src http://nginx.org/packages/debian/ `lsb_release -sc` nginx
EOT
  ;;
esac

aptitude update
aptitude -y install postgresql-9.2 nginx
log "$(rm -v /etc/nginx/sites-enabled/*)"
log "$(service nginx start)"


# Creating User
log "Create User '${USERNAME}'"
echo "${USERNAME}:${PASSWORD}:1000:1000::/home/${USERNAME}:/bin/bash" | newusers
cp -a /etc/skel/.[a-z]* /home/$USERNAME
mkdir /home/${USERNAME}/.ssh
echo $PUBKEY >> /home/${USERNAME}/.ssh/authorized_keys
chown -R $USERNAME /home/$USERNAME
echo "${USERNAME} ALL=(ALL) ALL" >> /etc/sudoers


# Installing rbenv
log "setting up rbenv"
as_user "git clone git://github.com/sstephenson/rbenv.git ~/.rbenv"
as_user "echo 'export PATH=\"\$HOME/.rbenv/bin:\$PATH\"' >> ~/.profile"
as_user "echo 'eval \"\$(rbenv init -)\"' >> ~/.profile"
as_user "git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build"
log "Installing Ruby ${RUBY_VER}"
as_user "rbenv install ${RUBY_VER} && rbenv rehash && rbenv global ${RUBY_VER}"
as_user "echo 'gem: --no-document' > ~/.gemrc && gem i bundler"


# Setting up Environment
if [ ! -n "$RR_ENV" ]; then
  RR_ENV="production"
fi
cat >> /etc/environment << EOF
RAILS_ENV="$RR_ENV"
RACK_ENV="$RR_ENV"
EOF


# Finishing
restartServices
log "StackScript Finished!"
echo "StackScript completed. I may need a reboot..." | wall