Last active
December 21, 2015 14:28
-
-
Save boof/6319637 to your computer and use it in GitHub Desktop.
Helper functions for vagrant provisioning scripts.
This file contains 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
#!/usr/bin/env bash | |
HOSTNAME=slave.fork.de | |
host $HOSTNAME >/dev/null || { | |
echo 'nameserver 194.6.194.241' >> /etc/resolvconf/resolv.conf.d/head | |
resolvconf -u | |
} | |
# checks if the shell supports the given command | |
function can () { | |
command -v "$@" >/dev/null 2>&1 | |
} | |
function has () { | |
dpkg -s "$@" 2>/dev/null | grep 'Status: install ok installed' >/dev/null | |
} | |
function aptitude_install () { | |
echo "Warning: aptitude_install is deprecated, use apt_install instead." 1>&2 | |
apt_install $@ | |
} | |
function apt_install () { | |
aptitude install -y "$@" >/dev/null 2>&1 | |
} | |
# setup apache to work well with vagrant | |
function setup_apache () { | |
has apache2 || { | |
echo "Setting up Apache HTTP server..." | |
apt_install apache2 | |
[ -f /etc/apache2/envvars.orig ] || { | |
cp /etc/apache2/envvars /etc/apache2/envvars.orig | |
truncate -s 0 /etc/apache2/envvars | |
cat /etc/apache2/envvars.orig \ | |
| sed 's/=www-data/=vagrant/g' \ | |
>> /etc/apache2/envvars | |
chown -R vagrant:vagrant /var/log/apache2 /var/lock/apache2 | |
} | |
rm -rf /var/www | |
ln -sf /vagrant /var/www | |
service apache2 restart >/dev/null 2>&1 | |
} | |
} | |
# setup mysql-server to work well with vagrant | |
function setup_mysql () { | |
has mysql-server-5.5 || { | |
echo "Setting up MySQL server..." | |
echo 'mysql-server-5.5 mysql-server/root_password password root' | debconf-set-selections | |
echo 'mysql-server-5.5 mysql-server/root_password_again password root' | debconf-set-selections | |
apt_install mysql-server-5.5 | |
echo '[client]' > ~/.my.cnf | |
echo 'user=root' >> ~/.my.cnf | |
echo 'password=root' >> ~/.my.cnf | |
chmod 600 ~/.my.cnf | |
mysql \ | |
<<SQL | |
CREATE USER 'vagrant'@'localhost' IDENTIFIED BY 'vagrant'; | |
GRANT USAGE ON *.* TO 'vagrant'@'localhost' IDENTIFIED BY 'vagrant'; | |
CREATE DATABASE IF NOT EXISTS vagrant CHARACTER SET utf8; | |
SQL | |
echo '[client]' > /home/vagrant/.my.cnf | |
echo 'user=vagrant' >> /home/vagrant/.my.cnf | |
echo 'password=vagrant' >> /home/vagrant/.my.cnf | |
chown vagrant:vagrant /home/vagrant/.my.cnf | |
chmod 600 /home/vagrant/.my.cnf | |
} | |
} | |
function setup_postgresql () { | |
has postgresql-9.1 || { | |
echo "Setting up PostgreSQL server..." | |
apt_install postgresql-9.1 | |
sudo -u postgres createuser --superuser vagrant | |
sudo -u postgres createdb --owner=vagrant vagrant | |
} | |
} | |
# setup apache to work with WordPress | |
function setup_wordpress () { | |
has libapache2-mod-php5 php5-mysql php5-gd || { | |
setup_apache | |
setup_mysql | |
echo "Setting up WordPress CMS..." | |
apt_install libapache2-mod-php5 php5-mysql php5-gd | |
[ -f /etc/apache2/sites-available/default.orig ] || { | |
cp /etc/apache2/sites-available/default /etc/apache2/sites-available/default.orig | |
truncate -s 0 /etc/apache2/sites-available/default | |
cat /etc/apache2/sites-available/default.orig \ | |
| sed 's/AllowOverride None/AllowOverride FileInfo Options/g' \ | |
>> /etc/apache2/sites-available/default | |
} | |
a2enmod rewrite >/dev/null | |
service apache2 restart >/dev/null 2>&1 | |
} | |
} | |
function setup_passenger () { | |
can passenger || { | |
setup_apache | |
echo 'Setting up Apache Passenger module...' | |
apt_install build-essential \ | |
zlib1g-dev libssl-dev libreadline5-dev libcurl4-openssl-dev apache2-prefork-dev | |
gem install --no-ri --no-rdoc passenger >/dev/null | |
passenger-install-apache2-module --auto 2>/dev/null \ | |
| sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" > passenger-install.log | |
# setting up module | |
grep LoadModule passenger-install.log > /etc/apache2/mods-available/passenger.load | |
grep PassengerRoot passenger-install.log > /etc/apache2/mods-available/passenger.conf | |
rm passenger-install.log | |
echo "PassengerDefaultRuby $(which ruby)" >> /etc/apache2/mods-available/passenger.conf | |
a2enmod passenger >/dev/null | |
# setting up default virtual host | |
wget --output-document=/etc/apache2/sites-available/default --quiet --no-http-keep-alive "http://$HOSTNAME/boxes/passenger-site" | |
service apache2 restart >/dev/null 2>&1 | |
} | |
} | |
function restart_rack () { | |
echo "Restarting passenger..." | |
su -c 'mkdir /vagrant/tmp 2>/dev/null; touch /vagrant/tmp/restart.txt' - vagrant | |
wget --spider --quiet http://localhost/ & | |
} | |
# build given Ruby version | |
function build_ruby () { | |
[ -x /usr/local/ruby-$1/bin/ruby ] && return | |
can ruby-build || { | |
echo "Installing Ruby builder..." | |
apt_install git build-essential | |
cd /usr/local/src | |
git clone https://github.com/sstephenson/ruby-build.git >/dev/null | |
cd ruby-build | |
./install.sh >/dev/null | |
} | |
# TODO if mirror does not work map ruby source to another mirror | |
# RADAR use a fork ruby-lang mirror | |
echo "Building Ruby $1..." | |
ruby-build $1 /usr/local/ruby-$1 >/dev/null 2>&1 | |
} | |
# installs Ruby (defined in /vagrant/.ruby-version or given as parameter) | |
# and modifies PATH | |
function setup_ruby () { | |
version=`cat /vagrant/.ruby-version 2>/dev/null` | |
version=${1:-$version} | |
build_ruby $version || { | |
echo "Could not install Ruby ${version}!" 1>&2 | |
exit 1 | |
} | |
echo "export PATH=/usr/local/ruby-$version/bin:\$PATH" \ | |
| install --backup=none /dev/stdin /etc/profile.d/ruby.sh | |
source /etc/profile.d/ruby.sh | |
} | |
# checks if a gem is bundled | |
function bundles () { | |
grep -o "gem ['\"]$1['\"]" /vagrant/Gemfile >/dev/null 2>&1 | |
} | |
# installs bundle from given path or /vagrant | |
function install_bundles () { | |
while : | |
do | |
gem install --no-ri --no-rdoc bundler >/dev/null && break | |
done | |
bundles pg && { | |
setup_postgresql | |
apt_install libpq-dev | |
} | |
echo "Installing bundle..." | |
su -c 'bundle install --no-deployment --path=~/gems --gemfile=/vagrant/Gemfile --quiet --no-cache --without doc test production' - vagrant | |
} | |
function setup_rails () { | |
setup_ruby | |
setup_passenger | |
install_bundles | |
echo "Migrating database..." | |
su -c 'cd /vagrant; bundle exec rake log:clear db:create db:migrate RAILS_ENV=development >/dev/null 2>/vagrant/log/migrate.log' - vagrant | |
restart_rack | |
# promote apache logs to rails log folder | |
ln -s /var/log/apache2/access.log /var/log/apache2/error.log /vagrant/log/ 2>/dev/null | |
# prevent ln to return an error code | |
return 0 | |
} | |
BASE_URI="http://$HOSTNAME/boxes/$PROJECT_NAME" | |
# returns the last modification date in a human readable format | |
function mtime () { | |
stat --format '%y' $1 | |
} | |
# converts a date into the HTTP date format | |
function httpdate () { | |
date --date=$1 +'%a, %e %b %Y %T GMT' | |
} | |
# downloads a resource if the resource is newer than the local file or the | |
# local file does not exist | |
function request () { | |
if [ -r "$1" ]; then | |
local date=`httpdate $(mtime $1)` | |
wget --quiet --no-http-keep-alive --output-document=$1.download --header="If-Modified-Since: $date" $BASE_URI/$1 && | |
mv $1.download $1 | |
else | |
wget --quiet --no-http-keep-alive --output-document=$1 $BASE_URI/$1 | |
fi | |
return $? | |
} | |
locale-gen en_US.UTF-8 >/dev/null | |
update-locale LC_ALL=en_US.UTF-8 | |
export LC_ALL=en_US.UTF-8 | |
echo "Updating package index..." | |
read -r -d '' APT_SOURCES << 'SOURCES' | |
deb mirror://mirrors.ubuntu.com/mirrors.txt precise main restricted universe multiverse | |
deb mirror://mirrors.ubuntu.com/mirrors.txt precise-updates main restricted universe multiverse | |
deb mirror://mirrors.ubuntu.com/mirrors.txt precise-backports main restricted universe multiverse | |
deb mirror://mirrors.ubuntu.com/mirrors.txt precise-security main restricted universe multiverse | |
SOURCES | |
echo "$APT_SOURCES" > /etc/apt/sources.list | |
aptitude update >/dev/null 2>&1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment