Wednesday, July 22, 2009

Workshop 7 - End of the Line: production site migration and maintenance

To Do:

Developers conclude their work with the OTBS and look at the options for deployment of the site. Examine the various platforms/software tools used for deployment such as UNIX environment suggested in the Discussion Notes, Mongrel or Mongrel cluster, Nginx, Subversion or Capistrano (during development stage), JRuby in the Java environment.

Which way?

The choice is up to you as this workshop presents just one option and you may like to use another, such as deploying the OTBS in a .NET or J2EE environment.

Can you get the OTBS Running in production mode as a minimal production server?

Share your success by posting progress comments and links etc to the Developers sub-forum site that has been set up for the Red team.

Focus Question
As either a developer or as an IT manager, what are the options available when deploying and maintaining the Ruby on Rails application online?

The following is a list of things that need to be completed to enable promotion to a production site:

1) Put the code into a version control system:

To do this I downloaded git version 1.5 from http://msysgit.googlecode.com/files/Git-1.5.6.1-preview20080701.exe.

I installed git and created a new repository for the my OTBS using the git GUI. I am not sure yet whether I need to upload to a public repository but if so I will use github.com as advised in the Learning Rails podcasts.

Downloaded PuTTy from http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html and installed it. This is a Windows SSH terminal program that is going to help me log into the deployment environment which has servers running on a Unix-derived operating system. Apparently I need to log in using the SSH protocol. (I will come back to this later).

Made sure my tests for the application were still working. I have XX tests running, but could do some more. They are mostly the ones introduced in the Rails Space book but modified for my application.

2) Update the database.yml as follows:

production:
adapter: mysql

database: otbs_production
username: whatever
password: whatever
host: localhost
timeout: 5000

3) Write some code to put some initial data into the database. This will be a new migration file as follows:

class StartupData < ActiveRecord::Migration
def self.up
if User.find(:all).empty?
User.create(:screen_name => 'TestUser', :email => 'testuser@hotmail.com', :password => '******')
end
end
def self.down

end
end


First of all I was getting errors because my file wasn't called 009Startup_Data.rb. Once I figured out that the class name needs to be the same as the file name that helped.

Ran the following command to create the test database:
rake db:create:all (the production database was commented out in the database.yml)
rake db:migrate

The rake db:migrate doesn't fail but it doesn't add the data into the test database. Tried the following:
rake db:migrate RAILS_ENV = test
Still no luck. No errors, but no data.

Ran the following ruby command:
ruby script user:create(...
but I got the error message ruby: Permission denied.

Have decided not to worry about this for the time being. Maybe I will have a great idea at 2am on how to fix it.

4) Installed Capistrano. According to Learning Rails (n.d.), Capistrano is a powerful utility for automating any kind of web deployment. It is nearly universally used by Rails developers.

I installed the Capistrano gem as follows:
ruby gem install capistrano

The version I have is 2.5.8. Capistrano runs on the development system. It performs actions on the server by connecting to it via SSH and issuing commands, just as you would from an SSH terminal session. (maybe I don't need PuTTy after all).

To configure the application for Capistrano, the first step is to “Capify” it. From your root application directory, enter in a terminal window:

capify .

This adds two files to your application: Capfile at the root of the app, and deploy.rb in the config directory. More on this later.

5) Choose a hosting site.
Luckily Craig has offered to create some sub domains for us to use. This is good because I have been doing some research today to find a free hosting site for a Ruby on Rails web site. I have checked out many web pages and hosting sites (my head is aching...) and as per Gary's post I could only find one hosting option from Heroku at http://heroku.com which has a package called Blossum which is free. Another site that has free hosting for students is at https://www.gohosting.com.au/free-web-hosting-accounts.html. The hosting is free but you need to purchase a domain name which costs $25 doh!

Craig has just posted that he will email me the sub domain info tonight so I can upload my site. Will get back to this later...

Craig has just emailed me the details of the sub domain he has set up for me. He has sent me information on running the Net2ftp which will allow me to FTP my files up to the site.

Ran Net2ftp with the required credentials and logged on to the server. The Net2ftp has functionality to upload numerous files from a zip.

Created a zip file of my OTBS application. Uploaded the files to the server.

Craig also provided details on logging on to the database server using phpMyAdmin 2.10.1.

Updated the database.yml with the correct details as provided by Craig.

ran the following command:
rake db:migrate RAILS_ENV = production

