Wednesday, March 21, 2012

Upstarting Thin - Aka: Using Upstart to Start and Manage Thin

Ubuntu created this great, simple way to manage startup processes on Linux called Upstart. The beauty of it is that it's much simpler to setup than the old init.d way and also easy to use. If you want to run Thin and have the processed managed by Upstart, here's the step by step.

This assumes you have a Rack based app ready to go.

1. Create an Upstart config file

Take this:

Change the APP_HOME environment variable to point to your Rack app.

Paste it into a file at /etc/init/thin.conf .

2. Start it

sudo start thin


Thursday, July 07, 2011

How to Use Github Private Gists for Configuration Files in your Apps

Configuration files for applications are a royal pain in the butt because they usually contain some kind of authentication credentials that you don't want to check in to version control. So every developer on your team has separate config files that they or you use environment variables (which really suck when you have more than a couple) and the pain gets even worse when you have multiple computers and many projects. I think I have at least 10-20 projects I am involved with at any one time so this is a royal pia.

I've been thinking for a while now that someone ought to create a config service that is simply a secure place to store, modify and retrieve your config files using some secure URL. But just yesterday I thought of a simple way to accomplish this now with Github's Gists. This may not be the most secure thing, but for open source projects and things that aren't critical, it should work just fine.

I'll be using Ruby for this example.

First, let's create a private gist. Go to https://gist.github.com/ , in the name this file text box,  enter `config.yml` and put the following into the big text area:



Click Create Private Gist.

Now let's use this Gist to load it into our application. First we need the URL to the raw text so click raw on the gist and you'll get a url like this:

https://raw.github.com/gist/1070883/1b3ba1a07c51abc0720afc9ca8cc25b7c8ed5277/config.yml

We need to remove the commit reference in that URL (the big long string) and when you do, it will look like this:

https://raw.github.com/gist/1070883/config.yml

That's the URL you want for this next step. Now each developer can use a different gist URL and everyone should keep their URL private so each developer can create a file called `private.yml` that does NOT get checked into version control and that file should contain a single line containing the URL above:



The Now that that is out of the way, we can load it into our app:



In Rails, you'd put this into one of your environment .rb files (like development.rb).

That's it. One less thing to store on your local machine and lose.

Monday, June 27, 2011

How to install mysql2 Ruby Gem without having MySQL installed

It's all about the libraries my friends. If you get something like this:

Installing mysql2 (0.2.11) with native extensions /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/installer.rb:551:in `rescue in block in build_extensions': ERROR: Failed to build gem native extension. (Gem::Installer::ExtensionBuildError)

        /usr/local/bin/ruby extconf.rb
checking for rb_thread_blocking_region()... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lmygcc... no
checking for mysql_query() in -lmysqlclient... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Then you just need to do this:


1) sudo apt-get install libmysqlclient-dev
2) sudo gem install mysql2



Monday, June 20, 2011

Converting Ruby Time to Milliseconds

Coming from Java land, I am really used to working in milliseconds and often times I want to time a block of code in milliseconds, so here's how I do it:

start_time = Time.now
# some code to time
end_time = Time.now
duration_in_ms = ((end_time.to_f - start_time.to_f) * 1000.0).to_i

Or to get current time since epoch in milliseconds:

(Time.now.to_f * 1000.0).to_i

Installing pg gem without having Postgres installed

If you get this error:


ubuntu@ip-10-204-65-124:~$ sudo gem install pg
Building native extensions.  This could take a while...
ERROR:  Error installing pg:
ERROR: Failed to build gem native extension.


        /usr/local/bin/ruby extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
 --with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***

Try:

sudo apt-get install libpq-dev 

Then try again: 

sudo gem install pg


Saturday, June 11, 2011

sed: How to replace a line containing a matching pattern

Since it's hard to pipe commands with sudo, here's a one liner that works with sudo:


    sudo sed -i '/SL_TEMPLATE_ID/ c\SL_TEMPLATE_ID=1071' myfile.txt

The -i tells it to modify the file in place. Get rid of the -i to test it first. 

Sunday, May 29, 2011

How to install Sqlite3 Ruby Gem on Ubuntu

Since I seem to run into this every time I try to get Rails running on a fresh OS install (which is all too often lately), I thought I'd just make myself a note:


sudo apt-get install libsqlite3-dev libsqlite3-ruby
sudo gem install sqlite3