Thursday, March 10, 2011

ChiliProject up in minutes on Heroku w/ Gmail and S3

Requirements

  • Heroku account
  • Gmail account ( or sendgrid account ) if you want to send email
  • Amazon S3 account if you want to store documents and files
The S3 account requires a credit card to setup, but has a free tier
with 5 GB of Amazon S3 storage, 20,000 Get Requests, 15GB of bandwidth
each month.



Walk-through

ChiliProject via git

mkdir groupprojects
cd groupprojects
git init
 
git remote add chiliproject https://github.com/chiliproject/chiliproject.git
git fetch chiliproject
git merge chiliproject/master
 
git add .
git commit -m 'clean version of base code' 

Heroku, Giternal and Rails via Bundler

# create the Gemfile
cat << EOF | tee Gemfile
source :rubygems 
gem 'rails', '2.3.5' 
gem 'i18n', '0.4.2' 
gem 'giternal' 
gem 'heroku' 
EOF
git add Gemfile
git commit -m 'Added Gemfile for rails, i18n, heroku, and giternal' 
 
# install bunlder then use it to install the gems in the Gemfile
gem install bundler
bundle install

Heroku App

Let's configure a place to push the code to! Choose your name well as
it sets up up my-project-name.heroku.com, so choose a good unused name.

heroku create my-project-name
All the configuration should happen with heroku config environment
variables. We'll put the at the beginning of each section, then
install components and configure them to use the ENV variables.

  • Note that this just configures environment variables on the heroku instance. We would need to export these to the local environment to do any local development.

Session Store and Secret

heroku config:add SESSION_SECRET=`ruby -e 'require "rubygems" ; require "active_support" ; \
   puts ActiveSupport::SecureRandom.hex(40)'` 
cat << EOF | tee config/initializers/session_store.rb
ActionController::Base.session = { 
  :session_key => '_redmine_session', 
  :secret => ENV['SESSION_SECRET'] 
} 
EOF
git add -f config/initializers/session_store.rb
git commit -m 'Added session key and secret to be populated via env variables' 

Gmail

heroku config:add GMAIL_SMTP_USER=username@gmail.com GMAIL_SMTP_PASSWORD=your-password
cat << EOF | tee config/giternal.yml
action_mailer_optional_tls: 
  path: vendor/plugins
  repo: http://github.com/collectiveidea/action_mailer_optional_tls.git
EOF
git add config/giternal.yml
 
giternal update
sed -i '' "\:vendor/plugins/action_mailer_optional_tls:d" .gitignore
 
git add vendor/plugins/action_mailer_optional_tls/
 
cat << EOF | tee config/initializers/gmail_tls.rb
if ENV['GMAIL_SMTP_USER'] and ENV['GMAIL_SMTP_PASSWORD'] 
  ActionMailer::Base.perform_deliveries = true 
  ActionMailer::Base.smtp_settings = { 
    :address => "smtp.gmail.com", 
    :port => 587, 
    :authentication => :plain, 
    :domain => ENV['GMAIL_SMTP_USER'], 
    :user_name => ENV['GMAIL_SMTP_USER'], 
    :password => ENV['GMAIL_SMTP_PASSWORD'], 
    :tls => true 
  } 
end 
EOF
git add config/initializers/gmail_tls.rb
git commit -m 'Added support for GMAIL that comes from ENV' 

Amazon S3

heroku config:add S3_ACCESS_KEY=your-access-key S3_SECRET_KEY=your-secret-key S3_BUCKET_NAME=your-bucket
 
cat << EOF | tee -a config/giternal.yml
redmine_s3: 
  path: vendor/plugins
  repo: https://github.com/edavis10/redmine_s3.git
EOF
git add config/giternal.yml
 
giternal update
 
sed -i '' "\:vendor/plugins/redmine_s3:d" .gitignore
git add vendor/plugins/redmine_s3/
 
cat << EOF | tee config/s3.yml
production: 
  access_key_id: <%= ENV['S3_ACCESS_KEY'] %>
  secret_access_key: <%= ENV['S3_SECRET_KEY'] %>
  bucket: <%= ENV['S3_BUCKET_NAME'] %> 
  cname_bucket: false 
 
development: 
  access_key_id: <%= ENV['S3_ACCESS_KEY'] %>
  secret_access_key: <%= ENV['S3_SECRET_KEY'] %>
  bucket: <%= ENV['S3_BUCKET_NAME'] %> 
  cname_bucket: false 
EOF
git add config/s3.yml
 
git commit -m 'Added s3 configuration that pulls keys and bucket from env variables' 

plugin assets

This dir needs to exist on heroku side, and is populated when the
plugins are installed. However git doesn't track directories, only
files.

mkdir public/plugin_assets
echo this must exist >  public/plugin_assets/README
git add -f public/plugin_assets/README
git commit -m 'Added plugin_asset dir for Heroku' 

pushing our code out to the configured heroku instance

First we push our local changes out, this may take a bit as we are uploading all our code.

git push heroku master
Now that our code is up, we want to setup the database and load
the default data set.

heroku rake db:migrate
heroku rake redmine:load_default_data REDMINE_LANG=en
You could login at this point, but it might be interesting to go ahead and disable the default admin account and create our own.

# lock default admin user account
heroku console "x=User.find(1) ; x.lock ; x.save" 
 
heroku console "user = User.new { |u| u.login='hacker'; \
       u.firstname='Hippie'; u.lastname='Hacker'; \
       u.mail='hhh@hippiehacker.org'; \
       u.admin=true; u.language='en'; \
       u.password='XXXXX' } ; \
       user.preference=UserPreference.create!(:time_zone=>'Auckland') ; \
       user.save! " 
There are a few settings you'll probably always want to set right off. Here's how to do it programattically.

# get your hostname and settins your URL value within the app settings
heroku console "setting = Setting.find_or_create_by_name('host_name') ; \
       setting.value=ENV['URL'] ; setting.save! " 
 
heroku console "title = Setting.find_or_create_by_name('app_title') ; \
      title.value='Our Little Cows' ; title.save! " 
 
heroku console "setting = Setting.find_or_create_by_name('welcome_text') ; \
       setting.value='Yet Another Cool Website' ; setting.save! " 
Format the heroku config for use as local variables:

heroku config --long | sed s/\>// | sed s/\^/export\ / | sed s/\ \*\=\ \*/=\"/ | sed s/\$/\"/
Finally launch the site in the browser.

heroku open

3 comments:

ylluminate said...
This comment has been removed by the author.
ylluminate said...

Hey, thanks for this writeup. I'm running into a roadblock here where it appears to be conflicting Rails versions somehow and the Gemfile needing some tweaking when I get to `heroku rake db:migrate`:

can't activate rails (= 2.3.12, runtime), already activated rails-2.3.5. Make sure all dependencies are added to Gemfile.
(continued: http://bit.ly/nrqWpA )

ylluminate said...

Turns out that the 2.0 release has a Gemfile that appears to essentially work on Heroku without too much effort. This writeup will need to be reworked.