<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Michael Crosby</title><link href="http://crosbymichael.com/" rel="alternate"></link><link href="http://crosbymichael.com/feeds/all.atom.xml" rel="self"></link><id>http://crosbymichael.com/</id><updated>2014-11-16T00:00:00+00:00</updated><entry><title>Creating containers - Part 1</title><link href="http://crosbymichael.com/creating-containers-part-1.html" rel="alternate"></link><updated>2014-11-16T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2014-11-16:creating-containers-part-1.html</id><summary type="html">&lt;p&gt;This is part one of a series of blog posts detailing how docker creates containers.
We will dig deep into the various pieces that are stitched together to see what
it takes to make &lt;code&gt;docker run ...&lt;/code&gt; awesome.&lt;/p&gt;
&lt;h2&gt;First, what is a container?&lt;/h2&gt;
&lt;p&gt;I think the various pieces of technology that goes into creating a container is fairly
commonplace. You should have seen a few cool looking charts in various presentations
about Docker where you get a quick "Docker uses namespaces, cgroups, chroot, etc."
to create containers.  But why does it take all these pieces to create a contaienr?&lt;br /&gt;
Why is it not a simple syscall and it's all done for me?
The fact is that container's don't exist, they are made up. There is no such
thing as a "linux container" in the kernel.  A container is a userland concept.&lt;/p&gt;
&lt;h2&gt;Namespaces&lt;/h2&gt;
&lt;p&gt;In part one I'll talk about how to create Linux namespaces in the context of how they
are used within docker.  In later posts we will look into how namespaces are combined
with other features like cgroups and an isolated filesystem to create something useful.&lt;/p&gt;
&lt;p&gt;First off we need a high level explanation of what a namespace does and why it's useful.
Basically, a namespace is a scoped view of your underlying Linux system. There are a
few different types of namespaces implemented inside the kernel.  As we dig into
each of the different namespaces below you can follow along by running
&lt;code&gt;docker run -it --privileged --net host crosbymichael/make-containers&lt;/code&gt;.
This has a few preloaded files and configuration to get your started.  Even though we 
will be creating namespaces inside an container that docker runs for us, don't let that
trip you up.  I opted for this approach as providing a container preloaded with all 
the dependencies that you need to run the examples is why we are doing this in the 
first place.  To make things a little easier, I'm using the &lt;code&gt;--net host&lt;/code&gt; flag so that
we are able to see your host's network interfaces within our demo container.  This will
be useful in the network examples.  We also need to provide the &lt;code&gt;--privilged&lt;/code&gt; flag so that
we have the correct permissions to create new namespaces within our container.&lt;/p&gt;
&lt;p&gt;If you are interested in what the Dockerfile looks like then here it is:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;FROM debian:jessie

RUN apt-get update &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; apt-get install -y &lt;span class="se"&gt;\&lt;/span&gt;
    gcc &lt;span class="se"&gt;\&lt;/span&gt;
    vim &lt;span class="se"&gt;\&lt;/span&gt;
    emacs

COPY containers/ /containers/
WORKDIR /containers
CMD &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;bash&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;I'll be doing the examples in C, as it's sometimes easier
to explain the lower level details better than the abstractions that Go provides.
So lets start...&lt;/p&gt;
&lt;h3&gt;NET Namespace&lt;/h3&gt;
&lt;p&gt;The network namespaces provides your own view of the network stack of your system. This
can include your very own &lt;code&gt;localhost&lt;/code&gt;.  Make sure you are in the &lt;code&gt;crosbymichael/make-containers&lt;/code&gt;
and run the command &lt;code&gt;ip a&lt;/code&gt; to view all the network interfaces of your host machine.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&amp;gt; ip a
root@development:/containers# ip a
1: lo: &amp;lt;LOOPBACK,UP,LOWER_UP&amp;gt; mtu &lt;span class="m"&gt;65536&lt;/span&gt; qdisc noqueue state UNKNOWN group default
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: &amp;lt;BROADCAST,MULTICAST,UP,LOWER_UP&amp;gt; mtu &lt;span class="m"&gt;1500&lt;/span&gt; qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:19:ca:f2 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe19:caf2/64 scope link
       valid_lft forever preferred_lft forever
3: eth1: &amp;lt;BROADCAST,MULTICAST,UP,LOWER_UP&amp;gt; mtu &lt;span class="m"&gt;1500&lt;/span&gt; qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:20:84:47 brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.103/24 brd 192.168.56.255 scope global eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe20:8447/64 scope link
       valid_lft forever preferred_lft forever
4: docker0: &amp;lt;NO-CARRIER,BROADCAST,MULTICAST,UP&amp;gt; mtu &lt;span class="m"&gt;1500&lt;/span&gt; qdisc noqueue state DOWN group default
    link/ether 56:84:7a:fe:97:99 brd ff:ff:ff:ff:ff:ff
    inet 172.17.42.1/16 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::5484:7aff:fefe:9799/64 scope link
       valid_lft forever preferred_lft forever
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Ok cool, so this is all the network interfaces currently on &lt;strong&gt;my&lt;/strong&gt; host system. Yours may look
a little different but you get the idea.  Now let's write some code to create a new network
namespace.  For this we will write a skeleton of a small C binary that uses the &lt;code&gt;clone&lt;/code&gt; syscall.  We will
start by using clone to run binaries that are already installed inside our demo container. 
The file &lt;code&gt;skeleton.c&lt;/code&gt; should be in the working directory of the demo container. 
We will use this file as the basis of all our examples.  Here is the code incase you don't feel
like running the container right now.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="cp"&gt;#define _GNU_SOURCE&lt;/span&gt;
&lt;span class="cp"&gt;#include &amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;#include &amp;lt;stdlib.h&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;#include &amp;lt;sched.h&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;#include &amp;lt;sys/wait.h&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;#include &amp;lt;errno.h&amp;gt;&lt;/span&gt;

&lt;span class="cp"&gt;#define STACKSIZE (1024*1024)&lt;/span&gt;
&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;child_stack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;STACKSIZE&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;clone_args&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// child_exec is the func that will be executed as the result of clone&lt;/span&gt;
&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;child_exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;stuff&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;clone_args&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;clone_args&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;stuff&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;execvp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;fprintf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stderr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;failed to execvp argments %s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                        &lt;span class="n"&gt;strerror&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;errno&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
                &lt;span class="n"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="c1"&gt;// we should never reach here!&lt;/span&gt;
        &lt;span class="n"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;EXIT_FAILURE&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;clone_args&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;clone_flags&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;SIGCHLD&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// the result of this call is that our child_exec will be run in another&lt;/span&gt;
        &lt;span class="c1"&gt;// process returning it&amp;#39;s pid&lt;/span&gt;
        &lt;span class="kt"&gt;pid_t&lt;/span&gt; &lt;span class="n"&gt;pid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
            &lt;span class="n"&gt;clone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;child_exec&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;child_stack&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;STACKSIZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;clone_flags&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;fprintf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stderr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;clone failed WTF!!!! %s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;strerror&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;errno&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
                &lt;span class="n"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;EXIT_FAILURE&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="c1"&gt;// lets wait on our child process here before we, the parent, exits&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;waitpid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;fprintf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stderr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;failed to wait pid %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="n"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;EXIT_FAILURE&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;EXIT_SUCCESS&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This is a small C binary that will allow you to run processes
like &lt;code&gt;./a.out ip a&lt;/code&gt;.  It uses the arguments that you pass on the cli as the arguments to whatever
process you want to use.  Don't worry about the specific implementation too much as it's the
changes we will be making that are the interesting aspects.  Remember, this will execute the 
binary and arguments of whatever program you want, this means if you want to run one of these demos
below and have it spawn a shell session so that you can poke around in your new namespace then go 
ahead.  It is a great way to explore and inspect these different namespaces at your own pace.
So to get started let's make a copy of this file to start working with the network namespace.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&amp;gt; cp skeleton.c network.c
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Ok, within this file there is a very
special var called &lt;code&gt;clone_flags&lt;/code&gt;.  This is where most of our changes will happen throughout this
post. Namespaces are primarily controlled via the clone flags.  The clone flag for the network
namespace is &lt;code&gt;CLONE_NEWNET&lt;/code&gt;.  We need to change the line in the file &lt;code&gt;int clone_flags = SIGCHLD;&lt;/code&gt; to
&lt;code&gt;int clone_flags = CLONE_NEWNET | SIGCHLD;&lt;/code&gt; so that the call to &lt;code&gt;clone&lt;/code&gt; creates a new network namespace
for our process.  Make this change in &lt;code&gt;network.c&lt;/code&gt; then compile and run.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&amp;gt; gcc -o net network.c
&amp;gt; ./net ip a
1: lo: &amp;lt;LOOPBACK&amp;gt; mtu &lt;span class="m"&gt;65536&lt;/span&gt; qdisc noop state DOWN group default
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The result of this run now looks very different from the first time we ran the &lt;code&gt;ip a&lt;/code&gt;
command.  We only see a &lt;code&gt;loopback&lt;/code&gt; interface in the output.  This is because our process
that was created only has a view of its network namespace and not of the host.
And that's it. That is how you create a new network namespace.  &lt;/p&gt;
&lt;p&gt;Right now this is pretty useless as you don't have any usable 
interfaces.  Docker uses this new network namespace to setup a &lt;code&gt;veth&lt;/code&gt; 
interface so that your container has it's own ip address
allocated on a bridge, usually &lt;code&gt;docker0&lt;/code&gt;.
We won't go down the path of how to setup interfaces in namespaces at this time.  We can save
that for another post.&lt;/p&gt;
&lt;p&gt;So now that we know how a network namespace is created lets look at the mount namespace.&lt;/p&gt;
&lt;h3&gt;MNT Namespace&lt;/h3&gt;
&lt;p&gt;The mount namespace gives you a scoped view of the mounts on your system. It's often
confused with jailing a process inside a &lt;code&gt;chroot&lt;/code&gt; or similar. This is not true!
The mount namespaces does not equal a filesystem jail.  So the next time you hear someone 
say that a container uses the mount namespace to "jail" the process inside it's own root
filesystem you can call bullshit because they don't know what they are talking about.  Do it, it's fun :)&lt;/p&gt;
&lt;p&gt;Let's start by making a copy of &lt;code&gt;skeleton.c&lt;/code&gt; again for our mount related changes. 
We can do a quick build
and run to see what our current mount points looks like with the &lt;code&gt;mount&lt;/code&gt; command.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&amp;gt; cp skeleton.c mount.c
&amp;gt; gcc -o mount mount.c
&amp;gt; ./mount mount
proc on /proc &lt;span class="nb"&gt;type &lt;/span&gt;proc &lt;span class="o"&gt;(&lt;/span&gt;rw,nosuid,nodev,noexec,relatime&lt;span class="o"&gt;)&lt;/span&gt;
tmpfs on /dev &lt;span class="nb"&gt;type &lt;/span&gt;tmpfs &lt;span class="o"&gt;(&lt;/span&gt;rw,nosuid,mode&lt;span class="o"&gt;=&lt;/span&gt;755&lt;span class="o"&gt;)&lt;/span&gt;
shm on /dev/shm &lt;span class="nb"&gt;type &lt;/span&gt;tmpfs &lt;span class="o"&gt;(&lt;/span&gt;rw,nosuid,nodev,noexec,relatime,size&lt;span class="o"&gt;=&lt;/span&gt;65536k&lt;span class="o"&gt;)&lt;/span&gt;
mqueue on /dev/mqueue &lt;span class="nb"&gt;type &lt;/span&gt;mqueue &lt;span class="o"&gt;(&lt;/span&gt;rw,nosuid,nodev,noexec,relatime&lt;span class="o"&gt;)&lt;/span&gt;
devpts on /dev/pts &lt;span class="nb"&gt;type &lt;/span&gt;devpts &lt;span class="o"&gt;(&lt;/span&gt;rw,nosuid,noexec,relatime,gid&lt;span class="o"&gt;=&lt;/span&gt;5,mode&lt;span class="o"&gt;=&lt;/span&gt;620,ptmxmode&lt;span class="o"&gt;=&lt;/span&gt;666&lt;span class="o"&gt;)&lt;/span&gt;
sysfs on /sys &lt;span class="nb"&gt;type &lt;/span&gt;sysfs &lt;span class="o"&gt;(&lt;/span&gt;rw,nosuid,nodev,noexec,relatime&lt;span class="o"&gt;)&lt;/span&gt;
/dev/disk/by-uuid/d3aa2880-c290-4586-9da6-2f526e381f41 on /etc/resolv.conf &lt;span class="nb"&gt;type &lt;/span&gt;ext4 &lt;span class="o"&gt;(&lt;/span&gt;rw,relatime,errors&lt;span class="o"&gt;=&lt;/span&gt;remount-ro,data&lt;span class="o"&gt;=&lt;/span&gt;ordered&lt;span class="o"&gt;)&lt;/span&gt;
/dev/disk/by-uuid/d3aa2880-c290-4586-9da6-2f526e381f41 on /etc/hostname &lt;span class="nb"&gt;type &lt;/span&gt;ext4 &lt;span class="o"&gt;(&lt;/span&gt;rw,relatime,errors&lt;span class="o"&gt;=&lt;/span&gt;remount-ro,data&lt;span class="o"&gt;=&lt;/span&gt;ordered&lt;span class="o"&gt;)&lt;/span&gt;
/dev/disk/by-uuid/d3aa2880-c290-4586-9da6-2f526e381f41 on /etc/hosts &lt;span class="nb"&gt;type &lt;/span&gt;ext4 &lt;span class="o"&gt;(&lt;/span&gt;rw,relatime,errors&lt;span class="o"&gt;=&lt;/span&gt;remount-ro,data&lt;span class="o"&gt;=&lt;/span&gt;ordered&lt;span class="o"&gt;)&lt;/span&gt;
devpts on /dev/console &lt;span class="nb"&gt;type &lt;/span&gt;devpts &lt;span class="o"&gt;(&lt;/span&gt;rw,nosuid,noexec,relatime,gid&lt;span class="o"&gt;=&lt;/span&gt;5,mode&lt;span class="o"&gt;=&lt;/span&gt;620,ptmxmode&lt;span class="o"&gt;=&lt;/span&gt;000&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This is what the mount points look like from within my demo container, yours may look different.
In order to create a new mount namespace we use the flag &lt;code&gt;CLONE_NEWNS&lt;/code&gt;. You may notice something may look
weird here.  Why is this flag not &lt;code&gt;CLONE_NEWMOUNT&lt;/code&gt; or &lt;code&gt;CLONE_NEWMNT&lt;/code&gt;?  This is because the mount
namespace was the first Linux namespace introduced and the name was just an undersight.
If you write code you will understand that as you start building a feature or an application,
you don't often have a full picture of the end result.  Anyway, let's add &lt;code&gt;CLONE_NEWNS&lt;/code&gt; to our
&lt;code&gt;clone_flags&lt;/code&gt; variable.  It should look something like &lt;code&gt;int clone_flags = CLONE_NEWNS | SIGCHLD;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Lets go ahead and build &lt;code&gt;mount.c&lt;/code&gt; again and run the same command.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&amp;gt; cp skeleton.c mount.c
&amp;gt; gcc -o mount mount.c
&amp;gt; ./mount mount
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Nothing changed.  Whaaat???  This is because the process that we run inside the new mount
namespace still has a view on &lt;code&gt;/proc&lt;/code&gt; of the underlying system.  The result is that the
new process sort of &lt;em&gt;inherits&lt;/em&gt; a view on the underlying mounts.
There are a few ways that we can prevent this, like using &lt;code&gt;pivot_root&lt;/code&gt;, but we will leave
that for an additional post on filesystem jails and how &lt;code&gt;chroot&lt;/code&gt; / &lt;code&gt;pivot_root&lt;/code&gt; interact with
the container's mount namespace.&lt;/p&gt;
&lt;p&gt;However, one way that we can try out our new mount namespace is to, well, mount something. 
Let's create a new &lt;code&gt;tmpfs&lt;/code&gt; mount in &lt;code&gt;/mytmp&lt;/code&gt; for this demo.  We will do this mount
in C and continue to run our same &lt;code&gt;mount&lt;/code&gt; command as the args to our mount binary.  In order to do a mount
&lt;strong&gt;inside&lt;/strong&gt; of our mount namespace we need  to add the code to the &lt;code&gt;child_exec&lt;/code&gt; function,
before the call to &lt;code&gt;execvp&lt;/code&gt;.  The code within the &lt;code&gt;child_exec&lt;/code&gt; function is run inside the newly 
created process, i.e., inside our new namespace.  The code in &lt;code&gt;child_exec&lt;/code&gt; should look like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;// child_exec is the func that will be executed as the result of clone&lt;/span&gt;
&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;child_exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;stuff&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;clone_args&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;clone_args&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;stuff&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;none&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/mytmp&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;tmpfs&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;fprintf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stderr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;failed to mount tmpfs %s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                        &lt;span class="n"&gt;strerror&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;errno&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
                &lt;span class="n"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;execvp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;fprintf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stderr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;failed to execvp argments %s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                        &lt;span class="n"&gt;strerror&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;errno&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
                &lt;span class="n"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="c1"&gt;// we should never reach here!&lt;/span&gt;
        &lt;span class="n"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;EXIT_FAILURE&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;We need to first create a directory &lt;code&gt;/mytmp&lt;/code&gt; before we compile and run with the changes above.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&amp;gt; mkdir /mytmp
&amp;gt; gcc -o mount mount.c
&amp;gt; ./mount mount
&lt;span class="c"&gt;# cutting out the common output...&lt;/span&gt;
none on /mytmp &lt;span class="nb"&gt;type &lt;/span&gt;tmpfs &lt;span class="o"&gt;(&lt;/span&gt;rw,relatime&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;I cut out the common output above from the first time I ran &lt;code&gt;mount&lt;/code&gt;.&lt;br /&gt;
The result is that you should see a new mount point for our &lt;code&gt;tmpfs&lt;/code&gt; mount. 
Nice!  Go ahead and run &lt;code&gt;mount&lt;/code&gt; in the current shell just for comparison.&lt;br /&gt;
Notice how the &lt;code&gt;tmpfs&lt;/code&gt; mount is not displayed?  That is because we created the mount inside
our own mount namespace, not in the parent's namespace.  &lt;/p&gt;
&lt;p&gt;Remember how I said that the mount namepaces does not equal a filesystem jail?  Go ahead and run our 
&lt;code&gt;./mount&lt;/code&gt; binary with the &lt;code&gt;ls&lt;/code&gt; command.  Everything is there.  Now you have proof!&lt;/p&gt;
&lt;h3&gt;UTS Namespace&lt;/h3&gt;
&lt;p&gt;The next namespace is the UTS namespace that is responsible for system identification.  This includes the
&lt;code&gt;hostname&lt;/code&gt; and &lt;code&gt;domainname&lt;/code&gt;.  It allows a container to have it's own hostname independently
from the host system along with other containers.  Let's start by making a copy of &lt;code&gt;skeleton.c&lt;/code&gt; and running 
the &lt;code&gt;hostname&lt;/code&gt; command with it.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&amp;gt; cp skeleton.c uts.c
&amp;gt; gcc -o uts uts.c
&amp;gt; ./uts hostname
development
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This should display your system's hostname (&lt;code&gt;development&lt;/code&gt; in my case).  Like earlier, let's add
the clone flag for the UTS namespace to the &lt;code&gt;clone_flags&lt;/code&gt; variable. The flag should be &lt;code&gt;CLONE_NEWUTS&lt;/code&gt;.
If you compile and run then you should see the exact same output. This is totally fine.
The values in the UTS namespace are inherited from the &lt;em&gt;parent&lt;/em&gt;.
However, within this new namespace, we can change the hostname without it affecting the &lt;em&gt;parent&lt;/em&gt; or other
container's that have a separate UTS namespace.&lt;/p&gt;
&lt;p&gt;Let's modify the hostname in the &lt;code&gt;child_exec&lt;/code&gt; function.  To do that, you will need to add
the &lt;code&gt;#include &amp;lt;unistd.h&amp;gt;&lt;/code&gt; header to gain access to the &lt;code&gt;sethostname&lt;/code&gt; function, as well as
the &lt;code&gt;#include &amp;lt;string.h&amp;gt;&lt;/code&gt; header to use &lt;code&gt;strlen&lt;/code&gt; needed by &lt;code&gt;sethostname&lt;/code&gt;.
The new body of the &lt;code&gt;child_exec&lt;/code&gt; function should look like the following:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;// child_exec is the func that will be executed as the result of clone&lt;/span&gt;
&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;child_exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;stuff&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;clone_args&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;clone_args&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;stuff&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;new_hostname&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;myhostname&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sethostname&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new_hostname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;strlen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new_hostname&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;fprintf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stderr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;failed to execvp argments %s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                        &lt;span class="n"&gt;strerror&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;errno&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
                &lt;span class="n"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;execvp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;fprintf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stderr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;failed to execvp argments %s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                        &lt;span class="n"&gt;strerror&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;errno&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
                &lt;span class="n"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="c1"&gt;// we should never reach here!&lt;/span&gt;
        &lt;span class="n"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;EXIT_FAILURE&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Ensure that &lt;code&gt;clone_flags&lt;/code&gt; within your main look like &lt;code&gt;int clone_flags = CLONE_NEWUTS | SIGCHLD;&lt;/code&gt; then
compile and run the binary with the same args.  You should now see the value that we set returned
from the &lt;code&gt;hostname&lt;/code&gt; command.
To verify that this change did not affect our current shell go ahead and run &lt;code&gt;hostname&lt;/code&gt; and make 
sure that you have your original value back.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&amp;gt; gcc -o uts uts.c
&amp;gt; ./uts hostname
myhostname
&amp;gt; hostname
development
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Awesome! We are doing good.&lt;/p&gt;
&lt;h3&gt;IPC Namespace&lt;/h3&gt;
&lt;p&gt;The IPC namespace is used for isolating interprocess communication, things like SysV message queues.
Let's make a copy of &lt;code&gt;skeleton.c&lt;/code&gt; for this namespace.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&amp;gt; cp skeleton.c ipc.c
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The way we are going to test the IPC namespace is by creating a message queue on the host, and
ensuring that we cannot see it when we spawn a new process inside it's own IPC namespace.
Let's first create a message queue in our current shell then compile and run our copy of the 
skeleton code to view the queue.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&amp;gt; ipcmk -Q
Message queue id: 65536
&amp;gt; gcc -o ipc ipc.c
&amp;gt; ./ipc ipcs -q
------ Message Queues --------
key        msqid      owner      perms      used-bytes   message
0xfe7f09d1 &lt;span class="m"&gt;65536&lt;/span&gt;      root       &lt;span class="m"&gt;644&lt;/span&gt;        &lt;span class="m"&gt;0&lt;/span&gt;            0
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Without a new IPC namespace you can see the same message queue that was created.
Now let's add the &lt;code&gt;CLONE_NEWIPC&lt;/code&gt; flag to our &lt;code&gt;clone_flags&lt;/code&gt; var to create a new IPC namespace for 
our process.
The &lt;code&gt;clone_flags&lt;/code&gt; var should look like &lt;code&gt;int clone_flags = CLONE_NEWIPC | SIGCHLD;&lt;/code&gt;.
Recompile and run the same command again:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&amp;gt; gcc -o ipc ipc.c
&amp;gt; ./ipc ipcs -q
------ Message Queues --------
key        msqid      owner      perms      used-bytes   message
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Done! The child process is now in a new IPC namespace and has completely separate view
and access to message queues.&lt;/p&gt;
&lt;h3&gt;PID Namespace&lt;/h3&gt;
&lt;p&gt;This one is fun. The PID namespace is a way to carve up the PIDs that one process can view and
interact with.  When we create a new PID namespace the first process will get to be the loved PID 1.
If this process exits the kernel kills everyone else within the namespace.
Let's start by making a copy of &lt;code&gt;skeleton.c&lt;/code&gt; for our changes.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&amp;gt; cp skeleton.c pid.c
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;To create a new PID namespace, we will have to set the &lt;code&gt;clone_flags&lt;/code&gt; with &lt;code&gt;CLONE_NEWPID&lt;/code&gt;.
The variable should look like &lt;code&gt;int clone_flags = CLONE_NEWPID | SIGCHLD;&lt;/code&gt;.  Let's test
by running &lt;code&gt;ps aux&lt;/code&gt; in our shell and then compile and run our &lt;code&gt;pid.c&lt;/code&gt; binary with
the same arguments.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&amp;gt; ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         &lt;span class="m"&gt;1&lt;/span&gt;  0.0  0.1  &lt;span class="m"&gt;20332&lt;/span&gt;  &lt;span class="m"&gt;3388&lt;/span&gt; ?        Ss   21:50   0:00 bash
root       &lt;span class="m"&gt;147&lt;/span&gt;  0.0  0.1  &lt;span class="m"&gt;17492&lt;/span&gt;  &lt;span class="m"&gt;2088&lt;/span&gt; ?        R+   22:49   0:00 ps aux
&amp;gt; gcc -o pid pid.c
&amp;gt; ./pid ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         &lt;span class="m"&gt;1&lt;/span&gt;  0.0  0.1  &lt;span class="m"&gt;20332&lt;/span&gt;  &lt;span class="m"&gt;3388&lt;/span&gt; ?        Ss   21:50   0:00 bash
root       &lt;span class="m"&gt;153&lt;/span&gt;  0.0  0.0   &lt;span class="m"&gt;5092&lt;/span&gt;   &lt;span class="m"&gt;728&lt;/span&gt; ?        S+   22:50   0:00 ./pid ps aux
root       &lt;span class="m"&gt;154&lt;/span&gt;  0.0  0.1  &lt;span class="m"&gt;17492&lt;/span&gt;  &lt;span class="m"&gt;2064&lt;/span&gt; ?        R+   22:50   0:00 ps aux
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;WTF???  We expected &lt;code&gt;ps aux&lt;/code&gt; to be PID 1 or atleast not see any other pids from the parent.
Why is that? &lt;strong&gt;proc&lt;/strong&gt;.  The process that we spawned still has a view of &lt;code&gt;/proc&lt;/code&gt; from the parent, i.e.
&lt;code&gt;/proc&lt;/code&gt; mounted on the host system.  So how do we fix this?  How to we ensure that our new process
can only view pids within it's namespace?  We can start by remounting &lt;code&gt;/proc&lt;/code&gt;.&lt;br /&gt;
Because we will be dealing with mounts, we can take the opportunity to take what we learned from the 
MNT namespace and combine it with our PID namespace so that we don't mess with the &lt;code&gt;/proc&lt;/code&gt; of our 
host system.&lt;/p&gt;
&lt;p&gt;We can start by including the clone flag for the mount namespace along side the
clone flag for pid.  It should look something like
&lt;code&gt;int clone_flags = CLONE_NEWPID | CLONE_NEWNS | SIGCHLD;&lt;/code&gt;.  We need to edit the 
&lt;code&gt;child_exec&lt;/code&gt; function and remount proc.  This will be a simple &lt;code&gt;unmount&lt;/code&gt; and &lt;code&gt;mount&lt;/code&gt; syscall
for the proc filesystem.  Because we are creating a new mount namespace we know that this will
not mess up our host system.  The result should look like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;// child_exec is the func that will be executed as the result of clone
static int child_exec&lt;span class="o"&gt;(&lt;/span&gt;void *stuff&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
        struct clone_args *args &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;struct clone_args *&lt;span class="o"&gt;)&lt;/span&gt;stuff&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;umount&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;/proc&amp;quot;&lt;/span&gt;, 0&lt;span class="o"&gt;)&lt;/span&gt; !&lt;span class="o"&gt;=&lt;/span&gt; 0&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                fprintf&lt;span class="o"&gt;(&lt;/span&gt;stderr, &lt;span class="s2"&gt;&amp;quot;failed unmount /proc %s\n&amp;quot;&lt;/span&gt;,
                        strerror&lt;span class="o"&gt;(&lt;/span&gt;errno&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="nb"&gt;exit&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;-1&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;mount&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;proc&amp;quot;&lt;/span&gt;, &lt;span class="s2"&gt;&amp;quot;/proc&amp;quot;&lt;/span&gt;, &lt;span class="s2"&gt;&amp;quot;proc&amp;quot;&lt;/span&gt;, 0, &lt;span class="s2"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; !&lt;span class="o"&gt;=&lt;/span&gt; 0&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                fprintf&lt;span class="o"&gt;(&lt;/span&gt;stderr, &lt;span class="s2"&gt;&amp;quot;failed mount /proc %s\n&amp;quot;&lt;/span&gt;,
                        strerror&lt;span class="o"&gt;(&lt;/span&gt;errno&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="nb"&gt;exit&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;-1&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;execvp&lt;span class="o"&gt;(&lt;/span&gt;args-&amp;gt;argv&lt;span class="o"&gt;[&lt;/span&gt;0&lt;span class="o"&gt;]&lt;/span&gt;, args-&amp;gt;argv&lt;span class="o"&gt;)&lt;/span&gt; !&lt;span class="o"&gt;=&lt;/span&gt; 0&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                fprintf&lt;span class="o"&gt;(&lt;/span&gt;stderr, &lt;span class="s2"&gt;&amp;quot;failed to execvp argments %s\n&amp;quot;&lt;/span&gt;,
                        strerror&lt;span class="o"&gt;(&lt;/span&gt;errno&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="nb"&gt;exit&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;-1&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        // we should never reach here!
        &lt;span class="nb"&gt;exit&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;EXIT_FAILURE&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Build and run this again to see what happens.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&amp;gt; gcc -o pid pid.c
&amp;gt; ./pid ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         &lt;span class="m"&gt;1&lt;/span&gt;  0.0  0.0   &lt;span class="m"&gt;9076&lt;/span&gt;   &lt;span class="m"&gt;784&lt;/span&gt; ?        R+   23:05   0:00 ps aux
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Perfect!  Our new PID namespace is now fully operational with the help of the mount namespace!&lt;/p&gt;
&lt;h3&gt;USER Namespace&lt;/h3&gt;
&lt;p&gt;The last namespace is the user namespace.  This namespace is the new kid on the block and allows you to have
users within this namespace that are not equal users outside of the namespace.  This is accomplished 
via GID and UID mappings.  &lt;/p&gt;
&lt;p&gt;This one has a simple demo application without specifying a mapping, even though it's a totally 
useless demo.  If we add the flag &lt;code&gt;CLONE_NEWUSER&lt;/code&gt; to our &lt;code&gt;clone_flags&lt;/code&gt; then run something like &lt;code&gt;id&lt;/code&gt; or &lt;code&gt;ls -la&lt;/code&gt; you will notice that we get &lt;code&gt;nobody&lt;/code&gt; within the user namespace.  This is because the current user is undefined
right now.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&amp;gt; cp skeleton.c user.c
&lt;span class="c"&gt;# add the clone flag&lt;/span&gt;
&amp;gt; gcc -o user user.c
&amp;gt; ./user ls -la
total 84
drwxr-xr-x &lt;span class="m"&gt;1&lt;/span&gt; nobody nogroup &lt;span class="m"&gt;4096&lt;/span&gt; Nov &lt;span class="m"&gt;16&lt;/span&gt; 23:10 .
drwxr-xr-x &lt;span class="m"&gt;1&lt;/span&gt; nobody nogroup &lt;span class="m"&gt;4096&lt;/span&gt; Nov &lt;span class="m"&gt;16&lt;/span&gt; 22:17 ..
-rwxr-xr-x &lt;span class="m"&gt;1&lt;/span&gt; nobody nogroup &lt;span class="m"&gt;8336&lt;/span&gt; Nov &lt;span class="m"&gt;16&lt;/span&gt; 22:15 mount
-rw-r--r-- &lt;span class="m"&gt;1&lt;/span&gt; nobody nogroup &lt;span class="m"&gt;1577&lt;/span&gt; Nov &lt;span class="m"&gt;16&lt;/span&gt; 22:15 mount.c
-rwxr-xr-x &lt;span class="m"&gt;1&lt;/span&gt; nobody nogroup &lt;span class="m"&gt;8064&lt;/span&gt; Nov &lt;span class="m"&gt;16&lt;/span&gt; 21:52 net
-rw-r--r-- &lt;span class="m"&gt;1&lt;/span&gt; nobody nogroup &lt;span class="m"&gt;1441&lt;/span&gt; Nov &lt;span class="m"&gt;16&lt;/span&gt; 21:52 network.c
-rwxr-xr-x &lt;span class="m"&gt;1&lt;/span&gt; nobody nogroup &lt;span class="m"&gt;8544&lt;/span&gt; Nov &lt;span class="m"&gt;16&lt;/span&gt; 23:05 pid
-rw-r--r-- &lt;span class="m"&gt;1&lt;/span&gt; nobody nogroup &lt;span class="m"&gt;1772&lt;/span&gt; Nov &lt;span class="m"&gt;16&lt;/span&gt; 23:02 pid.c
-rw-r--r-- &lt;span class="m"&gt;1&lt;/span&gt; nobody nogroup &lt;span class="m"&gt;1426&lt;/span&gt; Nov &lt;span class="m"&gt;16&lt;/span&gt; 21:59 skeleton.c
-rwxr-xr-x &lt;span class="m"&gt;1&lt;/span&gt; nobody nogroup &lt;span class="m"&gt;8056&lt;/span&gt; Nov &lt;span class="m"&gt;16&lt;/span&gt; 23:10 user
-rw-r--r-- &lt;span class="m"&gt;1&lt;/span&gt; nobody nogroup &lt;span class="m"&gt;1442&lt;/span&gt; Nov &lt;span class="m"&gt;16&lt;/span&gt; 23:10 user.c
-rwxr-xr-x &lt;span class="m"&gt;1&lt;/span&gt; nobody nogroup &lt;span class="m"&gt;8408&lt;/span&gt; Nov &lt;span class="m"&gt;16&lt;/span&gt; 22:40 uts
-rw-r--r-- &lt;span class="m"&gt;1&lt;/span&gt; nobody nogroup &lt;span class="m"&gt;1694&lt;/span&gt; Nov &lt;span class="m"&gt;16&lt;/span&gt; 22:36 uts.c
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This is a very simple example of the user namespace but you can go much deeper with it.  We will
save this for another post but the idea and hopes for the user namespace is that this will allow
us to run as "root" within the container but not as "root" on the host system.  Don't forget you 
can always change &lt;code&gt;ls -la&lt;/code&gt; to &lt;code&gt;bash&lt;/code&gt; and have a shell inside the namespace to poke around and learn
more.&lt;/p&gt;
&lt;h3&gt;In the end...&lt;/h3&gt;
&lt;p&gt;So to recap we went over the mount, network, user, PID, UTS, and IPC Linux namespaces.
The majority of the code that we changed was not much, just adding a flag most of the time.&lt;br /&gt;
The "hard work" is mostly managing the interactions between the various kernel subsystems 
in order to meet our requirements. Like most
of the descriptions before this, namespaces are just one of the tools that we use to make a
container.  I hope the PID example is a glimpse of how we use multiple namespaces together
in order isolate and begin the creation of a container.&lt;/p&gt;
&lt;p&gt;In future posts we will go into detail on how we jail the container's processes inside a root filesystem,
aka a docker image, as well as using cgroups and Linux capabilities.  By the end we should be able
to pull all these things together to create a container.&lt;/p&gt;
&lt;p&gt;Also a thanks to tibor and everyone that helps review my brain dump of a first draft ;)&lt;/p&gt;</summary></entry><entry><title>the lost packages of docker</title><link href="http://crosbymichael.com/the-lost-packages-of-docker.html" rel="alternate"></link><updated>2014-04-16T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2014-04-16:the-lost-packages-of-docker.html</id><summary type="html">&lt;p&gt;Docker is a large project that touches many different layers of the system.  Everything from a REST api to low level filesystem and execution calls.  Docker is also open source so where are all the awesome packages that make docker dock?  Why do the docker developers not contribute back reusable packages that I can import in my own projects?  Well we do and they are located in &lt;a href="https://github.com/dotcloud/docker/tree/master/pkg"&gt;pkg&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;While working on docker we try to keep the core as small as possible and break code out into general purpose packages that can be used in many different projects.  The hard part about this is that we cannot make separate git repos for the packages because of packaging reasons.  Right now it is still hard for distro maintainers to package and build Go applications because of the static nature of Go.  Everyone is doing the best that they can right now so while we work on the best way for distros to handle packaging our dependencies we place resuable Go packages in a &lt;code&gt;/pkg&lt;/code&gt; folder in the docker repository.  So what does docker have to offer? &lt;/p&gt;
&lt;hr /&gt;
&lt;h3&gt;netlink&lt;/h3&gt;
&lt;p&gt;We have a pure go implementation of netlink that you can use in your projects.  You can use this package to create veth interfaces, bridges, set ip, mtu, and other settings on network interfaces, move network interfaces into different linux namespaces, bla bla bla.  This is the same code that handles creating the veth pairs and assigning an ip to each of your docker containers.  &lt;/p&gt;
&lt;p&gt;Lets use this package to create bridge and assign an ip:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s"&gt;&amp;quot;github.com/dotcloud/docker/pkg/netlink&amp;quot;&lt;/span&gt;
        &lt;span class="s"&gt;&amp;quot;log&amp;quot;&lt;/span&gt;
        &lt;span class="s"&gt;&amp;quot;net&amp;quot;&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;create&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="n"&gt;bridge&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;netlink&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CreateBridge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;mydocker0&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;bridge&lt;/span&gt;
        &lt;span class="n"&gt;bridge&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;net&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;InterfaceByName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;mydocker0&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;ip&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ipNet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;net&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ParseCIDR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;10.0.41.1/16&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt; &lt;span class="n"&gt;an&lt;/span&gt; &lt;span class="n"&gt;ip&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;bridge&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;netlink&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NetworkLinkAddIp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bridge&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ip&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ipNet&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;bring&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;interface&lt;/span&gt; &lt;span class="n"&gt;up&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;netlink&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NetworkLinkUp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bridge&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;We can run the app above then run &lt;code&gt;ip a&lt;/code&gt; to see our new interface:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;2: mydocker0: &amp;lt;BROADCAST,MULTICAST,UP,LOWER_UP&amp;gt; mtu &lt;span class="m"&gt;1500&lt;/span&gt; qdisc noqueue state UP group default
    link/ether ae:1f:cb:af:f2:54 brd ff:ff:ff:ff:ff:ff
    inet 10.0.41.1/16 scope global mydocker0
    valid_lft forever preferred_lft forever
    inet6 fe80::ac1f:cbff:feaf:f254/64 scope link
    valid_lft forever preferred_lft forever
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The netlink package is not feature complete but hopefully not that you know it exists we can all work together on it to make it better.&lt;/p&gt;
&lt;h3&gt;user&lt;/h3&gt;
&lt;p&gt;Linux user and group functions that do not rely on external libs.&lt;/p&gt;
&lt;h3&gt;graphdb&lt;/h3&gt;
&lt;p&gt;Graphdb is a minimal graph database built ontop of sqlite for use with names and how they relate with one another.  It implements a small subset of what most graph databases have but provides a simple interface for expressing relationships between nodes.  This is used for container naming and linking inside docker.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s"&gt;&amp;quot;github.com/dotcloud/docker/pkg/graphdb&amp;quot;&lt;/span&gt;
        &lt;span class="s"&gt;&amp;quot;log&amp;quot;&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;graphdb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewSqliteConn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;/root/links.db&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;/parent&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;momma&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;child&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;/child&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;matt&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;otherKid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;/otherKid&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;koye&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt; &lt;span class="n"&gt;entity&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;matt&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;child&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;/parent/child&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;child&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;/parent/kidbyaothername&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;otherKid&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="nb"&gt;all&lt;/span&gt; &lt;span class="n"&gt;children&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;/parent&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;:::&lt;/span&gt;&lt;span class="n"&gt;bash&lt;/span&gt;
&lt;span class="o"&gt;~|&lt;/span&gt;&lt;span class="err"&gt;⇒&lt;/span&gt; &lt;span class="n"&gt;go&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="n"&gt;graph&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;go&lt;/span&gt;
&lt;span class="mi"&gt;2014&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mo"&gt;04&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;14&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;19&lt;/span&gt; &lt;span class="n"&gt;matt&lt;/span&gt;
&lt;span class="mi"&gt;2014&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mo"&gt;04&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;14&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;19&lt;/span&gt; &lt;span class="n"&gt;koye&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;h3&gt;listenbuffer&lt;/h3&gt;
&lt;p&gt;Listenbuffer allows you to immediately start listening on a unix, tcp, or udp socket but wait for your application to signal that it is ready before accepting connections.  &lt;/p&gt;
&lt;p&gt;This is used in docker so that you can start the daemon and a client request from an init script can be sent before the daemon has completed loading.  This ensures that the init script does not receive an error and your server can start accepting connections when it is ready.  It uses the kernel backlog for buffering the connections and is a super simple package that offers a lot of value if you have a long boot.&lt;/p&gt;
&lt;h3&gt;mount&lt;/h3&gt;
&lt;p&gt;Mount is a package to help you work with mounting lots of things.  It allows options to be passed fstab style and also offers mount table parsing functions so that your applications can easily find mountpoints.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;// do a readonly bind mount
if err := mount.Mount(&amp;quot;/myfile&amp;quot;, &amp;quot;/myotherfile&amp;quot;, &amp;quot;none&amp;quot;, &amp;quot;bind,ro&amp;quot;); err != nil {
    log.Fatal(err)
}
defer mount.Unmount(&amp;quot;/myotherfile&amp;quot;)
&lt;/pre&gt;&lt;/div&gt;


&lt;h3&gt;term&lt;/h3&gt;
&lt;p&gt;Term handles terminal sizing, raw mode, and other settings for interacting with a unix terminal on linux, darwin, and bsd. &lt;/p&gt;
&lt;h3&gt;cgroups&lt;/h3&gt;
&lt;p&gt;This package is currently in heavy development right now but it provides the cgroup functionality for libcontainer via the raw cgroup filesystem api and also the systemd api where supported.&lt;/p&gt;
&lt;h3&gt;libcontainer&lt;/h3&gt;
&lt;p&gt;Pkg is also where libcontainer resides.  It is the default execution driver for running docker containers.  It sets up namespaces, networking, mounts, drops caps, and manages the container processes.  You provide a root filesystem and a configuration on how libcontainer is supposed to execute a container and it does the rest.  It allows spawning new containers or attaching to an existing container.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;So in the end why did I write this post?  I wanted to bring some visibility to these packages.  While other projects are able to have separate git repositories for the different dependencies that they use and develop, docker has to keep these in it's main repository causing many people to not know that they exist.  So if some of these packages seem interesting to you, feel free to use them in your projects, contribute, write docs, tests, and make them awesome.  &lt;/p&gt;
&lt;p&gt;If you know of an interesting area currently in the docker code base that is not in a reusable package, submit a PR to make it one.  We are always looking for ways to make the docker core smaller and our work useful to others.&lt;/p&gt;</summary></entry><entry><title>Dockerfile Best Practices - take 2</title><link href="http://crosbymichael.com/dockerfile-best-practices-take-2.html" rel="alternate"></link><updated>2014-03-09T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2014-03-09:dockerfile-best-practices-take-2.html</id><summary type="html">&lt;p&gt;Much has changed since my first Dockerfile best practices post.  I'll leave the original post up for posterity and this post will include what has change and what you should do now.  &lt;/p&gt;
&lt;h3&gt;1: Don't boot init&lt;/h3&gt;
&lt;p&gt;Containers model processes not machines.  Even if you think that you need to do this you are probably wrong.  Next...&lt;/p&gt;
&lt;h3&gt;2: Trusted builds&lt;/h3&gt;
&lt;p&gt;Even if you don't like the name it is an awesome feature.  I have most of my github repositories added to trusted builds so that when I push a commit my new image is waiting on the index shortly after.  Also, I don't have to have separate repositories with the Dockerfile to share with others, it is all in one place.&lt;/p&gt;
&lt;p&gt;Just remember that this is not your playground for trying things out.  Build locally first before you push.  Docker ensures that what you build and run locally will build and run the same way when pushed elsewhere.  Develop and test locally, commit and push, and wait for the official image on the index with trusted builds.  &lt;/p&gt;
&lt;h3&gt;3: Don't upgrade in builds&lt;/h3&gt;
&lt;p&gt;Updates will be baked into the based images you don't need to &lt;code&gt;apt-get upgrade&lt;/code&gt; your containers.  Because of the isolation that happens this can often fail if something is trying to modify init or make device changes inside a container.  It also produces inconsistent images because you no longer have one source of truth of how your application should run and what versions of dependencies are included in the image.  &lt;/p&gt;
&lt;p&gt;If there are security updates that a base image needs, let upstream know so that they can update it for everyone and ensure that your builds are consistent again.&lt;/p&gt;
&lt;h3&gt;4: Use small base images&lt;/h3&gt;
&lt;p&gt;Some images are more bloated than others.  I suggest using &lt;code&gt;debian:jessie&lt;/code&gt; as your base.  If you are coming from ubuntu, you will find a more lightweight and happy home on debian.  It's small and does not contain any unneeded bloat.&lt;/p&gt;
&lt;h3&gt;5: Use specific tags&lt;/h3&gt;
&lt;p&gt;This is  important for your base images.  Your &lt;strong&gt;FROM&lt;/strong&gt; should always contain the full repository and tag for the base image that you are depending on.  &lt;code&gt;FROM debian:jessie&lt;/code&gt; not just &lt;code&gt;FROM debian&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;6: Group common operations&lt;/h3&gt;
&lt;p&gt;Your &lt;code&gt;apt-get update&lt;/code&gt; should be grouped with your &lt;code&gt;apt-get install&lt;/code&gt;.  Also, you should take advantage of the &lt;code&gt;\&lt;/code&gt; to span multiple lines on your installs.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c"&gt;# Dockerfile for https://index.docker.io/u/crosbymichael/python/ &lt;/span&gt;
FROM debian:jessie

RUN apt-get update &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; apt-get install -y &lt;span class="se"&gt;\&lt;/span&gt;
    git &lt;span class="se"&gt;\&lt;/span&gt;
    libxml2-dev &lt;span class="se"&gt;\&lt;/span&gt;
    python &lt;span class="se"&gt;\&lt;/span&gt;
    build-essential &lt;span class="se"&gt;\&lt;/span&gt;
    make &lt;span class="se"&gt;\&lt;/span&gt;
    gcc &lt;span class="se"&gt;\&lt;/span&gt;
    python-dev &lt;span class="se"&gt;\&lt;/span&gt;
    locales &lt;span class="se"&gt;\&lt;/span&gt;
    python-pip

RUN dpkg-reconfigure locales &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
    locale-gen C.UTF-8 &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
    /usr/sbin/update-locale &lt;span class="nv"&gt;LANG&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;C.UTF-8

ENV LC_ALL C.UTF-8
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Just remember that layers and the cache are good.  Don't be scared of having many layers because the cache is a life saver.  Also you should use upstream packages when you can.&lt;/p&gt;
&lt;h3&gt;7: Use your own base images&lt;/h3&gt;
&lt;p&gt;I'm not talking about running debbootstrap and making your own &lt;strong&gt;debian&lt;/strong&gt;, you're not tianon and no one wants your shitty base image with 500mb of shit that you think ppl need.  I'm talking about if you are running python applications have a python base image.  The Dockerfile in the previous example is used to build &lt;code&gt;crosbymichael/python&lt;/code&gt; that is used in many other images for python applications.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;FROM crosbymichael/python

RUN pip install butterfly
RUN &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;root\nroot\n&amp;quot;&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; passwd root

EXPOSE 9191
ENTRYPOINT &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;butterfly.server.py&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
CMD &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;--port=9191&amp;quot;&lt;/span&gt;, &lt;span class="s2"&gt;&amp;quot;--host=0.0.0.0&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Or another:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;FROM crosbymichael/python

RUN pip install --upgrade youtube_dl &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; mkdir /download
WORKDIR /download
ENTRYPOINT &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;youtube-dl&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
CMD &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;--help&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;As you can see this makes Dockerfiles that use your base very small and focused on the application that we are installing install.&lt;/p&gt;
&lt;p&gt;Let me know what you think or if you have any other questions.  &lt;/p&gt;</summary></entry><entry><title>Go Dynamic Plugins</title><link href="http://crosbymichael.com/go-dynamic-plugins.html" rel="alternate"></link><updated>2013-09-23T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2013-09-23:go-dynamic-plugins.html</id><summary type="html">&lt;p&gt;For me, my favorite feature of Go is the static binary.  I love compiling once and just dropping the binary into a container running the 2mb busybox image and having my app up and running.  No pip, no dlls, no problem.  However, this does come with a small price.  If you want to dynamically extend your Go app at runtime with plugins your SOL.&lt;/p&gt;
&lt;p&gt;If you look at the internals of the &lt;code&gt;database/sql&lt;/code&gt; package in Go you can see how they handle different database driver support but that is still resolved at build time.  A few people are handling plugins for Go via RPC, shelling out to another process, or what not, but all of those solutions still have their own issues to work through.  If you want to keep your plugins in the same process and have a natural feel then I think this solution is worth a look.&lt;/p&gt;
&lt;h2&gt;otto&lt;/h2&gt;
&lt;p&gt;There is a really cool open source project called &lt;a href="https://github.com/robertkrimen/otto"&gt;otto&lt;/a&gt; that implements a javascript runtime in pure Go.  This allows you to embed js in your Go apps very easily.  Yes, yes, I know it's javascript but it's not that bad when used in moderation.  &lt;/p&gt;
&lt;p&gt;Anyways, otto allows you to create a js runtime in your Go application and execute js, allow js to call go functions that were added to the runtime, and pass data to and from a javascript function.  In the example below I will be making a simple http server that returns a string depending if you are authorized or not.  The interesting part is that we will be passing the &lt;code&gt;http.Request&lt;/code&gt; to a javascript function to do runtime checks.  This allows us to change the code checking the request without stopping and rebuilding our Go code.&lt;/p&gt;
&lt;h2&gt;The app&lt;/h2&gt;
&lt;p&gt;Lets start by looking at our main func that will handle the web requests.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;func main() {
    http.HandleFunc(&amp;quot;/&amp;quot;, func(w http.ResponseWriter, r *http.Request) {
        if checkRequest(r) {
            fmt.Fprintf(w, &amp;quot;Welcome in\n&amp;quot;)
        } else {
            w.WriteHeader(http.StatusUnauthorized)
            fmt.Fprintf(w, &amp;quot;Your not allowed!\n&amp;quot;)
        }
    })

    if err := http.ListenAndServe(&amp;quot;:8080&amp;quot;, nil); err != nil {
        panic(err)
    }
}
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This is very standard and simple.  In the handler we make a call to &lt;code&gt;checkRequest&lt;/code&gt; to tell us if the request is valid or not.  Now lets look at the checkRequest func.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nx"&gt;func&lt;/span&gt; &lt;span class="nx"&gt;checkRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;runtime&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nx"&gt;loadPluginRuntime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;plugins.js&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;// If we don&amp;#39;t have a runtime all requests are accepted&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nx"&gt;runtime&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nx"&gt;runtime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ToValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nx"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// By convention we will require plugins have a set name&lt;/span&gt;
    &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nx"&gt;runtime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;checkRequest&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nx"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// If the js function did not return a bool error out&lt;/span&gt;
    &lt;span class="c1"&gt;// because the plugin is invalid&lt;/span&gt;
    &lt;span class="nx"&gt;out&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ToBoolean&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nx"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Fatalf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;\&amp;quot;checkRequest\&amp;quot; must return a boolean. Got %s&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;out&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This is where we interface with the js runtime.  We load the runtime in another func that I will show next.  If we don't have any plugins we will receive a nil runtime so we just return true.  If we do have a runtime then we need to convert our Go type ( the request ) into a js Value/Object so that we can pass it to the plugin method.  Right now otto has a issue with pointer values as js Objects so I dereference the request when passing to the &lt;code&gt;ToValue&lt;/code&gt; method.&lt;/p&gt;
&lt;p&gt;Next we call the js function &lt;code&gt;checkRequest&lt;/code&gt; and receive it's return value.  In order to call js functions we need to know the name of the function in our Go code.  I don't think that this is an issue because when people want to implement plugins for your application they will need to stick to certain naming conventions and interfaces in order for the two applications to interact together.  &lt;/p&gt;
&lt;p&gt;Now we need to take the return value from the js function and convert it back into a Go type that our program can understand.  We expect the &lt;code&gt;checkRequest&lt;/code&gt; function to return a bool so we use the &lt;code&gt;ToBoolean&lt;/code&gt; method to do the conversion.  If we get an err, it is probably because the js function did not return the correct type or nothing at all. &lt;/p&gt;
&lt;p&gt;Let's quickly look at the &lt;code&gt;loadPluginRuntime&lt;/code&gt; func.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;func loadPluginRuntime(name string) *otto.Otto {
    f, err := os.Open(name)
    if err != nil {
        if os.IsNotExist(err) {
            return nil
        }
        log.Fatal(err)
    }
    defer f.Close()
    buff := bytes.NewBuffer(nil)

    if _, err := buff.ReadFrom(f); err != nil {
        log.Fatal(err)
    }
    runtime := otto.New()
    // Load the plugin file into the runtime before we
    // return it for use
    if _, err := runtime.Run(buff.String()); err != nil {
        log.Fatal(err)
    }
    return runtime
}
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;In the load func we load a file with all the js functions and set them in the runtime.  Because this is an example I am run reading the file on each request.  In a prod system you will want to do something fancy but for this example it is fine.  Anyways, we load the file and then create the new runtime.  We use the &lt;code&gt;Run&lt;/code&gt; method and pass the contents of the file as a string so that our functions are loaded.&lt;/p&gt;
&lt;h2&gt;The Plugin&lt;/h2&gt;
&lt;p&gt;Lets checkout the &lt;code&gt;plugin.js&lt;/code&gt; file to see what the javascript implementation is to check the request.  &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;checkRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Only allow GET requests to the API&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Method&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;GET&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;And that is it.  We access the Method on the http.Request and check to make sure it is a GET request and not allow anything else.  You can change the plugin.js file and watch the responses change as you toggle between true and false.&lt;/p&gt;
&lt;p&gt;Otto is still new so I hope people will check out the project and contribute bug fixes and improvements so that otto can be a viable option for dynamic plugins in Go.  Of course you can also embed lua or v8 into your Go binary instead.  There are many options and this is only one.  &lt;/p&gt;
&lt;p&gt;I am also interested in embedding lua into Go.  I have notice a few large open source projects like Nginx and Redis embedding lua so why not give it a try.  Thanks and let me know what you think in the comments below.&lt;/p&gt;
&lt;h2&gt;Full App&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;bytes&amp;quot;&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;fmt&amp;quot;&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;github.com/robertkrimen/otto&amp;quot;&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;log&amp;quot;&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;net/http&amp;quot;&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;os&amp;quot;&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;Load&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;plugin&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;  &lt;span class="n"&gt;If&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt; &lt;span class="n"&gt;does&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;exist&lt;/span&gt;
&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;then&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="n"&gt;runtime&lt;/span&gt;
&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="n"&gt;loadPluginRuntime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;otto&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Otto&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsNotExist&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;buff&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewBuffer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;buff&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ReadFrom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;runtime&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;otto&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;Load&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;plugin&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt; &lt;span class="n"&gt;into&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;runtime&lt;/span&gt; &lt;span class="n"&gt;before&lt;/span&gt; &lt;span class="n"&gt;we&lt;/span&gt;
    &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;use&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;runtime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buff&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;runtime&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="n"&gt;checkRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;runtime&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;loadPluginRuntime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;plugins.js&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;If&lt;/span&gt; &lt;span class="n"&gt;we&lt;/span&gt; &lt;span class="n"&gt;don&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;t have a runtime all requests are accepted&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;runtime&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;runtime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ToValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;By&lt;/span&gt; &lt;span class="n"&gt;convention&lt;/span&gt; &lt;span class="n"&gt;we&lt;/span&gt; &lt;span class="n"&gt;will&lt;/span&gt; &lt;span class="n"&gt;require&lt;/span&gt; &lt;span class="n"&gt;plugins&lt;/span&gt; &lt;span class="n"&gt;have&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;runtime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;checkRequest&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;If&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;js&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="n"&gt;did&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;
    &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;because&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;plugin&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;invalid&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ToBoolean&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatalf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;&lt;span class="se"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="s"&gt;checkRequest&lt;/span&gt;&lt;span class="se"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="s"&gt; must return a boolean. Got &lt;/span&gt;&lt;span class="si"&gt;%s&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;/&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;checkRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fprintf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;Welcome in&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusUnauthorized&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fprintf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;Your not allowed!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ListenAndServe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;:8080&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;</summary></entry><entry><title>Advanced Docker Volumes</title><link href="http://crosbymichael.com/advanced-docker-volumes.html" rel="alternate"></link><updated>2013-09-14T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2013-09-14:advanced-docker-volumes.html</id><summary type="html">&lt;p&gt;I have noticed that many people have a hard time understanding what volumes are and how to use them effectively.  &lt;/p&gt;
&lt;p&gt;So what is a volume?  A volume can be a directory that is located outside of the root filesystem of your container.  This allows you to import this directory in other containers.  You can also use volumes to mount directories from your host machine inside a container.  &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c"&gt;# create a new volume in /www&lt;/span&gt;
docker run -v /www ubuntu &lt;span class="nb"&gt;echo &lt;/span&gt;yo

&lt;span class="c"&gt;# create a new volume mounted to your host machine&lt;/span&gt;
docker run -v /host/logs:/container/logs ubuntu &lt;span class="nb"&gt;echo &lt;/span&gt;momma
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;So pretty simple, right?  So what actually happens under the hood when you specify a volume?&lt;/p&gt;
&lt;h3&gt;Under the hood&lt;/h3&gt;
&lt;p&gt;So with the command &lt;strong&gt;docker run -v /www ubuntu echo yo&lt;/strong&gt; a container is created in &lt;code&gt;/var/lib/docker/containers&lt;/code&gt; with the rootfs setup for COW from the ubuntu image.  Then a new volume is created in &lt;code&gt;/var/lib/docker/volumes&lt;/code&gt; for the data that is placed in &lt;code&gt;/www&lt;/code&gt; for the container.  We can run &lt;strong&gt;docker inspect &lt;id&gt;&lt;/strong&gt; to see the volume configuration.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="err"&gt;...&lt;/span&gt; &lt;span class="err"&gt;stufff&lt;/span&gt;
    &lt;span class="s2"&gt;&amp;quot;HostnamePath&amp;quot;&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/var/lib/docker/containers/831d4ecbdf2e096475365b019b211f74aff7a3cea3c443570e1fd2f2bb0dc843/hostname&amp;quot;&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;&amp;quot;HostsPath&amp;quot;&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/var/lib/docker/containers/831d4ecbdf2e096475365b019b211f74aff7a3cea3c443570e1fd2f2bb0dc843/hosts&amp;quot;&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;&amp;quot;Volumes&amp;quot;&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;quot;/www&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/var/lib/docker/volumes/ec3c543bc92f114c2c568733541e89381881e5a62996d7084e07793f86280535/layer&amp;quot;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;&amp;quot;VolumesRW&amp;quot;&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;quot;/www&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;By inspecting the container we can see that we have a new volume and the full path to the volume's data on disk.  You will also notice the field &lt;strong&gt;VolumesRW&lt;/strong&gt; denoting that this volume is Read/Write.&lt;/p&gt;
&lt;p&gt;This simple from the user's perspective.  You just specify a &lt;strong&gt;-v&lt;/strong&gt; with the directory that you want to use as a volume and your done.  Now what's up with mounting directories from the host system?  &lt;/p&gt;
&lt;h4&gt;Host mounts&lt;/h4&gt;
&lt;p&gt;There are a few use cases where you want to mount a directory from your host system into the container.  Just remember that this is not portable.  You may have a specific directory structure on your development machine that is not on your production servers causing issues when you try to move the container.  Many people also feel like they need to mount the data directory of their database container to the host machine so that it can be backed up.  However, you can see all the files for your "normal" volumes are stored in &lt;code&gt;/var/lib/docker/volumes&lt;/code&gt; on the host filesystem.  Docker containers are not like VM's where your data is not locked into some disk image.  So usually mounting to the host is not needed.&lt;/p&gt;
&lt;h3&gt;Volumes From&lt;/h3&gt;
&lt;p&gt;Ok, so now we have a container running with a volume specified.  What can we do with it?  For this example lets say that we have container A that is a web frontend and it writes logs to &lt;code&gt;/logs/webapp&lt;/code&gt;.  When we ran the frontend container we specified &lt;code&gt;-v /logs/webapp&lt;/code&gt; to create a volume.  Next we need some type of process to collect the logs and push them into a central repository.  We can have container B start and reference the volume from A to gain access to it's logs.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;docker run -volumes-from A log-daemon collect
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;We use the &lt;strong&gt;-volumes-from&lt;/strong&gt; flag and pass the container's ID to gain access to it's volumes.  Now our daemon can cd into &lt;code&gt;/logs/webapp&lt;/code&gt; and start processing the logs.  Easy, simple, and boring.  Now lets do something cool.&lt;/p&gt;
&lt;h3&gt;The cool stuff&lt;/h3&gt;
&lt;p&gt;So how can we push volumes to the limit?  I currently have docker running a minecraft server.  I have a minecraft server instance running and the directory with all the world data is located in &lt;code&gt;/minecraft&lt;/code&gt;.  There is another cool project called &lt;a href="http://overviewer.org/"&gt;Overviewer&lt;/a&gt; that will generate a "google map" like, uhh, map of your world as an html site.  You give &lt;strong&gt;overviewer.py&lt;/strong&gt; the path to your world files and an output directory to write the result to.&lt;/p&gt;
&lt;p&gt;So this stack has three components.  The actual game server, a worker that takes data from the server and generates html, and a web server to serve the static file output of the map.  What docker and volumes allows us to do is to separate our concerns so that the web server does not have to be in the same container as the game server and the worker that generates the map does not have to run in the same container as the web or game servers.  Just like functions and classes in your code, containers should do one thing and do that well.&lt;/p&gt;
&lt;h4&gt;WebServer (web frontend)&lt;/h4&gt;
&lt;p&gt;So lets look at how the web server is built and configured.  It is a static file server that just serves files out of a directory to a specified port and ip.  If you want to see the web server code look &lt;a href="https://github.com/crosbymichael/.dotfiles/blob/master/bin/server.go"&gt;here&lt;/a&gt;.  &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;FROM busybox

ADD server server

VOLUME /www
USER daemon
EXPOSE 8000

ENTRYPOINT &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;/server&amp;quot;&lt;/span&gt;, &lt;span class="s2"&gt;&amp;quot;-h&amp;quot;&lt;/span&gt;, &lt;span class="s2"&gt;&amp;quot;0.0.0.0&amp;quot;&lt;/span&gt;, &lt;span class="s2"&gt;&amp;quot;-p&amp;quot;&lt;/span&gt;, &lt;span class="s2"&gt;&amp;quot;8000&amp;quot;&lt;/span&gt;, &lt;span class="s2"&gt;&amp;quot;-dir&amp;quot;&lt;/span&gt;, &lt;span class="s2"&gt;&amp;quot;/www&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The server knows nothing about the stack that it is running in or what it is serving.  it just serves files located in &lt;code&gt;/www&lt;/code&gt; ( which is a volume ) and nothing more.  Simple and clean.&lt;/p&gt;
&lt;h4&gt;Map Generator (worker)&lt;/h4&gt;
&lt;p&gt;Now lets look at our worker.  &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;FROM ubuntu

RUN &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;deb http://archive.ubuntu.com/ubuntu precise main universe&amp;quot;&lt;/span&gt; &amp;gt; /etc/apt/sources.list
RUN apt-get update
RUN apt-get upgrade -y

RUN &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;deb http://overviewer.org/debian ./&amp;quot;&lt;/span&gt; &amp;gt;&amp;gt; /etc/apt/sources.list
RUN apt-get update
RUN apt-get install -y --force-yes minecraft-overviewer wget

ENV VERSION 1.6.2

RUN wget --no-check-certificate https://s3.amazonaws.com/Minecraft.Download/versions/&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;VERSION&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;/&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;VERSION&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;.jar -P ~/.minecraft/versions/&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;VERSION&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;/

ENTRYPOINT &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;overviewer.py&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
CMD &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;/minecraft/world&amp;quot;&lt;/span&gt;, &lt;span class="s2"&gt;&amp;quot;/www&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The worker has a few more dependencies but this container does it's one thing.  Get world files from &lt;code&gt;/minecraft/world&lt;/code&gt;, create the map, and write the output to &lt;code&gt;/www&lt;/code&gt;.  This worker knows nothing about where or how it gets the files to process, it just processes world files into maps from it's local filesystem.&lt;/p&gt;
&lt;h4&gt;Minecraft server&lt;/h4&gt;
&lt;p&gt;Finally where the fun happens.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;FROM ubuntu

RUN &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;deb http://archive.ubuntu.com/ubuntu precise main universe&amp;quot;&lt;/span&gt; &amp;gt; /etc/apt/sources.list
RUN apt-get update
RUN apt-get upgrade -y

RUN apt-get install -y python-software-properties
RUN add-apt-repository ppa:webupd8team/java -y

RUN apt-get update
RUN &lt;span class="nb"&gt;echo &lt;/span&gt;oracle-java7-installer shared/accepted-oracle-license-v1-1 &lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; /usr/bin/debconf-set-selections

RUN apt-get install -y oracle-java7-installer
RUN apt-get install -y wget

RUN mkdir /minecraft
RUN wget -O /minecraft/minecraft.jar https://s3.amazonaws.com/Minecraft.Download/versions/1.6.2/minecraft_server.1.6.2.jar
RUN chmod +x /minecraft/minecraft.jar

VOLUME /minecraft
WORKDIR /minecraft
EXPOSE 25565

CMD java -Xmx1600M -Xms768M -jar minecraft.jar nogui
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;We run the minecraft.jar and use &lt;code&gt;/minecraft&lt;/code&gt; as our current working dir.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;Now we have our containers created and they have clear boundaries.  They are only concerned about doing their one job and have no frame of reference where they are located in the stack.  &lt;/p&gt;
&lt;h3&gt;Runtime&lt;/h3&gt;
&lt;p&gt;So now how do we glue this stack together with volumes?  We can start by running the minecraft and web servers.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nv"&gt;MINECRAFT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;$(&lt;/span&gt;docker run -d minecraft&lt;span class="k"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;MAPSERVER&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;$(&lt;/span&gt;docker run -d mapserver&lt;span class="k"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now that we have our servers running we use the worker to bridge both of them together with volumes.  Because we exposed &lt;code&gt;/minecraft&lt;/code&gt; with the world data from the &lt;strong&gt;MINECRAFT&lt;/strong&gt; container and &lt;code&gt;/www&lt;/code&gt; where static files are served from the &lt;strong&gt;MAPSERVER&lt;/strong&gt; container we need to tell the worker/generator to use the &lt;code&gt;-volumes-from&lt;/code&gt; both containers.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;docker run -volumes-from &lt;span class="nv"&gt;$MINECRAFT&lt;/span&gt; -volumes-from &lt;span class="nv"&gt;$MAPSERVER&lt;/span&gt; mapgenerator
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;img alt="Cloud" src="static/images/docker-volumes.png" /&gt;&lt;/p&gt;
&lt;p&gt;Just like magic the mapgenerator will pull the data out of &lt;code&gt;/minecraft/world&lt;/code&gt; and write the output into &lt;code&gt;/www&lt;/code&gt;, and once it is done we can remove the generator container because it contains zero data that we need.  It is an ephemeral processes that takes input and produces an output with no state in the middle.&lt;/p&gt;
&lt;p&gt;If you want the map to be generated every hour ( like I do ) you can just run the same command in a cron job.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;@hourly docker run -volumes-from &lt;span class="nv"&gt;$MINECRAFT&lt;/span&gt; -volumes-from &lt;span class="nv"&gt;$MAPSERVER&lt;/span&gt; mapgenerator
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Using volumes with this stack allows me to not worry about the generator process locking up the main game server, causing crashes, leaking memory and leaving artifacts on disk.  The game server runs and serves up &lt;a href="http://www.southparkstudios.com/full-episodes/s12e07-super-fun-time"&gt;super-fun-times&lt;/a&gt;. The web server just serves files from one directory and does not need to be bothered.  And the generator just takes input and produces output.  &lt;/p&gt;
&lt;p&gt;Docker does all the work while each container interacts with it's own filesystem and does not know the difference.  We don't have to mess with or restart the web or game servers.  It's almost like dependency injection but with an entire filesystem which is really cool.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Warning: this post has features of docker that are not in a current release.  Look for these features in 0.6.2&lt;/em&gt;&lt;/p&gt;</summary></entry><entry><title>Docker Events</title><link href="http://crosbymichael.com/docker-events.html" rel="alternate"></link><updated>2013-08-02T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2013-08-02:docker-events.html</id><summary type="html">&lt;p&gt;The proper way to do port allocation in docker is to &lt;code&gt;EXPOSE&lt;/code&gt; a private port in a container and let docker map the public port on the host.  This can be a pain for reverse proxies, load balances, etc but with the &lt;a href="http://docs.docker.io/en/latest/api/docker_remote_api_v1.3/#monitor-docker-s-events"&gt;events endpoint&lt;/a&gt; in docker you can listen for containers to start and inspect their port mappings.&lt;/p&gt;
&lt;p&gt;Here is a very simple Go app as an example on how to utilize this endpoint.&lt;/p&gt;
&lt;h3&gt;Code:&lt;/h3&gt;
&lt;p&gt;The first thing that we want to do is to add a few imports.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;encoding/json&amp;quot;&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;io&amp;quot;&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;log&amp;quot;&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;net/http&amp;quot;&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;We will need &lt;code&gt;json&lt;/code&gt; for decoding the information from the api, &lt;code&gt;io&lt;/code&gt; for &lt;code&gt;io.EOF&lt;/code&gt;, &lt;code&gt;log&lt;/code&gt; to print to stdout, and &lt;code&gt;http&lt;/code&gt; for the Client.&lt;/p&gt;
&lt;p&gt;Now lets create a few types to store our information.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;type Event struct {
    Id     string `json:&amp;quot;id&amp;quot;`
    Status string `json:&amp;quot;status&amp;quot;`
}

type Config struct {
    Hostname string
}

type NetworkSettings struct {
    IpAddress   string
    PortMapping map[string]map[string]string
}

type Container struct {
    Id              string
    Image           string
    Config          *Config
    NetworkSettings *NetworkSettings
}
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The api will return much more container information but I only care about a few fields and types in this example.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;func main() {
    c := http.Client{}
    res, err := c.Get(&amp;quot;http://localhost:4243/events&amp;quot;)
    if err != nil {
        log.Fatal(err)
    }
    defer res.Body.Close()

    // Read the streaming json from the events endpoint
    // http://docs.docker.io/en/latest/api/docker_remote_api_v1.3/#monitor-docker-s-events
    d := json.NewDecoder(res.Body)
    for {
        var event Event
        if err := d.Decode(&amp;amp;event); err != nil {
            if err == io.EOF {
                break
            }
            log.Fatal(err)
        }
        if event.Status == &amp;quot;start&amp;quot; {
            // We only want to inspect the container if it has started
            if container := inspectContainer(event.Id, c); container != nil {
                notify(container)
            }
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This is my &lt;code&gt;main()&lt;/code&gt; func.  We create a new http.Client that will be used to make GET requests to the docker api.  The first request that we make is to the events endpoint.  This will open a stream to our app writing json objects when events happen in the docker daemon.  We create a new json decoder to read from the Body of the response and decode Event types.  After we decode the event we only care if the container has been started so we use the &lt;code&gt;status&lt;/code&gt; field.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;func inspectContainer(id string, c http.Client) *Container {
    // Use the container id to fetch the container json from the Remote API
    // http://docs.docker.io/en/latest/api/docker_remote_api_v1.4/#inspect-a-container
    res, err := c.Get(&amp;quot;http://localhost:4243/containers/&amp;quot; + id + &amp;quot;/json&amp;quot;)
    if err != nil {
        log.Println(err)
        return nil
    }
    defer res.Body.Close()

    if res.StatusCode == http.StatusOK {
        d := json.NewDecoder(res.Body)

        var container Container
        if err = d.Decode(&amp;amp;container); err != nil {
            log.Fatal(err)
        }
        return &amp;amp;container
    }
    return nil
}
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now that a container has started we use the &lt;code&gt;id&lt;/code&gt; field from the event to then make another request to the docker api for the container information.  We use another json decoder but this time we decode the json result returned from the api into the Container type.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;func notify(container *Container) {
    settings := container.NetworkSettings

    if settings != nil &amp;amp;&amp;amp; settings.PortMapping != nil {
        // I only care about Tcp ports but you can also view Udp mappings
        if ports, ok := settings.PortMapping[&amp;quot;Tcp&amp;quot;]; ok {

            log.Printf(&amp;quot;Ip address allocated for: %s&amp;quot;, container.Id)

            // Log the public and private port mappings
            for privatePort, publicPort := range ports {
                // I am just writing to stdout but you can use this information to update hipache, redis, etc...
                log.Printf(&amp;quot;%s -&amp;gt; %s&amp;quot;, privatePort, publicPort)
            }
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;So now that we have the container information that we care about we need to get that data and make it useful.  All we need to do is to look at the NetworkSettings for the PortMappings to inspect Tcp ports.  You will receive a map where the key is the private port and the value is the public port.  I am just writing the values to stdout but you could use this data to update hipache for a new container, write nginx config changes, etc...  The possibilities are endless.  &lt;/p&gt;
&lt;p&gt;Let me know if you have any questions or comments below.&lt;/p&gt;
&lt;h3&gt;Full code:&lt;/h3&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;encoding/json&amp;quot;&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;io&amp;quot;&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;log&amp;quot;&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;net/http&amp;quot;&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nb"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Event&lt;/span&gt; &lt;span class="n"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Id&lt;/span&gt;     &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="sb"&gt;`json:&amp;quot;id&amp;quot;`&lt;/span&gt;
    &lt;span class="n"&gt;Status&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="sb"&gt;`json:&amp;quot;status&amp;quot;`&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nb"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Config&lt;/span&gt; &lt;span class="n"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Hostname&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nb"&gt;type&lt;/span&gt; &lt;span class="n"&gt;NetworkSettings&lt;/span&gt; &lt;span class="n"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;IpAddress&lt;/span&gt;   &lt;span class="n"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;PortMapping&lt;/span&gt; &lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nb"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Container&lt;/span&gt; &lt;span class="n"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Id&lt;/span&gt;              &lt;span class="n"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;Image&lt;/span&gt;           &lt;span class="n"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;Config&lt;/span&gt;          &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Config&lt;/span&gt;
    &lt;span class="n"&gt;NetworkSettings&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;NetworkSettings&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="n"&gt;inspectContainer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Container&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;Use&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;container&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;fetch&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;container&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt; &lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;the&lt;/span&gt; &lt;span class="nn"&gt;Remote&lt;/span&gt; &lt;span class="nn"&gt;API&lt;/span&gt;
    &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="n"&gt;docs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;docker&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;io&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;en&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;latest&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;docker_remote_api_v1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="c"&gt;#inspect-a-container&lt;/span&gt;
    &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;http://localhost:4243/containers/&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/json&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Body&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusCode&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusOK&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewDecoder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;container&lt;/span&gt; &lt;span class="n"&gt;Container&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;container&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;container&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="n"&gt;notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;container&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Container&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;settings&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;container&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NetworkSettings&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;settings&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PortMapping&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="n"&gt;only&lt;/span&gt; &lt;span class="n"&gt;care&lt;/span&gt; &lt;span class="n"&gt;about&lt;/span&gt; &lt;span class="n"&gt;Tcp&lt;/span&gt; &lt;span class="n"&gt;ports&lt;/span&gt; &lt;span class="n"&gt;but&lt;/span&gt; &lt;span class="n"&gt;you&lt;/span&gt; &lt;span class="n"&gt;can&lt;/span&gt; &lt;span class="n"&gt;also&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt; &lt;span class="n"&gt;Udp&lt;/span&gt; &lt;span class="n"&gt;mappings&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;ports&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PortMapping&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Tcp&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

            &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Ip address allocated for: &lt;/span&gt;&lt;span class="si"&gt;%s&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;container&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

            &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;Log&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;private&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt; &lt;span class="n"&gt;mappings&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;privatePort&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;publicPort&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt; &lt;span class="n"&gt;ports&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="n"&gt;am&lt;/span&gt; &lt;span class="n"&gt;just&lt;/span&gt; &lt;span class="n"&gt;writing&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;stdout&lt;/span&gt; &lt;span class="n"&gt;but&lt;/span&gt; &lt;span class="n"&gt;you&lt;/span&gt; &lt;span class="n"&gt;can&lt;/span&gt; &lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt; &lt;span class="n"&gt;information&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;update&lt;/span&gt; &lt;span class="n"&gt;hipache&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;redis&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;etc&lt;/span&gt;&lt;span class="o"&gt;...&lt;/span&gt;
                &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;&lt;span class="si"&gt;%s&lt;/span&gt;&lt;span class="s"&gt; -&amp;gt; &lt;/span&gt;&lt;span class="si"&gt;%s&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;privatePort&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;publicPort&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;http://localhost:4243/events&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Body&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;Read&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;streaming&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt; &lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;the&lt;/span&gt; &lt;span class="nn"&gt;events&lt;/span&gt; &lt;span class="nn"&gt;endpoint&lt;/span&gt;
    &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="n"&gt;docs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;docker&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;io&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;en&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;latest&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;docker_remote_api_v1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="c"&gt;#monitor-docker-s-events&lt;/span&gt;
    &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewDecoder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="n"&gt;Event&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;io&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;EOF&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;break&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Status&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;start&amp;quot;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;We&lt;/span&gt; &lt;span class="n"&gt;only&lt;/span&gt; &lt;span class="n"&gt;want&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;inspect&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;container&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="n"&gt;has&lt;/span&gt; &lt;span class="n"&gt;started&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;container&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;inspectContainer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;container&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;container&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;</summary></entry><entry><title>Go Helpful Commands</title><link href="http://crosbymichael.com/go-helpful-commands.html" rel="alternate"></link><updated>2013-08-01T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2013-08-01:go-helpful-commands.html</id><summary type="html">&lt;p&gt;Here are a few go commands that I find helpful that are hidden in documentation.&lt;/p&gt;
&lt;h3&gt;List all installed packages&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;go list all&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;Build entire pkg directory&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;go build . ./...&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;Install entire pkg directory&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;go install . ./...&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;Test entire pkg&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;go test . ./...&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;Format and simplify code&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;go fmt -s -w &amp;lt;file&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;Detect races&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;go test -race .&lt;/code&gt;&lt;/p&gt;</summary></entry><entry><title>Go toolbox</title><link href="http://crosbymichael.com/go-toolbox.html" rel="alternate"></link><updated>2013-07-20T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2013-07-20:go-toolbox.html</id><summary type="html">&lt;p&gt;I have been asked a few times about my Go development environment.  Because Go is still a very new language many people do not know where to start.  Here are a few tools that I have in my Go toolbox.&lt;/p&gt;
&lt;h2&gt;Tools&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/nsf/gocode"&gt;GoCode&lt;/a&gt; - Code completion daemon.&lt;/li&gt;
&lt;li&gt;&lt;a href="http://golang.org/cmd/gofmt/"&gt;gofmt&lt;/a&gt; - Format your code &lt;/li&gt;
&lt;li&gt;&lt;a href="http://golang.org/cmd/godoc/"&gt;godoc&lt;/a&gt; - Stdlib and third party documentation&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/golang/lint"&gt;lint&lt;/a&gt; - linter for Go&lt;/li&gt;
&lt;li&gt;&lt;a href="https://groups.google.com/forum/#!topic/golang-nuts/YLkP7fNJeOk"&gt;doc&lt;/a&gt; - Tool to search the docs via the cmd line&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/axw/gocov"&gt;gocov&lt;/a&gt; - Code coverage tool&lt;/li&gt;
&lt;li&gt;&lt;a href="http://ctags.sourceforge.net/"&gt;ctags&lt;/a&gt; - If your using vim you can use ctags for method and variable outlines&lt;/li&gt;
&lt;li&gt;&lt;a href="http://golang.org/doc/gdb"&gt;gdb&lt;/a&gt; - I usually just use print statements for debugging but the new version of gdb allows you to step through Go code&lt;/li&gt;
&lt;li&gt;&lt;a href="http://golang.org/doc/articles/race_detector.html"&gt;race detector&lt;/a&gt; - Check for race conditions in your code&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/davecheney/golang-crosscompile"&gt;cross compile&lt;/a&gt; - Compile go for different platforms&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Editors&lt;/h2&gt;
&lt;p&gt;Although I use vim for Go I have used Sublime Text 2 and SublimeGo.  It provides a very complete Go development setup and code completion if you don't want to make the investment in vim.  &lt;/p&gt;
&lt;p&gt;Let me know if I missed anything.&lt;/p&gt;</summary></entry><entry><title>Dockerfile Best Practices</title><link href="http://crosbymichael.com/dockerfile-best-practices.html" rel="alternate"></link><updated>2013-07-14T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2013-07-14:dockerfile-best-practices.html</id><summary type="html">&lt;p&gt;Dockerfiles provide a simple syntax for building images.  The following are a few tips and tricks to help you get the most out of Dockerfiles.&lt;/p&gt;
&lt;h3&gt;1: Use the cache&lt;/h3&gt;
&lt;p&gt;Each instruction in a Dockerfile commits the change into a new image which will then be used as the base of the next instruction.  If an image exists with the same parent and instruction ( except for ADD ) docker will use the image instead of executing the instruction, i.e. the cache. &lt;/p&gt;
&lt;p&gt;In order to effectively utilize the cache you need to keep your Dockerfiles consistent and only add the alterations at the end.  All my Dockerfiles start with the same 5 lines.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;FROM ubuntu
MAINTAINER Michael Crosby &amp;lt;michael@crosbymichael.com&amp;gt;

RUN &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;deb http://archive.ubuntu.com/ubuntu precise main universe&amp;quot;&lt;/span&gt; &amp;gt; /etc/apt/sources.list
RUN apt-get update
RUN apt-get upgrade -y
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Changing MAINTAINER instruction will force docker to execute the proceeding RUN instructions to update apt instead of hitting the cache.  &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1. Keep common instructions at the top of the Dockerfile to utilize the cache.&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;2: Use tags&lt;/h3&gt;
&lt;p&gt;Unless you are experimenting with docker you should always pass the &lt;code&gt;-t&lt;/code&gt; option to &lt;code&gt;docker build&lt;/code&gt; so that the resulting image is tagged.  A simple human readable tag will help you manage what each image was created for.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;docker build -t=&amp;quot;crosbymichael/sentry&amp;quot; .
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;2. Always pass &lt;code&gt;-t&lt;/code&gt; to tag the resulting image.&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;3: EXPOSE-ing ports&lt;/h3&gt;
&lt;p&gt;Two of the core concepts of docker are repeatability and portability.  Images should able to run on any host and as many times as needed.  With Dockerfiles you have the ability to map the private and public ports, however, you should &lt;strong&gt;never&lt;/strong&gt; map the public port in a Dockerfile.  By mapping to the public port on your host you will only be able to have one instance of your dockerized app running.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c"&gt;# private and public mapping&lt;/span&gt;
EXPOSE 80:8080

&lt;span class="c"&gt;# private only&lt;/span&gt;
EXPOSE 80
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;If the consumer of the image cares what public port the container maps to they will pass the &lt;code&gt;-p&lt;/code&gt; option when running the image, otherwise, docker will automatically assign a port for the container.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;3. Never map the public port in a Dockerfile.&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;4: CMD and ENTRYPOINT syntax&lt;/h3&gt;
&lt;p&gt;Both CMD and ENTRYPOINT are straight forward but they have a hidden, err, "feature" that can cause issues if you are not aware.  Two different syntaxes are supported for these instructions.  &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;CMD /bin/echo
&lt;span class="c"&gt;# or&lt;/span&gt;
CMD &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;/bin/echo&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This may not look like it would be an issues but the devil in the details will trip you up.  If you use the second syntax where the CMD ( or ENTRYPOINT ) is an array, it acts exactly like you would expect.  If you use the first syntax without the array, docker pre-pends &lt;code&gt;/bin/sh -c&lt;/code&gt; to your command.  This has always been in docker as far as I can remember.&lt;/p&gt;
&lt;p&gt;Pre-pending &lt;code&gt;/bin/sh -c&lt;/code&gt; can cause some unexpected issues and functionality that is not easily understood if you did not know that docker modified your CMD.  Therefore, you should always use the array syntax for both instructions because both will be executed exactly how you intended.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;4. Always use the array syntax when using CMD and ENTRYPOINT.&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;5. CMD and ENTRYPOINT better together&lt;/h3&gt;
&lt;p&gt;In case you don't know ENTRYPOINT makes your dockerized application behave like a binary.  You can pass arguments to the ENTRYPOINT during &lt;code&gt;docker run&lt;/code&gt; and not worry about it being overwritten ( unlike CMD ).  ENTRYPOINT is even better when used with CMD.  Let's checkout my &lt;a href="http://www.rethinkdb.com/"&gt;Rethinkdb&lt;/a&gt; Dockerfile and see how to use this.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c"&gt;# Dockerfile for Rethinkdb &lt;/span&gt;
&lt;span class="c"&gt;# http://www.rethinkdb.com/&lt;/span&gt;

FROM ubuntu

MAINTAINER Michael Crosby &amp;lt;michael@crosbymichael.com&amp;gt;

RUN &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;deb http://archive.ubuntu.com/ubuntu precise main universe&amp;quot;&lt;/span&gt; &amp;gt; /etc/apt/sources.list
RUN apt-get update
RUN apt-get upgrade -y

RUN apt-get install -y python-software-properties
RUN add-apt-repository ppa:rethinkdb/ppa
RUN apt-get update
RUN apt-get install -y rethinkdb

&lt;span class="c"&gt;# Rethinkdb process&lt;/span&gt;
EXPOSE 28015
&lt;span class="c"&gt;# Rethinkdb admin console&lt;/span&gt;
EXPOSE 8080

&lt;span class="c"&gt;# Create the /rethinkdb_data dir structure&lt;/span&gt;
RUN /usr/bin/rethinkdb create

ENTRYPOINT &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;/usr/bin/rethinkdb&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;

CMD &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;--help&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This is everything that is required to get Rethinkdb dockerized.  We have my standard 5 lines at the top to make sure the base image is updated, ports exposed, etc...  With the ENTRYPOINT set, we know that whenever this image is run, all arguments passed during &lt;code&gt;docker run&lt;/code&gt; will be arguments to the ENTRYPOINT ( /usr/bin/rethinkdb ).&lt;/p&gt;
&lt;p&gt;I also have a default CMD set in the Dockerfile to &lt;code&gt;--help&lt;/code&gt;.  What this does is incase no arguments are passed during &lt;code&gt;docker run&lt;/code&gt;, rethinkdb's default help output will display to the user.  This is same functionality that you would expect interacting with the rethinkdb binary.  &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;docker run crosbymichael/rethinkdb
&lt;/pre&gt;&lt;/div&gt;


&lt;hr /&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;Running &lt;span class="s1"&gt;&amp;#39;rethinkdb&amp;#39;&lt;/span&gt; will create a new data directory or use an existing one,
  and serve as a RethinkDB cluster node.
File path options:
  -d &lt;span class="o"&gt;[&lt;/span&gt; --directory &lt;span class="o"&gt;]&lt;/span&gt; path           specify directory to store data and metadata
  --io-threads n                    how many simultaneous I/O operations can happen
                                    at the same &lt;span class="nb"&gt;time&lt;/span&gt;

Machine name options:
  -n &lt;span class="o"&gt;[&lt;/span&gt; --machine-name &lt;span class="o"&gt;]&lt;/span&gt; arg         the name &lt;span class="k"&gt;for&lt;/span&gt; this machine &lt;span class="o"&gt;(&lt;/span&gt;as will appear in
                                    the metadata&lt;span class="o"&gt;)&lt;/span&gt;.  If not specified, it will be
                                    randomly chosen from a short list of names.

Network options:
  --bind &lt;span class="o"&gt;{&lt;/span&gt;all &lt;span class="p"&gt;|&lt;/span&gt; addr&lt;span class="o"&gt;}&lt;/span&gt;               add the address of a &lt;span class="nb"&gt;local &lt;/span&gt;interface to listen
                                    on when accepting connections&lt;span class="p"&gt;;&lt;/span&gt; loopback
                                    addresses are enabled by default
  --cluster-port port               port &lt;span class="k"&gt;for&lt;/span&gt; receiving connections from other nodes
  --driver-port port                port &lt;span class="k"&gt;for&lt;/span&gt; rethinkdb protocol client drivers
  -o &lt;span class="o"&gt;[&lt;/span&gt; --port-offset &lt;span class="o"&gt;]&lt;/span&gt; offset       all ports used locally will have this value
                                    added
  -j &lt;span class="o"&gt;[&lt;/span&gt; --join &lt;span class="o"&gt;]&lt;/span&gt; host:port           host and port of a rethinkdb node to connect to
  .................
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now lets run the container with the &lt;code&gt;--bind all&lt;/code&gt; argument.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;docker run crosbymichael/rethinkdb --bind all
&lt;/pre&gt;&lt;/div&gt;


&lt;hr /&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;info: Running rethinkdb 1.7.1-0ubuntu1~precise &lt;span class="o"&gt;(&lt;/span&gt;GCC 4.6.3&lt;span class="o"&gt;)&lt;/span&gt;...
info: Running on Linux 3.2.0-45-virtual x86_64
info: Loading data from directory /rethinkdb_data
warn: Could not turn off filesystem caching &lt;span class="k"&gt;for&lt;/span&gt; database file: &lt;span class="s2"&gt;&amp;quot;/rethinkdb_data/metadata&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;Is the file located on a filesystem that doesn&lt;span class="s1"&gt;&amp;#39;t support direct I/O (e.g. some encrypted or journaled file systems)?) This can cause performance problems.&lt;/span&gt;
&lt;span class="s1"&gt;warn: Could not turn off filesystem caching for database file: &amp;quot;/rethinkdb_data/auth_metadata&amp;quot; (Is the file located on a filesystem that doesn&amp;#39;&lt;/span&gt;t support direct I/O &lt;span class="o"&gt;(&lt;/span&gt;e.g. some encrypted or journaled file systems&lt;span class="o"&gt;)&lt;/span&gt;?&lt;span class="o"&gt;)&lt;/span&gt; This can cause performance problems.
info: Listening &lt;span class="k"&gt;for&lt;/span&gt; intracluster connections on port 29015
info: Listening &lt;span class="k"&gt;for&lt;/span&gt; client driver connections on port 28015
info: Listening &lt;span class="k"&gt;for&lt;/span&gt; administrative HTTP connections on port 8080
info: Listening on addresses: 127.0.0.1, 172.16.42.13
info: Server ready
info: Someone asked &lt;span class="k"&gt;for&lt;/span&gt; the nonwhitelisted file /js/handlebars.runtime-1.0.0.beta.6.js, &lt;span class="k"&gt;if&lt;/span&gt; this should be accessible add it to the whitelist.
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;And there it is, a full Rethinkdb instance running with access to the db and admin console by, interacting with the image the same way you interact with the binary.  Very powerful and yet extremely simple.  I love simple.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;5. ENTRYPOINT and CMD are better together.&lt;/strong&gt;&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;I hope this post helps you to get started working with Dockerfiles and building images that we all can use and benefit from.  Going forward, I believe that Dockerfiles will be a very important part of what makes docker so simple and easy to use whether you are consuming or producing images.  I plan to invest much of my time to provide a complete, powerful, yet simple solution to building docker images via the Dockerfile.&lt;/p&gt;</summary></entry><entry><title>Docker Wallpapers</title><link href="http://crosbymichael.com/docker-wallpapers.html" rel="alternate"></link><updated>2013-07-06T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2013-07-06:docker-wallpapers.html</id><summary type="html">&lt;p&gt;So we finally have the new docker &lt;a href="http://blog.docker.io/2013/06/announcing-new-docker-style/"&gt;logo&lt;/a&gt; but I have not seen any wallpapers or anything else.  The image posted is not very high resolution so I did some quick editing to fill in the background.  All credit goes to the original post and author, I just added more gray to avoid stretching on my Retina display.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Logo" src="http://blog.docker.io/wp-content/uploads/2013/06/Docker-logo-011.png" /&gt;&lt;/p&gt;
&lt;h3&gt;Downloads&lt;/h3&gt;
&lt;p&gt;&lt;a href="static/images/docker-2880-1800.png"&gt;Retina&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="static/images/docker-1920-1080.png"&gt;1920x1080&lt;/a&gt;&lt;/p&gt;</summary></entry><entry><title>Dockerfile deep dive</title><link href="http://crosbymichael.com/dockerfile-deep-dive.html" rel="alternate"></link><updated>2013-07-02T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2013-07-02:dockerfile-deep-dive.html</id><summary type="html">&lt;p&gt;&lt;a href="http://www.docker.io/"&gt;Docker&lt;/a&gt; is an amazing project.  There is plenty of great documentation and tutorials online to get you up and running with docker, building images, and deploying your apps.  There is not much that I can say that others have not said better.  Therefore, I am going to start a series of posts on the docker internals.  How to hack on the Dockerfiles, adding new instructions, and how other features are implemented in the code base.&lt;/p&gt;
&lt;h3&gt;Getting started&lt;/h3&gt;
&lt;p&gt;The first thing that you need to do to begin hacking on docker is to fork the repo on &lt;a href="https://github.com/dotcloud/docker"&gt;github&lt;/a&gt;.  If you already have a fork you need to make sure you stay up-to-date with the master branch.  Docker development is moving very fast, make sure that you have an upstream set in your fork so that you can pull and rebase changes for the master repository.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;origin  git@github.com:crosbymichael/docker.git &lt;span class="o"&gt;(&lt;/span&gt;fetch&lt;span class="o"&gt;)&lt;/span&gt;
origin  git@github.com:crosbymichael/docker.git &lt;span class="o"&gt;(&lt;/span&gt;push&lt;span class="o"&gt;)&lt;/span&gt;
upstream        git://github.com/dotcloud/docker.git &lt;span class="o"&gt;(&lt;/span&gt;fetch&lt;span class="o"&gt;)&lt;/span&gt;
upstream        git://github.com/dotcloud/docker.git &lt;span class="o"&gt;(&lt;/span&gt;push&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Next make sure that I have all the upstream changes pulled down before creating a dev branch.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;╭─michael@codeassistant  ~GOPATH/src/github.com/dotcloud/docker  ‹master›
╰─&amp;lt;&amp;lt;: git pull upstream master
remote: Counting objects: 133, &lt;span class="k"&gt;done&lt;/span&gt;.
remote: Compressing objects: 100% &lt;span class="o"&gt;(&lt;/span&gt;65/65&lt;span class="o"&gt;)&lt;/span&gt;, &lt;span class="k"&gt;done&lt;/span&gt;.
remote: Total &lt;span class="m"&gt;74&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;delta 49&lt;span class="o"&gt;)&lt;/span&gt;, reused &lt;span class="m"&gt;34&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;delta 9&lt;span class="o"&gt;)&lt;/span&gt;
Unpacking objects: 100% &lt;span class="o"&gt;(&lt;/span&gt;74/74&lt;span class="o"&gt;)&lt;/span&gt;, &lt;span class="k"&gt;done&lt;/span&gt;.
From git://github.com/dotcloud/docker
 * branch            master     -&amp;gt; FETCH_HEAD
 &lt;span class="m"&gt;19&lt;/span&gt; files changed, &lt;span class="m"&gt;203&lt;/span&gt; insertions&lt;span class="o"&gt;(&lt;/span&gt;+&lt;span class="o"&gt;)&lt;/span&gt;, &lt;span class="m"&gt;50&lt;/span&gt; deletions&lt;span class="o"&gt;(&lt;/span&gt;-&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;h3&gt;Volumes&lt;/h3&gt;
&lt;p&gt;So now that we are ready to start coding we need to know what we will be implementing.  Currently you can only specify volumes for your container when calling &lt;strong&gt;docker run -v&lt;/strong&gt;.  We want to be able to specify volumes inside a Dockerfile instead of on the cmd line.  This is what we will be implementing.  I'll start by creating a branch called &lt;strong&gt;buildfile-volumes&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;There are a few files that we will be focusing on.  &lt;strong&gt;buildfile.go&lt;/strong&gt; and &lt;strong&gt;builder.go&lt;/strong&gt;.  The &lt;strong&gt;buildfile.go&lt;/strong&gt; is where we parse the Dockerfile and build an image and &lt;strong&gt;builder.go&lt;/strong&gt; is where we take the image config and the config from the commandline to build the container.&lt;/p&gt;
&lt;h4&gt;Buildfile.go&lt;/h4&gt;
&lt;p&gt;So lets jump in and see how the Dockerfiles work.  So down near the bottom of the file you will see the Build method.  Parsing the Dockerfile is very simple, like it should be.  It starts by iterating over all the lines in the file and skipping comments and empty lines.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;line, err := file.ReadString(&amp;#39;\n&amp;#39;)
if err != nil {
    if err == io.EOF &amp;amp;&amp;amp; line == &amp;quot;&amp;quot; {
        break
    } else if err != io.EOF {
        return &amp;quot;&amp;quot;, err
    }
}
line = strings.Trim(strings.Replace(line, &amp;quot;\t&amp;quot;, &amp;quot; &amp;quot;, -1), &amp;quot; \t\r\n&amp;quot;)
// Skip comments and empty line
if len(line) == 0 || line[0] == &amp;#39;#&amp;#39; {
    continue
}
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Next we get the instruction, i.e. &lt;strong&gt;RUN&lt;/strong&gt; and the arguments to the instruction.  After we have the instruction and the arguments docker uses reflection to call the correct method in the buildfile.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;instruction := strings.ToLower(strings.Trim(tmp[0], &amp;quot; &amp;quot;))
arguments := strings.Trim(tmp[1], &amp;quot; &amp;quot;)
stepN += 1

method, exists := reflect.TypeOf(b).MethodByName(&amp;quot;Cmd&amp;quot; + strings.ToUpper(instruction[:1]) + strings.ToLower(instruction[1:]))
if !exists {
    fmt.Fprintf(b.out, &amp;quot;# Skipping unknown instruction %s\n&amp;quot;, strings.ToUpper(instruction))
    continue
}
ret := method.Func.Call([]reflect.Value{reflect.ValueOf(b), reflect.ValueOf(arguments)})[0].Interface()
if ret != nil {
    return &amp;quot;&amp;quot;, ret.(error)
}
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;To get the method of the buildfile by name, we prepend 'Cmd' and make sure that the first char of the instruction is upper case and the rest of the instruction is lowercase.  Therefore, if we want to add a new instruction &lt;strong&gt;VOLUME&lt;/strong&gt; we need a new method on the buildfile called &lt;strong&gt;CmdVolume&lt;/strong&gt;.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;func (b *buildFile) CmdVolume(args string) error {

}
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now we need to think about what type of args will be passed to the &lt;strong&gt;VOLUME&lt;/strong&gt; instruction.  We want to support one to many volumes passed as the args.  The standard is to pass a json array just like you down with CMD.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;VOLUME /redis/data
&lt;span class="c"&gt;# or&lt;/span&gt;
VOLUME &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;/redis/data&amp;quot;&lt;/span&gt;, &lt;span class="s2"&gt;&amp;quot;/redis/tmp&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;So the method for CmdVolume needs to look like this.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;func (b *buildFile) CmdVolume(args string) error {
        if args == &amp;quot;&amp;quot; {
                return fmt.Errorf(&amp;quot;Volume cannot be empty&amp;quot;)
        }

        var volume []string
        if err := json.Unmarshal([]byte(args), &amp;amp;volume); err != nil {
                volume = []string{args}
        }
        if b.config.Volumes == nil {
                b.config.Volumes = NewPathOpts()
        }
        for _, v := range volume {
                b.config.Volume[v] = struct{}{}
        }
        if err := b.commit(&amp;quot;&amp;quot;, b.config.Cmd, fmt.Sprintf(&amp;quot;VOLUME %s&amp;quot;, args)); err != nil {
                return err
        }
        return nil
}
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Basically if we are able to Unmarshal the value into a json array we will append those values to the config else we will just use the single value.&lt;/p&gt;
&lt;p&gt;Now we need to make sure that when a container is built we get any volumes added to the image config copied over to the container config.  Containers are built in the &lt;strong&gt;builder.go&lt;/strong&gt; file and we need to make sure the image config is merged with the user config.  One thing to note is that the user config ( values that come in from the cmd line on &lt;em&gt;docker run&lt;/em&gt; ) take precidence over image config values.  &lt;/p&gt;
&lt;p&gt;The MergeConfig function is located in &lt;strong&gt;utils.go&lt;/strong&gt;.  This is where we will add the new lines for merging the Volumes if no userConf Volumes exist.  &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;func MergeConfig(userConf, imageConf *Config) {
        if userConf.Hostname == &amp;quot;&amp;quot; {
                userConf.Hostname = imageConf.Hostname
        }
        // ...
        if userConf.Cmd == nil || len(userConf.Cmd) == 0 {
                userConf.Cmd = imageConf.Cmd
        }
        if userConf.Dns == nil || len(userConf.Dns) == 0 {
                userConf.Dns = imageConf.Dns
        }
        if userConf.Entrypoint == nil || len(userConf.Entrypoint) == 0 {
                userConf.Entrypoint = imageConf.Entrypoint
        }
        //New code
        if userConf.Volumes == nil || len(userConf.Volumes) == 0 {
                userConf.Volumes = imageConf.Volumes
        }
}
&lt;/pre&gt;&lt;/div&gt;


&lt;h3&gt;Unit tests and Docs&lt;/h3&gt;
&lt;p&gt;The last changes that we have to make are to update the unit tests and documentation with the new instruction.  Obviously you will need to execute the unit tests to make sure that your changes did not break anything.  &lt;/p&gt;
&lt;p&gt;Now it is time to build your docker binary and start user testing.&lt;/p&gt;
&lt;p&gt;I wrote a small Dockerfile to test the new instruction.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;FROM ubuntu

RUN mkdir /test

VOLUME &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;/test&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;There are a few tests that we can run to verify this change.  The first thing that we need to do is run docker build . to make sure the instruction is processed.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;docker build .
Uploading context 10240 bytes
Step 1 : FROM ubuntu
---&amp;gt; 8dbd9e392a96
Step 2 : RUN mkdir /test
---&amp;gt; Using cache
---&amp;gt; 5f7c55f3749c
Step 3 : VOLUME [&amp;quot;/test&amp;quot;]
---&amp;gt; Running in 5c453826366d
---&amp;gt; f738fc81dc78
Successfully built f738fc81dc78
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Next we can run docker inspect on the image id to make sure that the volume is added to the config.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="err"&gt;//docker&lt;/span&gt; &lt;span class="err"&gt;inspect&lt;/span&gt; &lt;span class="err"&gt;&amp;lt;imageid&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;   &lt;span class="nt"&gt;&amp;quot;Volumes&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="nt"&gt;&amp;quot;/test&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now run a container from the image, make sure the volume mounted, and inspect the container for the volume mount point.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="err"&gt;//docker&lt;/span&gt; &lt;span class="err"&gt;inspect&lt;/span&gt; &lt;span class="err"&gt;&amp;lt;containerid&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;Volumes&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;quot;/test&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/var/lib/docker/volumes/da262abbea2ee31ad27287155246cc00e8ec66fc87a503bf230514cb1e8cb3ca/layer&amp;quot;&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="nt"&gt;&amp;quot;VolumesRW&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;quot;/test&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;}}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;If everything looks good and your tests complete successfully, review your diff carefully, commit, push, and open a pull request.  You can view this the pull request on &lt;a href="https://github.com/dotcloud/docker/pull/1124"&gt;github&lt;/a&gt;.  Hopefully the maintainers of the project will not require too many changes to your pull request but always be open to changes and suggestions.&lt;/p&gt;
&lt;p&gt;Happy hacking.&lt;/p&gt;</summary></entry><entry><title>Remote Development</title><link href="http://crosbymichael.com/remote-development.html" rel="alternate"></link><updated>2013-06-07T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2013-06-07:remote-development.html</id><summary type="html">&lt;p&gt;In the past I have not been a huge fan of "the cloud" for all of the work that I need to do.  I still prefer native over web apps but I also like the convenience of having my data accessible everywhere.  However, over the past few months I have been doing more work in web apps and doing development over ssh on my own server.  This post will go over some of the necessary tools that is needed to make remote development awesome.&lt;/p&gt;
&lt;h3&gt;Cloud9&lt;/h3&gt;
&lt;p&gt;I used &lt;a href="https://github.com/ajaxorg/cloud9/"&gt;cloud9 ide&lt;/a&gt; for over a week.  I was able to get the app running on my server with a lot of pain and dependencies.  All of the nodejs app that I have tried to setup have 100's of dependencies and any one of them can fail on install.  Anyways, I was able to get the app running and was able to do small development tasks on it.  It is mostly geared towards web development but has syntax support for many other languages.  It ran fine for a while but it would always throw a few errors and I had an issue with data loss when I encountered an error on save.  After that I felt like I was tying a paper for college where I was paranoid about data loss and saving the Word doc ever few minutes.&lt;/p&gt;
&lt;p&gt;I wanted to install cloud9 on a LXC container so that it is properly sandboxed but the dependencies kept failing to install until I gave up.  I think a web ide is still premature for the type of development that I need to do and the responsivneness that I am used to.  &lt;/p&gt;
&lt;h3&gt;Remote Stack&lt;/h3&gt;
&lt;p&gt;So if I am not using a web ide what am I using?  I am running an Ubuntu 12.04 64bit server that I ssh into.  Ssh is secure, fast, and just works.  It provides the exact same shell that is on my local machine via my &lt;a href="https://github.com/crosbymichael/.dotfiles"&gt;.dotfiles&lt;/a&gt;.  Here is a quick list of my setup:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Setup&lt;/strong&gt;
&lt;em&gt; Zsh ( oh-my-zsh )
&lt;/em&gt; tmux
&lt;em&gt; vim
&lt;/em&gt; git
&lt;em&gt; vagrant
&lt;/em&gt; docker
&lt;em&gt; rdiff-backup
&lt;/em&gt; irssi
&lt;em&gt; python 2.7
&lt;/em&gt; Go 1.1
* compilers, libs, etc...&lt;/p&gt;
&lt;p&gt;With these tools I have a consistent and persistent development environment from any location.  The one thing that I do not like to do on the cmd prompt is managing sql databases.  I like to have a gui when working with a sql db.  &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Shell&lt;/strong&gt;
Zsh plus oh-my-zsh is very powerful and fast.  It provides everything that I need and works the same on linux and osx.  The plugins for many of the apps that I already use and tab completion make my workflow very efficient.  &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Editor&lt;/strong&gt;
An ssh workflow is not going to work if you use a heavy ide to write code.  Working as a .NET dev by day I couldn't imaging working with C# and .NET without Visual Studio because the framework is so large and the namespaces and inheritance chains can be very deep.  Being able to remember not only the large, OO and pattern heavy applications that are written in .NET along with the entire .NET frameworks (+ ASP stack) it is nearly impossible to navigate the projects without Visual Studio doing it for you.  However, the code that I write for open source, freelancing, and myself is Python, Go, and your standard web shit (html, css, js) I prefer a text editor over an ide.  &lt;/p&gt;
&lt;p&gt;My editor of choice is &lt;a href="http://vim.org"&gt;vim&lt;/a&gt;.  I have used vim for a long time.  I took a small six month break from vim and used Sublime Text 2 but it started to slow down after a while and I will still using vim when I ssh'd into a server.  So my editing skills were split between two editors and it was just time for me to come back home to &lt;em&gt;hjkl&lt;/em&gt;.  Plus Sublime Text 2 does not work over ssh.  It has good code completion for the languages that I work in so with my setup it is basically a very lightweight ide.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Vim" src="static/images/new-vim.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Tmux&lt;/strong&gt;
When working over ssh the biggest issue is persisting your state when you need to logout.  It is no fun when you have a few vim tabs, splits, and other processes running while coding then after you logout, then log back in you have to recreate everything to get back where you were.  I used to use screen for this but I have recently switched to tmux because of the added features.  Tmux allows you to persist your sessions, have multiple windows ( ssh sessions) and terminal splits to provide an entire workspace on one ssh connection.  You can logout and back in and start coding right away.  &lt;/p&gt;
&lt;p&gt;You can even have an irc client running in your terminal while working and easily switch back and forth with a keystroke.  Tmux (or screen) is a must have when working over ssh, it is what makes a productive workflow possible.  &lt;/p&gt;
&lt;p&gt;&lt;img alt="Tmux" src="static/images/tmux.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Virtualization&lt;/strong&gt;
Installing dependencies on your development boxes is not a wise idea when virtualization is so easy.  I use both vagrant with puppet and docker for running additional boxes on my machine.  This allows me to install and run postgres, redis, etc and take these resources down when not in use without having to junk my box up with all the dependencies that are required for modern development. &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Backup&lt;/strong&gt;
Even though you are "developing in the cloud" that does not mean you do not need to backup your workspace.  Backups are really simple with rdiff-backup.  It does incremental backups and is fast and efficient.  I do daily backups of my development folder to another hard drive via a cron job to fire rdiff-backup and a weekly backup to S3 of the files. &lt;/p&gt;
&lt;p&gt;That's about it.  Any computer with a terminal allows me to securely connect to my workspace and resume on whatever I was working on.  This setup also allows me to have a smaller and more portable computer because all the processing is done on my server that has fast cpus, ssd, and lots of ram.  It has taken me a while to get used to working on the server and not on my local file system.  I had to delete my development folder on my laptop (initially) so that I wouldn't be tempted to change files locally.  &lt;/p&gt;
&lt;p&gt;I do run the risk of not having internet access and not being able to access my files so I sill sync my development folder to my laptop.  Hopefully more people will be able to look at hardware as a keyboard and a screen to connect to an always online workspace that is where ever you are.&lt;/p&gt;</summary></entry><entry><title>Golang JSON RPC</title><link href="http://crosbymichael.com/golang-json-rpc.html" rel="alternate"></link><updated>2013-04-23T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2013-04-23:golang-json-rpc.html</id><summary type="html">&lt;p&gt;Go's rpc package has an example for the default rpc implementation but nothing for the jsonrpc package.  Here is how to get it work.  The rpc example uses some helpers built into the rpc package, I just looked at that code in Go's stdlib to figure out the proper way to get the jsonrpc package to work.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;fmt&amp;quot;&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;log&amp;quot;&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;net&amp;quot;&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;net/rpc&amp;quot;&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;net/rpc/jsonrpc&amp;quot;&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;test/server&amp;quot;&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="n"&gt;startServer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;arith&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Arith&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rpc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewServer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arith&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleHTTP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rpc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DefaultRPCPath&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rpc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DefaultDebugPath&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;net&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;tcp&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;:8222&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;listen error:&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Accept&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;go&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ServeCodec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jsonrpc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewServerCodec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;go&lt;/span&gt; &lt;span class="n"&gt;startServer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;net&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Dial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;tcp&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;localhost:8222&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="n"&gt;args&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Args&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;reply&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;

    &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;jsonrpc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Arith.Multiply&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;arith error:&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Arith: &lt;/span&gt;&lt;span class="si"&gt;%d&lt;/span&gt;&lt;span class="s"&gt;*&lt;/span&gt;&lt;span class="si"&gt;%d&lt;/span&gt;&lt;span class="s"&gt;=&lt;/span&gt;&lt;span class="si"&gt;%d&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;</summary></entry><entry><title>Go Lang</title><link href="http://crosbymichael.com/go-lang.html" rel="alternate"></link><updated>2013-04-12T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2013-04-12:go-lang.html</id><summary type="html">&lt;p&gt;If you don't know about Go it's a programming language for now.  Most of the languages that we use everyday are old, 10+ years old.  After so many years languages need to start to think about news ways to solve the same problems.  Go is a new language that takes a different stance on how you write and deploy code.  Go is a dynamically typed but statically compiled language.  I have "played" with Go pre 1.0 a few times and at the 1.0 release but it never really stuck.  I felt like I was writing to much code for what I wanted to accomplish.  However, this last time working with Go 1.1beta I have started to do a few projects and packages with Go and I am really impressed with the language.&lt;/p&gt;
&lt;h2&gt;Abstractions&lt;/h2&gt;
&lt;p&gt;I write a lot of Python code and I still enjoy writing Python more than any other language so I am used to a fairly high level of abstraction from the language.  Even working in C# with tools like LINQ you get trained to expect abstractions for working with collections.  Also when used correctly, I really like having generics available in my language.&lt;/p&gt;
&lt;p&gt;You miss these things with Go.  Go is at a lower level of abstraction in many areas.  Looping and working with arrays and slices ( variable sized collections ) is often back to the for i loop.  Going from LINQ to anything less hurts.  I find my self working with []byte more in Go than I have in any other language.  The Go stdlib does provide you with some abstractions in the io packages but you still are working with byte arrays often.&lt;/p&gt;
&lt;p&gt;Lets look at reading a file line by line in both Python and Go.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;names.txt&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;readlines&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Python is pretty straight forward.  Very simple to remember and can be done with no imports from the stdlib.  Now lets look at the Go example.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;bufio&amp;quot;&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;fmt&amp;quot;&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;os&amp;quot;&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;names.txt&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;defer&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="n"&gt;scanner&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bufio&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewScanner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;scanner&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Scan&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scanner&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;So the go example is a little more than just 3 lines of code.  We have the package declaration, 3 imports from the stdlib, and the go error handling code in the main func.  Even though Go has more code it is still simple and elegant.  You do have to learn the stdlib to do simple tasks but once you do, you have a very powerful set of tools.  The go example is every more amazing once you learn about implicit interface implementation.&lt;/p&gt;
&lt;h2&gt;No Classes&lt;/h2&gt;
&lt;p&gt;I like OOP and modeling classes and their interaction with each other to create a fluent and straight forward API.  However, I also feel like OOP is taken too far.  People will insist that you have to design a polymorphic interface, provide factories, and strategies when all you really need to a simple function.  I believe in design patterns for solving problems in software but I also believe in simplicity, clarity, and elegance in the code that I write.  For me, simple wins over "over engineering."  Knowing when to design and when to bang out a function comes with experience.&lt;/p&gt;
&lt;p&gt;With Go you have types and interfaces but no classes and inheritance.  At the beginning this was hard for me.  However, after working with packages, types, and interfaces in Go I do not miss classes.  I feel like my code is simple and straight forward with only types and packages.  It is OOP at it's purest, data with actions.  You can still attach methods to your types and you have public and internal access via upper and lower case names.  If a type and/or function begins with an upper case it is exported to be used outside of that package, if it is lower case it is internal to that package.&lt;/p&gt;
&lt;p&gt;Interfaces in Go are also very modern.  You can have a type and implement an interface without explicitly stating that you implement that interface.  If we have an interface that has a&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;Write([]byte) int
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;then in Go, any type that has a method Write that takes a byte array and returns an int implements that interface, which is exactly what you want.  With interfaces you are basically saying that you don't care about the type or other methods, all I care about is that whatever I have passed to me has a Write method that takes bytes and returns an int.&lt;/p&gt;
&lt;h2&gt;Why Go&lt;/h2&gt;
&lt;p&gt;So why am I writing a post on a language with no classes and missing certain abstractions that we are used to?  Because their some home run features that make Go an amazing language to work with.&lt;/p&gt;
&lt;h3&gt;Concurrency&lt;/h3&gt;
&lt;p&gt;There are many articles about concurrency in Go with gorutines and channels so I am not going to say much here except that they are awesome.&lt;/p&gt;
&lt;h3&gt;Compiled binary&lt;/h3&gt;
&lt;p&gt;Deploying a python application takes automation.  You need to to create a virtual environment, have a requirements file with all of your dependencies, and then finally copy your application code up to the server.  There are many tools for deploying python code and it really is not that bad but it is also not simple and there are many places that things can go wrong.&lt;/p&gt;
&lt;p&gt;With Go you have a compiled binary with all the dependencies compiled into the binary.  You do not even have to have the Go tool chain installed on the computer that you are deploying your go application to run the code.  You just drop the executable on the machine and your good to go, nothing else to do.  This is amazing.&lt;/p&gt;
&lt;p&gt;Also being compiled into native machine code makes go very fast.&lt;/p&gt;
&lt;h3&gt;Testing and Packages&lt;/h3&gt;
&lt;p&gt;Testing is simple and easy.  Automating testing is simple and easy.  Have you tried to automate C# unit tests?  Oh, you don't own VS Pro with unit testing?&lt;/p&gt;
&lt;p&gt;Installing dependencies is very easy with "go get".  You install dependencies exactly the same way as you have your own packages.&lt;/p&gt;
&lt;h3&gt;Go Doc&lt;/h3&gt;
&lt;p&gt;All of Go's documentation is auto generated by the commends in the code.  This includes packages in the stdlib and your own packages in your GOPATH.  I have been doing a lot of work in an area with no Internet access to I really appreciate being able to run the package documentation and other general Go documentation on my local machine.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;godoc -http=:8080
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This provides me with all the documentation and examples available if I was connected online to the golang.org website.&lt;/p&gt;
&lt;h2&gt;Parting words&lt;/h2&gt;
&lt;p&gt;Hopefully this has peaked your interest in Go and maybe you can give it a try.  I am going to start using Go as my main language for backend services and processes.&lt;/p&gt;</summary></entry><entry><title>SecureString How To</title><link href="http://crosbymichael.com/securestring-how-to.html" rel="alternate"></link><updated>2013-03-09T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2013-03-09:securestring-how-to.html</id><summary type="html">&lt;p&gt;Working with the SecureString class in .NET is fairly easy.  This class allows you to keep string data encrypted in memory.  Maybe you load connection strings, passwords, or financial data into memory when your application starts or loads an object so the least you can do is use the SecureString class to keep this information encrypted while the data stays in memory.&lt;/p&gt;
&lt;h2&gt;Adding your data&lt;/h2&gt;
&lt;p&gt;Adding a string content to the class is very easy.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;hushhush&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;Something I need to keep on the down low&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;secureString&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;SecureString&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;chr&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;hushhush&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ToCharArray&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;secureString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AppendChar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;secureString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MakeReadOnly&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;All you need to do is convert your string into a char array and append each char to the secure string.  After you have finished writing all data to the secure string you will want to call the MakeReadOnly method so that the instance becomes immutable.&lt;/p&gt;
&lt;h2&gt;Getting your data back out&lt;/h2&gt;
&lt;p&gt;Now this is where things get weird.  It's nice not to have a convenient method on the SecureString class to get to your data for security reasons but you actually have to work with pointers and unmanaged resources to get to the data.&lt;/p&gt;
&lt;p&gt;Because SecureString is a sealed class I made a little extension class that adds a GetString() method for reading the string data from the class.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SecureStringExtensions&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;GetString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="n"&gt;SecureString&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;IntPtr&lt;/span&gt; &lt;span class="n"&gt;pointer&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;IntPtr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Zero&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;chars&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

        &lt;span class="k"&gt;try&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;pointer&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Marshal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SecureStringToBSTR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;Marshal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pointer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;chars&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;chars&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;finally&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pointer&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;IntPtr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Zero&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;Marshal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ZeroFreeBSTR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pointer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The Marshal class actually has a BSTR method for use with the SecureString.  Just remember when working with unmanaged resources you will need to manually release and free the pointers that you used.&lt;/p&gt;
&lt;p&gt;Basically if you have not worked with Marshal and pointers in C# you will have to get the pointer to the char[] from the secure string.  Then we use Marshal.Copy to marshal the *char[] to our managed char[] using the length of the string.  A simple string.Join and we are home free.  Just remember to zero out our pointer to release the resources.&lt;/p&gt;</summary></entry><entry><title>F# Url Shortener</title><link href="http://crosbymichael.com/f-url-shortener.html" rel="alternate"></link><updated>2013-01-27T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2013-01-27:f-url-shortener.html</id><summary type="html">&lt;p&gt;Every year I try to learn a new language.  If you are a one trick pony you are not only limiting your self in opportunities but you are also limiting your thinking to the one paradigm that you know.&lt;/p&gt;
&lt;p&gt;So this year I wanted to learn something very different from what I am used to in the OO world ( Python, C#, Objective - C).  I decided to learn a functional language this year called F#.  I looked at a few different functional languages ( Scala, Erlang ) before choosing F#.  I felt that it would be the most practical language for what I do but still provide me with the paradigm shift I was seeking.  It does have OO parts being part of the .NET family but the functional paradigm still dominates it's landscape.&lt;/p&gt;
&lt;p&gt;Usually when I learn a new language I rewrite a program that I have already done before.  This helps me to focus on the language and not worry about the problem at hand because I already know the solution.  So this program is a url shortener that makes a request to the Google api via JSON.  I have written this url shortener in Python, Go, Objective - C, and C# so I know what I need to do and I have a reference.  So lets get started.&lt;/p&gt;
&lt;h2&gt;To the code!&lt;/h2&gt;
&lt;p&gt;The first thing we need to do to create this program in F# is to "open" our dependencies.  So at the top of a blank .fs file add these lines:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="n"&gt;light&lt;/span&gt;

&lt;span class="k"&gt;open&lt;/span&gt; &lt;span class="nn"&gt;System.Net&lt;/span&gt;
&lt;span class="k"&gt;open&lt;/span&gt; &lt;span class="nn"&gt;System.IO&lt;/span&gt;
&lt;span class="k"&gt;open&lt;/span&gt; &lt;span class="nn"&gt;System.Text&lt;/span&gt;
&lt;span class="k"&gt;open&lt;/span&gt; &lt;span class="nn"&gt;System.Web.Script.Serialization&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;We need the Net package for the WebRequest, IO for a StreamReader, Text for Encoding, and the Web.Script.Serialization for the JavaScriptSerializer so that we can convert the JSON response into a type that we can use.  You will have to add the System.Web.Extensions dll reference to your F# project to use the JavaScriptSerializer.&lt;/p&gt;
&lt;p&gt;Next we need to create a new type that the JavaScriptSerializer will deserialize the response to.  The response JSON looks like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;short url&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;&amp;quot;kind&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;kind of request&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;&amp;quot;longUrl&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;long url&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;We will have to make this type a class with a default constructor so that the JavaScriptSerializer can create the instance.  The members of the type; id, kind, and longUrl will have to be mutable so that the serializer can set the values.  By default all types in F# are immutable unless set to mutable.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt;
    &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="k"&gt;mutable&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="k"&gt;mutable&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="k"&gt;mutable&lt;/span&gt; &lt;span class="n"&gt;longUrl&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;longUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;So there is the Response type with the default constructor.  Next I will create another type that will hold all the request data that is needed.  This is more for convenience and not really needed.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;requestMethod&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;contentType&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now lets get the data required and populate a Request type.  We will receive the long url via the command line args and the other data such as request method and content type can be hard coded along with the url to the Google api.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nn"&gt;Environment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetCommandLineArgs&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;https://www.googleapis.com/urlshortener/v1/url&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;requestMethod&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;POST&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;.[&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
    &lt;span class="n"&gt;contentType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;application/json&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;h3&gt;Functions&lt;/h3&gt;
&lt;p&gt;We will need a few helper functions that can convert a string in to a byte array, read a Stream into a string, and deserialize the response string into our Response type.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;get_bytes&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="nn"&gt;Encoding&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nn"&gt;UTF8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetBytes&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;read_to_string&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="n"&gt;reader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;StreamReader&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;reader&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ReadToEnd&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;deserialize_response&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;serializer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;JavaScriptSerializer&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;serializer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Deserialize&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;read_to_string&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;These three functions are very straight forward and to the point.  One issue that I have ran into is that using classes from the .NET framework often requires a type annotation to be added to the function parameter(s) type inference cannot be used with overloaded methods.  It kinda sucks but it is what it is.  You will also notice the "use" keyword in the read_to_string function.  This is the same as the "using" keyword in C#.   Other than that everything is pretty obvious to a .NET developer looking at this code.&lt;/p&gt;
&lt;p&gt;Now we will need to create a WebRequest, add the default parameters, write our data to the request stream, and then read the response.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;add_params&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;webRequest&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;WebRequest&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;webRequest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ContentType&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;contentType&lt;/span&gt;
    &lt;span class="n"&gt;webRequest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Method&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;requestMethod&lt;/span&gt;
    &lt;span class="n"&gt;webRequest&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;write_to_stream&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;webRequest&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;WebRequest&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;webRequest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetRequestStream&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;get_bytes&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sprintf&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;{&lt;/span&gt;&lt;span class="se"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="s"&gt;longUrl&lt;/span&gt;&lt;span class="se"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="s"&gt;:&lt;/span&gt;&lt;span class="se"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="s"&gt;%s&lt;/span&gt;&lt;span class="se"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="s"&gt;}&amp;quot;&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;webRequest&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;get_response&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;webRequest&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;WebRequest&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;webRequest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetResponse&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetResponseStream&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;print_response&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;%s&amp;quot;&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Easy.  One of the things that you will notice is the lack of a return keyword.  This is because whatever is on the last line of a function is automatically returned.  You can see that in the write_to_stream function, even though it takes a webRequest as an input parameter, I set it on the last line so it can be returned.  In C# ( and other languages ) this is often known as a "Fluent api".  This will also help us a little bit later when we chain all of these functions together to process the request.&lt;/p&gt;
&lt;p&gt;You can also see the "&amp;lt;-" operator when I assign values from our request type to the webRequest.  You do not use the "=" operator for member assignment.&lt;/p&gt;
&lt;p&gt;Lastly, you may have noticed that I never created the actual webRequest in the above code snippet.  That is because in the final function we will create the webRequest and chain all of these functions together.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;get_short_url&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="nn"&gt;HttpWebRequest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;add_params&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;write_to_stream&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;get_response&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;deserialize_response&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;print_response&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;So get_short_url is the final function that ties everything together.  We create the webRequest and use it's value to chain all the remaining functions.  Each function consumes the result of the previous function this way we can use the "|&amp;gt;" to chain them together.  This is very LINQ like but taken to a whole new level in functional programming.&lt;/p&gt;
&lt;p&gt;So now all that is left is to get_short_url when a user passes in a long url on the command line.  For this we will use a pattern match in F#.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StartsWith&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;http&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;get_short_url&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;printfn&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;No url passed&amp;quot;&lt;/span&gt;

&lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nn"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ReadLine&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Because our Request type's data member is a string we can use the standard .NET string function "StartsWith" to see if the first chars are "http".  Then by using the "match with" we can check for each boolean value to determine our control flow.  If true, we will call our get_short_url function, if false, we will tell the user that they did not pass a url.  If you are on Windows you will want to add the "Console.ReadLine" call at the end of the file or the command prompt will exit without you seeing your results.&lt;/p&gt;
&lt;h2&gt;Complete Code&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="n"&gt;light&lt;/span&gt;

&lt;span class="k"&gt;open&lt;/span&gt; &lt;span class="nn"&gt;System.Net&lt;/span&gt;
&lt;span class="k"&gt;open&lt;/span&gt; &lt;span class="nn"&gt;System.IO&lt;/span&gt;
&lt;span class="k"&gt;open&lt;/span&gt; &lt;span class="nn"&gt;System.Text&lt;/span&gt;
&lt;span class="k"&gt;open&lt;/span&gt; &lt;span class="nn"&gt;System.Web.Script.Serialization&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt;
    &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="k"&gt;mutable&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="k"&gt;mutable&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="k"&gt;mutable&lt;/span&gt; &lt;span class="n"&gt;longUrl&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;longUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;requestMethod&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;contentType&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nn"&gt;Environment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetCommandLineArgs&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;https://www.googleapis.com/urlshortener/v1/url&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;requestMethod&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;POST&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;.[&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
    &lt;span class="n"&gt;contentType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;application/json&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;get_bytes&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="nn"&gt;Encoding&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nn"&gt;UTF8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetBytes&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;read_to_string&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="n"&gt;reader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;StreamReader&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;reader&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ReadToEnd&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;print_response&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;%s&amp;quot;&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;deserialize_response&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;serializer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;JavaScriptSerializer&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;serializer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Deserialize&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;read_to_string&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;add_params&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;webRequest&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;WebRequest&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;webRequest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ContentType&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;contentType&lt;/span&gt;
    &lt;span class="n"&gt;webRequest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Method&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;requestMethod&lt;/span&gt;
    &lt;span class="n"&gt;webRequest&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;write_to_stream&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;webRequest&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;WebRequest&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;webRequest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetRequestStream&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;get_bytes&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sprintf&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;{&lt;/span&gt;&lt;span class="se"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="s"&gt;longUrl&lt;/span&gt;&lt;span class="se"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="s"&gt;:&lt;/span&gt;&lt;span class="se"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="s"&gt;%s&lt;/span&gt;&lt;span class="se"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="s"&gt;}&amp;quot;&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;webRequest&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;get_response&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;webRequest&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;WebRequest&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;webRequest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetResponse&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetResponseStream&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;get_short_url&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="nn"&gt;HttpWebRequest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;add_params&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;write_to_stream&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;get_response&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;deserialize_response&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;print_response&lt;/span&gt;

&lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StartsWith&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;http&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;get_short_url&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;printfn&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;No url passed&amp;quot;&lt;/span&gt;

&lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nn"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ReadLine&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;So that's it.  I am still very fresh to F# and functional programming in general so my code does not look very elegant.  It feels a little verbose working with the .NET classes in a functional language but hopefully I will learn more and be able to remove some of the excess and make this program more idiomatic to functional programming.&lt;/p&gt;</summary></entry><entry><title>Infrastructure - Part 1</title><link href="http://crosbymichael.com/infrastructure-part-1.html" rel="alternate"></link><updated>2012-09-29T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2012-09-29:infrastructure-part-1.html</id><summary type="html">&lt;h4&gt;Servers&lt;/h4&gt;
&lt;p&gt;Running your own infrastructure is awesome, if your a hacker like me.  If your a square and you don't want to mess with your own hardware you have two choices if you are going to host your website or services online.  If you don't feel like spending $1k a year you will probably have a VPS or EC2 instance with less than 1GB of RAM.  &lt;/p&gt;
&lt;p&gt;You can get a shared hosting provider and use what they give you.  Basically PHP and Mysql.  I run a heavy Python stack and Postgesql.  PHP is not an option for me.  Also 1GB of ram is not enough.  I have to run a Minecraft server for myself and a few friends and I need at least 3-4GB of RAM just for Minecraft.  ( This is also a test of how robust my setup is because if Minecraft would go down my friend my hurt himself. )  I also like running other services like jenkins as a build server.  You still have to spend some money on things like a battery backup or upgrade your internet connection.&lt;/p&gt;
&lt;p&gt;The trick to running your website on your own hardware and internet connection is to host all your content, images, js, css, etc.. on a CDN and only have your servers render the HTML and do database operations.  Leave any content on S3.  I run my websites on my own server and have S3 host all the images and content.  You cannot tell the difference loading my page.&lt;/p&gt;
&lt;p&gt;This is the first post of all the shit I have running.  I'll try to give a high level overview and for some posts I will go into detail on the setup and settings.  Keep an eye out for some of my shell scripts and other cool downloads.&lt;/p&gt;
&lt;h4&gt;Ajenti&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Admin Console&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="Dashboard" src="static/images/ajenti-dashboard.png" /&gt;&lt;/p&gt;
&lt;p&gt;Ajenti is an easy to install Python app that provides information about services running on your server. It also provides a nice UI for reading logs, managing cron jobs and users, and many other tasks.  &lt;/p&gt;
&lt;p&gt;On the main dashboard I can see the load and memory usage for my main node and the build server. &lt;/p&gt;
&lt;p&gt;&lt;img alt="Plugins" src="static/images/ajenti-plugins.png" /&gt;&lt;/p&gt;
&lt;p&gt;Ajenti has a nice plugin system.  I have not dug into developing a plugin but it looks fairly straight forward.  The default selection is great and plugins are fast and easy to install.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Munin&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The charts that you are seeing are from Munin.  It is a system monitoring application that can monitory local and remote systems.  You need one computer as the server and then install munin-node on each computer (including the server) that you want to monitor.  Munin is in apt and you only have a few lines to modify in the munin configs after install to get it up and running.  You will have to have a HTTP server to serve the munin graphs so that ajenti can read them into the dashboard.&lt;/p&gt;
&lt;p&gt;Check out the rest of these screenshots and view the links at the bottom of the page.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Logs&lt;/em&gt;&lt;/strong&gt;
&lt;img alt="Logs" src="static/images/ajenti-logs.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Users&lt;/em&gt;&lt;/strong&gt;
&lt;img alt="Users" src="static/images/ajenti-users.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Health&lt;/em&gt;&lt;/strong&gt;
&lt;img alt="Health" src="static/images/ajenti-health.png" /&gt;&lt;/p&gt;
&lt;h4&gt;Links&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://ajenti.org/"&gt;Ajenti&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://munin-monitoring.org/"&gt;Munin&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</summary></entry><entry><title>Usage Patterns</title><link href="http://crosbymichael.com/usage-patterns.html" rel="alternate"></link><updated>2012-09-28T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2012-09-28:usage-patterns.html</id><summary type="html">&lt;p&gt;&lt;strong&gt;&lt;em&gt; Update: I will have updated graphs in a day or two.  My time tracking app was only exporting the top 10 applications and I just found the setting to export the top 25.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I always have an app running on my computer that logs all the time I spend in each application.  It helps to give some interesting insights how, what, how long, what days, and times do I do certain tasks.  You can see what hours of the day that you are productive and what hours you just surf the web.&lt;/p&gt;
&lt;h2&gt;Usage by App&lt;/h2&gt;
&lt;p&gt;&lt;img alt="Apps" src="static/images/hours-by-app.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Just for clarification:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;iTerm - terminal app&lt;/li&gt;
&lt;li&gt;Forklift - file/ssh/ftp file management&lt;/li&gt;
&lt;li&gt;SublimeText 2 - Text Editor&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So Chrome wins pretty easy but I do spend a lot of my time in a terminal.  I hate pixels and gui's.  I wish I could do everything from the command line.  The other 21% of my time is spent programming in SublimeText.  Even though it is not a large amount 5% of my time sitting at my computer is managing files in Forklift.  That is still a lot of time for something like moving a file from one directory to another.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Usage by Hour" src="static/images/usage-by-hour.png" /&gt;
&lt;img alt="Apps by hour of day" src="static/images/apps-by-time-of-day.png" /&gt;&lt;/p&gt;
&lt;p&gt;These two graphs show pretty much the same thing.  What hours of the day do I must most of my work.  8pm-10pm is when I do most of my work.  However I do like the area graph about what apps I am using at what time.&lt;/p&gt;
&lt;p&gt;You can see that I have a large spike in the morning with Chrome.  This is when I get up and surf the internet looking for news or reading RSS feeds.  I have a pretty constant usage of the terminal throughout the entire day but my development really picks up in the later hours with SublimeText and iTerm.  &lt;/p&gt;
&lt;h2&gt;Hours by day&lt;/h2&gt;
&lt;p&gt;&lt;img alt="Day of Week" src="static/images/hours-by-day-of-week.png" /&gt;&lt;/p&gt;
&lt;p&gt;Thursday, Saturday, and Sunday are my big computing days.  This mostly because I am at work throughout the week so I do what I want on the weekends.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Usage by Day" src="static/images/hours-by-day.png" /&gt;&lt;/p&gt;
&lt;p&gt;Finally my usage throughout the past few months.  You can read this graph a few different ways.  You can look at the usage trends over time and see if I am starting to use a computer more or less.  You can also look at how many hours I am on a computer during a single day.  On average it's about ~4.5hrs a day.  Not too bad at all.&lt;/p&gt;
&lt;p&gt;You can study the graphs if you want and see if doing something like this would be advantageous to you.  I always enjoy looking over data and seeing trends in my own life and how I use and interact with things.  You can use data like this to determine when you are most productive by hour or day, day of week, etc...  &lt;/p&gt;</summary></entry><entry><title>Development Infrastructure</title><link href="http://crosbymichael.com/development-infrastructure.html" rel="alternate"></link><updated>2012-09-10T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2012-09-10:development-infrastructure.html</id><summary type="html">&lt;p&gt;Having servers that automate much of your development process is cool.  Entities living out on the web just waiting to process any task that you throw their way.&lt;/p&gt;
&lt;p&gt;I currently have two servers in my cluster that run my network.  Everything from my public web sites, to mongodb and Postgresql, to private git and build servers.  Why have all of this?  Because it makes me happy when I push changes to my git server and within seconds the project is built, unit tests ran, and a report is emailed to me.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Servers" src="static/images/infrastructure.png" /&gt;&lt;/p&gt;
&lt;h2&gt;Public Site&lt;/h2&gt;
&lt;p&gt;I run a Python stack for my web server.  Python 2.7 with django as the framework running on-top of lighttpd.  Lighttpd also acts as the proxy server for the rest of the services that run on the cluster.  Postgresql is the datastore for my sites.  I still run mongodb on the server for quick, scheme-less storage.  &lt;/p&gt;
&lt;p&gt;On Node1 is my git repositories via &lt;a href="http://www.atlassian.com/software/stash/overview"&gt;Stash&lt;/a&gt;.  Stash is not necessary but it provides a nice UI for managing repositories and users.  I could do all of this via ssh but for $10 Starter License it is worth it.  You can easily browse and see diffs on the web as well.&lt;/p&gt;
&lt;p&gt;The only drawl back to Stash is that is runs on-top of Java and hogs RAM.  The Stash instance consumes about 700-900MB of RAM for not doing much.  The other memory hog is a minecraft server that I run.  Minecraft consumes about 2GB+.  This server has to be robust because if the minecraft sever goes down, I will have some angry users texting me until it's back online.&lt;/p&gt;
&lt;h2&gt;Build server&lt;/h2&gt;
&lt;p&gt;I run &lt;a href="http://jenkins-ci.org"&gt;Jenkins-Ci&lt;/a&gt; as my build server.  I have tried Bamboo for a week but the license was too restrictive for my needs, ( not enough jobs on the starter license ).  For my python projects Jenkins has many nice plugins.&lt;/p&gt;
&lt;p&gt;Even though the build server is running on Debian it can build both Python and .NET projects thanks to &lt;a href="https://github.com/mono/mono"&gt;mono&lt;/a&gt; ( xbuild ).  I am having some issues running nunit tests for a non-root user.  I have not been able to figure out what the issue is yet so my .NET projects do not have automated tests.&lt;/p&gt;
&lt;h2&gt;Server Software&lt;/h2&gt;
&lt;p&gt;Both servers run Debian and are administered via ssh, no passwords just ssh keys.  Each node does it's daily backup to the other with ~3day off-site dumps to Amazon S3.&lt;/p&gt;
&lt;h2&gt;Node1&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Debian 6&lt;/li&gt;
&lt;li&gt;lighttpd&lt;/li&gt;
&lt;li&gt;git 1.7+ ( built from src )&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.djangoproject.com"&gt;django&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.mongodb.org"&gt;mongodb&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;postgresql&lt;/li&gt;
&lt;li&gt;sun-java&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.atlassian.com/software/stash/overview"&gt;stash&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Amazon S3 as CDN for content&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Domains Hosted&lt;/strong&gt;
* &lt;a href="http://code-assistant.com"&gt;CodeAssistant&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;Node2&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Debian 6&lt;/li&gt;
&lt;li&gt;sun-java&lt;/li&gt;
&lt;li&gt;&lt;a href="http://jenkins-ci.org"&gt;jenkins&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="htts://github/mono/mono"&gt;mono&lt;/a&gt; ( built from git repo )&lt;/li&gt;
&lt;li&gt;git 1.7+( built from src )&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Future Changes&lt;/strong&gt;
Currently my blog &lt;a href="http://crosbymichael.com"&gt;crosbymichael.com&lt;/a&gt; ( this site ) is hosted on dreamhost.  Also HTTPAssistant is a wordpress site hosted on dreamhost as well.  My dreamhost service expires Feb. of next year so I will be moving both of these sites to Node1.  I will replace wordpress with a static one page landing page.  Hopefully I will have HTTPAssistant 2.0 done and replace that ugly thing.  &lt;/p&gt;</summary></entry><entry><title>web.py server setup</title><link href="http://crosbymichael.com/webpy-server-setup.html" rel="alternate"></link><updated>2012-08-04T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2012-08-04:webpy-server-setup.html</id><summary type="html">&lt;p&gt;Python is my favorite language to work with.  It is so versatile.  I can automate tasks, write quick scripts, process data, and even run a web application.  It is very portable and I love the syntax.  For a web framework I use web.py because it stays out of your way and does not impose itself on your application.  With other frameworks you have to do it their way or struggle the entire time.  It's awesome but setting up a "production" server is a bitch.  Tutorials suck and getting Python to run under Apache is a waste of 6+ hours with all the config bullshit.  Leave it and do it my way.&lt;/p&gt;
&lt;h2&gt;What you will learn:&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Install lighttpd on Ubuntu/Debian&lt;/li&gt;
&lt;li&gt;Install MySQL&lt;/li&gt;
&lt;li&gt;Setup web.py to run under lighttpd&lt;/li&gt;
&lt;li&gt;Setup MySQLDB for Python to MySQL&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Ubuntu Server&lt;/h3&gt;
&lt;p&gt;Ubuntu or any Debian distro will work with this tutorial.  I am currently running my server on Ubuntu Server 11.04.  You need to make sure that Apache is not installed on your server.  If it is remove everything Apache.  Make sure you have shell plus access to install software on your server.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;MySQL&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Now we will need to install MySQL on your server before we do anything else.  Just skip this part if you don't use MySQL.  This is very easy, just run these two commands to install MySQL and the development libraries that Python needs.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;apt-get install mysql-server libmysqlclient-dev
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Remember that you should be running these as root or sudo.  If you didn't know this just give up now.  You will also need to setup your MySQL username and permissions for your web application to use.  I am not going to go over this process because I am lazy.  Google it if you don't know how.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;lighttpd&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Now we can install the lighttpd server.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;apt-get install lighttpd
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;You can go ahead and browse to the site to see if lighttpd is functioning properly.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Python Build Tools and MySQLDB&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;With our web server and MySQL database setup on the server it is time to get our Python environment setup.  Start by installing the buildtools and other Python development tools.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;apt-get install python-setuptools python-dev build-essential
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This will install "easy_install" and other tools so we can build the python MySQL driver.  After this is complete we need to download and install the MySQLDB driver.  This is easy if you have the libmysqlclient-dev and setuptools installed.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;wget http://downloads.sourceforge.net/project/mysql-python/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz
tar -zxf MySQL-python-1.2.3.tar.gz
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This will download and unzip the driver.  Now "cd" in to the folder and run these two commands:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;python setup.py build
python setup.py install
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;&lt;em&gt;Web.py&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;All we have left to do is install web.py, flup, and configure lighttpd for our application.  The first two are easy.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;easy_install web.py
easy_install flup
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now lets configure lighttpd. &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;cd /etc/lighttpd/
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Replace the contents of lighttpd.conf with the text below:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;server.modules = (
    &amp;quot;mod_access&amp;quot;,
    &amp;quot;mod_alias&amp;quot;,
    &amp;quot;mod_compress&amp;quot;,
    &amp;quot;mod_accesslog&amp;quot;,
)

server.document-root        = &amp;quot;/var/www&amp;quot;
server.upload-dirs          = ( &amp;quot;/var/cache/lighttpd/uploads&amp;quot; )
server.errorlog             = &amp;quot;/var/log/lighttpd/error.log&amp;quot;
server.pid-file             = &amp;quot;/var/run/lighttpd.pid&amp;quot;
server.username             = &amp;quot;www-data&amp;quot;
server.groupname            = &amp;quot;www-data&amp;quot;


## Use ipv6 if available
#include_shell &amp;quot;/usr/share/lighttpd/use-ipv6.pl&amp;quot;


compress.cache-dir          = &amp;quot;/var/cache/lighttpd/compress/&amp;quot;
compress.filetype           = ( &amp;quot;application/x-javascript&amp;quot;, &amp;quot;text/css&amp;quot;, &amp;quot;text/html&amp;quot;, &amp;quot;text/plain&amp;quot; )

include_shell &amp;quot;/usr/share/lighttpd/create-mime.assign.pl&amp;quot;
include_shell &amp;quot;/usr/share/lighttpd/include-conf-enabled.pl&amp;quot;

server.modules   += ( &amp;quot;mod_fastcgi&amp;quot; )
server.modules   += ( &amp;quot;mod_rewrite&amp;quot; )

 fastcgi.server = ( &amp;quot;/index.py&amp;quot; =&amp;gt;
 (&amp;quot;/&amp;quot; =&amp;gt; ( &amp;quot;socket&amp;quot; =&amp;gt; &amp;quot;/tmp/fastcgi.socket&amp;quot;,
    &amp;quot;bin-path&amp;quot; =&amp;gt; &amp;quot;/var/www/index.py&amp;quot;,
    &amp;quot;max-procs&amp;quot; =&amp;gt; 1,
   &amp;quot;bin-environment&amp;quot; =&amp;gt; (
     &amp;quot;REAL_SCRIPT_NAME&amp;quot; =&amp;gt; &amp;quot;&amp;quot;
   ),
   &amp;quot;check-local&amp;quot; =&amp;gt; &amp;quot;disable&amp;quot;
 ))
 )

 url.rewrite-once = (
   &amp;quot;^/favicon.ico$&amp;quot; =&amp;gt; &amp;quot;/static/favicon.ico&amp;quot;,
   &amp;quot;^/static/(.*)$&amp;quot; =&amp;gt; &amp;quot;/static/$1&amp;quot;,
   &amp;quot;^/(.*)$&amp;quot; =&amp;gt; &amp;quot;/index.py/$1&amp;quot;,
 )
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Note the server.username and server.groupname.  This user/group will need proper permissions to your application files.  Study the config above.  Everything is simple and you should be able to change whatever settings that you need.&lt;/p&gt;
&lt;p&gt;Make sure you updated the server.document-root path to where the location that you are going to be placing your application in.  I just use /var/www for my applications.  This path will need to be updated in the fastcgi.server section along with your main python file for web.py.  I usually call my main application file "index.py" and the above config is setup to reflect that.  &lt;/p&gt;
&lt;p&gt;Now that we have lighttpd set up we can go into our /var/www directory and set up a sample web.py application ( if you do not already have a working app ).  We can just use this simple template.  Make a note of the shebang at the top of the file.  You will need to add that to your application file.  Just save the file as "index.py".&lt;/p&gt;
&lt;table class="highlighttable"&gt;&lt;tr&gt;&lt;td class="linenos"&gt;&lt;div class="linenodiv"&gt;&lt;pre&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class="code"&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c"&gt;#!/usr/bin/env python&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;web&lt;/span&gt;

&lt;span class="n"&gt;urls&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;/(.*)&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;Index&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;application&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;web&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;application&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;urls&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;globals&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="n"&gt;web&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;debug&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;GET&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;Hello World&amp;#39;&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;POST&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;Hello World&amp;#39;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;__main__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;application&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;Make the file's owner www-data and make it executable.  After that restart lighttpd.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;chown www-data:www-data index.py
chmod +x index.py
service lighttpd restart
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Go ahead and navigate to your site in your browser and you should see "Hello World".  If you do not, cat out the lighttpd error log at:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;cat /var/log/lighttpd/error.log
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;That's it.  I hope this helps to save hours of your life, hours that I wasted trying to get web.py to run under Apache bullshit.  This is the way to go, lighttpd is fast, easy to config, and perfect for web.py.&lt;/p&gt;</summary></entry><entry><title>Objective - C LINQ</title><link href="http://crosbymichael.com/objective-c-linq.html" rel="alternate"></link><updated>2012-07-06T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2012-07-06:objective-c-linq.html</id><summary type="html">&lt;p&gt;I have been working on two iOS applications over the past few days and have been longing  for a LINQ like syntax in Objective-C.  LINQ is an amazing way to work with collections and it really adds a productivity boost to your coding.  Since Objective C does not have LINQ I thought I would code it myself.  Right now it only supports non-destructive operations but here it is.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;@interface&lt;/span&gt; &lt;span class="bp"&gt;NSArray&lt;/span&gt; &lt;span class="nl"&gt;(IQuery)&lt;/span&gt;

&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;first&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;last&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;firstOrDefault:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="nv"&gt;predicate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;firstOrDefault&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;BOOL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;any&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;BOOL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;any:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="nv"&gt;predicate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;BOOL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;all:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="nv"&gt;predicate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;NSArray&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;ofClass:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Class&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;class&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;NSArray&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;where:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="nv"&gt;predicate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;NSArray&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;select:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="nv"&gt;predicate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;NSArray&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;skip:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="nv"&gt;predicate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;forEach:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="nv"&gt;action&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;@end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This is the current interface.  Most methods return BOOL or an NSArray so that the query operations can be chained together.&lt;/p&gt;
&lt;h2&gt;Code&lt;/h2&gt;
&lt;p&gt;What we want to do in this example is to take an array with strings and ints, filter it by class, then by strings starting with 'h'.  Here is how you do it:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="bp"&gt;NSArray&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="bp"&gt;NSArray&lt;/span&gt; &lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nl"&gt;initWithObjects&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                          &lt;span class="s"&gt;@&amp;quot;hot dog&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                          &lt;span class="s"&gt;@&amp;quot;hamburger&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                          &lt;span class="s"&gt;@&amp;quot;pizza&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                          &lt;span class="s"&gt;@&amp;quot;taco&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                          &lt;span class="s"&gt;@&amp;quot;pop&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                          &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                          &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                          &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                          &lt;span class="nb"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="bp"&gt;NSArray&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;filter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt; 
                           &lt;span class="nl"&gt;ofClass&lt;/span&gt;&lt;span class="p"&gt;:[&lt;/span&gt;&lt;span class="bp"&gt;NSString&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
                           &lt;span class="nl"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="nl"&gt;characterAtIndex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sc"&gt;&amp;#39;m&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                        &lt;span class="p"&gt;}];&lt;/span&gt;

&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;filter&lt;/span&gt; &lt;span class="nl"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;NSLog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;}];&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;So that is it.  The first IQuery method is "ofClass".  This takes a class and returns an NSArray of all the items in the array that are of that type(class).  It is the same idea as C#'s OfType&lt;T&gt;.  &lt;/p&gt;
&lt;p&gt;Next is "where".  This takes a predicate block that will return any item that satisfies the condition.  The syntax is not as clean as C# implementation but it's a start.&lt;/p&gt;
&lt;p&gt;Now after we have this filtered list with the chain of calls to "ofClass" and "where", we can now use the "forEach" method to enumerate the array, passing a block as the action to perform on each item.&lt;/p&gt;
&lt;h2&gt;Select&lt;/h2&gt;
&lt;p&gt;One last thing, if you want to select a certain property from an item in the array, you can use the "select" method to do so.  Here we select the price for each of the items.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="bp"&gt;NSArray&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="bp"&gt;NSArray&lt;/span&gt; &lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nl"&gt;initWithObjects&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                         &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;Items&lt;/span&gt; &lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nl"&gt;initWithNameAndPrice&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s"&gt;@&amp;quot;hamburger&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="bp"&gt;NSNumber&lt;/span&gt; &lt;span class="nl"&gt;numberWithInt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]],&lt;/span&gt; 
                         &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;Items&lt;/span&gt; &lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nl"&gt;initWithNameAndPrice&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s"&gt;@&amp;quot;hot dog&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="bp"&gt;NSNumber&lt;/span&gt; &lt;span class="nl"&gt;numberWithInt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]],&lt;/span&gt;
                         &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;Items&lt;/span&gt; &lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nl"&gt;initWithNameAndPrice&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s"&gt;@&amp;quot;pop&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="bp"&gt;NSNumber&lt;/span&gt; &lt;span class="nl"&gt;numberWithInt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]],&lt;/span&gt; 
                       &lt;span class="nb"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="bp"&gt;NSArray&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;prices&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="nl"&gt;select&lt;/span&gt;&lt;span class="p"&gt;:(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Items&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}];&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This will return an array of NSNumber items.  Nothing more.&lt;/p&gt;
&lt;p&gt;That's all for now.  If you are interested in the project I will have the binary to download soon.  Just email me with any comments or questions.&lt;/p&gt;</summary></entry><entry><title>Objective C Design Patterns - Factory</title><link href="http://crosbymichael.com/objective-c-design-patterns-factory.html" rel="alternate"></link><updated>2012-05-21T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2012-05-21:objective-c-design-patterns-factory.html</id><summary type="html">&lt;p&gt;Design patterns can help you solve complex coding problems with proven solutions.  This will be a mini series about different design pattern implementations in Objective - C.  I will be using a simple console app and updating the code on github with each new pattern.&lt;/p&gt;
&lt;h2&gt;Factory Pattern&lt;/h2&gt;
&lt;p&gt;This pattern provides a consistent way for object creation.  A factory provides one point of creation for one or a group of objects.  It also helps to not code to a concrete class.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;When to use it?&lt;/strong&gt;
&lt;em&gt; Many objects need created that share an interface
&lt;/em&gt; Creation of complex objects
&lt;em&gt; Hide concrete classes and implementation from client code
&lt;/em&gt; Provide one point of entry for the creation of a class or group of classes&lt;/p&gt;
&lt;h2&gt;Static Factory&lt;/h2&gt;
&lt;p&gt;Most of the time I find myself using a static factory instead of an abstract factory.  A static factory is what we are going to focus on now.  In the console app I have an interface for logging text to the console.  There are two implementations for this interface.  One uses NSLog and the other uses fprintf().  &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="cp"&gt;#import &amp;lt;Foundation/Foundation.h&amp;gt;&lt;/span&gt;

&lt;span class="k"&gt;@protocol&lt;/span&gt; &lt;span class="nc"&gt;CMConsoleOutput&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="bp"&gt;NSObject&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nl"&gt;outputText&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;@end&lt;/span&gt;


&lt;span class="c1"&gt;//Class one&lt;/span&gt;
&lt;span class="cp"&gt;#import &amp;quot;CMPrintLog.h&amp;quot;&lt;/span&gt;

&lt;span class="k"&gt;@implementation&lt;/span&gt; &lt;span class="nc"&gt;CMPrintLog&lt;/span&gt;

&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;outputText:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nv"&gt;content&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="n"&gt;fprintf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;%s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="n"&gt;UTF8String&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;@end&lt;/span&gt;


&lt;span class="c1"&gt;//Class two&lt;/span&gt;
&lt;span class="cp"&gt;#import &amp;quot;CMLogOutput.h&amp;quot;&lt;/span&gt;

&lt;span class="k"&gt;@implementation&lt;/span&gt; &lt;span class="nc"&gt;CMLogOutput&lt;/span&gt;

&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;outputText:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nv"&gt;content&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;NSLog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;@end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now we need a factory to choose what implementation the client should use without knowing the exact types of objects that are returned.  The client does not care what they get as long as it implements the interface.  It also helps to cut down the number of imports you have in your header files.  You no longer have to import 5 different class headers; you just have to import one factory header.  I went ahead an created the factory class file, typedef for identifying what type of output object the client wants, and a static method for creating/returning the object.  &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;ouputTypes&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Timestamp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;NoTimestamp&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;OutputTypes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;span class="k"&gt;@interface&lt;/span&gt; &lt;span class="nc"&gt;CMOutputFactory&lt;/span&gt; : &lt;span class="bp"&gt;NSObject&lt;/span&gt;

&lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;CMConsoleOutput&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;create:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;OutputTypes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;@end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now the only thing that we need to remember is to autorelease the objects that we create if you are not using ARC.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;@implementation&lt;/span&gt; &lt;span class="nc"&gt;CMOutputFactory&lt;/span&gt;

&lt;span class="p"&gt;+(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;CMConsoleOutput&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;create:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;OutputTypes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nv"&gt;type&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nl"&gt;Timestamp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;CMLogOutput&lt;/span&gt; &lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nl"&gt;NoTimestamp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;  &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;CMPrintLog&lt;/span&gt; &lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;@end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Finally we just need to use our new factory.  &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c"&gt;#import &amp;lt;Foundation/Foundation.h&amp;gt;&lt;/span&gt;
&lt;span class="c"&gt;#import &amp;quot;CMOutputFactory.h&amp;quot;&lt;/span&gt;

&lt;span class="nb"&gt;int&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;const&lt;/span&gt; &lt;span class="n"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[])&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@autoreleasepool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="nb"&gt;id&lt;/span&gt; &lt;span class="n"&gt;withTimestamp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;CMOutputFactory&lt;/span&gt; &lt;span class="n"&gt;create&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;Timestamp&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="nb"&gt;id&lt;/span&gt; &lt;span class="n"&gt;noTimestamp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;CMOutputFactory&lt;/span&gt; &lt;span class="n"&gt;create&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;NoTimestamp&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;withTimestamp&lt;/span&gt; &lt;span class="n"&gt;outputText&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Output without a time stamp&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;noTimestamp&lt;/span&gt; &lt;span class="n"&gt;outputText&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Without a time stamp&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Very easy, we only have one import and a consistent way to create objects.  It is very straight forward for people consuming an API's classes when you provide a factory for object creation.  They do not need to know the inner workings of your API, they just want a certain type of functionality and your factory provides that.  If you have any questions just ask.&lt;/p&gt;</summary></entry><entry><title>CodeAssistant</title><link href="http://crosbymichael.com/codeassistant.html" rel="alternate"></link><updated>2012-05-14T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2012-05-14:codeassistant.html</id><summary type="html">&lt;p&gt;My second application was published this week.  &lt;/p&gt;
&lt;p&gt;&lt;img alt="CodeAssistant" src="static/images/codeassistant.png" /&gt;&lt;/p&gt;
&lt;p&gt;CodeAssistant is a simple tool to test code snippets in virtually any language on Windows.  It has been in development for four months and has been a great learning experience for domain driven development and .NET's WPF.  &lt;/p&gt;
&lt;p&gt;The application is build on top of WPF using the MVVM design pattern.  I created a MVVM framework called MVVM Async that you can find it on &lt;a href="https://github.com/crosbymichael/mvvm-async"&gt;github&lt;/a&gt;.  WPF and MVVM are still in their infancy and there are some rough edges and verbose syntax that needs fixed ( command delegates anyone? ).  I like WPF and MVVM but I wish they would have just used the proved MVC tooling form ASP.NET MVC in WPF.  There are a lot of developers familiar with MVC, partials, and the such that would have been a good fit for WPF.  But no, they had to build another framework and you have to relearn everything all over again.  &lt;/p&gt;
&lt;p&gt;This has been the first project where I have built it from the ground up with domain driven design and solid patterns.  Each of the layers in the application are completely decoupled from the others and are in their own projects.  The view/application layer only has a reference to the ViewModel project and the ViewModel project only references the Domain layer.&lt;/p&gt;
&lt;h2&gt;Future Projects&lt;/h2&gt;
&lt;p&gt;I don't have any plans for a future project.  I have been wanting to work on a game.  Desktop or iOS, I have not decided.  I may play with the XNA development toolkit before making a decision.  A friend of mine said that Corona is a good platform to build games on.  I have a few game ideas but don't know yet.&lt;/p&gt;
&lt;p&gt;If anyone has a game that they want to work on, it does not matter what platform, I can code in any language, let me know.  &lt;/p&gt;</summary></entry><entry><title>Setup Ruby on Rails with Nginx and Unicorn</title><link href="http://crosbymichael.com/setup-ruby-on-rails-with-nginx-and-unicorn.html" rel="alternate"></link><updated>2012-02-27T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2012-02-27:setup-ruby-on-rails-with-nginx-and-unicorn.html</id><summary type="html">&lt;p&gt;I lost an entire day of coding trying to get his to work.  We are all lucky that I am a developer and not a systems admin.&lt;/p&gt;
&lt;p&gt;However around 9pm I finally have it running.  I burned through a lot of screwed up VM's trying to get this to work but finally made it.  I tried Apache, Nginx, Passenger, and finally Nginx and Unicorn worked like it should.  I don't know what the issues were but the actual rails app was not being ran.  &lt;/p&gt;
&lt;p&gt;I'll try to go through every single step I make, from installing Debian and the settings that I use to the installing and compiling gems, rails, and Nginx.  I will also try to make this into a bash script to automate much of the install incase you f something up.&lt;/p&gt;
&lt;h2&gt;Requirements&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Email&lt;/li&gt;
&lt;li&gt;Rails&lt;/li&gt;
&lt;li&gt;MySQL&lt;/li&gt;
&lt;li&gt;Nginx&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;Installing Debian 6&lt;/h1&gt;
&lt;p&gt;I am going to use Debain 6 ( net install ) because it has the bare minimum.  Only what we install is what it is going to have.  You can also use Ubuntu Server 10.04 or 11.x with these commands.  They should work the same.  I have tested this on Ubuntu Server 10.04 so I know it works.&lt;/p&gt;
&lt;p&gt;Ok boot into your linux install and configure your languages.  I set the hostname as:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;nginx-rails-server
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;img alt="Hostname" src="static/images/hostname.png" /&gt;&lt;/p&gt;
&lt;p&gt;The domain name to my url.&lt;/p&gt;
&lt;p&gt;Create your root and user passwords then for the disk partitioning just use the entire hard drive.  I also choose to keep all system and user files on one partition.&lt;/p&gt;
&lt;p&gt;Pick one of the package servers for debian.  It does not matter.&lt;/p&gt;
&lt;h2&gt;Install Package Selection&lt;/h2&gt;
&lt;p&gt;Here is my package selection for install.  We do not want a webserver or anything else.  I usually install the Mail server so that I don't have to mess with it but since we are going to use Nginx and install mySQL manually all we want is the SSH and Mail servers installed right now.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Packages" src="static/images/default-packages.png" /&gt;&lt;/p&gt;
&lt;p&gt;Install GRUB boot load and restart.&lt;/p&gt;
&lt;p&gt;Once your booted up, login really quick and run ifconfig to get the ip of the machine.  We will want to SSH into it so we can use our desktop Terminal ( copy and paste ) instead of using a keyboard on the actual machine or through the VM.&lt;/p&gt;
&lt;p&gt;Once you have the IP login as root via ssh.  Lets nano and paste in the setup script from below.  Chmod 755 it and let it rip.&lt;/p&gt;
&lt;h1&gt;Application Install&lt;/h1&gt;
&lt;p&gt;&lt;img alt="Setup Script" src="static/images/bash-setup.png" /&gt;&lt;/p&gt;
&lt;p&gt;So now we need to get our base applications installed on the server.  If you are using Ubutnu Server you will want to run:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;sudo apt-get update
sudo apt-get upgrade
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Also from here on out I will not place sudo infront of all the commands that need it.  If you don't konw when you need to run the command with sudo or when not to, just go home.  Don't waste my time emailing why when you run a command on Ubuntu it says "permission denied."&lt;/p&gt;
&lt;p&gt;Here is the script that I will run to get the base apps installed from apt.&lt;/p&gt;
&lt;table class="highlighttable"&gt;&lt;tr&gt;&lt;td class="linenos"&gt;&lt;div class="linenodiv"&gt;&lt;pre&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class="code"&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

apt-get update
apt-get upgrade -y


apt-get install wget build-essential ruby1.8 ruby1.8-dev irb1.8 rdoc1.8 zlib1g-dev libopenssl-ruby1.8 libopenssl-ruby libzlib-ruby libssl-dev libpcre3-dev -y

apt-get install vim -y
apt-get install git-core -y
apt-get install screen -y
apt-get install ctags -y
apt-get install curl -y
apt-get install libsqlite3-dev -y
apt-get install libcurl4-openssl-dev -y
apt-get install libssl-dev -y
&lt;/pre&gt;&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;If you notice I did not install mySQL in here because it will ask for the root passwords and I wanted this base install script to be fully automatic.&lt;/p&gt;
&lt;p&gt;Now that that is done lets go ahead and isntall mySQL&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;apt-get install mysql-server libmysqlclient16 libmysqlclient-dev mysql-client mysql-common -y
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;When asked, pick a good mySQL root password.&lt;/p&gt;
&lt;h3&gt;Gems&lt;/h3&gt;
&lt;p&gt;Now that we have everything from apt installed lets get rubygems installed.  I don't want to install gems via apt so we are going to grab the tarbal and install that way.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;cd /opt/
wget http://production.cf.rubygems.org/rubygems/rubygems-1.5.1.tgz
tar -zxvf rubygems-1.5.1.tgz
cd /opt/rubygems-1.5.1/
ruby setup.rb
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This will install gems in:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;/usr/bin/gem1.8
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Logout of your bash session and them back in and you should be able to type gem1.8 to execute some commands.&lt;/p&gt;
&lt;p&gt;Create another bash setup script for all these gem installs.  They will take a while so it's easier to just let it roll.  I chose not to install the rdoc and ri because this is the server and we don't need it.&lt;/p&gt;
&lt;table class="highlighttable"&gt;&lt;tr&gt;&lt;td class="linenos"&gt;&lt;div class="linenodiv"&gt;&lt;pre&gt;1
2
3
4
5
6
7
8&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class="code"&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
gem1.8 install fastthread --no-rdoc --no-ri 
gem1.8 install rake rack --no-rdoc --no-ri 
gem1.8 install rails --no-rdoc --no-ri 
gem1.8 install mysql --no-rdoc --no-ri 
gem1.8 install unicorn --no-rdoc --no-ri 
gem1.8 install sqlite3-ruby --no-rdoc --no-ri 
gem1.8 install execjs --no-rdoc --no-ri
&lt;/pre&gt;&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;

&lt;h2&gt;Javascript engine&lt;/h2&gt;
&lt;p&gt;Rails needs a javascript engine to run.  Pick your favorite one and install it.  I will isntall node.js for this.  I will not use apt or gems to install node.js but will grab the latest stable release from github and compile it.&lt;/p&gt;
&lt;p&gt;Lets grab the latest stable release from github and download it in our /opt/ dir.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;wget -O nodejs.zip https://github.com/joyent/node/zipball/v0.6.11
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;One thing that I forgot to install is zip so that we can unzip this file.  So quickly install&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;apt-get install zip
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;then unzip node.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;unzip nodejs.zip
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Lets cd into joyent-node-* folder and start building it&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;./configure
make
make install
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Also make sure that you install coffee-rails and jquery-rails.  I got this error when trying to make the test app below.  It may be a good idea to make a test app as root so bundler to handle these for us.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;gem1.8 install coffee-rails --no-rdoc --no-ri
gem1.8 install jquery-rails --no-rdoc --no-ri
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;img alt="Error" src="static/images/js-engine.png" /&gt;&lt;/p&gt;
&lt;h2&gt;Testing rails&lt;/h2&gt;
&lt;p&gt;Now that we have everything setup lets login to our user account ( not root ) and create a sample rails app and run it with the builtin WEBrick server to make sure that rails works on our server.&lt;/p&gt;
&lt;p&gt;In your home dir:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;rails new testRails
cd testRails/
rails server
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;img alt="Webrick" src="static/images/webrick.png" /&gt;&lt;/p&gt;
&lt;p&gt;Your server should start up fine so navigate to it's ip and make sure that rails is running and the environment links displays information about your setup.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Test App" src="static/images/test-page.png" /&gt;&lt;/p&gt;
&lt;p&gt;Hopefully your rails test app looks like this in the browser.  If not then don't even try to go forward.  Get the WEBrick server working first and then move on.&lt;/p&gt;
&lt;h1&gt;Nginx&lt;/h1&gt;
&lt;p&gt;We will go ahead and install nginx via the apt repo.  We first need to add the key to the resposity so download it then add it.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;wget http://nginx.org/keys/nginx_signing.key
apt-key add nginx_signing.key
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now add the repos to your /etc/apt/sources.list&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;deb&lt;/span&gt; &lt;span class="s"&gt;http://nginx.org/packages/debian/&lt;/span&gt; &lt;span class="kp"&gt;squeeze&lt;/span&gt; &lt;span class="kp"&gt;nginx&lt;/span&gt;
&lt;span class="k"&gt;deb-src&lt;/span&gt; &lt;span class="s"&gt;http://nginx.org/packages/debian/&lt;/span&gt; &lt;span class="kp"&gt;squeeze&lt;/span&gt; &lt;span class="kp"&gt;nginx&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Then&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;apt-get update
apt-get install nginx
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;It's a really quick install.  After the install is finished you can test to make sure that it works by going to your machines ip address.  You should see the welcome to nginx text.&lt;/p&gt;
&lt;h2&gt;User group and dir&lt;/h2&gt;
&lt;p&gt;Now lets setup a new directory that nginx and our rails apps will be in.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;cd /var
mkdir www
usermod -a -G www-data nginx
usermod -a -G www-data YOURUSERNAME

chgrp -R www-data www/
chmod -R 775 www/
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now our user and nginx should be part of www-data group and own /var/www&lt;/p&gt;
&lt;h2&gt;Nginx Config&lt;/h2&gt;
&lt;p&gt;Now lets grab the config file form the unicorn repo and place it in the nginx config directory overwriting the default config.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;cd /etc/nginx/
cp nginx.conf nginx.conf.old
rm nginx.conf
wget https://raw.github.com/defunkt/unicorn/master/examples/nginx.conf
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now we need to setup our paths in the nginx.conf file.&lt;/p&gt;
&lt;p&gt;Uncomment this line:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;listen 80 default deferred; # for Linux
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Change the root paths to there our apps public dir is:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;root /var/www/testapp/public;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Save and restart nginx&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;/etc/init.d/nginx restart
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now lets get unicorn setup with init.d and make a test app in the www dir via our other user.&lt;/p&gt;
&lt;h2&gt;Create the test app&lt;/h2&gt;
&lt;p&gt;Login as your normal user and create a new app called "testapp" in /var/www&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;rails new testapp
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Everything should go smooth if you have things setup right.  You don't need root or sudo to make the test app in var/www because we added ourself and nginx to the group that owns the folder.  Lets cd into the test app's config dir and download the unicorn config file.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;cd testapp/config/
wget -O unicorn.rb https://raw.github.com/defunkt/unicorn/master/examples/unicorn.conf.rb
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Vim or nano into it and setup the paths to your app.  Here is my config:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;# Sample verbose configuration file for Unicorn (not Rack)
#
# This configuration file documents many features of Unicorn
# that may not be needed for some applications. See
# http://unicorn.bogomips.org/examples/unicorn.conf.minimal.rb
# for a much simpler configuration file.
#
# See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete
# documentation.

# Use at least one worker per core if you&amp;#39;re on a dedicated server,
# more will usually help for _short_ waits on databases/caches.
worker_processes 4

# Since Unicorn is never exposed to outside clients, it does not need to
# run on the standard HTTP port (80), there is no reason to start Unicorn
# as root unless it&amp;#39;s from system init scripts.
# If running the master process as root and the workers as an unprivileged
# user, do this to switch euid/egid in the workers (also chowns logs):
# user &amp;quot;unprivileged_user&amp;quot;, &amp;quot;unprivileged_group&amp;quot;

# Help ensure your application will always spawn in the symlinked
# &amp;quot;current&amp;quot; directory that Capistrano sets up.
working_directory &amp;quot;/var/www/testapp&amp;quot; # available in 0.94.0+

# listen on both a Unix domain socket and a TCP port,
# we use a shorter backlog for quicker failover when busy
listen &amp;quot;/tmp/.sock&amp;quot;, :backlog =&amp;gt; 64
listen 8080, :tcp_nopush =&amp;gt; true

# nuke workers after 30 seconds instead of 60 seconds (the default)
timeout 30

# feel free to point this anywhere accessible on the filesystem
pid &amp;quot;/var/www/testapp/shared/pids/unicorn.pid&amp;quot;

# By default, the Unicorn logger will write to stderr.
# Additionally, ome applications/frameworks log to stderr or stdout,
# so prevent them from going to /dev/null when daemonized here:
stderr_path &amp;quot;/var/www/testapp/shared/log/unicorn.stderr.log&amp;quot;
stdout_path &amp;quot;/var/www/testapp/shared/log/unicorn.stdout.log&amp;quot;

# combine Ruby 2.0.0dev or REE with &amp;quot;preload_app true&amp;quot; for memory savings
# http://rubyenterpriseedition.com/faq.html#adapt_apps_for_cow
preload_app true
GC.respond_to?(:copy_on_write_friendly=) and
  GC.copy_on_write_friendly = true

before_fork do |server, worker|
  # the following is highly recomended for Rails + &amp;quot;preload_app true&amp;quot;
  # as there&amp;#39;s no need for the master process to hold a connection
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!

  # The following is only recommended for memory/DB-constrained
  # installations.  It is not needed if your system can house
  # twice as many worker_processes as you have configured.
  #
  # # This allows a new master process to incrementally
  # # phase out the old master process with SIGTTOU to avoid a
  # # thundering herd (especially in the &amp;quot;preload_app false&amp;quot; case)
  # # when doing a transparent upgrade.  The last worker spawned
  # # will then kill off the old master process with a SIGQUIT.
  # old_pid = &amp;quot;#{server.config[:pid]}.oldbin&amp;quot;
  # if old_pid != server.pid
  #   begin
  #     sig = (worker.nr + 1) &amp;gt;= server.worker_processes ? :QUIT : :TTOU
  #     Process.kill(sig, File.read(old_pid).to_i)
  #   rescue Errno::ENOENT, Errno::ESRCH
  #   end
  # end
  #
  # Throttle the master from forking too quickly by sleeping.  Due
  # to the implementation of standard Unix signal handlers, this
  # helps (but does not completely) prevent identical, repeated signals
  # from being lost when the receiving process is busy.
  # sleep 1
end


after_fork do |server, worker|
  # per-process listener ports for debugging/admin/migrations
  # addr = &amp;quot;127.0.0.1:#{9293 + worker.nr}&amp;quot;
  # server.listen(addr, :tries =&amp;gt; -1, :delay =&amp;gt; 5, :tcp_nopush =&amp;gt; true)

  # the following is *required* for Rails + &amp;quot;preload_app true&amp;quot;,
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection

  # if preload_app is true, then you may also want to check and
  # restart any other shared sockets/descriptors such as Memcached,
  # and Redis.  TokyoCabinet file handles are safe to reuse
  # between any number of forked children (assuming your kernel
  # correctly implements pread()/pwrite() system calls)
end
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;They are easily found and just point them to your app dir.&lt;/p&gt;
&lt;p&gt;![Unicorn Config][8]&lt;/p&gt;
&lt;p&gt;Now lets run unicorn and test out our app.  Make two new folders in your app before login back in as root and running unicorn.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;mkdir /shared
cd shared/
mkdir pids
mkdir log
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Login as root to run unicorn:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;unicorn_rails -c /var/www/testapp/config/unicorn.rb -D
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now go to your server ip in your browser and you should see the same thing with the ability to click the envirionment link with data displayed.  If you do no see any data then unicorn is not running or setup correctly.  If you do see it then we are done.  It's working and ready to go.  You may want to add unicorn to an init.d script so that it starts with your server.&lt;/p&gt;
&lt;h2&gt;Unicorn init.d&lt;/h2&gt;
&lt;p&gt;As root:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;cd /etc/init.d/
vim unicorn
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Here is the init.d script that I am using so just paste it into the file.&lt;/p&gt;
&lt;table class="highlighttable"&gt;&lt;tr&gt;&lt;td class="linenos"&gt;&lt;div class="linenodiv"&gt;&lt;pre&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class="code"&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c"&gt;#! /bin/sh&lt;/span&gt;

&lt;span class="c"&gt;# File: /etc/init.d/unicorn&lt;/span&gt;

&lt;span class="c"&gt;### BEGIN INIT INFO&lt;/span&gt;
&lt;span class="c"&gt;# Provides:          unicorn&lt;/span&gt;
&lt;span class="c"&gt;# Required-Start:    $local_fs $remote_fs $network $syslog&lt;/span&gt;
&lt;span class="c"&gt;# Required-Stop:     $local_fs $remote_fs $network $syslog&lt;/span&gt;
&lt;span class="c"&gt;# Default-Start:     2 3 4 5&lt;/span&gt;
&lt;span class="c"&gt;# Default-Stop:      0 1 6&lt;/span&gt;
&lt;span class="c"&gt;# Short-Description: starts the unicorn web server&lt;/span&gt;
&lt;span class="c"&gt;# Description:       starts unicorn&lt;/span&gt;
&lt;span class="c"&gt;### END INIT INFO&lt;/span&gt;

&lt;span class="nv"&gt;DAEMON&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/usr/bin/unicorn
&lt;span class="nv"&gt;DAEMON_OPTS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;-c /var/www/testapp/config/unicorn.rb -D&amp;quot;&lt;/span&gt;
&lt;span class="nv"&gt;NAME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;unicorn
&lt;span class="nv"&gt;DESC&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Unicorn app for testapp&amp;quot;&lt;/span&gt;
&lt;span class="nv"&gt;PID&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/var/web/testapp/shared/pid/unicorn.pid

&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt; in
  start&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;echo&lt;/span&gt; -n &lt;span class="s2"&gt;&amp;quot;Starting &lt;/span&gt;&lt;span class="nv"&gt;$DESC&lt;/span&gt;&lt;span class="s2"&gt;: &amp;quot;&lt;/span&gt;
    &lt;span class="nv"&gt;$DAEMON&lt;/span&gt; &lt;span class="nv"&gt;$DAEMON_OPTS&lt;/span&gt;
    &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="nv"&gt;$NAME&lt;/span&gt;&lt;span class="s2"&gt;.&amp;quot;&lt;/span&gt;
    &lt;span class="p"&gt;;;&lt;/span&gt;
  stop&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;echo&lt;/span&gt; -n &lt;span class="s2"&gt;&amp;quot;Stopping &lt;/span&gt;&lt;span class="nv"&gt;$DESC&lt;/span&gt;&lt;span class="s2"&gt;: &amp;quot;&lt;/span&gt;
        &lt;span class="nb"&gt;kill&lt;/span&gt; -QUIT &lt;span class="sb"&gt;`&lt;/span&gt;cat &lt;span class="nv"&gt;$PID&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;
    &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="nv"&gt;$NAME&lt;/span&gt;&lt;span class="s2"&gt;.&amp;quot;&lt;/span&gt;
    &lt;span class="p"&gt;;;&lt;/span&gt;
  restart&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;echo&lt;/span&gt; -n &lt;span class="s2"&gt;&amp;quot;Restarting &lt;/span&gt;&lt;span class="nv"&gt;$DESC&lt;/span&gt;&lt;span class="s2"&gt;: &amp;quot;&lt;/span&gt;
        &lt;span class="nb"&gt;kill&lt;/span&gt; -QUIT &lt;span class="sb"&gt;`&lt;/span&gt;cat &lt;span class="nv"&gt;$PID&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;
    sleep 1
    &lt;span class="nv"&gt;$DAEMON&lt;/span&gt; &lt;span class="nv"&gt;$DAEMON_OPTS&lt;/span&gt;
    &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="nv"&gt;$NAME&lt;/span&gt;&lt;span class="s2"&gt;.&amp;quot;&lt;/span&gt;
    &lt;span class="p"&gt;;;&lt;/span&gt;
  reload&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="nb"&gt;echo&lt;/span&gt; -n &lt;span class="s2"&gt;&amp;quot;Reloading &lt;/span&gt;&lt;span class="nv"&gt;$DESC&lt;/span&gt;&lt;span class="s2"&gt; configuration: &amp;quot;&lt;/span&gt;
        &lt;span class="nb"&gt;kill&lt;/span&gt; -HUP &lt;span class="sb"&gt;`&lt;/span&gt;cat &lt;span class="nv"&gt;$PID&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;
        &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="nv"&gt;$NAME&lt;/span&gt;&lt;span class="s2"&gt;.&amp;quot;&lt;/span&gt;
        &lt;span class="p"&gt;;;&lt;/span&gt;
  *&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Usage: &lt;/span&gt;&lt;span class="nv"&gt;$NAME&lt;/span&gt;&lt;span class="s2"&gt; {start|stop|restart|reload}&amp;quot;&lt;/span&gt; &amp;gt;&lt;span class="p"&gt;&amp;amp;&lt;/span&gt;2
    &lt;span class="nb"&gt;exit &lt;/span&gt;1
    &lt;span class="p"&gt;;;&lt;/span&gt;
&lt;span class="k"&gt;esac&lt;/span&gt;

&lt;span class="nb"&gt;exit &lt;/span&gt;0
&lt;/pre&gt;&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;Edit to your correct paths then:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;chmod +x unicorn
/usr/sbin/update-rc.d -f unicorn defaults
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Reboot and see if nginx and unicorn are successfully servering your app.  &lt;/p&gt;
&lt;p&gt;Everything should work on the defult page via port 80.  If you do not see images or the environment info that means unicorn is not running.  Check the log files that we setup in the config ( the paths ) and see what is happening.  I hope this tutorial works for everyone.  I had a lot of trouble setting this up so hopefully you will not repeat my pain.&lt;/p&gt;</summary></entry><entry><title>Multithreading in C#</title><link href="http://crosbymichael.com/multithreading-in-c.html" rel="alternate"></link><updated>2012-01-28T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2012-01-28:multithreading-in-c.html</id><summary type="html">&lt;p&gt;From my point of view the reason why we create multithreaded applications is so the user experience does not suffer.  We have operations that need completed but running these on the main thread will block the UI and piss off our users.&lt;/p&gt;
&lt;p&gt;Most of the operations have some similarities.  Atleast for myself, the most common use case is the user requests information, we process it, then display the results back.  That is the most common use that I use background threads for.  When we display these results back, we need to write code that is executed on the Main thread to update the UI.&lt;/p&gt;
&lt;p&gt;Working with delegates, callbacks, and IAsyncResults suck.  They cause you to write more methods and change your code.  There should be an easy way to write code normally and have all the benefits of async programming.  That is what I try to accomplish in my WPF MVVM-Async framework.  It allows you to write code normally and create multithreading applications with lambdas.&lt;/p&gt;
&lt;h2&gt;MVVM-Async&lt;/h2&gt;
&lt;p&gt;So first thing that you need to do to take advantage of the framework is to make your ViewModel inherit from ViewModelBase and pass it a &lt;T&gt; type for the model.  By doing this you automatically get access to the Model object.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;public class EditorViewModel : ViewModelBase&amp;lt;EditorModel&amp;gt;
...
this.Model.EditorModelProperties...
&lt;/pre&gt;&lt;/div&gt;


&lt;h2&gt;Async&lt;/h2&gt;
&lt;p&gt;Now just code and when you want to dispatch a process in a background thread call DispatchAsync() and pass it a lambda.  &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;DispatchAsync(()=&amp;gt; {

    int i = 0;
    bla bla bla

});
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The framework will automatically spawn a new thread and execute whatever you placed inside the lambda on that thread.  You can even call another method and pass any variables to it.  By using lambdas you stay in the scope of the method calling DispatchAsync.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;DispatchAsync(() =&amp;gt; {

    SomeOtherFunc(name, date, age);

});
&lt;/pre&gt;&lt;/div&gt;


&lt;h2&gt;Updating the UI&lt;/h2&gt;
&lt;p&gt;So now we have this lambda executing on a background thread and processing data.  We need to take that data and update our UI.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;DispatchAsync(() =&amp;gt; {

    string fullName = GetFullName(first, last);

    MyViewsTextBoxDataSource = fullName;

});
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This is very simple but you get the point.  If we were to try to set one of our view's controls from a background thread we would throw an exception because you can only set Controls on the thread that created it.  So without using callbacks we can do this easily with the framework.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;DispatchAsync(() =&amp;gt; {

    string fullName = GetFullName(first, last);

    DispatchMainAsync(() =&amp;gt; {

        MyViewsTextBoxDataSource = fullName;

    });

});
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;How hard was that?  If we want to dispatch a lambda to the main thread that updates the UI, we can write it inline, with all our variables in scope, and have that code executed on the main thread.  No exceptions.  &lt;/p&gt;
&lt;p&gt;Easy, simple, and you don't have to have callbacks or other crap to mess with.  If you do want a callback when a thread has finished executing the framework does support this.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;void DispatchAsyncWithCallback(action, callback);
void DispatchAsyncWithCallback(action, callback, whereTo);
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The whereTo tells the framework on what thread do you want the callback executed one.  Call me back on a background thread or call me back on the main thread. &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;DispatchAsyncWithCallback(() =&amp;gt; {

    doSomeStuff();

}, () =&amp;gt; {

    doSomeStuffOnTheCallback();

}, DispatchOn.Main); //Dispatch the callback on the main thread.
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;It's all done inline.&lt;/p&gt;
&lt;h2&gt;Queues&lt;/h2&gt;
&lt;p&gt;There is also support of Queues.  You can load up a DispatchQueue with tasks and invoke the entire queue.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;void AddQueue(DispatchQueue queue);
void InvokeQueue(string name);
void ProcessAllQueues();
&lt;/pre&gt;&lt;/div&gt;


&lt;h2&gt;Timed Execution&lt;/h2&gt;
&lt;p&gt;You can also tell the framework when to dispatch a thread.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;DispatchIn(() =&amp;gt; { somethingToDo(); }, &amp;quot;5min&amp;quot;);
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Or at a specific time.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;DateTime someTime;
DispatchAt(() =&amp;gt; { somethingToDo(); }, someTime);
&lt;/pre&gt;&lt;/div&gt;


&lt;h2&gt;Final&lt;/h2&gt;
&lt;p&gt;I am still working on this and will be releasing the code soon after CodeAssistant is complete.  CodeAssistant is using this framework.&lt;/p&gt;</summary></entry><entry><title>tCal - iCal events in terminal</title><link href="http://crosbymichael.com/tcal-ical-events-in-terminal.html" rel="alternate"></link><updated>2012-01-14T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2012-01-14:tcal-ical-events-in-terminal.html</id><summary type="html">&lt;p&gt;I just puked up some old code for a little app that displays your iCal events in the terminal.  I have it as one of my startup scripts so when I start a new bash session I see my events for the next 7 days.  It's a very simple but cool app.  I am not proud of the design but I just cleaned up the code tonight and threw it on &lt;a href="http://goo.gl/CNyGZ"&gt;tCal on Github&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;Usage&lt;/h2&gt;
&lt;p&gt;It is very simple to use.  Just throw it in a folder that is in your PATH and type tCal to see your events for the next 5 days (default).  if you want more or less days then just:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;tCal -d 20
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;There is no config to setup with your calendars, it just works.&lt;/p&gt;
&lt;p&gt;Here is a little taste for the code.  Head over to Github for it all.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[])&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;@autoreleasepool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;CMCalendar&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;calendar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;CMCalendar&lt;/span&gt; &lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="n"&gt;CMArgumentParser&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;parser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;CMArgumentParser&lt;/span&gt; &lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="n"&gt;NSInteger&lt;/span&gt; &lt;span class="n"&gt;days&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;@try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;days&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="bp"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="n"&gt;parser&lt;/span&gt; &lt;span class="nl"&gt;argumentForName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s"&gt;@&amp;quot;-d&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;integerValue&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;@catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;NSException&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;exception&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;days&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DEFAULTDAYS&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="bp"&gt;NSArray&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;calendar&lt;/span&gt; &lt;span class="nl"&gt;getEventsForDays&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;days&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ITerminalOutput&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="n"&gt;display&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;If you have any ideas just send them to me.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://dev.crosbymichael.com/downloads/?file=tCal-1.0.zip"&gt;Download Binary&lt;/a&gt;&lt;/p&gt;</summary></entry><entry><title>Bash Informational Startup</title><link href="http://crosbymichael.com/bash-informational-startup.html" rel="alternate"></link><updated>2011-11-11T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2011-11-11:bash-informational-startup.html</id><summary type="html">&lt;p&gt;If you use Terminal on your Mac like I do.  It is the first application that is run at starup and the last to close.  When you start Terminal in a new window or tab that is a great time to review your computer stats, iCal events, and disk sizes.  I saw a lifehacker post abut a menu bar app that shows all the disks that are connected to your Mac and their size and free space.  I also use todotxt for my todo lists and because it does not support dates for your tasks I keep my dates for events in iCal.  I wanted to see all of this whenever I startup a new bash session so here is how I did it.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Bash Startup" src="static/images/bash-start-info.png" /&gt;&lt;/p&gt;
&lt;p&gt;So this is what I see when I start a new bash session.  All my information that I want is there plain text and easy to read.  &lt;/p&gt;
&lt;p&gt;There is my computers uptime at the very top with load averages.&lt;/p&gt;
&lt;p&gt;Next is all the disk space information of my internal hard drive and external USB hard drives.  This is a straight forward easy command.&lt;/p&gt;
&lt;p&gt;Next up is my iCal events for today and tommorrow.  You can easily set how many days forward you want.  This is done by a simple Objective - C command line app that I wrote.  &lt;/p&gt;
&lt;p&gt;Last is todotxt.  All my tasks.  &lt;/p&gt;
&lt;p&gt;So here is the last part of my bash_profile that makes this all happen.  You can download all my app that displays your iCal events from my github of dotfiles (it's in the bin dir).  You will also find all kinds of awesome stuff in it. &lt;/p&gt;
&lt;h2&gt;End of my .bash_profile&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;echo &amp;quot;-----------------------------------------------&amp;quot;
#Display uptime
uptime
echo &amp;quot;-----------------------------------------------&amp;quot;
#Display disk space usage
df -hl
echo &amp;quot;-----------------------------------------------&amp;quot;
echo &amp;quot;Up coming events:&amp;quot;
echo &amp;quot;-----------------&amp;quot;
#Call to my cmd line app to show iCal events
CommandLineAssistant
echo &amp;quot;-----------------------------------------------&amp;quot;
echo &amp;quot;Todo Tasks:&amp;quot;
echo &amp;quot;-----------&amp;quot;
#List my todos 
todo list
echo &amp;quot;&amp;quot;
&lt;/pre&gt;&lt;/div&gt;</summary></entry><entry><title>Compile Vim for Python</title><link href="http://crosbymichael.com/compile-vim-for-python.html" rel="alternate"></link><updated>2011-10-13T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2011-10-13:compile-vim-for-python.html</id><summary type="html">&lt;h2&gt;Update&lt;/h2&gt;
&lt;p&gt;I just added the newest MacVim compiled for python using the 10.7 Lion SDK.  You can downloading it below.&lt;/p&gt;
&lt;p&gt;If you want to use the Python auto complete in vim then you have to compile vim for python on your system. If you are running OSX I have just compiled MacVim for Snow Leopard and I am working on a compiled Lion version but the SL one works fine.  Then you can setup an alias for vim so that it points to the binary in the MacVim app so you can still use vim from the command line and as the MacVim app.&lt;/p&gt;
&lt;p&gt;It works really well and is fairly easy this way.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://dev.crosbymichael.com/downloads/?file=MacVimSL.tbz"&gt;MacVim with Python SL&lt;/a&gt;
&lt;a href="http://dev.crosbymichael.com/downloads/?file=MacVim-Lion.tar.bz2"&gt;MacVim with Python Lion&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;First download the already compiled MacVim.app from the link above and extract it to your Applications folder.&lt;/p&gt;
&lt;p&gt;Then download:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.vim.org/scripts/download_script.php?src_id=10872"&gt;pythoncomplete.vim&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;vim files and put it in your ~/.vim/autoload/ folder.  It should be named:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;pythoncomplete.vim
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;If you need, you can see my .dotfiles on github that already has my vimrc and vim dir in it.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/crosbymichael/.dotfiles"&gt;My dotfiles&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Then just add:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;filetype plugin on
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Last thing if you want to run this version form terminal just add:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;alias vim=/Applications/MacVim.app/Contents/MacOS/Vim
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;to your ~/.bash_profile&lt;/p&gt;
&lt;p&gt;You can see all these settings and more in my dotfiles.&lt;/p&gt;
&lt;p&gt;All done.  To use the auto complete just hit control+x then still holding down the control key it o&lt;/p&gt;</summary></entry><entry><title>Python talking to Arduino</title><link href="http://crosbymichael.com/python-talking-to-arduino.html" rel="alternate"></link><updated>2011-10-08T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2011-10-08:python-talking-to-arduino.html</id><summary type="html">&lt;p&gt;I just setup my Arduino with an LCD display.  You can follow the tutorial &lt;a href="http://arduino.cc/en/Tutorial/LiquidCrystalSerial"&gt;here&lt;/a&gt; to get the LCD and board wired up.  We will also be using the sample code from that tutorial.&lt;/p&gt;
&lt;h2&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The issue that I ran into was displaying messages from the Arduino IDE's SerialMonitor worked fine but when I was trying to display a message that I sent via python, it was not showing on the screen.  After some help I finally have it working so this is how you fix it.&lt;/p&gt;
&lt;h2&gt;Python Code&lt;/h2&gt;
&lt;p&gt;You will need to install pyserial so that you can talk to your board via USB.  It's simple if you have build tools installed so just:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;sudo easy_install pyserial
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now if we look at the code that does not work first you can see what you need to change.  We need to import serial and then setup the connection to the board.  &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;serial&lt;/span&gt; 
&lt;span class="n"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;serial&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Serial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;pathtoyourboard&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9600&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;You will need to look in your Arduino IDE for the path to your board and the 9600 is the rate that is definded in the Arduino code that we are loading onto it.  Now we need to write some data do the Arduino and close the connection.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;port.write(&amp;#39;sometext&amp;#39;)
port.close()
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;If we load the code from the tutorial that I linked at the beginning of this post onto your board and then ran this python code, we would just see a blank LCD.  The probelm is that whenever the serial connection is first made, it takes the Arduino two seconds to get that port open and setup.  So for our python code to work we need to import the time module and add a sleep for atleast 2 seconds right after our port = serial.Serial statement.  Here is the what the final python code will look like with it working and the message changed.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;serial&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;time&lt;/span&gt;

&lt;span class="n"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;serial&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Serial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;/dev/tty.usbmodemfd141&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9600&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;My&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;name&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;is&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;michael&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;So this code makes the connection and waits 2 seconds for the Arduino to get setup, then starts writing data to it.  Everything works find and it's good to go.  So in the future when you are working with Python and Arduino via USB, wait 2 seconds before sending data to it or it will not get it.  &lt;/p&gt;</summary></entry><entry><title>How to backup gmail the right way</title><link href="http://crosbymichael.com/how-to-backup-gmail-the-right-way.html" rel="alternate"></link><updated>2011-10-04T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2011-10-04:how-to-backup-gmail-the-right-way.html</id><summary type="html">&lt;p&gt;So like many people my life is stored in my gmail.  I trust google to keep my data safe but since so many things, from contacts to many different online accounts, are all registered under your gmail address it is important to have that data whenever you need it.  There are some services online that will backup your gmail but it is still stored on their servers.  I feel a lot better if it was stored locally on my computers so that I have a copy of my emails whenever I need them.  So this is how you do it.&lt;/p&gt;
&lt;h2&gt;Got Your Back&lt;/h2&gt;
&lt;p&gt;Got Your Back is a python app that will connect to your gmail via oAuth and suck down your messages in simple txt files.  They have an *.eml extension but you can open all these files with a text editor and see the contents of your emails.  It is fast and also does incremental backups.  So after the initial backup of your account, Got Your Back will only download the messages that it doe not have instead of downloading everything each time you do a backup.  I use a simple cron job to get all my new messages everyday, automatically without having to think about it.  &lt;/p&gt;
&lt;h2&gt;Installing&lt;/h2&gt;
&lt;p&gt;You can get to the download page of got your back &lt;a href="" title="http://code.google.com/p/got-your-back/downloads/list"&gt;here.&lt;/a&gt;  They have downloads for Unix and Windows systems.  I have an server setup running debian that handles all my account backups so this tutorial will focus on running gyb (got your back) on Unix systems.  So download the &lt;a href="" title="http://code.google.com/p/got-your-back/downloads/detail?name=gyb-alpha-17-python-src.zip&amp;amp;can=2&amp;amp;q="&gt;source package&lt;/a&gt; or use subversion to download the source onto your computer:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;svn checkout http://got-your-back.googlecode.com/svn/trunk/ got-your-back-read-only
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;If you download the zip extract it to whatever folder you want and cd into the directory.  Now because gyb uses oAuth to login to your account you will need to initially run this script on a computer with a desktop.  Because I use a server over ssh to download my backups I need to first get the oAuth token on a computer with a web browser first.&lt;/p&gt;
&lt;h2&gt;Getting oAuth Tokens&lt;/h2&gt;
&lt;p&gt;So now that we are cd into the got-your-back directory run this command to authenticate and get an estimate of the size and number of messages that need to be backed up.  &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;python gyb --email youremail@gmail.com --estimate
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;So make sure that you input your correct email address.  If you are already logged into your email account on your computer and you are trying to backup a different account, you will need to logout first.  So gyb will tell you that it will launch a browser, so just hit Enter and you will see a gmail page asking if you want to authenticate gyb to your gmail.  Hit yes and then go back to the command line.  No hit Enter again if it is still waiting and wait for it to make an estimate of your gmail's size and number of messages.  &lt;/p&gt;
&lt;p&gt;So now it should give you an estimate.  I had 28k messages to download at 800mb.  It does not download attachments.  If you do an ls now you will see a new file.  It will be a file with your email address and an extension of .cfg.  This file now holds your oAuth token so if you are going to run the backups on a server over ssh, you will have to copy this file over to your server for it to work.  So either zip, tar, or 7z the entire gyb directory with your email's config file in it and transfer it to your server.  If you are just going to run it on the same computer then you don't have to do anything yet.  &lt;/p&gt;
&lt;h2&gt;Inital backup&lt;/h2&gt;
&lt;p&gt;So now that you have the config file and gyb on whatever computer that you want to setup the automated backups on lets run the initial backup.  First lets make the gyb.py file runnable.  &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;chmod 755 gyb.py
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;So run this command to perform the initial backup.  &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;python gyb.py --email youremail@gmail.com --backup
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This will perform the initial backup so depending on how many messages your estimate was, this make take awhile.  &lt;/p&gt;
&lt;h2&gt;Cron job for everyday backups&lt;/h2&gt;
&lt;p&gt;So now that we have an initial backup we will need to do an incremental backup everyday for all of the new messages that we receive.  So lets add a cron job with crontab that will run gyb.py everyday.  So remember where that file is on your computer because we will need it. &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;crontab -e
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Then input this into your crontab file with the proper changes.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;0 2 * * * python /home/michael/got-your-back-read-only/gyb.py --email youremail@gmail.com --backup
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;So what this does is make a backup at 3am everyday running gyb.py with your specified email.  Easy and done.  It will now do incremental backups of only your new messages and you will always be able to get that data because it is now backed up locally on your computer or server.  &lt;/p&gt;</summary></entry><entry><title>How to manage your website with git</title><link href="http://crosbymichael.com/how-to-manage-your-website-with-git.html" rel="alternate"></link><updated>2011-09-28T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2011-09-28:how-to-manage-your-website-with-git.html</id><summary type="html">&lt;p&gt;So this is much easier if you have a static site that is generated using pelican or jerkyll.  Both of these apps are great for generating blogs out of markdown or other simple text markup language.  I use pelican because it is built with python and that is what I develop in.  &lt;/p&gt;
&lt;h2&gt;Local&lt;/h2&gt;
&lt;p&gt;So on your local machine you will need your basic folder structure setup and if you are not already using git to manage your files, now is the time to start.  In terminal cd into your site dir and:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;git init
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This is create the local git repo that you need.  Go ahead and commit the files that you already have done and now we can work on the server.  &lt;/p&gt;
&lt;h2&gt;Server&lt;/h2&gt;
&lt;p&gt;Make sure that you have ssh access to your web server.  The first thing you need to do is cd into your users main directory, not your www or directory that hosts the files that are displayed to your users and we will make a new directory:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;mkdir cmsite.git
cd cmsite.git
git init --bare
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This will create a bare git repo that will accept the files when you do a push to it.  Now we will need to setup a hook that will checkout the latest push into the actual directory that hosts our content.  So now we need to add the hook the the hooks directory in cmsite.git in my case.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;vim hooks/post-receive
#!/bin/sh
GIT_WORK_TREE=/home/crosbymichael/crosbymichael.com git checkout -f
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Make sure that this file is executable so do a quick:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;chmod 755 hooks/post-receive
&lt;/pre&gt;&lt;/div&gt;


&lt;h2&gt;Final Steps&lt;/h2&gt;
&lt;p&gt;Now we just need to setup our new remote repo on our local machine.  So cd into your local git repo and type:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;git remote add website ssh://username@yoursite.com/~/cmsite.git
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now all your have to do is push to production when you need to update your site and git does all the rest.  &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;git push website master
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Because you can have more than one remote in git, you could have a test site and your production site, and another offsite repo for backups.  You can run tests on your test site and then when everything is good to go you just push to production.  &lt;/p&gt;</summary></entry><entry><title>AVAudioPlayer Memory Leak</title><link href="http://crosbymichael.com/avaudioplayer-memory-leak.html" rel="alternate"></link><updated>2011-09-11T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2011-09-11:avaudioplayer-memory-leak.html</id><summary type="html">&lt;p&gt;This is a little bug in the AVFoundation framework. When using the AVAudioPlayer in &lt;AVFoundation/AVAudioPlayer.h&gt; you will get memory leaks when running in the iOS Simulator but not on devices.&lt;/p&gt;
&lt;p&gt;It is a pain whenever you do most of your testing on the desktop during development and then start testing on device after you have a working app. So just remember if you are using AVAudioPlayer on the iOS Simulator and you are getting memory leaks that look like this:&lt;/p&gt;
&lt;p&gt;&lt;img alt="AVAudio Leak" src="static/images/avaudio.png" /&gt;&lt;/p&gt;
&lt;p&gt;Look at the type of leaks.
note the responsible library is AudioToolbox not even AVFoundation, go ahead and profile it on a device and see if the leaks go away. If they don't, then it's something that you need to fix in your code, if they do then you are good to go.&lt;/p&gt;</summary></entry><entry><title>SVN to GIT and Extracting Individual Projects</title><link href="http://crosbymichael.com/svn-to-git-and-extracting-individual-projects.html" rel="alternate"></link><updated>2011-08-15T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2011-08-15:svn-to-git-and-extracting-individual-projects.html</id><summary type="html">&lt;h4&gt;This post is still not formatting.  I switched from worpress and this is a long post and have not had time to update it.&lt;/h4&gt;
&lt;p&gt;So here is the issue.  You first started to use version control and because you were coding solo and the low learning curve, you started to use Subversion (SVN).  It worked great for you because you had your own repository and you added all your projects to it and it was easy.  Then you started to work with other people on projects and the very first commit that you made to your svn repo ended in conflicts.  Not a good way to start.  So you started to use more powerful version controle systems like GIT and now you wonder why you ever used svn in the first place.&lt;/p&gt;
&lt;p&gt;However, you have a years worth of revision history sitting in your svn repository that you want out and placed into your shinny new git repo.  You have already been working in your git repo for a few months, you just checked out the latest copy from your svn repo and added it to a new git repo, and now you have a branches and commits of all your recent work.  It's time to convert your svn to git and unite your past revision history with the present.  Lets get to work.&lt;/p&gt;
&lt;p&gt;Video is at the bottom if this is confusing.&lt;/p&gt;
&lt;p&gt;Problems:&lt;/p&gt;
&lt;p&gt;SVN repo has many different projects in the one large repository&lt;/p&gt;
&lt;p&gt;SVN has a year worth of revision history with commits from many different projects&lt;/p&gt;
&lt;p&gt;You checked out the latest copy from svn and made git repositories for each of your projects&lt;/p&gt;
&lt;p&gt;These individual project git repos now have many commits of their own and branches of your recent work&lt;/p&gt;
&lt;p&gt;Goal:&lt;/p&gt;
&lt;p&gt;Extract individual projects with only their complete revision history from svn and merge that with your current work and revision history in your individual git repos.&lt;/p&gt;
&lt;p&gt;Lets look at our starting point.  SVN revision history and GIT revision history.&lt;/p&gt;
&lt;p&gt;Last svn file change and git's last change.  You can see that the files are different.  SVN on left and GIT on right.&lt;/p&gt;
&lt;p&gt;1:&lt;/p&gt;
&lt;p&gt;First we need to suck our svn repo down to a machine where we have shell access.  I had a large 4GB svn repo with 25+ projects in it.  It had 280 commits from various projects.  ie: commit 278 from Encoder Assistant changes, commit 279 from HTTP Assistant changes, and commit 280 from another project.  So the svn repo was tracking overall revision history for the entire repo and not individual projects.  This was a mistake on my part, I was new to svn and my hosting provider offered free private svn repos to use.  So it was a free way for me to have revision history and also offsite backup of my code.&lt;/p&gt;
&lt;p&gt;To grab your svn repo from various locations you need to first make a new folder and cd into it.  Yes, you will have to use the terminal.&lt;/p&gt;
&lt;p&gt;git svn clone url-to-repo&lt;/p&gt;
&lt;p&gt;(not the working copy, the actual svn repo)&lt;/p&gt;
&lt;p&gt;Local file:///Users/michael/svnrepo&lt;/p&gt;
&lt;p&gt;Http  http://yoursite.com/svnrepo&lt;/p&gt;
&lt;p&gt;SSH ssh://username@yoursite.com/svnrepo&lt;/p&gt;
&lt;p&gt;So to cone I will do this:&lt;/p&gt;
&lt;p&gt;[highlight]git svn clone file:///Users/michael/Development/Video/WIP/SVNTOGIT/[/highlight]&lt;/p&gt;
&lt;p&gt;If you get an error with the wrong url to the svn repo then you will have to delete any folders in your current folder before you can try again.&lt;/p&gt;
&lt;p&gt;We can see by the terminal output and then doing an ls that it received 3 revisions(commits) and created a new folder of the svn repo name.  Now cd into it and you can see that there are two projects exactly in the same as the svn repo.  If we want to check to see if git cloned the full revision history we can type:&lt;/p&gt;
&lt;p&gt;[highlight]git log[/highlight]&lt;/p&gt;
&lt;p&gt;See it's all there.&lt;/p&gt;
&lt;p&gt;If you only had one project in your svn repo then you can skip the next step.&lt;/p&gt;
&lt;p&gt;2:&lt;/p&gt;
&lt;p&gt;Now we need to extract one project out of this new git repo.  We only want one project per repo in our new setup.  To do this we need to do a filter.&lt;/p&gt;
&lt;p&gt;Lets extract the contents of ProjectOne to the root dir of this new git repo.  (If you have more than one project you will want to clone this git repo so that you can keep do this over and over again for each project.)&lt;/p&gt;
&lt;p&gt;[highlight]git filter-branch --subdirectory-filter ProjectOne/ -- --all[/highlight]&lt;/p&gt;
&lt;p&gt;Now you can see that it rewrote the branch and by doing an ls you can see the contents of ProjectOne are now in the root of your repo.  There is only one code.txt file from this repo.&lt;/p&gt;
&lt;p&gt;Lets do a git log again and see that only the ProjectOne revision history is extracted with the ProjectOne files.&lt;/p&gt;
&lt;p&gt;You can see only two commits, the init and Added more code.  These are only the commits that affected ProjectOne.  Nothing for ProjectTwo.&lt;/p&gt;
&lt;p&gt;Lets make a new dir to keep the same folder structure as your CURRENT git repo that we have been working in.&lt;/p&gt;
&lt;p&gt;[highlight]mkdir ProjectOne[/highlight]&lt;/p&gt;
&lt;p&gt;Now we need to move the files back into this project one folder.&lt;/p&gt;
&lt;p&gt;[highlight]mv * ProjectOne [/highlight]&lt;/p&gt;
&lt;p&gt;This will give you an error about moving ProjectOne into it's self but it works and moves every other file and folder into our new ProjectOne folder.  You can cd into it and check for your self.&lt;/p&gt;
&lt;p&gt;Now we need to commit the changes that we have just done to the repo.&lt;/p&gt;
&lt;p&gt;[highlight]git add .[/highlight]&lt;/p&gt;
&lt;p&gt;This adds and removed all changes that we have made.&lt;/p&gt;
&lt;p&gt;[highlight]git commit -a[/highlight]&lt;/p&gt;
&lt;p&gt;This will commit all the changes we made.  Give it a message for the commit.&lt;/p&gt;
&lt;p&gt;3:&lt;/p&gt;
&lt;p&gt;Now we need to merge the revision history, branches, etc from our CURRENT repo with the new git repo's svn revisions.  We are still in the same folder of the new git repo that we have created.&lt;/p&gt;
&lt;p&gt;Lets create a new branch to add our current work into.  We will now refer to our git repo with the svn history as our OLD repo and the git repo with the CURRENT changes as the CURRENT repo.  This will get confusing to pay attention.&lt;/p&gt;
&lt;p&gt;To create and new branch and check it out at the same time we do:&lt;/p&gt;
&lt;p&gt;[highlight]git checkout -b current-repo[/highlight]&lt;/p&gt;
&lt;p&gt;Now we need to pull in the changes from the CURRENT repo into the current-repo branch of the OLD repo (the one with the svn revision history and extracted ProjectOne)&lt;/p&gt;
&lt;p&gt;git pull pathtorepo&lt;/p&gt;
&lt;p&gt;So in my example&lt;/p&gt;
&lt;p&gt;[highlight]git pull ~/Development/Video/WIP/GitCopy/svn-repo/[/highlight]&lt;/p&gt;
&lt;p&gt;So if you notice, it pulled the CURRENT repo into the OLD repo and when it tried to merge the changes into the branch it received a conflict.  Don't worry, resolving merging conflict's in git is easy.  If you are on OSX like me or have a merge tool setup, ie: FileMerge all you have to do when you receive a conflict on a merge it type:&lt;/p&gt;
&lt;p&gt;[highlight]git mergetool[/highlight]&lt;/p&gt;
&lt;p&gt;It will tell you what file you are going to be working with and when you hit enter, it will launch the tool so you can work on the conflict in the file.&lt;/p&gt;
&lt;p&gt;Now we can see the code on the left is from the last commit from our svn repo and the code on the right is from our CURRENT git repo with all the changes.  Now you just go down through the differences and select the left or right to keep.  After you are finished,  I am just going to keep all the right side changes, save the file and close your merge tool.&lt;/p&gt;
&lt;p&gt;Now if we type:&lt;/p&gt;
&lt;p&gt;[highlight]git status [/highlight]&lt;/p&gt;
&lt;p&gt;We can see that we have the modified file to commit.  If you have untracked files or if you have files with a .orig extension, conflicted files, add or remove those before committing.&lt;/p&gt;
&lt;p&gt;[highlight]rm ProjectOne/code.txt.orig[/highlight]&lt;/p&gt;
&lt;p&gt;Now commit&lt;/p&gt;
&lt;p&gt;[highlight]git commit -a[/highlight]&lt;/p&gt;
&lt;p&gt;You can see that git has already added a comment for you stating that this was a merge from another repo and also adds the repo path or url.  Write and quit your comment tool and it's done.  If we type in:&lt;/p&gt;
&lt;p&gt;[highlight]gitk[/highlight]&lt;/p&gt;
&lt;p&gt;This will bring up an gui to show our git repos history.&lt;/p&gt;
&lt;p&gt;Now you can see that we not only have the Init and  Added more code commit's that were originally from the svn repo but also all the commits from the git project added to our new branch.  If you want to do another merge to merge the changes from the current-repo branch back into the master branch for the project you can do that and now we can finally call this repository our CURRENT, full revision history, repository of awesomeness.&lt;/p&gt;
&lt;p&gt;If you were able to follow along, your welcome.&lt;/p&gt;</summary></entry><entry><title>Setup your own Git Server over SSH</title><link href="http://crosbymichael.com/setup-your-own-git-server-over-ssh.html" rel="alternate"></link><updated>2011-08-08T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2011-08-08:setup-your-own-git-server-over-ssh.html</id><summary type="html">&lt;p&gt;The thing about dvsc that makes me nervous is that all the code and revision history is on your local computer. This makes it very fast when committing, branching, and merging but what if you have a drive failure or worse? All your code is gone if you don't have backups. You have many different ways to backup your git and mercurial repositories. Especially this open-source project that I am working when it is done: DVCS-Backup&lt;/p&gt;
&lt;p&gt;If you want to work with others on a project and need a git server setup to be an intermediary, then this is how you can do it as well as having an offsite backup copy of your repos.&lt;/p&gt;
&lt;p&gt;First find a server that you are going to use. Setup keyless ssh access to it following my tutorial here&lt;/p&gt;
&lt;p&gt;After that ssh into your server and make a new user that will manage the repos if sharing or just user your own account if it's just for you.&lt;/p&gt;
&lt;p&gt;Install git on your server. I run debian so it's a simple:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;apt-get install git-core
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;To get git installed.&lt;/p&gt;
&lt;p&gt;Now create a new folder for the repo that you want to push to. I have many repos on my development computer that I work from so we are going to push an already populated repo to the server.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;mkdir somereponame.git
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;You add the .git to the end of the folder name to that it's easy to distinguish between other folders. .git means this is a git repo.&lt;/p&gt;
&lt;p&gt;Now cd into it and create a bare repo.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;git init --bare
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now that server part is done. Easy.&lt;/p&gt;
&lt;p&gt;Now go to your local dev computer and cd into the repo that you want to push to the server. Make sure that you don't have a remote origin already setup in this repo. To clear it out just:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;git remote rm origin
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now we need to set the origin to your remote server.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;git remote add origin ssh://sshusername@yourserver.com/~/myrepo.git
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now your done. Time to push. If you have more than one branch, you can either push one or all the branches to the server. I just push all of them because I want the server to have a full backup or my local repo.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;git push origin --all
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;But for inividual branches:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;git push origin branchname
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;That's it. Now when you want to get data from the server just do a pull.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;git pull origin branchname
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;If you want to clone the repo to a new folder:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;git clone ssh://sshusernane@yourserver.com/~/myrepo.git
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Or you can make a new dir, cd into it and create a new local repo, set the origin to your server and pull specific branches.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;mkdir newrepo
cd newrepo
git init
git remote add origin ssh://sshusername@yourserver.com/~/myrepo.git
git pull origin branchname
&lt;/pre&gt;&lt;/div&gt;</summary></entry><entry><title>Flex 4.5 vs Pure AS3</title><link href="http://crosbymichael.com/flex-45-vs-pure-as3.html" rel="alternate"></link><updated>2011-08-07T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2011-08-07:flex-45-vs-pure-as3.html</id><summary type="html">&lt;p&gt;I have built a few applications for Android using the Flex framework.  It is a great framework and I can build, test, and publish apps very quickly.  It takes productivity to the max.&lt;/p&gt;
&lt;p&gt;However, I have noticed that some of my apps take a noticeable time to launch on my device for the first time.  Even simple apps like QuickIP where all it does it check for external and wifi IP address.  So I ran a few comparison tests between pure AS3 projects and Flex projects to see three things.  Startup time, file (swf) size, and memory usage.  All important on a mobile device.  Here are the results.&lt;/p&gt;
&lt;h2&gt;Mobile Pure AS3&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Startup Time: 99ms&lt;/li&gt;
&lt;li&gt;SWF Size: 1.5kb&lt;/li&gt;
&lt;li&gt;Memory Usage*: 6.328mb&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Mobile Flex mxml&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Startup Time: 653ms&lt;/li&gt;
&lt;li&gt;SWF Size: 2.839mb&lt;/li&gt;
&lt;li&gt;Memory usage*: 21.397mb&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Memory usage is a sample taken after 5 seconds of the app being launched.  All the apps are just code to test startup time and memory usage after 5 seconds.  The Flex mobile project was a blank project.  Each project launched a view that was run on my desktop, not on a device.&lt;/p&gt;
&lt;h2&gt;So how does this compare?&lt;/h2&gt;
&lt;h2&gt;AS3 over Flex&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;AS3 launches in 15.16% of the time it takes a Flex project&lt;/li&gt;
&lt;li&gt;AS3's file size is .05% of the Flex projects size&lt;/li&gt;
&lt;li&gt;AS3 uses 29.58% of the memory that a Flex project uses&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As for the desktop, the numbers are closer to the same.  Remember that I launched the mobile project on my desktop rather than on a device so the tests have a consistent test environment.&lt;/p&gt;
&lt;h2&gt;Desktop Pure AS3&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Startup Time: 96ms&lt;/li&gt;
&lt;li&gt;SWF Size: 1.5kb&lt;/li&gt;
&lt;li&gt;Memory Usage*: 6.184mb&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Desktop Flex mxml&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Startup Time: 556ms&lt;/li&gt;
&lt;li&gt;SWF Size: 2.114mb&lt;/li&gt;
&lt;li&gt;Memory usage*: 19.021mb&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As you can see the desktop Flex project is a little lighter on the resources than a comparable Mobile Flex app.  I even did some tests on the desktop such as only using spark and no mx.  I have even changed the WindowedApplication to just Application (no visible window as launch) and was able to get the startup time down to 450ms and only using ~15mb of ram.  I tried to do tests with a Flex project's initial application file as a .as instead of a .mxml file but they were unsuccessful in launching a visible window.  However just launching an  initial .as instead of .mxml did make the Flex project have the same ~99ms startup time, ~1.5kb swf size, and ~6mb memory footprint.&lt;/p&gt;
&lt;p&gt;So the question is... Is the speed of productivity in building an app on the Flex framework worth the loss of speed running the app?  I guess it depends on what type of app you are building and what your user base is.  The big difference between the Flex framework and pure As3 is components.  You have to build all the components your self in pure AS3 projects.  But how will productivity change for pure As3 projects after you already have a library of components built?  What if you make all your assets and skin the Flex components anyway with bitmap or fxg assets?  Do the components matter then?&lt;/p&gt;
&lt;p&gt;These are the questions that I am asking my self currently.  I am going to rebuild my QuickIP app with pure AS3 since it has all custom bitmap assets then compare the performance on my Android device and see if the time of development makes a noticeable jump in performance.&lt;/p&gt;</summary></entry><entry><title>Setup Password-less Login over SSH</title><link href="http://crosbymichael.com/setup-password-less-login-over-ssh.html" rel="alternate"></link><updated>2011-08-07T00:00:00+00:00</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2011-08-07:setup-password-less-login-over-ssh.html</id><summary type="html">&lt;p&gt;Everyone needs a fast instance in a datacenter to work with. Amazon's EC2 or any other VPS provider will give you access. Sometimes you need to transfer files around fast or do some data processing. Having a fast instance will help you get your job done faster.&lt;/p&gt;
&lt;p&gt;I have a tutorial that I needed to upload to an FTP server and do an FXP copy. The file is resting on another FTP server form a basic hosting provider. I do not have FXP access. So I launched an instance on EC2. Logged in via ssh. wget the file from my hosting server. Then connected to the marketplace FTP and uploaded the file. Easy, fast, and all the transferring was done on someone else's bandwidth.&lt;/p&gt;
&lt;p&gt;To make this easier here is how to setup ssh to use a ssh key file instead of a password. This has more advantages than just making it quicker to ssh into my instances.  You can use this to setup automated backups using rsync.&lt;/p&gt;
&lt;h2&gt;How To:&lt;/h2&gt;
&lt;p&gt;On your local machine you need to jump into terminal for this. Go to your home dir and make a new .ssh folder if it does not exist. To see if it does exist just type:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;ls -a
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;If not exist then:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;mkdir .ssh
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now cd into it:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;cd .ssh
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now we need to generate an rsa key file:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;ssh-keygen -t rsa
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Do not enter a passphrase, just leave it blank.&lt;/p&gt;
&lt;p&gt;Now you need to ssh into your instance or remote computer and make an .ssh folder in your user home folder. Same as above.&lt;/p&gt;
&lt;p&gt;Now cd into and make a new file called:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;authorized_keys
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;In this file, paste the contents of your local computers newly generated id_rsa.pub file into it.&lt;/p&gt;
&lt;p&gt;Now, still on your remote computer or instance, you need to change the permission of the authorized_keys file to 644:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;chmod 644 authorized_keys
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;All done. Exit your ssh session and login again and you will notice that you don't have to enter a password now.&lt;/p&gt;
&lt;p&gt;I suggest changing your password to something really long and secure since you won't be using it anymore, unless logging into to it from a different computer.&lt;/p&gt;</summary></entry></feed>