Tuesday, August 18, 2009

Deploying Thoth to Heroku

This is a short instruction how to use Thoth with Heroku. Thoth is a Ruby blog engine, and Heroku is a Ruby hosting platform - there is a convenient free hosting plan. Instructions are targeted towards the latest version of Thoth - 0.3.0.
I'll keep this brief however, first goes disclaimer:
I don't develop in Ruby, I haven't done anything in Ruby yet. All I know about Ruby and Rails is from a book I read long time ago, going through it lightning fast without much retention. There are possible better ways to do this however this approach works. Anyhow, no guarantees.

Get and install the latest version
First of all there is no official 0.3.0 release, or at least I could not find it. Version 0.2.1 did not work on my machine so I had to get the latest version of code, created a gem out of it, installed it and it worked fine after that. Instructions how to get the latest version are at the Thoth homepage.

Create a thoth site
On the same page are instructions how to run it. Create a site, give it whatever name, you'll need to copy those file later on to your heroku application. After following all instructions you can test your new thoth blog.

Create a heroku app
Create your heroku application following instructions from the quick start. You'll need to install git and setup ssh public key. Create heroku application, copy all files from your previously created thoth site, initialize git repository and add files to git. Now everything is practicaly the same as in the quick start - except I could not create thoth site into already existing folder.

Now goes exciting stuff - how to make this really work on heroku.
Getting it all to work
Since there isn't official 0.3.0 version of thoth we need to unpack it into vendor/gems folder of our heroku application first. Then config.ru has to be fixed so that version of thoth is used:



Dir.glob(File.dirname(__FILE__) + "/vendor/gems/*").each do |path|
gem_name = File.basename(path.gsub(/-[\d\.]+$/, ''))
$LOAD_PATH << path + "/lib/"
require gem_name
end

#require 'thoth'

So require 'thoth' is basically replaced by the code to load all gems from vendor folder - got a bit of help from here.

Now specify all required gems. You can read detailed instructions on installing gems on heroku or just create .gems file with following contents:

innate --version 2009.06.12
ramaze --version 2009.06.12
cssmin
jsmin
sanitize

Fix thoth.config file to use correct database connection:

db: <%= ENV['DATABASE_URL'] || ('sqlite:///'+ (Thoth::HOME_DIR) +'/db/live.db') %>


It's postgresql but it seems to be working for now. Another setting needs to be changed and that is url:
# Base URL of your site. This is necessary in order for Thoth to know how
# to construct links. Be sure to set this correctly or links may not work.
url: http://someappname.heroku.com/

Now everything is almost ready, the only thing left is database. You need to run thoth --migrate to crate ./db/live.db sqlite database. Then push it to heroku following instructions, basically one command - something like: heroku db:push sqlite://db/live.db

After this you can push the whole application to heroku and it should work.

Peace of cake, now you have your free personal blog in Ruby.

5 comments:

  1. Thanks for this useful tutorial. Could you be a little more specific about the database setting that you had for postgressql? Did you just leave the db: line in with nothing after the colon? Or did you use the postgresql uri to heroku's database, and if so, could you say how it was formatted?

    I can't seem to get heroku to pickup the database, even though I've migrated it locally and pushed it up in the same way as you show.

    In the past I've never had trouble getting Heroku to find the right database, but am running into trouble on this one. Thanks,
    -Erik

    ReplyDelete
  2. you need to have:
    db: <%= ENV['DATABASE_URL'] %>

    for your live settings - try that out and let me know if it works.


    http://docs.heroku.com/database#using-the-databaseurl-environment-variable-sequel--datamapper
    There's a line there that I've used:
    Sequel.connect(ENV['DATABASE_URL'] || 'sqlite://my.db')
    Basically config parameter db is used for Sequel.connect.


    There's another catch and that is Post.rb has to be fixed so it works properly on postgressql (due to case sensitivity). I'll add comment with the fix later today.

    ReplyDelete
  3. oh and I have to update .gems file since dependencies changed with latest version of heroku.
    I think it's now:
    innate --version 2009.10
    ramaze --version 2009.10
    ...

    ReplyDelete
  4. Thanks for the link to the heroku docs; I found that too after figuring out that Ramaze is based on Sequel. Even though they said that on the front page I didn't realize it for a while, and it took a little while to remember that you could use Erubis templating inside the YAML config file.

    I have the database setup (no problems with the postgressql case-sensitivity, so it's possible that it's been fixed now), but I'm getting a new problem - when I try to login I'm being authenticated properly but can't get any access to the admin area. I think this is because of Varnish, Heroku's caching layer, but am still investigating this.

    I'll post to this new blog that I'm setting up what my solution is (once it's working) and will put up the solution here too once I figure it out. Thanks for your help, -Erik

    ReplyDelete
  5. There's a problem with helpers/admin.rb I think. I'm away on holidays so I don't have computer with me. I think it's authentication key or something like that. It seems creation of it is giving different value on login and when the check is done. I've changed it to use cookie('innate.sid') instead of ip, and I've put fixed value instead of HOME_DIR. Changing that fixed it. I'm back from holidays in 3 days so if you don't figure it out you can email me on mlalevic [at] gmail and we'll sort it out.

    ReplyDelete