Wednesday, November 19, 2008

Post-install / post-update scripts for ruby gems

This post outlines a hack that allows a gem to run Ruby code when the gem is installed or updated, which in effect gives post-install / post-update hooks. The method described here works with any reasonable version of ruby and rubygems (I only tested as far back as ruby 1.8.4 and rubygems 0.9.4, and everything looked good.)

Summary
Add a fake extension, use extconf.rb to run your code and then simulate successful compilation. Use the links at the end for example code.


Detailed Description
RubyGems isn't supposed to run arbitrary code during gem installations, but it supports building extensions for gems. This is a really sweet feature, and makes rubygems a nice tool for cross-platform package management. That's good and all, but the part that we care about is that the process of building an extension starts by running extconf.rb in the extension's directory, which is responsible for producing a Make file that will orchestrate the building process.

Knowing this, the first thing that comes to one's mind is - let's add an extension to the gem, and put the hook code in extconf.rb. However, there's one more issue left. If rubygems believes that your gem's extension hasn't been built properly, it will not finish installing the gem, and it will spit out a nasty error message.

In order to work around that, we need to trick rubygems' build process, so we need to bypass 3 checks:
  1. a Make file is generated - create an empty Makefile
  2. make all and make install run successfully - generate a make file that contains empty all and install targets; create fake make binaries to cover the case when the user doesn't have a build environment (Linux/Mac: an executable make with a /bin/sh shebang should work; Windows: an empty nmake.bat should trick the Windows port of rubygems)
  3. an extension binary is generated - create empty files your_extension_name.so and your_extension_name.dll
You can implement this yourself, or you can depend on my zerg_support gem, and use the method there, like the example code below does. I promise I won't mind if you copy-paste the code, so you don't have to take an extra dependency :)

I haven't tested out the Windows plan yet, but I have good reasons to believe it should work (I've built gems with real extensions on Windows some time ago).


