Skip to content

Instantly share code, notes, and snippets.

@monkseal
Created June 10, 2015 17:10
Show Gist options
  • Save monkseal/5bcfb5e90b6d29d7b550 to your computer and use it in GitHub Desktop.
Save monkseal/5bcfb5e90b6d29d7b550 to your computer and use it in GitHub Desktop.
Provision script for CentOS 6 with Ruby and Postgres 9.4
#!/bin/bash
echo "Installing package dependencies"
sudo yum update
sudo yum install -y apr-devel apr-util-devel autoconf automake curl-devel \
g++ gcc gcc-c++ git glibc-headers httpd-devel libxml2 \
libxml2-devl libxslt libxslt-devel libyaml-devel make \
openssl-devel patch readline \
readline-devel zlib zlib-devel
echo "Installing Posgtgresql Server and dependencies"
sudo yum install -y http://yum.postgresql.org/9.4/redhat/rhel-6-x86_64/pgdg-centos94-9.4-1.noarch.rpm
sudo yum install -y postgresql94 postgresql94-devel postgresql94-server postgresql94-contrib libpq-dev
if ! [ -d /home/vagrant/.rbenv ]; then
echo "Installing rbenv"
git clone git://github.com/sstephenson/rbenv.git ~/.rbenv
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
echo "export PATH=\$HOME/.rbenv/bin:/usr/pgsql-9.4/bin:\$PATH" >> /home/vagrant/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
source ~/.bash_profile
rbenv install 2.2.2
rbenv rehash
rbenv global 2.2.2
gem update --system
gem install bundler
fi
echo "Gem home is $GEM_HOME"
echo "Path is $PATH"
###########################################
## Database setup
###########################################
POSTGRES_RUNNING=$(service postgresql-9.4 status | egrep "postgresql.*running" | wc -l)
if [ "$POSTGRES_RUNNING" -ne 1 ]; then
echo 'Starting Postgres'
sudo /sbin/service postgresql-9.4 initdb
sudo /sbin/service postgresql-9.4 start
sudo /sbin/chkconfig postgresql-9.4 on
else
echo "Postgres is running"
fi
DATABASE_YML="/vagrant/config/database.yml"
APP_DB_USERNAME="blog_user"
APP_DB_PASSWORD="blog_user91x91x"
if [ -f $DATABASE_YML ];
then
echo "File $DATABASE_YML exists."
else
echo "File $DATABASE_YML does not exist, creating."
# create role myapp with createdb login password 'password1';
_EOF_
echo "Creating postgres user:"
CREATE_USER_SQL="CREATE ROLE $APP_DB_USERNAME PASSWORD '$APP_DB_PASSWORD' CREATEDB INHERIT LOGIN;"
sudo -u postgres psql --command="$CREATE_USER_SQL"
ALTER_USER_SQL="ALTER USER $APP_DB_USERNAME WITH ENCRYPTED PASSWORD '$APP_DB_PASSWORD'"
sudo -u postgres psql --command="$ALTER_USER_SQL"
ALTER_POSTGRES_USER_SQL="ALTER USER postgres WITH ENCRYPTED PASSWORD 'postgres'"
sudo -u postgres psql --command="$ALTER_POSTGRES_USER_SQL"
echo "Updating postgresql connection info"
sudo cp /var/lib/pgsql/9.4/data/pg_hba.conf .
sudo chmod 666 pg_hba.conf
sed 's/ident/md5/' < pg_hba.conf > pg_hba2.conf
echo 'host all all 0.0.0.0/0 md5' >> pg_hba2.conf
sudo cp pg_hba2.conf /var/lib/pgsql/9.4/data/pg_hba.conf
sudo chmod 600 /var/lib/pgsql/9.4/data/pg_hba.conf
sudo cp /var/lib/pgsql/9.4/data/postgresql.conf .
sudo chmod 666 postgresql.conf
sed "s/^#listen_addresses.*$/listen_addresses = '0.0.0.0'/" < postgresql.conf > postgresql2.conf
sudo cp postgresql2.conf /var/lib/pgsql/9.4/data/postgresql.conf
sudo chmod 600 /var/lib/pgsql/9.4/data/postgresql.conf
sudo /sbin/service postgresql-9.4 restart
# psql -U
cat > $DATABASE_YML <<_EOF_
default: &default
adapter: postgresql
encoding: utf8
min_messages: warning
pool: 5
timeout: 5000
username: $APP_DB_USERNAME
password: $APP_DB_PASSWORD
host: localhost
development:
<<: *default
database: blog_development
test:
<<: *default
database: blog_test
production:
<<: *default
database: blog_production
_EOF_
fi
cd /vagrant
echo "Installing gems"
bundle install
echo "Running migrations"
bundle exec rake db:create:all --trace
bundle exec rake db:migrate --trace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment