Demo of Rails 2.2 internationalization

in Ruby on Rails, Wed, 27 Aug 2008 22:12:00 GMT

Rails 2.2 is going to make it much, much easier to do internationalized sites. Check out Clemens Kofler’s sample app running live or peruse his code. Thread safety? Internationalization? What stock objections to Rails will we have left?!

Porting xgps to Qtopia for the Freerunner

by Jim Morris in Wolfmans Howlings, Wed, 27 Aug 2008 21:46:34 GMT

I was getting bored waiting for Trolltech to release the next version of Qtopia for the Freerunner, so I ported the xgps client from gpsd's distribution to Qtopia.

As you may know by now, Qtopia does not have X11 so none of the existing X11 based or GTK based GPS clients work. I was exploring GPSD because I wanted to to be able to get a one time position for my sunset calculator which requires your current latitude and longitude. Although GPSD is not well suited for that (thats a whole other blog entry), I did notice you can connect to it over the ethernet, so I was playing with cgps and xgps that you find in the GPSD tar file, running on my desktop, talking to gpsd running on my Freerunner.

So wanting to dust of my Qt skills (which were so ancient I barely even recognize Qt4) I decided to port xgps to Qt.

I first did a pretty straight forward port to my Desktop Qt4/X11.

I designed the form using designer, then plugged in the code taken as closely as I could from xgps. This was not 1:1 by any means as XLib and Qt are not similar ;) I ended up just taking the ideas and algorithms from the xgps program and recoding.

So that took a day or so, as I was relearning Qt as I went, I have to say the documentation for Qt is excellent, unlike another graphics library I could mention (I'm looking at you GTK).

I got the basics working, and as my GPS fix inside was not very good (works about 50/50), I found you could also point any gpsd based client at gpsd.rellim.com and always get good data from there, this is great for testing.

So I had qtgps (for want of a better name) running on my desktop, looking pretty much the same as xgps, with the satellite list the sky view and of course the basic Fix data.

Now the trick to port it to FR, so I could run this on my FR.

The UI wasn't going to fit on the FR screen so I revamped the UI in Designer, by basically dragging all the views into a tabbed frame. So the satellite list, the sky view, the position information and the movement information all had their own tab, this fits pretty well on a tiny screen.

Other than that I didn't have to do much. I had to create a new qtgps.pro file for Qtopia, as the qmake qt one is somewhat different. I had to use the skeleton main.cpp that all Qtopia apps seem to use, but the bulk of the code remains the same.

I did have an interesting time trying to figure out how to emit a signal from the gps callback thread to the main window, not sure if I chose the right way to do it but it worked... I created a singleton QObject based class, and allowed it to be called statically, then that does the emit. This is used to signal the main display thread that new gps data has arrived. (Note this won't work if the singleton can be called concurrently from multiple threads, Google c singleton pattern to see how to do that).

// Singleton for sending signal from a callback
class ExtSig : public QObject
{
    Q_OBJECT
    public:
        static ExtSig *self();
        void send(struct gps_data_t* p, char* buf);

    signals:
        void sendit(struct gps_data_t* p, char* buf);

    private:
        ExtSig();
};

// Singleton defns for emiting a signal from a callback
ExtSig::ExtSig(){}
ExtSig *ExtSig::self()
{
    static ExtSig inst;
    return &inst;
}

void ExtSig::send(struct gps_data_t* p, char* buf)
{
    emit sendit(p, buf);
}

// in callbeck call...
// signal the GUI thread we have new gps data
    ExtSig::self()->send(p, buf);

Ok so how can you write your own Qtopia applications? There are at least two ways, 1) use the VMPlayer based Toolchain that Trolltech provide, or 2) setup your Linux workstation to use the toolkit. 1) is easier, and just works on pretty much anything that runs VMPlayer and has a lot of memory. 2) is faster and used less memory. I chose 2) but I did play with 1).

I won't go into how to do 1) as it is pretty much straight forward, just download the iso, and install it (either from a loopback device or from a burned CD).

The second method is not so well documented (if at all) so here is what I did to make it work on a Ubuntu Hardy i686 32-bit workstation with KDE installed..

Download the toolchain and install it into /opt/toolchains, it seems it must be there otherwise it doesn't quite work.

Then download the latest qtopia snapshot and put that in a working directory and set QTOPIA_DEPOT_PATH to that directory.

Then create a build directory for the qtopia build and set the QPEDIR environment variable to that path.

In my case I have QTOPIA_DEPOT_PATH=/opt/QtopiaSDK/qtopia-opensource-src-4.3.2-snapshot-20080815 and QPEDIR=/opt/QtopiaSDK/build/qtopia-4.3.2

