<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">

  <title>Odes of the Occult</title>
  
  <link href="http://ku1ik.com/" />
  <updated>2012-01-21T18:23:56+01:00</updated>
  <id>http://ku1ik.com/</id>
  <author>
    <name>Marcin Kulik</name>
    
  </author>

  
  <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/SickillNet" /><feedburner:info uri="sickillnet" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry>
    <title>systemd socket activation and Ruby</title>
    <link href="http://feedproxy.google.com/~r/SickillNet/~3/Yk9maZ-lG4c/systemd-socket-activation-and-ruby.html" />
    <updated>2012-01-21T00:00:00+01:00</updated>
    <id>http://ku1ik.com/2012/01/21/systemd-socket-activation-and-ruby</id>
    <content type="html">&lt;p&gt;For anyone who doesn't know what systemd is:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;systemd is a system and service manager for Linux, compatible with SysV and
LSB init scripts. systemd provides &lt;strong&gt;aggressive parallelization&lt;/strong&gt;
capabilities, uses &lt;strong&gt;socket and D-Bus activation&lt;/strong&gt; for starting services,
offers &lt;strong&gt;on-demand starting of daemons&lt;/strong&gt;, keeps track of processes using
Linux cgroups, supports snapshotting and restoring of the system state,
maintains mount and automount points and implements an elaborate
transactional dependency-based service control logic.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.freedesktop.org/wiki/Software/systemd"&gt;www.freedesktop.org/wiki/Software/systemd&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;It's quite similar to Apple's launchd (used in OSX) and is fully utilizing
powerful features of the latest Linux kernel. systemd is default init system in
latest Fedora, openSUSE and Mandriva and is available for many other Linux
distros as alternative boot solution. I hope Ubuntu's upstart team will give up
soon because having systemd on Ubuntu servers would be awesome. For more info
and idea behind the project I recommend reading &lt;a href="http://0pointer.de/blog/projects/systemd.html"&gt;Lennart Poettering's
announcement&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One of the great features of this init system is &lt;a href="http://0pointer.de/blog/projects/socket-activation.html"&gt;socket
activation&lt;/a&gt; of system
services. In short, services are lazily started when they're actually needed.
Systemd listens on the sockets for them and starts the services on first
incoming connection, passing them the listening sockets. Started services just
start accepting clients on these sockets (without calling
&lt;code&gt;socket()+bind()+listen()&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;It appears that the protocol for passing sockets to service processes is very
simple. Environment variable &lt;em&gt;LISTEN_PID&lt;/em&gt; is set to the PID of the service
process and another environment variable &lt;em&gt;LISTEN_FDS&lt;/em&gt; is set to the number of
listening sockets passed. Socket descriptors start from number 3 and are
sequential. For example, &lt;em&gt;LISTEN_FDS&lt;/em&gt; with value of 2 means process should
accept connections on 2 sockets with descriptors 3 and 4.&lt;/p&gt;

&lt;p&gt;I'll show you how all this works on an example echo server written in ruby. The
server will send back what it receives. Additionally it will send information
telling if listening socket came from systemd or not to each new connected
client.&lt;/p&gt;

&lt;p&gt;But first we need to create the socket unit file that specifies where systemd
should listen on behalf of our service.
&lt;em&gt;/etc/systemd/system/echo-server.socket&lt;/em&gt; file can look as simple as this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[Socket]
ListenStream=8888
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Next, we need service unit file that specifies what binary to start when
connections start coming. &lt;em&gt;/etc/systemd/system/echo-server.service&lt;/em&gt; file may
look like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[Service]
ExecStart=/home/kill/.rvm/bin/ruby-1.9.2-p290 /home/kill/bin/echo-server.rb
User=kill
StandardOutput=syslog
StandardError=syslog
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I have ruby 1.9.2 installed via RVM so I'm running my ruby script with RVM's
wrapper specifying full paths (remember init process runs as root). I'm also
setting the user on whose behalf the process should be run and I'm asking
systemd to log process' stdout/stderr to syslog (simplifies debugging).&lt;/p&gt;

&lt;p&gt;Now, the echo server (&lt;em&gt;/home/kill/bin/echo-server.rb&lt;/em&gt;):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#!/usr/bin/env ruby

require 'socket'

SD_LISTEN_FDS_START = 3

from_systemd = false

if ENV['LISTEN_PID'].to_i == $$
  # use existing socket passed from systemd
  server_socket = Socket.for_fd(SD_LISTEN_FDS_START + 0)
  from_systemd = true
else
  # create new listening socket on port 8888
  server_socket = Socket.tcp_server_sockets(8888)
end

Socket.accept_loop(server_socket) do |client_socket, addr|
  client_socket.send("OHAI! systemd socket: #{from_systemd}\n", 0)

  while (data = client_socket.recv(1000)).size &amp;gt; 0
    client_socket.send(data.upcase, 0)
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Implementation is very simple, still I'm gonna explain it a little bit as it
illustrates the use of systemd socket activation protocol and the fallback -
normal way of creating server socket.&lt;/p&gt;

&lt;p&gt;Like I mentioned earlier, descriptors of systemd passed sockets start with 3:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;SD_LISTEN_FDS_START = 3
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We check if &lt;em&gt;LISTEN_PID&lt;/em&gt; points to our echo-server.rb process:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;if ENV['LISTEN_PID'].to_i == $$
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If so, we're creating new &lt;code&gt;Socket&lt;/code&gt; instance for existing descriptor (3). Socket
unit file tells systemd to listen on one port only (8888) so we can assume
there's only one socket descriptor passed:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  # use existing socket passed from systemd
  server_socket = Socket.for_fd(SD_LISTEN_FDS_START + 0)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If &lt;em&gt;LISTEN_PID&lt;/em&gt; doesn't match our process we just create TCP socket the usual
way:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;else
  # create new listening socket on port 8888
  server_socket = Socket.tcp_server_sockets(8888)
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Finally, in &lt;code&gt;Socket.accept_loop(server_socket) do { ... }&lt;/code&gt; we handle incoming
clients.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/SickillNet/~4/Yk9maZ-lG4c" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://ku1ik.com/2012/01/21/systemd-socket-activation-and-ruby.html</feedburner:origLink></entry>
  
  <entry>
    <title>Tunnelling VirtualBox guest's network traffic</title>
    <link href="http://feedproxy.google.com/~r/SickillNet/~3/Mm_1SsxfyUE/tunnel-vm-network-ssh.html" />
    <updated>2011-11-12T00:00:00+01:00</updated>
    <id>http://ku1ik.com/2011/11/12/tunnel-vm-network-ssh</id>
    <content type="html">&lt;p&gt;This post is quite different than my usual ones. It's not about programming
at all. Usually I'm not writing about how I solve various, non-programming
related problems but this time I want to share what I achieved as this may be
helpful to others facing similar scenario. I couldn't find similar solution
explained anywhere so I'm putting this for anyone seeking such information.&lt;/p&gt;

&lt;h2&gt;The problem&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Tunnel whole network traffic of VirtualBox guest machine through remote
server located in different country.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Basically what I needed was a virtual machine that was connecting to internet
with London's IP address. Simple port forwarding was not an option as the
machine needed to connect from many apps (not only web browser) to multiple
(unknown to me) hosts and ports.&lt;/p&gt;

&lt;h2&gt;The idea&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Setup SSH tunnel from my local machine (VirtualBox host) to my Linode server
in London and tell VirtualBox to use this tunnel as guest VM's ethernet
interface endpoint.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Seems simple. And after a few failed attempts it turned out to be really
simple. You just need to know few concepts and run few commands here and there.&lt;/p&gt;

&lt;h2&gt;The solution&lt;/h2&gt;

&lt;p&gt;Below is the complete solution that worked well for me. I use following symbols
for involved machines:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;P - remote machine with London's IP, used as a &lt;strong&gt;p&lt;/strong&gt;roxy&lt;/li&gt;
&lt;li&gt;H - local machine, VirtualBox &lt;strong&gt;h&lt;/strong&gt;ost&lt;/li&gt;
&lt;li&gt;G - virtual machine, VirtualBox &lt;strong&gt;g&lt;/strong&gt;uest, needs public IP of P&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Solution is build on OpenSSH-based VPN. From &lt;a href="http://backreference.org/2009/11/13/openssh-based-vpns/"&gt;waldner's
post&lt;/a&gt; at
&lt;a href="http://backreference.org/"&gt;backreference.org&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;This is a poorly documented yet really useful feature of Openssh. It allows you
to connect two tun/tap interfaces together, to create a layer-2 or layer-3
network between remote machines. This results in OpenVPN-like VPNs (but much
simpler and, admittedly, less scalable).&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;With presented solution virtual machine (guest) can be running any OS: Linux,
Windows, whatever.&lt;/p&gt;

&lt;h3&gt;Foreplay&lt;/h3&gt;

&lt;p&gt;You need to have root access on the machine that will work as a proxy (P). This
machine needs to have &lt;em&gt;eth0&lt;/em&gt; interface configured with public IP address
(London in my case).&lt;/p&gt;

&lt;p&gt;The sshd running on it should allow root logins (I'll explain later why) and
setting up ssh tunnels. Make sure you have following entries in
&lt;code&gt;/etc/ssh/sshd_config&lt;/code&gt; on P:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# /etc/ssh/sshd_config

PermitRootLogin yes
PermitTunnel yes
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Restart sshd if needed.&lt;/p&gt;

&lt;h3&gt;Step I&lt;/h3&gt;

&lt;p&gt;First you need layer-2 (ethernet) ssh tunnel. Run following on H as root:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ ssh -o Tunnel=ethernet -w 0:0 root@&amp;lt;P hostname&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;replacing &lt;code&gt;&amp;lt;P hostname&amp;gt;&lt;/code&gt; with real hostname of your proxy machine.&lt;/p&gt;

&lt;p&gt;This will setup L2 link and create &lt;em&gt;tap0&lt;/em&gt; network interfaces on both machines
(H and P). Because only privileged users can create network interfaces you need
to run this command as root, ssh-ing to remote host as root user as well.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;* &lt;code&gt;0:0&lt;/code&gt; in above command specifies numbers for &lt;/em&gt;tap&lt;em&gt; interfaces for both
machines. You can safely use 0 for both of them as you probably don't have any
other existing tap interfaces.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;Step II&lt;/h3&gt;

&lt;p&gt;Now you need to create
&lt;a href="http://en.wikipedia.org/wiki/Network_address_translation"&gt;NAT&lt;/a&gt; on the proxy
machine (P). This will make traffic from &lt;em&gt;tap0&lt;/em&gt; interface to be seen with P's
public IP address (going through its &lt;em&gt;eth0&lt;/em&gt; interface).&lt;/p&gt;

&lt;p&gt;Run following on P as root:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ echo 1 &amp;gt; /proc/sys/net/ipv4/ip_forward
$ /usr/sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
$ /usr/sbin/iptables -A FORWARD -i eth0 -o tap0 -m state --state RELATED,ESTABLISHED -j ACCEPT
$ /usr/sbin/iptables -A FORWARD -i tap0 -o eth0 -j ACCEPT
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Masquerade ready. Now bring P's &lt;em&gt;tap0&lt;/em&gt; interface up and configure it:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ ifconfig tap0 up
$ ifconfig tap0 192.168.0.1 netmask 255.255.255.0
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;em&gt;* You can use different network than 192.168.0.* for NAT if P is already
attached to such network.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;Step III&lt;/h3&gt;

&lt;p&gt;Next step is to bring H's &lt;em&gt;tap0&lt;/em&gt; interface up. Run following on local machine
(H) as root:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ ifconfig tap0 up
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There is no need to assign IP to this interface as it will be directly
connected to virtual machine's network interface.&lt;/p&gt;

&lt;h3&gt;Step IV&lt;/h3&gt;

&lt;p&gt;Now configure virtual machine to use the tunnel.&lt;/p&gt;

&lt;p&gt;Open VM network settings (on H), select &lt;em&gt;Bridged adapter&lt;/em&gt; and choose &lt;em&gt;tap0&lt;/em&gt; as a
bridged device.&lt;/p&gt;

&lt;h3&gt;Step V&lt;/h3&gt;

&lt;p&gt;Finally start virtual machine (G) and configure its network interface.&lt;/p&gt;

&lt;p&gt;The guest machine can be either Linux or Windows (or just anything you want).
Just setup the interface to be configured as below:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;IP: 192.168.0.2
Netmask: 255.255.255.0
Gateway: 192.168.0.1
DNS: 8.8.8.8 / 8.8.4.4 (use Google's ones for simplicity)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To confirm that VM (G) is visible to the world with public IP of P run:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ curl icanhazip.com
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;or open &lt;a href="http://icanhazip.com/"&gt;icanhazip.com&lt;/a&gt; in the browser.&lt;/p&gt;

&lt;h2&gt;Simplifying&lt;/h2&gt;

&lt;p&gt;You can put commands from steps I, II and III in shell scripts to simplify the
task in case you want to run it frequently. I have made 2 bash scripts.&lt;/p&gt;

&lt;p&gt;First on on my local machine (H):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# vm-tunnel.sh

$ ssh -o Tunnel=ethernet -w 0:0 root@&amp;lt;P hostname&amp;gt; "~/taptap.sh"
$ ifconfig tap0 up
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Second one on proxy machine (P) in root's home:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# /root/taptap.sh

$ echo 1 &amp;gt; /proc/sys/net/ipv4/ip_forward
$ /usr/sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
$ /usr/sbin/iptables -A FORWARD -i eth0 -o tap0 -m state --state RELATED,ESTABLISHED -j ACCEPT
$ /usr/sbin/iptables -A FORWARD -i tap0 -o eth0 -j ACCEPT

$ ifconfig tap0 up
$ ifconfig tap0 192.168.0.1 netmask 255.255.255.0
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Thanks to these 2 scripts I can summon my VPN with one command:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ sudo vm-tunnel.sh
&lt;/code&gt;&lt;/pre&gt;
&lt;img src="http://feeds.feedburner.com/~r/SickillNet/~4/Mm_1SsxfyUE" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://ku1ik.com/2011/11/12/tunnel-vm-network-ssh.html</feedburner:origLink></entry>
  
  <entry>
    <title>My EuRuKo 2010 talk</title>
    <link href="http://feedproxy.google.com/~r/SickillNet/~3/se3wKEoCGyU/euruko-2010.html" />
    <updated>2011-10-23T00:00:00+02:00</updated>
    <id>http://ku1ik.com/2011/10/23/euruko-2010</id>
    <content type="html">&lt;p&gt;I was speaking at &lt;a href="http://euruko2010.org/"&gt;EuRuKo conference&lt;/a&gt; last year (2010)
and I'm just putting this here for archiving purposes.&lt;/p&gt;

&lt;p&gt;Here's a video of the talk:&lt;/p&gt;

&lt;iframe src="http://player.vimeo.com/video/12665769?title=0&amp;amp;byline=0&amp;amp;portrait=0&amp;amp;color=f0004c" width="555" height="312" frameborder="0" webkitAllowFullScreen allowFullScreen&gt;&lt;/iframe&gt;


&lt;p&gt;And here are the (slightly updated) slides:&lt;/p&gt;

&lt;script src="http://speakerdeck.com/embed/4ea40c5caaf429005100c2c0.js"&gt;&lt;/script&gt;

&lt;img src="http://feeds.feedburner.com/~r/SickillNet/~4/se3wKEoCGyU" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://ku1ik.com/2011/10/23/euruko-2010.html</feedburner:origLink></entry>
  
  <entry>
    <title>Coloration + terminal Vim + 256 colors</title>
    <link href="http://feedproxy.google.com/~r/SickillNet/~3/hEU1np6End8/coloration-vim-256-colors.html" />
    <updated>2011-10-22T00:00:00+02:00</updated>
    <id>http://ku1ik.com/2011/10/22/coloration-vim-256-colors</id>
    <content type="html">&lt;p&gt;Year has passed since last &lt;a href="http://coloration.sickill.net/"&gt;Coloration&lt;/a&gt;
update. Time has come for new improvements. Brand new Coloration v0.3.1
focuses on Vim theme writer and includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Line numbering (&lt;code&gt;LineNr&lt;/code&gt;) background matching cursor line highlight style
(&lt;code&gt;CursorLine&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;New style for &lt;code&gt;ColorColumn&lt;/code&gt; also matching cursor line highlight&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Highlighting for terminal Vim running in 256 colors capable terminal&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Let's focus on the last one.&lt;/p&gt;

&lt;p&gt;Nowadays most terminal emulators support 256 colors. Additionally to 16 base
colors everyone knows about they also support 216 colors from 6x6x6 color cube
and 24 shades of grey. Look
&lt;a href="http://www.mudpedia.org/wiki/Xterm_256_colors"&gt;here&lt;/a&gt; for details. If you don't
use 256 colors capable terminal then start using it now. &lt;code&gt;xterm&lt;/code&gt; had it in
1999. &lt;code&gt;gnome-terminal&lt;/code&gt;, &lt;code&gt;konsole&lt;/code&gt;, &lt;code&gt;iTerm2&lt;/code&gt; and many more have it.&lt;/p&gt;

&lt;p&gt;Vim colorschemes allows you to specify &lt;code&gt;ctermfb&lt;/code&gt; and &lt;code&gt;ctermbg&lt;/code&gt; from the range
0-255. People usually only use 0-15 in their themes in order to be compatible
with &lt;em&gt;16-color-my-grandpa-uses&lt;/em&gt; terminals. That's so wrong! Coloration is
fast-forwarding us to the future with it's updated Vim theme writer. It
converts the colors used by the gui Vim version (GVim/MacVim) to their xterm256
nearest equivalents with simple approximation.&lt;/p&gt;

&lt;p&gt;Sunburst theme converted from Textmate theme:&lt;/p&gt;

&lt;p&gt;&lt;img src="https://github.com/downloads/sickill/coloration/sunburst-comparison.png" alt="Sunburst theme in Vim" /&gt;&lt;/p&gt;

&lt;p&gt;Twilight theme converted from Textmate theme:&lt;/p&gt;

&lt;p&gt;&lt;img src="https://github.com/downloads/sickill/coloration/twilight-comparison.png" alt="Twilight theme in Vim" /&gt;&lt;/p&gt;

&lt;p&gt;Now &lt;a href="http://coloration.sickill.net/"&gt;go convert&lt;/a&gt; your old, dusty Textmate
themes. Oh, and I've put Sunburst and Monokai themes on github
(&lt;a href="https://github.com/sickill/vim-sunburst"&gt;vim-sunburst&lt;/a&gt;,
&lt;a href="https://github.com/sickill/vim-monokai"&gt;vim-monokai&lt;/a&gt;) for all you lazy
guys.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/SickillNet/~4/hEU1np6End8" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://ku1ik.com/2011/10/22/coloration-vim-256-colors.html</feedburner:origLink></entry>
  
  <entry>
    <title>Better Rails production box</title>
    <link href="http://feedproxy.google.com/~r/SickillNet/~3/jcDgPvjZkjw/better-rails-production-box.html" />
    <updated>2011-09-26T00:00:00+02:00</updated>
    <id>http://ku1ik.com/2011/09/26/better-rails-production-box</id>
    <content type="html">&lt;p&gt;Here are 3 simple things to do in order to improve deployment process of your
Rails application and friendliness of your production server. Nothing new here
but many good practices are often overlooked even by experienced developers.&lt;/p&gt;

&lt;h2&gt;Set RAILS_ENV in .bash_profile&lt;/h2&gt;

&lt;p&gt;I see my fellow devs typing this on production server all the time:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;script/rails console production
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Or this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;RAILS_ENV=production rake some_task
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It's obvious that on production box you want it to run in production
environment, right? Set &lt;em&gt;RAILS_ENV&lt;/em&gt; in &lt;em&gt;.bash_profile&lt;/em&gt; and save your fingers:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# ~/.bash_profile
export RAILS_ENV="production"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This might not be the case if your hosting service is like
&lt;a href="http://heroku.com"&gt;Heroku&lt;/a&gt; or similar as they export &lt;em&gt;RAILS_ENV&lt;/em&gt; for you
automatically. But if you're on self-managed VPS or
&lt;a href="http://aws.amazon.com/ec2/"&gt;EC2&lt;/a&gt; instance you can ease your work with this
trivial setup.&lt;/p&gt;

&lt;h2&gt;Use logrotate&lt;/h2&gt;

&lt;p&gt;Create &lt;em&gt;/etc/logrotate.d/rails_logs&lt;/em&gt; file with following content:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;/var/www/*/shared/log/*.log {
  daily
  missingok
  rotate 30
  compress
  delaycompress
  copytruncate
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That will tell &lt;a href="http://linuxcommand.org/man_pages/logrotate8.html"&gt;logrotate&lt;/a&gt;
to rotate log files daily, compress them, keep last 30 days and don't choke
when file is missing. &lt;em&gt;copytruncate&lt;/em&gt; is important here as it will make sure log
file currently used by Rails app is not moved but truncated. That way the app
can just keep on logging without reopening log file.&lt;/p&gt;

&lt;p&gt;Don't forget about this one if you manage production box yourself. And do it
when you initially setup the box, not "later". "Later" often means "when app is
down due to not enough disk space". Srsly.&lt;/p&gt;

&lt;h2&gt;Use maintenance page&lt;/h2&gt;

&lt;p&gt;When deploying with long running or non-trivial (more than add column)
migrations you should use maintenance page of some sort. With capistrano you
can just use (before running migrations):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cap production deploy:web:disable
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and (after they finish):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cap production deploy:web:enable
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Default maintenance page put by capistrano is kind of ugly
so you should make your own matching your site design. In order to do this
prepare &lt;em&gt;app/views/layouts/maintenance.html.erb&lt;/em&gt; and override
&lt;em&gt;deploy:web:disable&lt;/em&gt; task in &lt;em&gt;config/deploy.rb&lt;/em&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;namespace :deploy do
  namespace :web do
    task :disable, :roles =&amp;gt; :web do
      require 'erb'
      on_rollback { run "rm #{shared_path}/system/maintenance.html" }

      reason = ENV['REASON']
      deadline = ENV['UNTIL']
      template = File.read('app/views/layouts/maintenance.html.erb')
      page = ERB.new(template).result(binding)

      put page, "#{shared_path}/system/maintenance.html", :mode =&amp;gt; 0644
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That will put your custom page in &lt;em&gt;#{shared_path}/system/maintenance.html&lt;/em&gt;
(also accessible via &lt;em&gt;public/system/maintenance.html&lt;/em&gt; by webserver).&lt;/p&gt;

&lt;p&gt;On the other end, you should configure webserver to respect presence of this
file. Here is config snippet for &lt;a href="http://wiki.nginx.org/"&gt;Nginx&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;server {
  listen 80;
  server_name example.org;

  ...

  # Maintenance page support

  set $maintenance 0;

  if (-f $document_root/system/maintenance.html) {
    set $maintenance 1;
  }

  if ($request_uri ~* (jpg|jpeg|gif|png|js|css)$) {
    set $maintenance 0;
  }

  if ($maintenance) {
    return 503;
  }

  error_page 503 @maintenance;
  location @maintenance {
    rewrite ^(.*)$ /system/maintenance.html break;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It will make sure that when &lt;em&gt;maintenance.html&lt;/em&gt; file is there Nginx will serve
it and return 503 (Service unavailable) HTTP status. Proper response status is
important here as it gives search engines the message: don't index me now
please, come back later. You don't want your maintenance page get indexed,
do you?&lt;/p&gt;

&lt;p&gt;Above Nginx config also allows assets to be served as usual when maintenance
mode is on - especially useful if you don't host them on separate host/subdomain
and you want the page to look nice and match presence of your full site.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/SickillNet/~4/jcDgPvjZkjw" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://ku1ik.com/2011/09/26/better-rails-production-box.html</feedburner:origLink></entry>
  
  <entry>
    <title>Formatting XML in Vim with indent command</title>
    <link href="http://feedproxy.google.com/~r/SickillNet/~3/5vZt6eaD2V0/formatting-xml-in-vim-with-indent-command.html" />
    <updated>2011-09-08T00:00:00+02:00</updated>
    <id>http://ku1ik.com/2011/09/08/formatting-xml-in-vim-with-indent-command</id>
    <content type="html">&lt;p&gt;Today I had a need to look at XML doc fetched from &lt;a href="http://code.google.com/apis/calendar/data/2.0/developers_guide.html"&gt;Google Calendar API&lt;/a&gt;. I saved it to a file and opened in Vim. Unfortunately API output was generated to be consumed by machines rather than humans.&lt;/p&gt;

&lt;p&gt;First I tried &lt;code&gt;gg=G&lt;/code&gt; command. &lt;strong&gt;=&lt;/strong&gt; is used to auto-indent selected line(s) and &lt;code&gt;gg=G&lt;/code&gt; re-indents whole file. Usually it works great, especially for source code files. It does not reformat code, it only changes indentation. And that's good, &lt;strong&gt;I&lt;/strong&gt; should be the one to control look of my code. But for XML I want it to do full reformatting. I'm not writing XML and in all of the cases when I open such docs in my editor I only want to look at well-formatted, human-readable XML.&lt;/p&gt;

&lt;p&gt;From Vim help on &lt;em&gt;equalprg&lt;/em&gt; setting:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;External program to use for "=" command.  When this option is empty
the internal formatting functions are used; either 'lisp', 'cindent'
or 'indentexpr'.  When Vim was compiled without internal formatting,
the "indent" program is used.
...&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;"Bingo! Let's use &lt;em&gt;xmllint&lt;/em&gt; for that!" - I thought immediately. &lt;em&gt;xmllint&lt;/em&gt; command comes bundled with &lt;em&gt;libxml&lt;/em&gt; package on Unix-like systems and does really good job at producing pretty output. You can use it like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# shell
xmllint --format --recover foo.xml
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Okee, so my first approach to reformatting XML in Vim was:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:1,$!xmllint --format --recover - 2&amp;gt;/dev/null
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Not bad. But writing it every time (or remembering) would be painful. Let's use mentioned &lt;em&gt;equalprg&lt;/em&gt; option:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# .vimrc
set equalprg=xmllint\ --format\ --recover\ -\ 2&amp;gt;/dev/null
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ok... but Y U USE XMLLINT WHEN I'M INDENTIN' MY RUBY CODE?? Ahaa! &lt;em&gt;equalprg&lt;/em&gt; need to be set locally only for XML-type buffers. Autocommand did the trick:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# .vimrc
au FileType xml setlocal equalprg=xmllint\ --format\ --recover\ -\ 2&amp;gt;/dev/null
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Restarted Vim, typed &lt;code&gt;gg=G&lt;/code&gt; and said "Hell yeah!".&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/SickillNet/~4/5vZt6eaD2V0" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://ku1ik.com/2011/09/08/formatting-xml-in-vim-with-indent-command.html</feedburner:origLink></entry>
  
  <entry>
    <title>Bitpocket as a Dropbox alternative</title>
    <link href="http://feedproxy.google.com/~r/SickillNet/~3/MTuFbZsygoE/bitpocket-as-a-dropbox-alternative.html" />
    <updated>2011-07-18T00:00:00+02:00</updated>
    <id>http://ku1ik.com/2011/07/18/bitpocket-as-a-dropbox-alternative</id>
    <content type="html">&lt;p&gt;As an excuse for trying out &lt;a href="http://posterous.com"&gt;Posterous&lt;/a&gt; email
posting and its Autopost feature (in this case to Twitter and
Identi.ca) I'll present you Bitpocket.&lt;/p&gt;

&lt;p&gt;In short, Bitpocket is a small but smart bash script that does two-way
directory synchronization resembling Dropbox sync. Simply it just uses
&lt;em&gt;rsync&lt;/em&gt; to make the actual sync. It runs rsync twice: first syncing
from remote to local machine, then from local to remote machine. This
way all new files that appeared on remote are fetched to local machine
and all new locally created files are replicated on remote machine.&lt;/p&gt;

&lt;p&gt;But additionally it makes sure that file deletion is properly
propagated in this 2-way synchronization, which isn't possible by
&lt;em&gt;just&lt;/em&gt; running rsync twice. The problem is rsync deletes all new
created files or brings back the files you deleted
depending on which direction we sync in first (and when using &lt;em&gt;--delete&lt;/em&gt; option). Bitpocket solves that
by tracking names of created and deleted files between its invocations
and using these lists as a source for &lt;em&gt;--exclude-from&lt;/em&gt; rsync option
when doing first, remote -&amp;gt; local sync.&lt;/p&gt;

&lt;p&gt;To make sure Bitpocket behaves like I wanted it to behave I've
&lt;a href="https://github.com/sickill/bitpocket/blob/master/spec/bitpocket_spec.rb"&gt;spec'ed it&lt;/a&gt;.
Yes, it's a bash script, not a ruby code, but who said I can't use
rspec to test a bash script?&lt;/p&gt;

&lt;p&gt;For details about usage see &lt;a href="https://github.com/sickill/bitpocket"&gt;Bitpocket's
README&lt;/a&gt;. I'm using it instead of
Dropbox for few weeks now and it does quite good job. Give it a try.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/SickillNet/~4/MTuFbZsygoE" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://ku1ik.com/2011/07/18/bitpocket-as-a-dropbox-alternative.html</feedburner:origLink></entry>
  
  <entry>
    <title>Resque Mailer says: Put your emails to background</title>
    <link href="http://feedproxy.google.com/~r/SickillNet/~3/fa2weYOGzxU/resque-mailer-says-put-your-emails-to-background.html" />
    <updated>2011-04-05T00:00:00+02:00</updated>
    <id>http://ku1ik.com/2011/04/05/resque-mailer-says-put-your-emails-to-background</id>
    <content type="html">&lt;p&gt;It's no secret that handling HTTP requests should be quick. Very quick. Your
business' "to be or not to be" may depend on how "fast" is you site. "Fast"
site for end-users is when they're clicking and they don't need to switch to
other tabs to kill time while waiting for page to open.&lt;/p&gt;

&lt;p&gt;How to make your site fast?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;buy more RAM (and install Oracle) - it costs $$$&lt;/li&gt;
&lt;li&gt;lazy load what you can after initial page - complicates the code&lt;/li&gt;
&lt;li&gt;do any processing in background - that one is quick win!&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Depending on the application there will always be need for some combination of
above. Best practice however, is: &lt;strong&gt;Webserver should not do any heavy data
processing when handling request&lt;/strong&gt;. It should just process the request and
return response as soon as possible.&lt;/p&gt;

&lt;p&gt;It means that all you can do is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;parsing params&lt;/li&gt;
&lt;li&gt;loading resource(s)&lt;/li&gt;
&lt;li&gt;changing state of resource(s)&lt;/li&gt;
&lt;li&gt;&lt;em&gt;utilizing background processing workers for data grinding&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;rendering response body from template&lt;/li&gt;
&lt;li&gt;returning response to client&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;All of the above steps except last one are optional but they're all usually
required in most apps.  Point 4 is the important here. Developers often forget
about it or are just too lazy to implement it.  But it makes huge difference!
Waiting 10 seconds for page load because the controller needs to finish
something... it's unacceptable.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/defunkt/resque"&gt;Resque&lt;/a&gt; to the rescue! Very reliable,
framework independent, fast "Redis-backed library for creating background jobs"
by Github's &lt;a href="https://github.com/defunkt"&gt;defunkt&lt;/a&gt;. I use various Ruby
web-frameworks to build my webapps, the choice is not always Rails but Resque
can be used with anything.  Good stuff.&lt;/p&gt;

&lt;p&gt;Anyway, what is not that common is sending e-mails asynchronously. Yeah, some
people never thought about it.  But sending them right from controller costs.
Building mail message and sending it involves creating lots of ruby objects,
rendering templates, preparing mail headers and eventually passing it to local
MTA or sometimes to remote SMTP server. Sounds like a lot of time, especially
when you use remote SMTP server.&lt;/p&gt;

&lt;p&gt;But who would want to create dedicated background worker for this task? Who
would want to enqueue such a job instead of doing simple
&lt;code&gt;Notifications.signup(@user).deliver&lt;/code&gt; in the controller? Not me.&lt;/p&gt;

&lt;p&gt;Enter &lt;a href="https://github.com/zapnap/resque_mailer"&gt;ResqueMailer&lt;/a&gt;. Originally
created for Rails 2 by &lt;a href="http://blog.zerosum.org/"&gt;Nick Plante&lt;/a&gt;, updated by me
to work with both Rails 2.x and 3.x. ResqueMailer allows you to move processing
of Rails mailers out of controller to an async Resque worker with minimal fuss.&lt;/p&gt;

&lt;p&gt;Just put the gem into your Rails project Gemfile:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;gem 'resque_mailer'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and then include the &lt;code&gt;Resque::Mailer&lt;/code&gt; module into your Mailer class:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Notifications &amp;lt; ActionMailer::Base
  include Resque::Mailer

  def signup(...)
    ...
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Instaaant gratification!&lt;/p&gt;

&lt;p&gt;This is it. Now start your worker and enjoy faster responses. For information
on ResqueMailer's optional settings see project's
&lt;a href="https://github.com/zapnap/resque_mailer"&gt;github repository&lt;/a&gt;. For details about setting up
Resque and running workers go to
&lt;a href="https://github.com/defunkt/resque/blob/master/README.markdown"&gt;README&lt;/a&gt; of
Resque project.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/SickillNet/~4/fa2weYOGzxU" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://ku1ik.com/2011/04/05/resque-mailer-says-put-your-emails-to-background.html</feedburner:origLink></entry>
  
  <entry>
    <title>Github's Gist and Gnome Keyring</title>
    <link href="http://feedproxy.google.com/~r/SickillNet/~3/zBkbfA2E9-w/gist-and-gnome-keyring.html" />
    <updated>2011-04-03T00:00:00+02:00</updated>
    <id>http://ku1ik.com/2011/04/03/gist-and-gnome-keyring</id>
    <content type="html">&lt;p&gt;I like to keep my &lt;a href="https://github.com/sickill/dotfiles"&gt;dotfiles&lt;/a&gt; in git repository but I never put my &lt;code&gt;.gitconfig&lt;/code&gt;
there because it included my GitHub API token, that is used for example by &lt;em&gt;&lt;a href="https://github.com/defunkt/gist"&gt;gist&lt;/a&gt;&lt;/em&gt;.
Git allows you to get output of a system commands as values (by prepending value with exclamation mark) for settings
in &lt;em&gt;[alias]&lt;/em&gt; section of &lt;code&gt;.gitconfig&lt;/code&gt; only but I just found out that &lt;em&gt;gist&lt;/em&gt; script can also do this trick for
&lt;em&gt;github.token&lt;/em&gt; setting. Thanks to that we can prepare some script that gets token from different place, use it
in &lt;code&gt;.gitconfig&lt;/code&gt;, put config into repository and worry no more about publishing sensitive data.&lt;/p&gt;

&lt;p&gt;I put the token in Gnome Keyring as it feels pretty secure. Just go to
&lt;em&gt;System -&gt; Preferences -&gt; Passwords and Encryption Keys&lt;/em&gt;, press &lt;em&gt;Ctrl+N&lt;/em&gt;, choose &lt;em&gt;Stored password&lt;/em&gt;.
Select &lt;em&gt;login&lt;/em&gt; keyring, enter "GitHub API Token" as description and your token as password. Now we need
a way to get this token out of keyring in shell script. Fortunately there are python language bindings
for Gnome Keyring.&lt;/p&gt;

&lt;p&gt;Python script for retrieving passwords from keyring can look like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#!/usr/bin/env python

import sys
import gnomekeyring as gk

if len(sys.argv) &amp;gt; 2:
    ring_name = sys.argv[2]
else:
    ring_name = 'login'

for key in gk.list_item_ids_sync(ring_name):
    item = gk.item_get_info_sync(ring_name, key)
    if item.get_display_name() == sys.argv[1]:
        sys.stdout.write(item.get_secret())
        break
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Simple. It prints out password with given name to standard output, without newline character (so it's easier
for other scripts to use it). Let's save it to &lt;code&gt;~/bin/keyring-get-pass.py&lt;/code&gt; and try it:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ python ~/bin/keyring-get-pass.py 'GitHub API Token'
my-secret-token-i-wont-show-you$
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Cool. By default we get secrets from &lt;em&gt;login&lt;/em&gt; keyring. This is for convenience as &lt;em&gt;login&lt;/em&gt; keyring is being
unlocked at system login time on Gnome (at least on Ubuntu) and it won't ask us to unlock it when running this script.
If we need to get password from different keyring then its name can be passed as the second argument to the script.&lt;/p&gt;

&lt;p&gt;Now, let's use the script in &lt;code&gt;.gitconfig&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[github]
  user = sickill
  token = !python ~/bin/keyring-get-pass.py 'GitHub API Token'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you have other solutions for avoiding publishing passwords and tokens in your dotfiles (like config templates etc)
tell me, I'm eager to hear!&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/SickillNet/~4/zBkbfA2E9-w" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://ku1ik.com/2011/04/03/gist-and-gnome-keyring.html</feedburner:origLink></entry>
  
  <entry>
    <title>Open3 And The PID Of The Spawn</title>
    <link href="http://feedproxy.google.com/~r/SickillNet/~3/2MOqGL68MwQ/open3-and-the-pid-of-the-spawn.html" />
    <updated>2010-09-18T00:00:00+02:00</updated>
    <id>http://ku1ik.com/2010/09/18/open3-and-the-pid-of-the-spawn</id>
    <content type="html">&lt;p&gt;Open3 is a standard way of starting subprocess from ruby if you need IO objects for stdin, stdout and/or sterr.
It goes like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;require "open3"

stdin, stdout, stderr = Open3.popen3('sort')
stdin.puts "oso de peluche"
stdin.puts "del ratón"
stdin.close
while !stdout.eof?
  puts stdout.readline
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Simple. I needed one more thing: PID of the spawned process to be able to send it some SIGNALs.
After looking at &lt;a href="http://ruby-doc.org/core/classes/Open3.html"&gt;open3 docs&lt;/a&gt; I realized there is no way to get the PID.
Several posts like &lt;a href="http://blog.tewk.com/?p=74"&gt;this one&lt;/a&gt; made me believe that &lt;a href="http://github.com/ahoward/open4"&gt;open4&lt;/a&gt;
is the only way to go.&lt;/p&gt;

&lt;p&gt;But I didn't gave up. I didn't want to add open4 as a dependency of my code only because of this small shortcoming.
After a while of googling I found &lt;a href="http://ruby-doc.org/ruby-1.9/classes/Open3.html"&gt;open3 documentation for ruby 1.9&lt;/a&gt;.
How it's different?&lt;/p&gt;

&lt;p&gt;"Open stdin, stdout, and stderr streams and start external executable. In addition, a thread for waiting the started
process is noticed. The thread has a thread variable :pid which is the pid of the started process."&lt;/p&gt;

&lt;p&gt;Hurray!&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;stdin, stdout, stderr, wait_thr = Open3.popen3(cmd)
pid = wait_thr[:pid]  # pid of the started process.
Process.kill("USR1", pid)  # send it USR1 signal
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So MRI 1.9 solves the problem by returning additional element with reference to waiting thread with :pid variable set on it.
How about other ruby implementations?&lt;/p&gt;

&lt;p&gt;JRuby doesn't return waiting thread reference from open3() call but it has open4 incorporated into its standard library in
the shape of IO.popen4 method:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;pid, stdin, stdout, stderr = IO.popen4(cmd)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Unfortunately, both MRI 1.8.x and Rubinius don't include this functionality in their standard lib.&lt;/p&gt;

&lt;p&gt;So, is open4 the only reliable way? Let's see:&lt;/p&gt;

&lt;p&gt;MRI 1.8.7:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;ruby-1.8.7-p249 &amp;gt; open4 "cat"
 =&amp;gt; [31373, #&amp;lt;IO:0xb741c8ac&amp;gt;, #&amp;lt;IO:0xb741c870&amp;gt;, #&amp;lt;IO:0xb741c7f8&amp;gt;]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;MRI 1.9.2:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;ruby-1.9.2-p0 &amp;gt; open4 "cat"
 =&amp;gt; [31498, #&amp;lt;IO:fd 4&amp;gt;, #&amp;lt;IO:fd 5&amp;gt;, #&amp;lt;IO:fd 7&amp;gt;]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;JRuby:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;jruby-1.5.2 &amp;gt; open4 "cat"
NotImplementedError: fork is unsafe and disabled by default on JRuby
        from /home/kill/.rvm/gems/jruby-1.5.2/gems/open4-1.0.1/lib/open4.rb:23:in `popen4'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Rubinius:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;rbx-1.0.1-20100603 &amp;gt; open4 "cat"
 =&amp;gt; [31635, #&amp;lt;IO:0x212&amp;gt;, #&amp;lt;IO:0x214&amp;gt;, #&amp;lt;IO:0x216&amp;gt;]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Damn, looks like we cannot use open4 under JRuby. Fortunately, there is IO.popen4 which should be used instead.
Solution that will work in all ruby implementations might look like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;if IO.respond_to?(:popen4)
  def open4(*args)
    IO.popen4(*args)
  end
else
  require 'open4'
end

open4("cat")
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Summarizing, go with open4/IO.popen4 if you want to be sure your code works on all ruby implementations.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/SickillNet/~4/2MOqGL68MwQ" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://ku1ik.com/2010/09/18/open3-and-the-pid-of-the-spawn.html</feedburner:origLink></entry>
  
  <entry>
    <title>Announcing Coloration, editor color scheme converter</title>
    <link href="http://feedproxy.google.com/~r/SickillNet/~3/JuA5nd5Wp5Y/announcing-coloration-editor-color-scheme-converter.html" />
    <updated>2010-07-24T00:00:00+02:00</updated>
    <id>http://ku1ik.com/2010/07/24/announcing-coloration-editor-color-scheme-converter</id>
    <content type="html">&lt;p&gt;Without further ado I'm introducing Coloration - editor/IDE color scheme converter. It is an evolution of &lt;a href="/blog/tag/tm2jed"&gt;tm2jed&lt;/a&gt; tool and
at the moment it can convert Textmate color themes (in XML plist format) to Vim, JEdit and Kate/KWrite/KDevelop color schemes. So if you are Textmate-&gt;Vim convert
or you just envy Textmate users for their good looking, dark themes now you have no excuse to not try out Coloration.&lt;/p&gt;

&lt;p&gt;Here's how Vim with Sunburst theme looks like:&lt;/p&gt;

&lt;p&gt;&lt;a href="/images/posts/gvim-sunburst.png" rel="lightbox"&gt;&lt;img src="/images/posts/gvim-sunburst-small.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want to give Coloration a try you have two options. Either you can use online version at &lt;a href="http://coloration.sickill.net/"&gt;coloration.sickill.net&lt;/a&gt;
or you can install ruby gem &lt;em&gt;coloration&lt;/em&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;gem install coloration
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It will give you &lt;em&gt;tm2vim&lt;/em&gt;, &lt;em&gt;tm2jedit&lt;/em&gt; and &lt;em&gt;tm2katepart&lt;/em&gt; commands. Note it requires ruby 1.9.&lt;/p&gt;

&lt;p&gt;Let me know if you find it useful. Source code is available at &lt;a href="http://github.com/sickill/coloration"&gt;github.com/sickill/coloration&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Enjoy!&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/SickillNet/~4/JuA5nd5Wp5Y" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://ku1ik.com/2010/07/24/announcing-coloration-editor-color-scheme-converter.html</feedburner:origLink></entry>
  
  <entry>
    <title>Thoughts on Ruby gem dependencies</title>
    <link href="http://feedproxy.google.com/~r/SickillNet/~3/vZJ8aSGSDHY/thoughts-on-ruby-gem-dependencies.html" />
    <updated>2010-07-03T00:00:00+02:00</updated>
    <id>http://ku1ik.com/2010/07/03/thoughts-on-ruby-gem-dependencies</id>
    <content type="html">&lt;h2&gt;The problem&lt;/h2&gt;

&lt;p&gt;Having &lt;a href="http://gembundler.com"&gt;gem bundler&lt;/a&gt; is great. First, you can forget about gem version collisions (damn you activation errors!). Second, you can forget about manually installing gems on all your machines. Third, you can use git repositories and local directories as gem sources which is neat and is invaluable when working on your own gems. But bundler is a workaround. Yes, it's a workaround for poor rubygems design. Let's look at few examples of the problems I've noticed.&lt;/p&gt;

&lt;p&gt;I'll start with &lt;a href="http://github.com/krobertson/dm-paperclip"&gt;dm-paperclip gem&lt;/a&gt;. Recently I was porting my Merb app to Rails 3 and I've encountered few problems with this little sucker. The biggest headache I had with validations. Note it was fixed recently in dm-paperclip, but makes good example of problem that still exists in relation to other gems. dm-paperclip was doing this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;...
unless defined?(DataMapper::Validate).nil?
...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;What's the problem here? Let's say you have following lines in your Gemfile:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;gem "dm-validations"
gem "dm-paperclip"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Looks like everything should work. Wrong! Rails 3 requires all the gems from your Gemfile by calling &lt;code&gt;Bundler.require Rails.env&lt;/code&gt; but &lt;code&gt;Bundler.require&lt;/code&gt; doesn't use order of gems specified in Gemfile. For me it first required dm-paperclip and then dm-validations, so when dm-paperclip was being required &lt;code&gt;DataMapper::Validate&lt;/code&gt; wasn't defined yet. I heard that it may change in future and Bundler will respect the order, to some degree of course. Does this problem lay in bundler or in dm-paperclip? In none of them. Bundler doesn't know about "optional" dependencies of dm-paperclip and dm-paperclip has no way to tell us/bundler it can benefit from some optional lib.&lt;/p&gt;

&lt;p&gt;Anyway, this is good example why explicit requiring of gems seems better in this scenario. By adding &lt;code&gt;require "this"&lt;/code&gt; and &lt;code&gt;require "that"&lt;/code&gt; in places where you actually need it you're breaking DRY principle (you already have it in Gemfile) but you can have perfect control of the order. This way you can solve above dm-paperclip problem. And by using explicit requires you can move your app away from bundler (to i.e. &lt;a href="http://github.com/defunkt/rip"&gt;rip&lt;/a&gt; or &lt;a href="http://github.com/jbarnette/isolate"&gt;Isolate&lt;/a&gt;) if you want to and you don't need to add these requires because they're already in the app. But why would you need to think about gem require order in your Rails app? You shouldn't think about it. And you don't always know what are optional libraries for the gems you're using.&lt;/p&gt;

&lt;p&gt;Let's look at second issue. Multiple markdown processing libraries. There's BlueCloth, Maruku, Kramdown, RDiscount, rpeg-markdown and some more. All of them are doing the same thing but the difference is mainly in performance. BlueCloth and Maruku are pure ruby libraries, RDiscount and rpeg-markdown are bindings to fast C libraries. But they all have the same interface:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;BlueCloth.new(markdown_string).to_html          # bluecloth
Maruku.new(markdown_string).to_html             # maruku
Kramdown::Document.new(markdown_string).to_html # kramdown
RDiscount.new(markdown_string).to_html          # rdiscount
Markdown.new(markdown_string).to_html           # rpeg-markdown
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now, if gem A depends on Maruku and gem B depends on RDiscount I'll have two markdown libraries with identical interface required in my app. I know that RDiscount is faster because it's C and I'd like all of my app's functionalities to use it but gem A will use slower Maruku. Sad but true.&lt;/p&gt;

&lt;p&gt;Third rubygems issue is something in the middle between dm-paperclip+bundler problem and markdown problem. &lt;a href="http://github.com/plataformatec/devise"&gt;Devise&lt;/a&gt;, "Flexible authentication solution for Rails with Warden" supports many ORMs (ActiveRecord, DataMapper, Mongoid). Now, all code related to integration with mentioned ORMs is &lt;a href="http://github.com/plataformatec/devise/tree/master/lib/devise/orm/"&gt;included in devise itself&lt;/a&gt;. Devise checks which ORM is available at runtime (like in dm-paperclip example) and requires appropriate file choosing from many available alternatives (like in markdown example). It's far from being perfect. How this could be improved?&lt;/p&gt;

&lt;p&gt;It'd be probably better to put integration code in separate gems. This way people involved in development of ORMs could work on integration without access to Devise repository. Yeah, I know, it's not a big deal on github, just fork and send pull request. But it worked really well for dm-rails. DataMapper guys were more interested in getting DM work under Rails 3 than Rails developers and they definitely knew more about DM specifics. They provide the gem and everyone is happy. But if there were many gems providing this functionality for Devise then user would be responsible for installing it. There's better solution on my mind though, read on.&lt;/p&gt;

&lt;h2&gt;Possible solutions&lt;/h2&gt;

&lt;p&gt;To solve first mentioned issue let's just add optional dependencies to rubygems. It could work in following way.&lt;/p&gt;

&lt;p&gt;Gem author specifies list of optional dependencies:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Gem::Specification.new do |s|
  ...
  s.optional_dependency "nokogiri", "for tidying output"
  s.optional_dependency "bar", "for bar support"
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You are installing the gem:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ gem install foo
Installed foo.
Optional dependencies for foo:
- nokogiri (for tidying output)
- bar (for bar support)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Or you could install it with optional deps:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ gem install foo --with-optional-deps
Installed nokogiri.
Installed bar.
Installed foo.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Benefits of having explicit optional deps would be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;user could see what are the optionals and decide if he wants them&lt;/li&gt;
&lt;li&gt;automatic tools like bundler could be configured to install optional deps, either for all gems or for specific ones&lt;/li&gt;
&lt;li&gt;bundler could require optional gems before the one which "optionally" depends on them, solving require order issues&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;To solve second and third issue we can add "provider gems". What's that? Decent Linux package managers like &lt;a href="http://archlinux.org"&gt;Archlinux&lt;/a&gt;'s &lt;a href="http://www.archlinux.org/pacman/"&gt;pacman&lt;/a&gt; allows you to specify that the package provides the same functionality and the same interface as the other one. In rubygems it could look like this:&lt;/p&gt;

&lt;p&gt;Gem authors of all mentioned markdown processors could specify that their gem provides "markdown":&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Gem::Specification.new do |s|
  ...
  s.provides "markdown"
  ...
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Additionally they would need to provide unified interface, in this case simple &lt;code&gt;Markdown = RDiscount&lt;/code&gt; should do the trick.&lt;/p&gt;

&lt;p&gt;Now, you as gem user could use it like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;require "markdown"
Markdown.new(markdown_string).to_html
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This way gem A and B can depend on "markdown", and you decide which one you want to install. &lt;code&gt;gem install A&lt;/code&gt; or &lt;code&gt;bundle install&lt;/code&gt; in this case could show you this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ gem install A
Gem "A" depends on "markdown". Please select provider gem from following alternatives:
1. BlueCloth
2. Maruku
3. ....
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Of course having two "markdown providers" with the same interface (note that &lt;code&gt;require "markdown"&lt;/code&gt; is also part of the interface) would be impossible in the same gem environment but this can easily be solved by using Bundler, Isolate or &lt;a href="http://rvm.beginrescueend.com/"&gt;awesome rvm&lt;/a&gt;'s gemsets. Only problem I see here is when one gem depends on "maruku", not on "markdown", another gem depends on "rdiscount", and your app depends on both of these gems... yuck.&lt;/p&gt;

&lt;p&gt;"Provider gems" can be easily applied to Devise's case:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Gem::Specification.new do |s|
  s.name "devise"
  s.dependency "devise-orm-proxy"
  ...
end

Gem::Specification.new do |s|
  s.name "devise-orm-mongoid"
  s.provides "devise-orm-proxy"
  ...
end

Gem::Specification.new do |s|
  s.name "devise-orm-dm"
  s.provides "devise-orm-proxy"
  ...
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Another example why "provider gems" can be good idea is extlib / AS collision. For example dm-paperclip depends on extlib because it needs inflector. But you can't easily use it in Rails 3 app at the moment because AS+extlib = "UsersesController" :) dm-core used to use extlib for inflections and has been recently converted to AS for some reasons.&lt;/p&gt;

&lt;p&gt;"Provider gems" can solve also this problem. Look at this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Gem::Specification.new do |s|
  s.name "dm-core"
  s.dependency "inflector"
  ...
end

Gem::Specification.new do |s|
  s.name "dm-paperclip"
  s.dependency "inflector"
  ...
end

Gem::Specification.new do |s|
  s.name "extlib"
  s.provides "..."
  s.provides "inflector"
  s.provides "..."
end

Gem::Specification.new do |s|
  s.name "activesupport"
  s.provides "..."
  s.provides "inflector"
  s.provides "..."
end
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;I realize that these proposed solutions are not ideal and there are some edge cases which need some more thought but maybe it will become a good start for further discussion about the problem. &lt;a href="http://github.com/rubygems/rubygems"&gt;Rubygems code is on github now&lt;/a&gt; so we can fork it and improve it!&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/SickillNet/~4/vZJ8aSGSDHY" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://ku1ik.com/2010/07/03/thoughts-on-ruby-gem-dependencies.html</feedburner:origLink></entry>
  
  <entry>
    <title>Quick update on Open File Fast 1.0.1</title>
    <link href="http://feedproxy.google.com/~r/SickillNet/~3/6kNJCW_IEZU/quick-update-on-open-file-fast-1-0-1.html" />
    <updated>2010-06-09T00:00:00+02:00</updated>
    <id>http://ku1ik.com/2010/06/09/quick-update-on-open-file-fast-1-0-1</id>
    <content type="html">&lt;p&gt;This is really short update on OpenFileFast. Recently new JEdit (4.3 final with new ProjectViewer plugin) and Netbeans (6.9RC2) came out and they&amp;#8217;ve both changed some APIs. The changes were not backwards compatible and &lt;span class="caps"&gt;OFF&lt;/span&gt; stopped working for them. Fortunately the fix was simple and I&amp;#8217;ve prepared updated builds of the plugin for JEdit and NB.&lt;/p&gt;
&lt;p&gt;Here are the links for 1.0.1 update:&lt;/p&gt;
&lt;p&gt;Netbeans &lt;span class="caps"&gt;NBM&lt;/span&gt; package: &lt;a href="https://github.com/downloads/sickill/off-plugin/off-netbeans-1.0.1.nbm"&gt;off-netbeans-1.0.1.nbm&lt;/a&gt;&amp;quot;&lt;br /&gt;
JEdit jar file: &lt;a href="https://github.com/downloads/sickill/off-plugin/off-jedit-1.0.1.jar"&gt;off-jedit-1.0.1.jar&lt;/a&gt;&amp;quot;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/SickillNet/~4/6kNJCW_IEZU" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://ku1ik.com/2010/06/09/quick-update-on-open-file-fast-1-0-1.html</feedburner:origLink></entry>
  
  <entry>
    <title>Open File Fast reached 1.0</title>
    <link href="http://feedproxy.google.com/~r/SickillNet/~3/oGM9qyo-OjY/open-file-fast-reached-1-0.html" />
    <updated>2010-01-17T00:00:00+01:00</updated>
    <id>http://ku1ik.com/2010/01/17/open-file-fast-reached-1-0</id>
    <content type="html">&lt;p&gt;After almost a year of development and several beta versions Open File Fast finally became stable and mature enough to bump its version to 1.0. There&amp;#8217;s not much new in this release except several bugfixes and improved look of search results list.&lt;/p&gt;
&lt;p&gt;Now it looks like this:&lt;/p&gt;
&lt;p class="images"&gt;&lt;img src="http://farm3.static.flickr.com/2722/4281138765_af0d6ecc0d_o.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Notice that entered characters are now displayed in bold indicating where they were found in each filename. File path is now right aligned and has lighter color. It doesn&amp;#8217;t come into way anymore when looking at results and lets you localize the file you seek even faster.&lt;/p&gt;
&lt;p&gt;Netbeans version of plugin was updated to work in Netbeans 6.8, JEdit&amp;#8217;s one works now in final JEdit 4.3 version (with ProjecViewer 2.9.1+).&lt;/p&gt;
&lt;p&gt;Netbeans &lt;span class="caps"&gt;NBM&lt;/span&gt; package: &lt;a href="https://github.com/downloads/sickill/off-plugin/off-netbeans-1.0.nbm"&gt;off-netbeans-1.0.nbm&lt;/a&gt;&amp;quot;&lt;br /&gt;
JEdit jar file: &lt;a href="https://github.com/downloads/sickill/off-plugin/off-jedit-1.0.jar"&gt;off-jedit-1.0.jar&lt;/a&gt;&amp;quot;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/SickillNet/~4/oGM9qyo-OjY" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://ku1ik.com/2010/01/17/open-file-fast-reached-1-0.html</feedburner:origLink></entry>
  
  <entry>
    <title>Quick and dirty hack for RSpec's before(:all)</title>
    <link href="http://feedproxy.google.com/~r/SickillNet/~3/UQklWzmDT4A/quick-and-dirty-hack-for-rspec-before-all.html" />
    <updated>2009-11-23T00:00:00+01:00</updated>
    <id>http://ku1ik.com/2009/11/23/quick-and-dirty-hack-for-rspec-before-all</id>
    <content type="html">&lt;p&gt;RSpec is generally nice testing framework. It supports &lt;em&gt;before&lt;/em&gt; and &lt;em&gt;after&lt;/em&gt; hooks which can be invoked before/after &lt;em&gt;each&lt;/em&gt; test case or &lt;em&gt;all&lt;/em&gt; test cases.
&lt;em&gt;before(:all)&lt;/em&gt; is a little confusing though. It runs your "before" block before all "describes" and "contexts", also nested ones. Here's example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;describe User do
  before(:all) { puts "preparing for war" }
  it "should foo" do
    ...
  end
  context "active" do
    it "should bar" do
      ...
    end
  end
  context "inactive" do
    it "should baz" do
      ...
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I would expect "preparing for war" to show up once. But &lt;em&gt;before(:all)&lt;/em&gt; was called thrice. First, for top level "describe", then two times for "contexts".
There were &lt;a href="https://rspec.lighthouseapp.com/projects/5645/tickets/819-beforeall-executes-multiple-times-with-nested-example-groups"&gt;some suggestions&lt;/a&gt; to change this behaviour or to add and option to &lt;a href="https://rspec.lighthouseapp.com/projects/5645/tickets/632"&gt;skip call for nested groups&lt;/a&gt; but nothing has changed recently. People are even trying some crazy hacks like &lt;a href="http://www.swombat.com/getting-rspec-beforeall-and-nested-contexts-w"&gt;this&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;What I needed was to wipe database for every model spec and every request spec because factory generated records made my build unstable (due to uniqueness validations). After trying few things I ended up with using &lt;em&gt;before(:all)&lt;/em&gt; with some condition. First, I've added &lt;em&gt;before_top_level_group&lt;/em&gt; method to Spec::Runner::Configuration and saved it in spec/before_top_level_group.rb:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$_groups = []

class Spec::Runner::Configuration
  def before_top_level_group
    before(:all) do
      top_level_group = self.class.to_s[/^.+ExampleGroup::([^:]+)/, 1]
      unless $_groups.any? { |g| top_level_group == g }
        $_groups &amp;lt;&amp;lt; top_level_group
        yield
      end
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then in spec_helper I used it like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;require 'before_top_level_group'

Spec::Runner.configure do |config|
  config.before_top_level_group do
    # re-migrate db for each top-level group (it usually equals one *_spec.rb file)
    DataMapper.auto_migrate!
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Voila! I know it's a dirty hack but it works for me and I'll be using it until RSpec is patched or I switch my testing framework to something else (&lt;a href="http://github.com/chneukirchen/bacon/"&gt;Bacon&lt;/a&gt; looks nice).&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/SickillNet/~4/UQklWzmDT4A" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://ku1ik.com/2009/11/23/quick-and-dirty-hack-for-rspec-before-all.html</feedburner:origLink></entry>
  
  <entry>
    <title>More Rack::Shell goodies for all Rack worshippers</title>
    <link href="http://feedproxy.google.com/~r/SickillNet/~3/b_Hi1nqZmGk/more-rack-shell-goodies-for-all-rack-worshippers.html" />
    <updated>2009-11-19T00:00:00+01:00</updated>
    <id>http://ku1ik.com/2009/11/19/more-rack-shell-goodies-for-all-rack-worshippers</id>
    <content type="html">&lt;p&gt;&lt;em&gt;Rack::Shell&lt;/em&gt; got a lot of attention lately and I received some feature requests/ideas from great ruby hackers. &lt;a href="http://github.com/hassox"&gt;Daniel Neighman&lt;/a&gt;, currently working on &lt;a href="http://pancakestacks.wordpress.com/2009/11/19/pancakes-console/"&gt;Pancake&lt;/a&gt;, pointed me towards Bryan Helmkamp's &lt;a href="http://github.com/brynary/rack-test"&gt;rack-test&lt;/a&gt;. This awesome piece of code is now used by &lt;em&gt;racksh&lt;/em&gt; to simulate HTTP requests to your Rack application. Yay!&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;% racksh
Rack::Shell v0.9.4 started in development environment.
&amp;gt;&amp;gt; $rack.get "/"
=&amp;gt; #&amp;lt;Rack::MockResponse:0xb68fa7bc @body="&amp;lt;html&amp;gt;...", @headers={"Content-Type"=&amp;gt;"text/html", "Content-Length"=&amp;gt;"1812"}, @status=200, ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Check out &lt;a href="http://github.com/sickill/racksh/blob/master/README.markdown"&gt;README&lt;/a&gt; for details. Here are just few examples what's possible:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$rack.get "/", {}, { 'REMOTE_ADDR' =&amp;gt; '123.45.67.89' }
$rack.header "User-Agent", "Firefox"
$rack.post "/users", :user =&amp;gt; { :name =&amp;gt; "Jola", :email =&amp;gt; "jola@misi.ak" }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now you can build and test Sinatra apps in single &lt;em&gt;racksh&lt;/em&gt; session &lt;a href="http://gist.github.com/239134"&gt;like this&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Another nice thing in new version is support for session setup through config files. Rack::Shell supports configuration file &lt;em&gt;.rackshrc&lt;/em&gt; which is loaded from two places during startup: user's home dir and application directory (in this order). You can put any ruby code in it, but it's purpose is to setup your session, ie. setting headers which will be used for all $rack.get/post/... requests.&lt;/p&gt;

&lt;p&gt;For example to set user agent to Firefox and re-migrate db if loaded environment is &lt;em&gt;test&lt;/em&gt; put following in &lt;em&gt;.rackshrc&lt;/em&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# .rackshrc

$rack.header "User-Agent", "Firefox"
DataMapper.auto_migrate! if $rack.env == "test"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can also make requests in config file:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# .rackshrc

$rack.put "/signin", :login =&amp;gt; "jola", :password =&amp;gt; "misiacz"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will ensure you are always logged in when you start &lt;em&gt;racksh&lt;/em&gt; :)&lt;/p&gt;

&lt;p&gt;Full documentation and sources are on &lt;a href="http://github.com/sickill/racksh"&gt;github&lt;/a&gt;, gems for all versions on &lt;a href="http://gemcutter.org/gems/racksh"&gt;gemcutter.org&lt;/a&gt;. Enjoy!&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/SickillNet/~4/b_Hi1nqZmGk" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://ku1ik.com/2009/11/19/more-rack-shell-goodies-for-all-rack-worshippers.html</feedburner:origLink></entry>
  
  <entry>
    <title>Rails-like console for any Rack based ruby web app</title>
    <link href="http://feedproxy.google.com/~r/SickillNet/~3/lY5FVz1F4sU/rails-like-console-for-any-rack-based-ruby-web-app.html" />
    <updated>2009-11-15T00:00:00+01:00</updated>
    <id>http://ku1ik.com/2009/11/15/rails-like-console-for-any-rack-based-ruby-web-app</id>
    <content type="html">&lt;p&gt;I always miss script/console from Rails while developing my Sinatra apps, especially ones built with DataMapper where I need to auto-migrate my db. Sinatra doesn't come with any comparable solution as it's not a full framework, but rather library for creating simple web apps. Recently I tried &lt;a href="http://heroku.com/"&gt;Heroku&lt;/a&gt; platform and their "heroku console" command inspired me to create something similar - &lt;em&gt;racksh&lt;/em&gt; aka &lt;em&gt;Rack::Shell&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://github.com/sickill/racksh"&gt;racksh&lt;/a&gt; is a console for Rack based ruby web applications. It's like Rails' &lt;em&gt;script/console&lt;/em&gt; or Merb's &lt;em&gt;merb -i&lt;/em&gt;, but for any app built on Rack. You can use it to load application environment for Rails, Merb, Sinatra, Camping, Ramaze or your own framework provided there is &lt;em&gt;config.ru&lt;/em&gt; file in app's root directory.&lt;/p&gt;

&lt;p&gt;It's purpose is to allow developer to introspect his application and/or make some initial setup, ie. running mentioned &lt;em&gt;DataMapper.auto_migrate!&lt;/em&gt;. It's mainly aimed at apps that don't have similar facility (like Sinatra) but can be used without problems with Merb or Rails apps.&lt;/p&gt;

&lt;p&gt;How it works? It loads whole application environment like Rack web server, but it doesn't run the app. Simply, methods like &lt;em&gt;use&lt;/em&gt; or &lt;em&gt;run&lt;/em&gt; which are normally invoked on Rack::Builder instance are being stubbed.&lt;/p&gt;

&lt;p&gt;Instalation is as easy as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;gem install racksh -s http://gemcutter.org
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then to open console run following inside rack application directory (containing config.ru file):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;racksh
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To specify location of config.ru set CONFIG_RU env variable:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;CONFIG_RU=~/projects/foobar/config.ru racksh
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Executing ruby code inside application environment and printing results is also supported:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;racksh Order.all
racksh "Order.first :created_at =&amp;gt; Date.today"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Default Rack environment is set to &lt;em&gt;development&lt;/em&gt; but it can be changed by setting RACK_ENV env variable:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;RACK_ENV=production racksh
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now I don't need to create some kind of &lt;em&gt;console.rb&lt;/em&gt; for my new Rack app, I just use &lt;em&gt;racksh&lt;/em&gt;. Enjoy!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;UPDATE: &lt;a href="/blog/2009/11/19/more-rack-shell-goodies-for-all-rack-worshippers.html"&gt;Read here&lt;/a&gt; for more info.&lt;/em&gt;&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/SickillNet/~4/lY5FVz1F4sU" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://ku1ik.com/2009/11/15/rails-like-console-for-any-rack-based-ruby-web-app.html</feedburner:origLink></entry>
  
  <entry>
    <title>Subdomain shared cookies in Merb's specs</title>
    <link href="http://feedproxy.google.com/~r/SickillNet/~3/Q0OUYDkU1Z4/subdomain-shared-cookies-in-merbs-specs.html" />
    <updated>2009-11-13T00:00:00+01:00</updated>
    <id>http://ku1ik.com/2009/11/13/subdomain-shared-cookies-in-merbs-specs</id>
    <content type="html">&lt;p&gt;I&amp;#8217;m currently working on Merb app which makes use of subdomains for user accounts. In order to have user authenticated on both base domain and subdomains I set cookie domain like this in init.rb:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; Merb::Config.use do |c|
    c[:default_cookie_domain] = ".mydomain.com"
  end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Dot at the start of domain name specifies that the cookie will be set for &amp;#8220;mydomain.com&amp;#8221; and all its subdomains. This works perfectly in browser(s) but I&amp;#8217;ve encountered a problem in my request specs. After authenticating at &amp;#8220;mydomain.com&amp;#8221; app redirects to &amp;#8220;username.mydomain.com&amp;#8221;. But next request to either &amp;#8220;mydomain.com&amp;#8221; or &amp;#8220;username.mydomain.com&amp;#8221; shows that session is no longer authenticated. After some reading through merb-core sources I&amp;#8217;ve found that Merb::Test::Cookie can&amp;#8217;t properly handle cookies with domain set to &amp;#8216;.foo.com&amp;#8217; (with dot at the start).&lt;/p&gt;
&lt;p&gt;This is because Merb::Test::Cookie#valid? makes some regexp check comparing current request domain with domain from cookie, which fails for cookie domain set to &amp;#8216;.foo.com&amp;#8217;. I&amp;#8217;ve created a fix and spec for this scenario. Patch can be found in &lt;a href="http://github.com/sickill/merb/commit/a1f1a511b189cfa1ae908cfe481d0ba7bd00860b"&gt;this commit&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;del&gt;Until it gets merged and released with new Merb version (I hope in 1.1)&lt;/del&gt; It was already merged into Merb master branch, but until the 1.1 release the simplest workaround is to monkey patch Merb::Test::Cookie class, ie. in spec_helper.rb like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;class Merb::Test::Cookie
  def valid?(uri)
    domain_ = domain.start_with?('.') ? domain[1..-1] : domain
    uri_path = uri.path.blank? ? "/" : uri.path
    uri.host =~ Regexp.new("#{Regexp.escape(domain_)}$") &amp;amp;&amp;amp;
    uri_path =~ Regexp.new("^#{Regexp.escape(path)}")
  end
end&lt;/code&gt;&lt;/pre&gt;&lt;img src="http://feeds.feedburner.com/~r/SickillNet/~4/Q0OUYDkU1Z4" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://ku1ik.com/2009/11/13/subdomain-shared-cookies-in-merbs-specs.html</feedburner:origLink></entry>
  
  <entry>
    <title>Finish Him completes them all!</title>
    <link href="http://feedproxy.google.com/~r/SickillNet/~3/D5ZaEhjlZhU/finish-him-completes-them-all.html" />
    <updated>2009-11-02T00:00:00+01:00</updated>
    <id>http://ku1ik.com/2009/11/02/finish-him-completes-them-all</id>
    <content type="html">&lt;p&gt;Once again extending JEdit&amp;#8217;s feature set. This time word completion. JEdit already has two word completion mechanisms: built-in one and TextAutocomplete plugin. However none of them provides as smooth completion as Netbeans or Textmate&amp;#8217;s ones. First I was thinking about creating some BeanShell macro too improve my completion experience but it looked like to much logic for just a macro. So I ended up with new plugin, and this time written in&amp;#8230; &lt;a href="http://www.scala-lang.org"&gt;Scala&lt;/a&gt; (finally an opportunity to start learning Scala on real project).&lt;/p&gt;
&lt;p&gt;It&amp;#8217;s called &amp;#8220;Finish Him!&amp;#8221; and following comparison shows how it&amp;#8217;s different from existing solutions for JEdit.&lt;/p&gt;
&lt;p&gt;JEdit&amp;#8217;s built in word completion:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;shows popup when triggered (-1)&lt;/li&gt;
	&lt;li&gt;doesn&amp;#8217;t sort suggestions by distance from the caret (-1)&lt;/li&gt;
	&lt;li&gt;searches for suggestions in other (visible) buffers (+1)&lt;br /&gt;
Total: -1&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;TextAutocomplete:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;shows popup automatically (-1)&lt;/li&gt;
	&lt;li&gt;doesn&amp;#8217;t sort suggestions by distance from the caret (-1)&lt;/li&gt;
	&lt;li&gt;doesn&amp;#8217;t search for suggestions in other (visible) buffers (-1)&lt;br /&gt;
Total: -3&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Finish Him!:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;doesn&amp;#8217;t show popup when triggered, just inserts next suggestion (+1)&lt;/li&gt;
	&lt;li&gt;sorts suggestions by distance from the caret (+1)&lt;/li&gt;
	&lt;li&gt;searches for suggestions in other visible buffers (+1)&lt;br /&gt;
Total: 3&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Finish Him! kills other solutions with score of 3. I know that these things may look like details but after using Netbeans word completion you know that it&amp;#8217;s the simplest, fastest and most &amp;#8220;correct&amp;#8221; (by proper suggestions sorting) word completion. If you&amp;#8217;re using JEdit then check it out, it&amp;#8217;s now available on &lt;a href="http://plugins.jedit.org/plugins/?FinishHim"&gt;Plugin Central&lt;/a&gt; so it can be installed directly from JEdit&amp;#8217;s plugin manager. For sources look &lt;a href="http://github.com/sickill/finish-him"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/SickillNet/~4/D5ZaEhjlZhU" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://ku1ik.com/2009/11/02/finish-him-completes-them-all.html</feedburner:origLink></entry>
  
  <entry>
    <title>Coderack, a Rack middleware contest launched</title>
    <link href="http://feedproxy.google.com/~r/SickillNet/~3/-P6ZphG8AuY/coderack-rack-middleware-contest.html" />
    <updated>2009-10-15T00:00:00+02:00</updated>
    <id>http://ku1ik.com/2009/10/15/coderack-rack-middleware-contest</id>
    <content type="html">&lt;p&gt;You&amp;#8217;ve probably heard about &lt;a href="http://rack.rubyforge.org/"&gt;Rack&lt;/a&gt;, right? Few months ago during the conversation with my teammate about potential and possibilities of Rack, and Rack middleware we&amp;#8217;ve came up with an idea of creating some coding contest. We wanted to encourage Ruby developers to explore the power of Rack because we believe it&amp;#8217;s the best thing since sliced bread. And what better way than to hold a contest? And now we can proudly say that it happened! We&amp;#8217;ve just launched it and got really positive feedback about it so far.&lt;/p&gt;
&lt;p&gt;But what is this &lt;a href="http://coderack.org"&gt;CodeRack&lt;/a&gt; thingie all about? Like I&amp;#8217;ve mentioned first goal is to show Rack&amp;#8217;s power and encourage its use in Ruby web apps. The secondary goal of the contest is to generate a set of open source solutions that will solve real problems and inspire others. Every entry will be released under the &lt;span class="caps"&gt;MIT&lt;/span&gt; open source license.&lt;/p&gt;
&lt;p&gt;Programmers are encouraged to submit contest entries that will be judged based on the cleverness of the application and the elegance of the code. Entries can be submitted at &lt;a href="http://coderack.org"&gt;coderack.org&lt;/a&gt; until midnight &lt;span class="caps"&gt;CEST&lt;/span&gt; November 15th. Finalists are scheduled to be announced on the 1st of December and public voting will run for one month. The final winners will be announced on the 5th of January.&lt;/p&gt;
&lt;p&gt;The first round of the contest will be judged by an elite panel of judges including Ben Bangert (Pylons framework), Chris Wanstrath and PJ Hyett of GitHub, Joshua Peek of 37Signals, Yehuda Katz of Engine Yard (also Rails core team member), Ryan Tomayko of Heroku, Core Rails team member Matt Aimonetti, and the Rails Envy team of Gregg Pollack and Jason Seifer. Once the finalists have been selected by the panel, the public will vote for the top prize winners.&lt;/p&gt;
&lt;p&gt;Prizes have been donated by Bytemark Hosting, GitHub, Jetbrains, Mindmeister, Freelance Total, Heroku, Rackspace Hosting, Peepcode, BDDCasts, and Zenbe Shareflow. The top prize includes a dedicated quad core server package and is valued at over $3000. Every entrant will receive a credit from bddcasts.com and $30 credit from Heroku! All finalists will receive a package including Zenbe Shareflow subscriptions, a RubyMine license from JetBrains, and five credits from bddcasts.com. Details of all of the prize packages will soon be available on the contest website.&lt;/p&gt;
&lt;p&gt;So, want to become a Rackstar? It&amp;#8217;s easy, create some interesting code and submit it to the contest. We&amp;#8217;ve already collected over 45 entries in just few days. Oh, and I&amp;#8217;ve almost forgot, CodeRack site itself is using middlewares, one of them being Ryan Tomayko&amp;#8217;s awesome Rack::Cache.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/SickillNet/~4/-P6ZphG8AuY" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://ku1ik.com/2009/10/15/coderack-rack-middleware-contest.html</feedburner:origLink></entry>
  
</feed>