This ran all of my migrations except the last one which sets up the default data. As previously mentioned, I couldn't even get this migration to run in test. However, the results gave me some errors which indicated that I had misspelled 2 of the field names. Updated the names and ran the following command:
rake db:migrate StartupData

This command didn't work at all and got the error "Don't know how to build task 'StartupData'".

Looked up the error and it seems that this is an old rake command. Advised to run rake -T to find out what commands are valid.

Decided to run the following on my test database now that I knew what the problem was:
rake db:migrate:reset RAILS_ENV = test

This redid all of my migrations including the StartupData migration, so now my test database was up to date and correct. Ran the following command for production which worked a treat:
rake db:migrate:reset RAILS_ENV = production

Logged on to the server and had a look. My database is now up to date and contains the data that I wanted. yah!!!

Now my database is up to date in production and my files have been ftp'd maybe my site will be working.

Tried the url http:\\karen.itc565.com but unfortunately it doesn't work. I am getting a 403 Forbidden error.

Craig is going to look at the permissions to see if this is the problem. There is probably another 50 things I need to do first :)

Meanwhile, I stumbled across another free hosting site at http:\\www.heliohost.org/
home/features-mainmenu-35/featuresscriptingmenu/rubyonrails
and another link to RailsWebHosts at http://wiki.rubyonrails.org/rails/pages/RailsWebHosts
(it's interesting what you find when you're not looking for it)
. I have put these sites onto delicious under the tag "webframeworks".

Decided to upload my files to github and then use Capistrano to deploy to the server and see if that makes any difference.

Registered with github.com and created my public repository. Used PuTTy to create a Public and Private Key Pair. Used the public key in github which was necessary to create the repository.

Used the following commands:
git remote add origin git@github.com:karenjcarter/OTBS.git
git push origin master

The second command didn't work and the following output is produced but the input is locked and therefore it is impossible to type y and accept the unknown host key:

The server's host key is not cached in the registry. You have no guarantee that the server is the
computer you think it is.
The server's rsa2 key fingerprint is:
ssh-rsa 2048 49:60:1f:71:90:8b:cc:48:a2:29:f8:a2:3a:1a:53:43
If you trust this host, enter "y" to add the key to PuTTY's cache and carry on connecting.
If you want to carry on connecting just once, without adding the key to the cache, enter "n".
If you do not trust this host, press Return to abandon the connection.
Store key in cache? (y/n)

No matter what I do I can't answer y. This is a windows problem. Apparently everything works great from Linux...

Uninstalled and reinstalled git. The same error occurs.
Did some research and to get around it I ran puTTy with the git@github.com:karenjcarter/OTBS.git host name. The same error appeared but I was able to say Yes and thus fix the problem.

Went back and issued the following command again:
git push origin master

Recieved the following error:
FATAL ERROR: Disconnected: No supported authentication methods available Pushing to g...@github.com:karenjcarter/OTBS.git fatal: The remote end hung up unexpectedly.

This error is because I am not authenticated, so I need to run Pageant(part of PuTTY) so the private key can be used. My research says that before starting an SSH connection with PuTTY, you need to run Pageant.exe. Pageant is a background process that handles authentication requests. You just add your private key in Pageant and it will keep it available for new connections, without the need to retype the password every time.

Ran Pageant by double clicking its icon in the notification area and added my private key file.
Ran the following command and it actually worked :
git push origin master

My files were copied up into the repository on github.

Now to deploy the files to the server with Capistrano.

Ran the following command:
cap deploy:setup

Of course this doesn't work. After mucking around with the deploy.rb file for many hours I have given up for the time being. Apparently Capistrano doesn't work very well with Windows...

Here is my error:
connection failed for: Karen.itc565.com (Errno::ETIMEDOUT: A connection attempt
failed because the connected party did not properly respond after a period of ti
me, or established connection failed because connected host has failed to respon
d. - connect(2))

As I already have the files on the server, I have given up on Capistrano. I will now try and run some commands on the server with PuTTy.

Loaded PuTTY, added the required URL and tried to connect. Unfortunately this didn't work and I just get a timeout error. I am therefore unable to contact to the server through SSH.

I am unable to progress any futher, but I have a good understanding of the things that can go wrong with deploying a RoR's application from a Windows platform. eg everything

My recommendation is therefore to leave plenty of time for deployment or deploy from a *nix platform.

References
Deploying to a Public Web Server (n.d.). Retrieved July 29, 2009 from http://www.buildingwebapps.com/podcasts/7098-deploying-to-a-public-web-server/show_notes

No comments:

Post a Comment