Now we need to build the qtopia snapshot, you can use the result to copy to /opt/Qtopia on your FR, or not, but you need to do this to get the tools to build your own apps.

Now follow these steps to build Qtopia:-

> cd $QPEDIR
> $QTOPIA_DEPOT_PATH/configure -device ficgta01
> make
> make install

Go get some coffee or Dinner :) this will take a while.....

Now you will have a full Qtopia build in $QPEDIR/image

You can use the following script to copy this to your FR, it is modified from Trolltechs update script. It will keep your settings. However you will have needed to have flashed to the 0808 version of Qtopia and kernel for this to work.

#!/bin/sh
QTOPIA_DIR=$QPEDIR/image
QTOPIA_IMAGE=qtopia.tar.gz
PHONEIP=192.168.0.200
# sudo ifconfig usb0 192.168.0.200 up

tar -C $QTOPIA_DIR czvf $QTOPIA_IMAGE
cat $QTOPIA_IMAGE | ssh "root@$PHONEIP" '(set -x;rm -f /tmp/restart-qtopia;killall qpe; mkdir -p /opt/Qtopia;rm -rf /opt/Qtopia/*; cd /opt/Qtopia;gunzip |tar xvf -;/etc/init.d/qpe start &)'

Whether you do that or just keep the Trolltech released image you are now ready to write your own apps. I recommend you study the Qt docs Qtopia is pretty much Qt4.3 at the moment.

As an example you can use the qtgps.tar.gz as an example app or use $QTOPIA_DEPOT_PATH/examples/application

I created a build/myapps directory, and then tar xvfz qtgps.tar.gz into that directory. Then...

> cd build/myapps/qtgps
> $QPEDIR/bin/qtopiamake
> make

This will create a qtgps excutable that you can copy to your FR and run.

A nice trick I found is to run a GUI app from the ssh into FR you can do this...

> ssh 192.168.0.200
> . /opt/Qtopia/qpe.env
> ./qtgps

The app will popup on the FR screen, you could also run it from the terminal on the FR or from the file manager on the FR.

To create a new application you can do this..

> cd build/myapps/mynewapplication
> ... edit some .cpp and .h files
> $QPEDIR/bin/qtopiamake -project
> ... edit mynewapplication.pro appropriately
> $QPEDIR/bin/qtopiamake
> make

The skeleton main.cpp should look like this...

#include "mynewapp.h"
#include <qtopiaapplication.h>

QTOPIA_ADD_APPLICATION(QTOPIA_TARGET,MyNewApp)
QTOPIA_MAIN

The rest of the files are up to you. I like to create the UI using designer...

> $QPEDIR/bin/designer mynewapp.ui

look in either example.cpp or qtgps.cpp and qtgps.h on how to initialize a designer based UI.

There are lots of example apps on the Trolltech docs site.

If you want to run the app locally rather than on your FR, you can use $QPEDIR/bin/qvfb you will need to do this to make the size correct...

> export QWS_DISPLAY=QVFb:mmWidth43:mmHeight58:0
> $QPEDIR/bin/qvfb -width 480 -height 640 &
> sleep 2
> ./qtgps -qws gpsd.rellim.com

This will run qtgps on the local workstation in a simulation of the FR screen (without qtopia), using gps data from gpsd.rellim.com.

You can also run Qtopia in that screen but you will need to build a native version of qtopia to do that, in that case I would use the VMPlayer version of the toolchain as it has it all built in.

I haven't quite figured out how to build an ipkg of the Qtopia apps yet. Although this seems to work...

> make packages FORMAT=ipk

I can't install the resulting ipk with opkg. Qtopia likes to build qpk files and only install them over the internet, that would probably work and is how the VMPlayer version of the SDK does it.

The source code to this project is now on github http://github.com/wolfmanjm/qtopiagps/tree/master

SQLite3-Ruby 1.2.3

by Jamis Buck in the { buckblogs :here }, Wed, 27 Aug 2008 15:11:00 GMT

SQLite3-Ruby version 1.2.3 is now available. It is a maintenance release, fixing just a few things:

  • The permissions on the database.rb and translator.rb files in 1.2.2 were incorrect, resulting in broken sqlite3-ruby installations for many *nix users. This is now fixed.
  • A few more Ruby 1.9 compatibility issues were patched.
  • Some optimizations were applied to speed up iterating over result sets.

To install or upgrade:


  gem install sqlite3-ruby

Thanks!

View: SQLite3-Ruby 1.2.3 - More entries from the { buckblogs :here }, Ruby

Juggernaut: Server-side push for Rails

in Ruby on Rails, Wed, 27 Aug 2008 13:50:00 GMT

