Vixiom Axioms

February 27, 2007

Flash Remoting for Django

Filed under: Ruby on Rails Alastair @ 7:49 pm

It took a little while longer than Flash Remoting for Rails to come out but there’s now a Remoting implementation for Django. Not yet 1.0 but then neither is Django! (officially, it might as well be)

One of the reasons I choose Rails over Django was Remoting support, I might have to reconsider now…

Digg! submit Flash Remoting for Django to stumbleupon.com submit Flash Remoting for Django to del.icio.us submit Flash Remoting for Django to reddit.com Like this post? subscribe to the feed.

February 22, 2007

Restore browser history when using AJAX

Filed under: Ruby on Rails Alastair @ 7:29 pm

Yahoo explains, via Don MacAskill’s blog (my new favorite)

Digg! submit Restore browser history when using AJAX to stumbleupon.com submit Restore browser history when using AJAX to del.icio.us submit Restore browser history when using AJAX to reddit.com Like this post? subscribe to the feed.

February 21, 2007

How to: Ignore all files in a directory (folder) with subversion

Filed under: svn Alastair @ 2:41 pm

The ignore command for subversion can be confusing. Ignoring certain files types is pretty easy:

$ svn propset svn:ignore “*.log” log

but ignoring folders is tricky and was giving me headaches for a while. I was using file_column (in Rails) to save uploaded photos and whenever I did a svn status I would get a list of all my test uploads:

?      public/photo/filename/106
?      public/photo/filename/116
?      public/photo/filename/126
?      public/photo/filename/117
?      public/photo/filename/127
?      public/photo/filename/128
?      public/photo/filename/129

Which made it hard to find files I wanted to add. Here’s how to ignore a directory:

svn propset svn:ignore “*” public/photo/
Digg! submit How to: Ignore all files in a directory (folder) with subversion to stumbleupon.com submit How to: Ignore all files in a directory (folder) with subversion to del.icio.us submit How to: Ignore all files in a directory (folder) with subversion to reddit.com Like this post? subscribe to the feed.

February 18, 2007

MT GS RoR A-Z: The complete guide to Rails on Media Temple’s Grid Server

This tutorial will walk you through setting up everything you need to deploy Rails apps on Media Temple’s Grid Server. The information on each part of the setup is available elsewhere but I thought it would be helpful to have it all in one place. The tutorial will cover setting up a rails container, creating ssh keys on the server, installing rails, creating a subversion repository, and deploying your app with Capistrano.

Prerequisites: You have a (gs) hosting account setup, a SSH client (OS X Terminal.app or PuTTy if you use Windows), and no fear of Unix commands.

Throughout the tutorial I’ll be calling the rails app ‘myapp’ replace that with the name of your application and ‘mydomain.com’ replace that with your domain.

1. SETUP THE RAILS CONTAINER

Log into your Media Temple account center and click on the ‘GridContainers’ control

In the Container List click ‘manage’ on the Ruby on Rails container

Enable the container by clicking ‘enable container’

Select ‘(gs) Container Base’ if you app will require the base amount of RAM, from experience unless you’re doing processor intensive things like resizing very large images with RMagick the base container will be fine.

Confirm the selection on the next page and then wait for your container to be enabled (takes about 5 min). Once you have the container enabled go back the main control panel, click ‘Server Administrator’ and make sure you have SSH enabled.

Since this is a Rails app you’ll most likely need a database, go back to the main control panel and click ‘Manage Databases’

Click ‘Add A Database’

Then in the ‘Specify Your Database Name’ field enter ‘myapp_production’ it’s actual name will then be db#{sitenumber}_myapp_production (sitenumber being your MT site number). Choose MySQL or PostgreSQL from ‘Select Database Type’ and click next.

The container is set up, next up SSH Keys.

2. SSH KEYS