Code Map
Adding an extension to your gemspec (assumes you're using hoe or echoe):
http://rails-pwnage.rubyforge.org/svn/trunk/zerg/Rakefile

Placing your hook in extconf.rb:
http://rails-pwnage.rubyforge.org/svn/trunk/zerg/ext/zerg_setup_hook/extconf.rb

Tricking rubygems into thinking an extension was built (see emulate_extension_install):
http://rails-pwnage.rubyforge.org/svn/trunk/zerg_support/lib/zerg_support/gems.rb

Monday, September 22, 2008

Daemons in Ruby on Rails applications

Summary
I have created a Rails plug-in, daemonz, that automatically starts and stops the daemons associated with an application, without too much configuration. It works in both production and developer mode. Read below if you care about the motivation behind not using an existing tool.

The Story
In the life of every Ruby on Rails app, there comes a time when the boundaries of a single process are simply too narrow. It might be the need for speed-ups (memcached), full-text indexing (ferret), or background work (simple-daemon), or something entirely different. One thing is certain - daemons will start accompanying your applications.

My application, Movie Nights, desperately needed daemons for all the above reasons. I've been putting that off for as much as possible, because I didn't want to make my developers learn how to start all these processes, and I didn't want the hassle of having to remember all that. When I really had to do this, I knew there is only one right answer: automate.

So I looked into monit and god. They're awesome for the production environment, which didn't really help - I already have a deployment tool, which can handle daemons. I wanted something that would work under the development environment, and would automatically start/stop everything, and I accepted that I'll have to build my own.

Sadly, I don't have time to do this the right way (an integrated system that manages all the dependencies of a Rails app), so I settled for the quick way. Enter daemonz, a bandaid (not really a full solution) for the problem I've described above. Once it is configured, it knows how to start and stop the daemons in most non-trivial circumstances, e.g. having a cluster of app servers, or having the Rails framework brought up by tools. It's not a nice and elegant solution, but it works, and it took a few hours to code.

Enjoy!
(If you're not a Rails programmer, hopefully you can at least enjoy the speed that Movie Nights has gained by performing all its Facebook API calls in a background process.)

Monday, September 1, 2008

Never Gone

I've been at Google for the past couple of months. But I haven't been (completely) idle. I dusted off my IAP creation (Movie Rails), and adapted (read: pretty much re-wrote) it to work with the new Facebook. I give you Movie Nights.

Why did I do this? Mainly to satiate my passion for Ruby and Rails (no, I didn't want to say Ruby on Rails). And there's also fbFund, which has been accepting submissions until this past Friday. If they happen to like the entry that some of my bold friends and I have put together, we'll have some nice cash to turn this idea into the best movie application ever. And my mom will finally use something I've coded.

Saturday, April 26, 2008

Ubuntu 8.04 on Mac Mini

Update: please read my new post if you want to install Ubuntu 8.10 or 9.04.

So, Ubuntu 8.04 came out yesterday. I promptly got my Mini to not boot by trying to install it using the same procedure I used for 7.10. One day later, I figured out a (small) sequence of steps that yields a working dual-boot of Leopard and Ubuntu 8.04. Here's what I did:

  1. Use the Leopard (Desktop or Server) install disc to re-partition the disk to 1 partition, then install Leopard.
  2. Install Software Updates. Needs to be done twice.
  3. Start up Boot Camp, and resize your OSX partition. Quit Boot Camp when it offers to start the Windows installation.
  4. Download and install rEFIt: http://refit.sourceforge.net/
  5. Make rEFIt take over the boot process (it should do that by itself but that doesn't work for me):

    • Open Terminal
    • cd /efi/refit

    • ./enable-always.sh


  6. Optionally, switch the boot default to Linux: open /efi/refit/refit.conf in TextEdit, and uncomment the line saying #legacyfirst (at the very bottom)
  7. Reboot and insert your Ubuntu 8.04 disc. The rEFIT screen should pop up. Don't worry if you've never seen it before. You'll notice the difference.
  8. Start installing Ubuntu. Choose manual partitioning, delete the big FAT32 partition that Boot Camp created (leave the EFI partition alone though), and create the root and swap partitions in the free space.
  9. On the last install screen, click Advanced, and replace (hd0) with (hd0,2). This is necessary so that Grub installs in the right place.
  10. Upon rebooting, go to Partitioning Tool (second icon in the bottom row) in rEFIt. It will offer to update the MBR to reflect the EFI partition table. Accept. Then the Mini will reboot again.
  11. Power off the Mini. Then power it back on. Now you can boot Ubuntu or Leopard.
I know that the last steps look like black magic (pulled out of my behind). I didn't get it to work otherwise. These steps work. Please comment if you find a shorter sequence.

Asides from getting Ubuntu to boot, rEFIt is handy because it rescues you in case something blows up. When I bricked the Mini, I was running on Apple's boot loader. rEFIt was still be able to boot OSX, even when I messed up the grub install options.

Enjoy your Mini servers :)

Tuesday, February 19, 2008

Interesting eBay affiliate strategy

Call me dumb, but I didn't see this before.

This company registered as an eBay affiliate, and its 'value add' is that it gives people money back on their eBay purchases. I feel dumb for not having done that myself (ok, maybe I don't feel so bad... I'm not quite yet allowed to make money in the US anyway).

This is a refreshing turn from the usual deal, which is more like "give us $10 a month and you might qualify for some of our awards if you go to really expensive places and buy stuff at list price".

If you sign up for kickitback, please mention victorcostan as the referrer :)

Monday, November 19, 2007

My first Ruby gem

This past weekend has a special importance for me. I am finally able to stand by my favorite language, Ruby, with more than just words.

To cut a long story short, I'm playing with smart cards for my research. I didn't find any good library for accessing cards from Ruby, so I was faced with either writing my own extension, or leaving my favorite language for something else. My desire for beauty triumphed over my laziness, and my first Ruby extension was born.

It wasn't as bad as I expected. It took me 4 half-days to figure out PC/SC, ruby's extension mechanism, gems, and Rubyforge. And in the fifth day I rested got it to work under Linux/Windows.

So, yay, I've made my small contribution to the Ruby community. If you're curious to check it out, go to its Rubyforge page.

Wednesday, November 7, 2007

Resetting a Blackberry, All the Way

Why should you read this?
You have a Java (read: past 2 years) Blackberry. You want to start from a clean slate, and you realize that Wipe Handheld only goes half way there. You want to take advantage of the hours I spent googling this issue, and trust me it's non-trivial.

My Motivation (skip if you don't care)
My beloved Pearl started acting up, and I wanted to start from a clean slate. I did some unconventional experiments, so I was pretty sure that starting fresh would bring an advantage to the table.

I searched blackberry reset and firmware reflash and what not, and got no answer. In the end, I got bored and clicked some random links (I believe they had to do with hacking your Berry), and got to what I wanted.

So I'd like to share what I learned. Hopefully, Google will like the fact that I'm using its blogging tool and index the page with a good rank, so other people don't waste as much time as I did.

Proposed Solution
Without further ado, here's how I reset my Blackberry. It worked for me. Comments with refinements to my method are welcome.
  1. Backup your data
    I can't help here, because I didn't want to backup my data. Everything is synced with my Mac, so why risk restoring the cause of my issues?

  2. Wipe your handheld
    I'm not sure this is necessary, but I did it, and it can't hurt. Go to the Settings application, then get to Security > General Settings. Push the menu key (the one with the Blackberry logo) and select (yes, you've guessed it right!) Wipe Handheld from the menu. Let it do its thing for a few minutes.

  3. Get the tool to wipe your handheld for real
    The tool is pretty geeky (text-mode), but does the job. It's a wrapper around an even more geeky tool (javaloader from the Blackberry SDK). Here's a link to a place where you can get it from.
    http://www.blackberryforums.com/general-blackberry-discussion/18789-jl_cmder.html
    Alternately, you can google JL_cmder and you should be able to find it.

  4. Wipe your Berry for real
    Connect your Blackberry to your PC via the USB cable. Exit Desktop Manager if it pops up. Then run the tool, choose wipe, and follow the on-screen pointers.
    Warning: those people aren't kididing. Pay attention on this step, so you don't brick your Berry.

  5. Get the firmware you want to install
    Go to blackberry.com to be pointed to the download site for your carrier. Or google the firmware you want. Google search works well for this part, so there's no need for me to post links. Install both the desktop software (Desktop Manager) and the firmware (Blackberry calls it handheld software).

  6. Install the new firmware
    Run Desktop Manager, while your Berry is still connected to the PC. Application Loader should pop up by itself, and let you choose which components of the firmware you want to install. I skipped the foreign languages, as well as applications that I don't use, and was happy to get the free space.
    Be patient. This can take anywhere from 30 minutes to an hour. Don't mess with the phone during this time, unless you have the time to go through a bricked phone replacement.
  7. Restore your data
    If you did a backup in step 1, now's the time to restore it. If not, you'll probably spend a couple of hours downloading your applications and redoing the phone's settings.

That's it. Enjoy, and share any refinements you come up with!