Juggernaut is a combination of a small Ruby server, a Flash bridge, and a plugin that makes it easy to do server-side push systems in Rails. I played with this idea with Rich Killmer a few years ago and even made a small demo system to present at a conference, but never made it to the finish line of something releasable. So it’s fantastic to see that the guys behind Juggernaut did.

daemon_controller: Automatic Daemon Process Management

in Ruby Inside, Wed, 27 Aug 2008 10:24:01 GMT

After tackling the difficult task of improving Rails deployment, Phusion - the creators of Passenger (mod_rails) recently announced the availability of daemon_controller - a library (rather than a stand-alone tool) for managing daemons. It lets you write applications that manage daemons in a robust manner (e.g. mongrel_cluster or UltraSphinx could be adapted to use this library).

The primary motivation for using daemon_controller is to make it easier to have other applications (such as Rails apps) start daemons without encountering race conditions or parallel attempts. With a little work, daemon_controller makes it possible for your app to safely launch all the daemons that its functionality relies upon (UltraSphinx, BackgrounDRb, etc.).

The library is simple to use - though it does require that you know how to start the daemon, how to contact the daemon, and you must know where it will put its PID and log files. Source code is available at http://github.com/FooBarWidget/daemon_controller/tree/master

Ruby Hoedown 2008 Videos Available

in Ruby Inside, Tue, 26 Aug 2008 21:22:01 GMT

 

Videos from the recent Ruby Hoedown conference are now available at the Confreaks site.

The talks this year are split between traditional talks and "Lightning Talks" - 5 minute presentations that quickly highlight a single package or aspect of Ruby.  The talks cover a wide range of topics including Archaeopteryx - a Ruby MIDI/Music generator, easy phone calling with ruby, cloud computing, the usual testing and design patterns talks, and a slew of other quirky and useful presentations.

If you don't want to sit through the talks at normal speed, you can download the presentations in .mp4 format and increase the playback speed in your video viewer of choice.  I've found that the videos are perfectly watchable at 2x speed in Quicktime (Command-K > Playback speed.)

Cap 1.4.1? Go 1.4.2. Now.

by Jamis Buck in the { buckblogs :here }, Tue, 26 Aug 2008 15:43:00 GMT

Are you currently using Capistrano 1.4.1? If so, drop everything (I mean it, do this RIGHT NOW) and install Capistrano 1.4.2.

Why, you ask?

Capistrano 1.4.1 will work just fine, right up until you decide you want to experiment with Capistrano 2. When you do that, Cap 2.3 will install net-ssh 2.x, which kills Capistrano 1.4.1 in all kinds of really obscure ways.

The good news is that Cap 1.4.2 is completely compatible with Cap 1.4.1. It adds no new features. The only “bug” it fixes is that if you ever install net-ssh 2.x, Cap 1.4.2 will still happily continue to work.

Ultimately, an upgrade to Cap 2 is recommended, but I understand it’s not feasible for everyone. So, if you’re one of those who can’t go cap2 yet, please please please PLEASE PLEASE FOR THE LOVE OF ALL THAT IS HOLY upgrade to Cap 1.4.2. It’ll make your life easier, and it’ll make my life easier (because I won’t have to keep troubleshooting the same issues over and over). Thanks!

Moving away from blogspot

by Ola Bini in Ola Bini on Java, Lisp, Ruby and AI, Tue, 26 Aug 2008 15:14:00 GMT
This will be my last post on this blog. For several reasons I like the idea of keeping more in control over my blog and the environment surrounding it. I also have some things I'd like to publish that isn't well suited for the blog format, and moving to another location means that I can keep all my content in the same place. More long term I'm planning on migrating information about my open source projects there to.

But what you need to know is this: This blog ends. A new blog is born. All my old entries have been migrated. The important addresses for the new blog is:
And that's it. The new content will obviously be available at http://olabini.com, but right now this site just redirects to the blog.

The blog is dead, long live the blog.

Some functional programming and OCaml koans

by Mauricio Fernandez in Eigenclass, Tue, 26 Aug 2008 10:12:29 GMT

let rec

One day, a disciple of another sect came to Xavier Leroy and said mockingly:

"The OCaml compiler seems very limited: why do you have to indicate when a function is recursive, cannot the compiler infer it?"

Xavier paused for a second, and replied patiently with the following story:

"One day, a disciple of another sect came to Xavier Leroy and said mockingly..."

Polymorphic answers

A novice came to Jacques Garrigue and spoke nervously: "I don't get rank-2 polymorphism. What is it good for? When to use it? How can I understand it?". Jacques asked: "Do you want an answer to each question, or the answer to all your questions?." The novice was enlightened.