SSH keys will allow you to log into your server without a password; "wait, no password, isn’t that insecure?" actually it’s more secure as long as you and only you have access to your local machine. Although SSH is very secure it’s still possible (though unlikely) for your password to be intercepted when you login. Setting up SSH keys, as the name implies, creates a key on the server that maps to your local machine (a known host), you then no longer have to enter your password when you login in which is safer and requires less memorizing and typing (my two least favorite things!).

Fire up the OS X Terminal (or PuTTy), once it launches you’ll be in your home directory. Type

$ ssh-keygen -t dsa

that will generate the keys on your local machine, change into the .ssh directory

$ cd .ssh

and copy the keys to your server (replacing ‘mydomain’ with your domain)

$ scp id_dsa.pub serveradmin%mydomain@mydomain:./id_dsa.pub

log into your server the old fashioned way

$ ssh serveradmin%mydomain@mydomain

then setup the keys and make sure no other users can read them (each command on a seperate line)

$ mkdir .ssh
$ cd .ssh
$ touch authorized_keys2
$ chmod 600 authorized_keys2
$ cat ../id_dsa.pub >> authorized_keys2
$ rm ../id_dsa.pub

Now when you login you won’t be asked for your password.

3. INSTALL RAILS

Log in (or remain logged in to) the server using you brand new keys.

Then setup another option to save typing your password over and over.

$ mtr generate_config

entering your username (serveradmin%mydomain@mydomain) and password. Then set up ruby gems (each command on a seperate line).

$ mtr setup_rubygems
$ source ~/.bash_profile
$ gem update --system --source=http://gems.mediatemple.net/

then install rails NOTE: running Rails 2.0 as a RubyGem is not yet supported see this article

$ gem install rails -y

and the appropriate database driver, for MySQL

$ gem install mysql --source=http://gems.mediatemple.net/

or for PostgreSQL

$ gem install postgres --source=http://gems.mediatemple.net/

Next install Mongrel

gem install mongrel --source=http://gems.mediatemple.net -v 1.1.1

If you wanted to you could now create a Rails app on the server, but first we’ll create a subversion repository…

4. SETUP A SUBVERSION REPOSITORY

Subversion is a great tool for tracking changes to an application when working with a team of developers or even or your own. You can roll back code to a previous revision and avoid overwriting another person’s work. On your Media Temple server change into your home directory then into the ‘data’ directory.

$ cd $home
$ cd data

and create a ’svn’ directory.

$ mkdir svn

Subversion is already installed on Media Temple so creating a repository is a one line affair

$ svnadmin create --fs-type fsfs svn/repository

That will create the ‘repository’ inside of the svn directory, next we set up the standard directories for Subversion. You need to type the complete path to your repository when adding directories. First find out your site number

$ pwd

This will spit out something like

/home/123/users/.home/data

‘123′ is your site number. In the command below replace mysitenum with that number (Note: using the backslash character means you can type multiple lines before issuing the command).

$ svn mkdir --message="Setting up the directories..." \
> file:///home/mysitenum/data/svn/repository/trunk \
> file:///home/mysitenum/data/svn/repository/tags \
> file:///home/mysitenum/data/svn/repository/branches

Running that command also commited your first revision to the repository (that was easy). The –message option lets others know what changes happened during a revision.

You can now load a rails app into your repository. On your local machine change into the rails app for the site you’re working with.

$ cd path/to/my/app

You’ll know if you’re in the right place if you type ‘ls’ and get a list of rails folders

$ ls
README          components      doc             public          tmp
Rakefile        config          lib             script          vendor
app             db              log             test

Subversion stores revisions in the ‘trunk’ check that out into your rails app (making sure to change mydomain.com and mysitenum to your variables). Note: the ‘.’ after trunk is required (it means do this here)

$ svn checkout svn+ssh://serveradmin%mydomain.com@mydomain.com/home/mysitenum/data/svn/repository/trunk .

Before we commit any files to the repository we should set some files to be ignored, like logs. First add all the files

$ svn add *

Then set the files you want to ignore

