2013-06-14 02:42:13 +02:00
|
|
|
#!/bin/sh
|
|
|
|
#
|
|
|
|
# deploy foodsoft to heroku
|
|
|
|
#
|
|
|
|
# To be run from foodsoft's root. A new temporary git branch will be created
|
|
|
|
# from the current working directory - it's safest to run this with a clean
|
|
|
|
# working directory.
|
|
|
|
#
|
|
|
|
# Use environment variables to customize variables below.
|
|
|
|
# You need to have a working heroku client.
|
|
|
|
#
|
|
|
|
|
|
|
|
# rails environment to deploy
|
|
|
|
[ "$RAILS_ENV" ] || RAILS_ENV=production
|
|
|
|
# heroku application name
|
|
|
|
[ "$APP" ] || APP=foodsoft-translation
|
|
|
|
# heroku region to create application in
|
|
|
|
[ "$REGION" ] || REGION=eu
|
|
|
|
|
|
|
|
|
|
|
|
# make sure required software is available
|
|
|
|
if ! heroku >/dev/null 2>&1; then
|
|
|
|
echo "Please install heroku." 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if ! git version >/dev/null 2>&1; then
|
|
|
|
echo "Please install git." 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
# required settings
|
|
|
|
if [ "$RAILS_ENV" = "translation" -a ! "$LOCALEAPP_KEY" ]; then
|
|
|
|
echo "Need to specify LOCALEAPP_KEY key for translation environment" 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# create app if it doesn't exist
|
|
|
|
if ! heroku apps | grep -q "^$APP\s"; then
|
|
|
|
heroku create "$APP" --region "$REGION"
|
|
|
|
heroku addons:add heroku-postgresql:dev --app "$APP"
|
|
|
|
heroku pg:promote `heroku config | grep 'HEROKU_POSTGRESQL_.*_URL' | cut -d: -f1`
|
2013-10-28 00:49:00 +01:00
|
|
|
# user-env-compile needed because config.assets.initialize_on_precompile is true
|
|
|
|
heroku labs:enable user-env-compile --app "$APP"
|
2013-06-14 02:42:13 +02:00
|
|
|
fi
|
|
|
|
heroku config:set RACK_ENV="${RAILS_ENV}" RAILS_ENV="${RAILS_ENV}" --app "$APP"
|
|
|
|
|
|
|
|
# create temporary branch with heroku-specific changes
|
|
|
|
touch ._tmp_havestash
|
|
|
|
OLDSTASH=`git stash list | wc -l`
|
|
|
|
git stash save -q -a "stored changes before creating heroku-$APP"
|
|
|
|
ORIG_BRANCH=`git status --b --porcelain | head -n 1 | sed 's|^#*\s*||;s|\.\.\..*$||'`
|
|
|
|
BRANCH="_tmp-heroku-$APP"
|
|
|
|
if ! git checkout -b "$BRANCH"; then
|
|
|
|
echo "Could not create to temporary branch '$BRANCH', aborting." 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
# remove sqlite3 dependency as it doesn't install on heroku
|
|
|
|
sed -i "s|^\\(\\s*gem\\s\\+'sqlite3'\\)|#\1|" Gemfile
|
|
|
|
sed -i "s|^\\(\\s*sqlite3\\b\)|#\1|" Gemfile.lock
|
|
|
|
# make sure postgresql db is present, as it is the default heroku db
|
2013-09-06 19:31:15 +02:00
|
|
|
echo "
|
|
|
|
gem 'pg'" >>Gemfile
|
2013-06-14 02:42:13 +02:00
|
|
|
# always use unicorn
|
2013-09-06 19:31:15 +02:00
|
|
|
echo "
|
|
|
|
gem 'unicorn'" >>Gemfile
|
2013-06-14 02:42:13 +02:00
|
|
|
echo 'web: bundle exec unicorn -p $PORT -E $RACK_ENV' >Procfile
|
2013-09-06 19:31:15 +02:00
|
|
|
# don't complain when mail cannot be sent,
|
|
|
|
# XXX when you're hosting a production instance, use a real smtp server instead
|
|
|
|
sed -i 's|\(#\s*\)\?\(config\.action_mailer\.raise_delivery_errors\)\s*=.*|\2 = false|' config/environments/${RAILS_ENV}.rb
|
|
|
|
sed -i 's|\(#\s*\)\?\(config\.action_mailer\.delivery_method\)\s*=.*|\2 = :smtp|' config/environments/${RAILS_ENV}.rb
|
2013-06-14 02:42:13 +02:00
|
|
|
# do not ignore deployment files
|
2013-10-28 00:49:00 +01:00
|
|
|
sed -i 's|^\(config/.*\.yml\)|#\1|' .gitignore
|
2013-06-14 02:42:13 +02:00
|
|
|
sed -i 's|^\(config/initializers/secret_token.rb\)|#\1|' .gitignore
|
|
|
|
sed -i 's|^\(config/environments/development.rb\)|#\1|' .gitignore
|
|
|
|
# make sure we have a full configuration
|
|
|
|
# TODO pull this from heroku when exists?
|
|
|
|
if [ ! -e config/app_config.yml ]; then
|
|
|
|
echo "config/app_config.yml not present, copying config/app_config.yml.SAMPLE"
|
|
|
|
cp config/app_config.yml.SAMPLE config/app_config.yml
|
|
|
|
fi
|
|
|
|
# keep secret token from currently deployed app, else generate new one
|
|
|
|
heroku git:remote --app="$APP" >/dev/null 2>&1
|
|
|
|
if git show heroku/master:config/initializers/secret_token.rb >/dev/null 2>&1; then
|
|
|
|
git show heroku/master:config/initializers/secret_token.rb >config/initializers/secret_token.rb
|
|
|
|
else
|
|
|
|
cat >config/initializers/secret_token.rb <<-EOF
|
|
|
|
# auto-generated secret key
|
|
|
|
Foodsoft::Application.config.secret_token = '`openssl rand -hex 128`'
|
|
|
|
EOF
|
|
|
|
fi
|
2013-10-28 00:49:00 +01:00
|
|
|
# update Gemfile.lock after Gemfile updates (required by heroku)
|
|
|
|
bundle install --quiet
|
2013-06-14 03:10:32 +02:00
|
|
|
# configure localeapp, manually to include environment
|
2013-06-14 02:42:13 +02:00
|
|
|
if [ "$LOCALEAPP_KEY" ]; then
|
|
|
|
cat >config/initializers/localeapp.rb <<EOF
|
|
|
|
require 'localeapp/rails'
|
|
|
|
|
|
|
|
Localeapp.configure do |config|
|
|
|
|
config.api_key = '$LOCALEAPP_KEY'
|
2013-09-18 19:21:54 +02:00
|
|
|
config.sending_environments = []
|
2013-06-14 02:42:13 +02:00
|
|
|
config.polling_environments = ['$RAILS_ENV']
|
|
|
|
end
|
|
|
|
EOF
|
2013-09-06 19:31:15 +02:00
|
|
|
echo "
|
|
|
|
gem 'localeapp'" >>Gemfile
|
2013-06-14 03:10:32 +02:00
|
|
|
# also do not cache so we get locale updates
|
2013-09-06 19:31:15 +02:00
|
|
|
sed -i 's|\(#\s*\)\?\(config\.cache_classes\)\s*=.*|\2 = false|' config/environments/${RAILS_ENV}.rb
|
2013-10-28 00:49:00 +01:00
|
|
|
bundle exec localeapp pull
|
2013-06-14 02:42:13 +02:00
|
|
|
fi
|
|
|
|
# TODO add more extensive database seed
|
|
|
|
|
2013-12-05 15:52:32 +01:00
|
|
|
# XXX don't use redis for now
|
|
|
|
echo "Resque.inline = true" >config/initializers/resque.rb
|
|
|
|
|
2013-06-14 02:42:13 +02:00
|
|
|
# and push = deploy
|
|
|
|
git add -A
|
|
|
|
git commit -q -m "heroku changes for environment ${RAILS_ENV}" -a
|
|
|
|
git push -f heroku $BRANCH:master
|
|
|
|
# create/update database
|
|
|
|
if !heroku run rake db:version >/dev/null 2>&1; then
|
|
|
|
heroku run rake db:setup
|
|
|
|
else
|
|
|
|
heroku run rake db:migrate
|
|
|
|
fi
|
2013-06-14 03:10:32 +02:00
|
|
|
|
|
|
|
# restart just to be sure
|
2013-10-28 00:49:00 +01:00
|
|
|
#heroku ps:restart
|
2013-06-14 02:42:13 +02:00
|
|
|
|
|
|
|
# return to original branch
|
|
|
|
git checkout -q "$ORIG_BRANCH" && git stash pop -q
|
|
|
|
git branch -q -D "$BRANCH"
|
|
|
|
rm -f ._tmp_havestash
|
|
|
|
|