Leroy instructs a student

The venerable master Leroy was walking with his student. Wishing to start a discussion with his master, the apprentice said "Master, I've heard that all loops must be replaced with tail-recursive functions. Is that true?" Leroy looked commiseratively at his student and replied "Foolish pupil, many tail-recursive functions are merely inefficient loops."

The student spent the next few weeks replacing tail-recursive functions with explicit loops. He finally showed his code to master Leroy, seeking his approval. Leroy hit him with a stick. "When will you learn? Explicit loops are a poor man's tail-recursive functions." At that moment, the student became enlightened.

Complete solutions

Anton had been studying the dining philosophers problem and was ecstatic when he presented a novel solution to Damien Doligez. The great master did not show the same enthusiasm, though: all he said was "the solution is not complete". Anton knew that Doligez had written a machine-checked proof of correctness for the concurrent GC he developed in the early 90s for Caml Light, and began to work. After struggling with Coq, he came back to Doligez.

"I have proved that my solution prevents starvation and livelock."

Again, Doligez replied: "the solution is not complete".

Anton retorted: "My correctness proof has been verified mechanically. Moreover, my simulations show that my method allows for a large degree of concurrency, and it's easy to implement efficiently. Surely the solution is complete now?"

The Zen master then explained: "You claim to have solved the dining philosophers problem, yet you haven't expounded where the spaghetti come from."

Pierre Weis and the variance annotations


Read more...

Mack 0.7.0: A Significant Update to A Powerful Ruby Web App Framework for Distributed Apps

in Ruby Inside, Tue, 26 Aug 2008 02:28:18 GMT

macklogo.png We first covered Mack in April, when I billed it as a "fast, best of the rest, Web app framework." Mack, a Ruby-based Web application framework, developed by a team led by Mark Bates, has continued to grow over the past several months and today announced a significant release, Mack 0.7.0.