$ svn propset svn:ignore "*.log" log
$ svn revert log/*

Now you can commit your site to the repository

$ svn commit --message="Ini Rails App."

And update your code just to be sure you’re on the latest revision

$ svn update

You should get a message ‘At revision 2.’. Your app is now under version control! And the best reason to have it under version control is… Capistrano.

5. DEPLOYMENT WITH CAPISTRANO

Capistrano makes deploying your app as easy as typing one line, but first you need to set it up. Again on your local machine install Capistrano [UPDATE] Some people have had problems with Capistrano 2.0, I’ve changed the command to install 1.4 [/UPDATE]

$ sudo gem install capistrano -v 1.4.0

Then install Media Temple’s Capistrano

$ gem install mt-capistrano --source=http://gems.mediatemple.net/

And apply Capistrano to your app (make sure you’re in your app’s directory, note the ‘.’ means apply to ‘this’ location)

$ cap --apply-to . myapp

Next download the Media Temple deploy recipe from http://gems.mediatemple.net/deploy.rb (overwriting the one Capistrano made in myapp/config) then open deploy.rb and add your app’s info (all the lines that begin with ’set’):

require mt-capistrano

set :site,         "mysitenum"
set :application,  "myapp"
set :webpath,      "myapp.mydomain.com"
set :domain,       "mydomain.com"
set :user,         "serveradmin%mydomain.com"
set :password,     "xxxxxxxx"

# repository on (gs)
set :repository, "svn+ssh://#{user}@#{domain}/home/#{site}/data/svn/repository/trunk"

# repository elsewhere
#set :repository, “svn+ssh://user@other.com/usr/local/svn/repo/app1/trunk”
#set :repository, "https://other.com/usr/local/svn/repo/app1/trunk”
#set :svn_password, “xxxxxxx”

# these shouldn’t be changed
role :web, "#{domain}"
role :app, "#{domain}"
role :db,  "#{domain}", :primary => true
set :deploy_to,    "/home/#{site}/containers/rails/#{application}"

#set :migrate_env, "VERSION=0"

#task :after_update_code, :roles => :app do
#  put(File.read(’deploy/database.yml.mt’), "#{release_path}/config/database.yml", :mode => 0444)
#end

task :after_symlink, :roles => :app do
  run "#{mtr} -u #{user} -p #{password} generate_htaccess #{application}"
end

save the file and then modify your config/database.yml file to use the Media Temple database for your production environment.

production:
  adapter: mysql
  database: db123_myapp_production
  username: db123
  password: xxxxxxxx
  host: internal-db.s123.gridserver.com

then run a subversion status

$ svn status

you should see a couple of new files in there with ‘?’ before them, that means subversion doesn’t know about them. To add them type

$ svn add config/deploy.rb
$ svn add lib/tasks/capistrano.rake

Then commit them to the repository and update your local copy (Note: -m is shorthand for –message).

$ svn commit -m="config deploy.rb"
$ svn update

Now we can deploy the app so it will be live on your Grid Server domain. Run each of these commands on one line:

$ cap mt_add
$ cap setup
$ cap update_code
$ cap symlink
$ cap migrate
$ cap mt_create_link
$ cap mt_start

If all the commands ran succesfully go to myapp.mydomain.com and view your site! Now open myapp/public/index.html (the default ruby splash page) and change the header <h1> from ‘Welcome aboard’ to ‘Welcome to Capistrano Deployment on MT’ save the file and do a svn commit

$ svn commit -m="welcome page header change"
$ svn update

then with your update safely in the repository you just do ‘cap deploy’

$ cap deploy

Then go check your site and see the change! Any time you make an update you want to push live commit it to subversion and then cap deploy, That’s it!

Digg! submit MT GS RoR A-Z: The complete guide to Rails on Media Temple’s Grid Server to stumbleupon.com submit MT GS RoR A-Z: The complete guide to Rails on Media Temple’s Grid Server to del.icio.us submit MT GS RoR A-Z: The complete guide to Rails on Media Temple’s Grid Server to reddit.com Like this post? subscribe to the feed.

Powered by WordPress