Sunday, April 21, 2013

How to deploy a Rails app to Heroku on openSUSE 12.3


One of the most easy ways to deploy a Rails application for use by others is Heroku. They provide Rails on their platform as a service. Their most basic plan is free, but it usually is enough for development and use for testing. You can then scale up to production use by paying for additional services and capacity. You just push your code, and Heroku takes care of running all the services needed to actually serve the application.

They have an excellent guide about Getting Started with Rails 3.x on Heroku. If you follow these steps you'll quickly have your app running in the cloud. There are a few things which are specific to when you do it on openSUSE 12.3. Here is a quick summary.

Heroku uses PostgreSQL as database backend. To minimize problems caused by divergencies in environments it's a good idea to also use PostgreSQL on your local development system. I wrote a separate guide How to run Rails with PostgreSQL on openSUSE 12.3, which explains how to do that on openSUSE 12.3.

Heroku provides a client, which you can use to manage your apps. For openSUSE 12.3 you'll need the standalone version. Install it following the instructions. To make it accessible on the command line just as "heroku", create a symlink to your local bin directory:
sudo ln -s /usr/local/heroku/bin/heroku ~/bin/heroku
Login to Heroku with
heroku login
and follow the instructions from the Heroku guide.

If you already have the app created on Heroku, you can connect your local git checkout to it by doing
git remote add heroku git@heroku.com:APPNAME.git
Replace APPNAME by the name of your application on Heroku.

You can then deploy by
git push heroku master
If you follow the recommendation of the Heroku guide to use Unicorn as web server and Foreman to run it locally, you need to make bundler available under the "bundle" command, otherwise Foreman will not start:
sudo ln -s /usr/bin/bundle1.9 /usr/bin/bundle
Then you can start your application locally with
foreman1.9 start
and you can access it at localhost:5000.

That's it. Have fun deploying your Rails application to Heroku on openSUSE.

Friday, April 19, 2013

How to run Rails with PostgreSQL on openSUSE 12.3

Ruby on Rails uses SQLite as backend database by default. Using PostgreSQL instead is not difficult, but it needs a little bit of setup and configuration to get the database up and running and connect the Rails app to it. What you need to do slightly varies with the platform you are on. Here is how you do it for a development environment on openSUSE 12.3. Running it as a production database is a different story and needs quite some more considerations to address questions of performance, scalability, backup, availability, etc. But for a development environment using PostgreSQL with Rails is quite simple.

First of all you need to install PostgreSQL:
sudo zypper install postgresql92-server postgresql92-devel
This installs the latest available version. The devel package is needed to build the Rails drivers.

Then start the database server:
sudo /usr/sbin/rcpostgresql start
If you want to automatically start the server on boot enable the service with:
sudo chkconfig -a postgresql
To finalize the setup of the database server you need to create a user, which Rails uses to access the database. You can choose whatever username you like, but the setup is most simple, if you use the username of the user under which you do your Rails development. The -d option allows the user to create databases, so the corresponding rake tasks can do their job.
sudo sudo -u postgres createuser -d USERNAME
You can check the results and later the content of the database directly in PostgreSQL by using the PostgreSQL console. You can start it with the following command. As with the createuser command, via sudo you impersonate the postgre user as root, and then are allowed to connect locally to the database server.
sudo sudo -u postgres psql
By querying the users table you can check the existing users and their rights on the PostgreSQL console:
SELECT * FROM pg_user;
To make Rails talk to PostgreSQL instead of the default SQLite backend, you just need to exchange the database driver by adding the pg gem to the Gemfile and removing the sqlite3 gem. Then run bundler to install the gems:
bundle19 install
To connect Rails to the database, you need to create the database configuration in Rails. Create a file config/database.yml with something like the following content. You need to replace USERNAME by the name of the database user you create in one of the previous steps, and APPNAME by the name of the Rails application. You are free to use whatever name you want there, but usually using something like the appname makes it easier to keep an overview.
development:
    adapter: postgresql
    database: APPNAME_development
    username: USERNAME
test:
    adapter: postgresql
    database: APPNAME_test
    username: USERNAME
production:
    adapter: postgresql
    database: APPNAME_production
    username: USERNAME
Then finally you need to create the databases with:
rake db:create
and create the database tables by running the migrations of your Rails app:
rake db:migrate
That's all. Now your Rails app runs on PostgreSQL, and you can do all your development and testing as usual.