Mack is a unique Ruby Web app framework due to its heavy focus on reusability across multiple applications. Mack supports distributed objects - yes, between multiple applications, distributed views and layouts, and even distributed routing (all of your Mack apps can be aware of each other's routing). Mack has a Google group for discussion purposes, and Mark has put together a great screencast demonstrating how to create a simple application with the framework.

I caught up with developer Mark Bates to ask a few questions about the framework:

Why would you recommend someone try Mack over the alternatives (Merb, Rails, Ramaze, Sinatra, etc.)?

Mark Bates: I would recommend that people try a lot of different frameworks before they decide which one to go with - that's what I did. I tried over a dozen frameworks back in January 2008 when we decided to move away from Rails. None of them were right for the problems we had to solve. If you're looking for a framework that is simple, fast, easy to use, and yet powerful, than Mack might be right for you.

Mack has a large focus on giving developers the tools to build SOA, distributed, and portal-like applications. Mack is easily extensible and easy to configure to suite your projects. The core of Mack is small and lightweight. The mack-more series of gems allows people to pull in the extras that absolutely need.

What are the new features in Mack 0.7.0?

The two biggest features in 0.7.0 would have to be Distributed Objects and Distributed Views/Layouts, which round out the three big distributed features, along with distributed routes. These features are what really set Mack aside from all the other frameworks out there right now. Using the power of Rinda we're able to have a bunch of Mack applications talking seamlessly together with almost zero configuration.

In addition to the new distributed features, we've added a ton of helpers, including a whole suite of form helpers, Transactional support for tests using Active Record, DataMapper was already supported. Great improvements to the mack-data_factory content generating system, including a ton of new content types. A whole slew of bug fixes, and general overall improvements. A bunch of new generators and enhancements to existing ones.

What have you learned / discovered in the process of developing Mack?

One of the biggest things that I've learned since I've started developing Mack is that I truly believe in the KISS philosophy, Keep It Simple Stupid, and that it is the only way to code. One of big frustrations I've had with other frameworks, and one in particular, has been that if you crack open the source code to try and track down a bug it's nearly impossible. There are just thousands of lines of code, and eight levels of redirection before you find the things you're looking for. When I talk to people who want to commit that's the first thing I tell them, keep it simple. I want people to be able to open the source and be able to very quickly follow it through. Keeping things simple also has other side effects, apart from readability. It helps keep things fast, and helps keeps the bugs to a minimum.

Is deploying Mack applications as easy as Rails or Merb apps?

Since Mack is a Rack application at heart, it deploys with Phusion Passenger with just a simple config.ru file that Passenger requires. If you're using Thin, there's a Mack adapter built in, so it's very easy to deploy with a Thin cluster. Mackwiki.com (a new Mack-based wiki) is hosted using a cluster of 3 Thin servers behind Nginx. Again, KISS, is evident here. I want Mack apps to be easy to deploy. I've fought through some tough deployments with other frameworks, and I don't want other people to have to go through that.

What’s Hot on GitHub - August 2008

in Ruby Inside, Mon, 25 Aug 2008 15:40:02 GMT

Github is a great resource for finding new projects within the Ruby community. It has become an extremely popular place for Ruby and Rails developers to congregate lately, so I wanted to list some of the new projects, and some of the updated ones, that I have found interesting and that are too small for their own blog post. Let us know if you like this as we might turn it into a regular series on Ruby Inside!

This month's picks:

  • Uppercut - A simple DSL for creating Jabber agents within Ruby. Seems quite young but full of potential.
  • AintABlog - Open-source tumblog application. Imagine your own tumblr account, but really customizable.
  • factory_girl_on_rails - If you use factory_girl (like I do) then this will provide you with automatic inclusion of test/factories/* and also new generators for creating factories.
  • object_daddy - A nice alternative to factory_girl. It helps you kill rails fixtures, keep your code DRY and reduce the complexity of your tests.
  • Chris Wanstrath's Ruby Hoedown Keynote - An unusual entry for Github, but as Chris runs GitHub, this makes sense. It's a transcript of Chris's great keynote presentation at the recent Ruby Hoedown conference.
  • Backchat - a one-file Merb application that allows you to quickly add commenting (think disqus) functionality on to any site.
  • HomeMarks - a Rails application that provides "start page" type functionality. A great example of Rails code using unobtrusive JavaScript. Very slick.
  • Inline File Editing - Not a project, but a new Github feature. You can now edit files in your repositories within the browser and Github deals with the housekeeping. Perfect for the most minor tweaks.
  • Adhearsion - A popular VoIP framework (a bit like a "Rails for telephony") that has just migrated over to using GitHub.

NeverBlock: Fast, Non-Blocking IO In Ruby Without Changing Program Flow

in Ruby Inside, Mon, 25 Aug 2008 11:30:46 GMT

neverblock.pngNeverBlock is a Ruby (1.9) library developed by eSpace - an Egyptian Web 2.0 development team - that could make your life a whole lot easier if you have to deal with blocking IO operations that hold up all your Ruby threads.

NeverBlock makes it easy to get the benefits of non-blocking IO (IO operations that aren't held up by mutexes) in your Ruby apps without having to take the usual route of redesigning your app to be event-based. You get all the benefits of event-based code (lower CPU overhead, lower memory use, less hangups) but with the benefit of a normal program flow model.

The catch? It's Ruby 1.9 only - relying heavily on fibers - and the documentation isn't particularly strong yet so it'll take a bit of work to figure out. It could be worth it, though, as in benchmarks on the NeverBlock-based Postgres library, throughput is shown as significantly increased where a percentage of queries are complex and slow (and would usually block up the client).

If you're still interested, NeverBlock is available on Github - there's also an official home page and documentation available.

This post is sponsored by AlphaSights Ltd - AlphaSights are recruiting. If you're looking for a Ruby on Rails opportunity, can work in Cambridge, UK and enjoy the buzz of a brand new well-funded startup then look no further. AlphaSights are recruiting from entry level to senior positions and offer very competitive salaries and a great working environment.

ThoughtWorks Sweden is available

by Ola Bini in Ola Bini on Java, Lisp, Ruby and AI, Mon, 25 Aug 2008 11:02:00 GMT
I would like to announce that ThoughtWorks Sweden is now in motion. We have business cards and an office. Everyone is returning from their long lovely Swedish summer vacations.

This means that ThoughtWorks Sweden is ready, and available for work. If you or your business have a project you need help with, don't hesitate to contact me (at obini@thoughtworks.com) or Marcus Ahnve (at mahnve@thoughtworks.com).

We are located in Stockholm, but we are open for work anywhere in the Nordic regions.

So what kind of work are we most suited for? Our sweet spot is in delivery and technical advisory regarding Java, Ruby and JRuby. And if you're interested in understanding how our Agile approach can change your company, we can do organizational transformation projects and also coaching and advisory.

Don't hesitate to get in touch!

Zero to Production in 15 Minutes

by Charles Nutter in Headius, Sun, 24 Aug 2008 17:30:00 GMT
There still seems to be confusion about the relative simplicity or difficulty of deploying a Rails app using JRuby. Many folks still look around for the old tools and the old ways (Mongrel, generally), assuming that "all that app server stuff" is too complicated. I figured I'd post a quick walkthrough to show how easy it actually is, along with links to everything to get you started.

Here's the full session, in all its glory, for those who just want the commands:
~/work ➔ java -Xmx256M -jar ~/Downloads/glassfish-installer-v2ur2-b04-darwin.jar
~/work ➔ cd glassfish
~/work/glassfish ➔ chmod a x lib/ant/bin/*
~/work/glassfish ➔ lib/ant/bin/ant -f setup.xml
~/work/glassfish ➔ bin/asadmin start-domain
~/work/glassfish ➔ cd ~/work/testapp
~/work/testapp ➔ jruby -S gem install warbler
~/work/testapp ➔ jruby -S gem install activerecord-jdbcmysql-adapter
~/work/testapp ➔ vi config/database.yml
~/work/testapp ➔ warble
~/work/testapp ➔ ../glassfish/bin/asadmin deploy --contextroot / testapp.war
Now, on to the full walkthrough!

Prerequisites

JRuby 1.1.3 or higher

None of the steps in the main walkthrough require JRuby, since Warbler works fine under other Ruby implementations. But if you want to install and test against the JDBC ActiveRecord adapters, JRuby's the way. And in general, if you're deploying on JRuby, you should probably test and build on JRuby as well. Go to www.jruby.org under "Download!" and grab the latest 1.1.x "bin" distribution. I link here JRuby 1.1.3 tarball and JRuby 1.1.3 zip for your convenience. Download, unpack, put in PATH. That's all there is to it.

Java 5 or higher

Most systems already have a JVM installed. Here's some links to OpenJDK downloads for various platforms, in case you don't already have one.
  • Windows, Linux, Solaris: Download the JDK directly from Sun's Java SE Downloads page. I typically download the JDK (Java Development Kit) because I find it convenient to have Java sources, compilers, and debugging tools available, but this walkthrough should work with the JRE (Java Runtime Environment) as well. Linux and Solaris users should also be able to use their packaging system of choice to install a JDK.
  • OS X: The 32-bit Intel macs can't run the Apple Java 6, so you'll want to look at the Soylatte Java 6 build for OS X to get the best performance. A small warning...it doesn't have Cocoa-based UI components, so it will use X11 if you start up a GUI app.
  • BSDs: FreeBSD users should check the FreeBSD Java downloads page. I believe there's a port for FreeBSD and package/port for OpenBSD but I couldn't dig up the details. We have had users on both platforms, though, so I know they work fine.
  • Others: There's basically a JDK for just about every platform, so if you're not on one of these just do a little digging. All you need to know is that it needs to be a full Java 5 or higher implementation.
Rails 2.0 or higher

Hopefully by now most of you are on a 2.x version of Rails. This walkthrough will assume you've got Rails 2.0 installed. If you're using JRuby, it's a simple "jruby -S gem install rails" or if you've got JRuby's bin in PATH, "gem install rails" should do the trick. Note that the Warbler (described later) should work in any Ruby implementation, since it's just a packager and it includes JRuby.

Step One: The App Server

The words "Application Server" are terrifying to most Rubyists, to the point that they'll refuse to even try this deployment model. Of course, the ones that try it usually agree it's a much cleaner way to deploy apps, and generally they don't want to go back to any of the alternatives.

Much of the teeth-gnashing seems to surround the perceived complexity of setting a server up. That was definitely the case 5 years ago, but today's servers have been vastly simplified. For this walkthrough, I'll use GlassFish, since it's FOSS, fast, and easy to install.

I'm using GlassFish V2 UR2 (that's Version 2, Update Release 2) since it's very stable and by most accounts the best app server available, FOSS or otherwise. Not that I'm biased or anything. At any rate, it's hard to argue with the install process.

1. Download from the GlassFish V2 UR2 download page. The download links start about halfway down the page and are range from 53MB (English localization) to 93MB (Multilanguage) in size.

2. Run the GlassFish installer. The .jar file downloaded is an executable jar containing the installer for GlassFish as well as GlassFish itself. The -Xmx specified here increases the memory cap for the JVM from its default 64MB to 256MB, since the archive gets unpacked in memory.
~/work ➔ java -Xmx256M -jar ~/Downloads/glassfish-installer-v2ur2-b04-darwin.jar
glassfish
glassfish/docs
glassfish/docs/css
glassfish/docs/figures
...
glassfish/updatecenter/README
glassfish/updatecenter/registry/SYSTEM/local.xml
installation complete
~/work ➔
Before the unpack begins, the installer will pop up a GUI asking you to accept the GlassFish license.
Read the license or not...it's up to you. But to accept, you need to at least pretend you read it and scroll the license to the bottom.
The installer will proceed to unpack all the files for GlassFish into ./glassfish.

3. Run the GlassFish setup script. In the unpacked glassfish directory, there are two .xml files: setup.xml and setup-cluster.xml. Most users will just want to use setup.xml here, but if you're interested in clustering several GlassFish instances across machine, you'll want to look into the clustered setup. I won't go into it here.

The unpacked glassfish dir also contains Apache's Ant build tool, so you don't need to download it. If you already have it available, your copy should work fine, and the chmod command below--which sets the provided Ant's bin scripts executable--would be unnecessary. If you're on Windows, the bin scripts are bat files, so they'll work fine as-is.

Two items to note: you should probably move the glassfish dir where you want it to live in production, and you should run the installer with the version of Java you'd like GlassFish to run under. Both can be changed later, but it's better to just get it right the first time.
~/work ➔ cd glassfish
~/work/glassfish ➔ chmod a x lib/ant/bin/*
~/work/glassfish ➔ lib/ant/bin/ant -f setup.xml
Buildfile: setup.xml

all:
[mkdir] Created dir: /Users/headius/work/glassfish/bin

get.java.home:

setup.init:
...
jar-unpack:
[unpack200] Unpacking with Unpack200
[unpack200] Source File :/Users/headius/work/glassfish/lib/appserv-cmp.jar.pack.gz
[unpack200] Dest. File :/Users/headius/work/glassfish/lib/appserv-cmp.jar
[delete] Deleting: /Users/headius/work/glassfish/lib/appserv-cmp.jar.pack.gz
...
do.copy.unix:
[copy] Copying 1 file to /Users/headius/work/glassfish/config
[copy] Copying 1 file to /Users/headius/work/glassfish/bin
[copy] Copying 1 file to /Users/headius/work/glassfish/bin
...
create.domain:
[exec] Using port 4848 for Admin.
[exec] Using port 8080 for HTTP Instance.
[exec] Using port 7676 for JMS.
...
BUILD SUCCESSFUL
Total time: 29 seconds
~/work/glassfish ➔
4. Start up your GlassFish server. It's as simple as one command now.
~/work/glassfish ➔ bin/asadmin start-domain
Starting Domain domain1, please wait.
Log redirected to /Users/headius/work/glassfish/domains/domain1/logs/server.log.
Redirecting output to /Users/headius/work/glassfish/domains/domain1/logs/server.log
Domain domain1 is ready to receive client requests. Additional services are being started in background.
Domain [domain1] is running [Sun Java System Application Server 9.1_02 (build b04-fcs)] with its configuration and logs at: [/Users/headius/work/glassfish/domains].
Admin Console is available at [http://localhost:4848].
Use the same port [4848] for "asadmin" commands.
User web applications are available at these URLs:
[http://localhost:8080 https://localhost:8181 ].
Following web-contexts are available:
[/web1 /__wstx-services ].
Standard JMX Clients (like JConsole) can connect to JMXServiceURL:
[service:jmx:rmi:///jndi/rmi://charles-nutters-computer.local:8686/jmxrmi] for domain management purposes.
Domain listens on at least following ports for connections:
[8080 8181 4848 3700 3820 3920 8686 ].
Domain does not support application server clusters and other standalone instances.

~/work/glassfish ➔
Congratulations! You have installed GlassFish. Simple, eh?

A few tips for using your new server:
  • There's a web-based admin page at http://localhost:4848 where the admin login is admin/adminadmin by default. You'll want to change that password. Select "Application Server" on the left and then "Administrator Password" along the top.
  • Poke around the admin console to get a feel for the services provided. You won't need any of them for the rest of this walkthrough, but you might want to dabble some day. And if you want to set up a connection pool later on (which ActiveRecord-JDBC supports) this is where you'll do it.
  • Most folks will probably want to set up init scripts to ensure GlassFish is launched at server startup. That's outside the scope of this walkthrough, but it's pretty simple. I'll update this page (and it's equivalent on the JRuby Wiki) once I know more.
  • GlassFish works just fine as a standalone server, but many users will want to proxy it through Apache or another web server. Again, this is outside the scope of this walkthrough, but it should be as simple as configuring a virtual host or a set of matching URLs to hit the GlassFish server at port 8080 (which is the default port for web applications). For apps I'm running, however, I just use GlassFish.
Step 2: Package your App

This step is made super-trivial by Nick Sieger's Warbler. It includes JRuby itself and provides a simple set of commands to package up your app, add a packaging config file, and more. In this case, I'll just be packaging up a simple Rails app.

Note that Warbler works just fine under non-JRuby Ruby implementations, since it's all Ruby code. But again, if you're deploying with JRuby, it's probably a good idea to test and build with JRuby as well.
~/work ➔ jruby -S gem install warbler
Successfully installed warbler-0.9.10
1 gem installed
Installing ri documentation for warbler-0.9.10...
Installing RDoc documentation for warbler-0.9.10...
~/work ➔ cd testapp
~/work/testapp ➔ ls .
README Rakefile app config db doc lib log public script test tmp vendor
~/work/testapp ➔ warble
jar cf testapp.war -C tmp/war .
~/work/testapp ➔
And that's essentially all there is to it. You will get a .war file containing your app, JRuby, Rails, and the Ruby standard library. This one file is now a deployable Rails applications, suitable for any app server, any OS, and any platform without a recompile. The target server doesn't even have to have JRuby or Rails installed.

Step 3: Deploy your Application

There's two ways you can deploy. You can either go to the Admin Console web page, select "Web Applications" from the "Applications" category on the left, and deploy the file there, or you can just use GlassFish's command-line interface. I will demonstrate the latter, and I'm providing the optional contextroot flag to deploy my app at the root context.
~/work/testapp ➔ ../glassfish/bin/asadmin deploy --contextroot / testapp.war
Command deploy executed successfully.
~/work/testapp ➔
That's it? Yep, that's it! If we now hit the server at port 8080, we can see the app is deployed.
Step 4: Tweaking

There's a few things you can do to tweak your deployment a bit. The first would be to generate a warble.rb config file and adjust settings to suit your application.
~/work/testapp ➔ warble config
~/work/testapp ➔ head config/warble.rb
# Warbler web application assembly configuration file
Warbler::Config.new do |config|
# Temporary directory where the application is staged
# config.staging_dir = "tmp/war"

# Application directories to be included in the webapp.
config.dirs = %w(app config lib log vendor tmp)
...
In this file you can set the min/max number of Rails instances you need, additional files and directories to include, additional gems and libraries to include, and so on. The file is heavily commented, so you should have no trouble figuring it out, but otherwise the Warbler page on the JRuby Wiki is probably your best source of information. And since a lot of people ask how many instances they should use, I'll provide a definitive answer: it depends. Try the defaults, and scale up or down as appropriate. Hopefully with Rails 2.2 this will no longer be needed, as is the case for Merb and friends.

The other tweak you'll probably want to look into is using the JDBC-based ActiveRecord adapters instead of the pure Ruby versions (or the C-based versions, if you're migrating from the C-based Ruby impls). This is generally pretty simple too. Install the JDBC adapter for your database, and tweak your database.yml. Here's the commands on my system:
~/work/testapp ➔ jruby -S gem install activerecord-jdbcmysql-adapter
Successfully installed jdbc-mysql-5.0.4
Successfully installed activerecord-jdbcmysql-adapter-0.8.2
2 gems installed
Installing ri documentation for jdbc-mysql-5.0.4...
Installing ri documentation for activerecord-jdbcmysql-adapter-0.8.2...
Installing RDoc documentation for jdbc-mysql-5.0.4...
Installing RDoc documentation for activerecord-jdbcmysql-adapter-0.8.2...
~/work/testapp ➔ vi config/database.yml
~/work/testapp ➔
And the modified database.yml file:
...
# This was changed from "adapter: mysql"
production:
adapter: jdbcmysql
encoding: utf8
...
Now repackage ("warble" command again), redeploy, and you're done!

Conclusion

Hopefully this walkthrough clears up some confusion around JRuby on Rails deployment to an app server. It's really a simple process, despite the not-so-simple history surrounding Enterprise Application Servers, and GlassFish almost makes it fun :)
View: Zero to Production in 15 Minutes - More entries from Headius, Ruby

DoS Vulnerabilities in REXML

in Ruby on Rails, Sat, 23 Aug 2008 08:15:00 GMT

The ruby-security team have published an advisory about a DoS bug affecting REXML users. Almost all rails applications will be affected by this vulnerability and you’re strongly advised to take the mitigating steps recommended in the advisory. If you’re not sure whether your application could be affected, you should upgrade.

The announcement contains details describing the monkeypatch solution, but to summarise:

Versions 1.2.6 and earlier

  1. Copy the fix file into RAILS_ROOT/lib
  2. Require the file from environment.rb require ‘rexml-expansion-fix’

Versions 2.0.0 and later

Copy the fix file into RAILS_ROOT/config/initializers, it will be required automatically.

This fix is also available as a gem, to install it run:

gem install rexml-expansion-fix

Then add require ‘rexml-expansion-fix’ to your environment.rb file. The manual fix and the gem are identical, if you have applied one you do not need to apply the other.

View: DoS Vulnerabilities in REXML - More entries from Ruby on Rails, Ruby

 

Ruby

A dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.

Feeds