<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Matthías Páll Gissurarson on Medium]]></title>
        <description><![CDATA[Stories by Matthías Páll Gissurarson on Medium]]></description>
        <link>https://medium.com/@tritlo?source=rss-9904c930b05d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*_sbXe4hEueECW5ftdyAPGQ.png</url>
            <title>Stories by Matthías Páll Gissurarson on Medium</title>
            <link>https://medium.com/@tritlo?source=rss-9904c930b05d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 02:51:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tritlo/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Using AWS EFS, Netshare And Docker-In-Docker To Create Distributed Docker Volumes.]]></title>
            <link>https://medium.com/@tritlo/using-efs-netshare-and-docker-in-docker-to-create-distributed-docker-volumes-f85ffdd96d3e?source=rss-9904c930b05d------2</link>
            <guid isPermaLink="false">https://medium.com/p/f85ffdd96d3e</guid>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[aws-efs]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[volume-plugins]]></category>
            <category><![CDATA[netshare]]></category>
            <dc:creator><![CDATA[Matthías Páll Gissurarson]]></dc:creator>
            <pubDate>Thu, 07 Jul 2016 20:25:32 GMT</pubDate>
            <atom:updated>2016-07-07T20:56:57.811Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<p>So I’ve been working a lot with docker lately, but I always hit a wall when I try to do multi-host syncing of volumes. I’ve long suspected that the solution would be to use <strong>Amazon</strong> <strong>EFS</strong>, which was very recently made generally available. In the following, I present a solution to creating distributed docker volumes on <strong>AWS</strong>.</p><p>In this post I make use of the<strong><em> aws-cli</em></strong> and <strong><em>jq</em></strong>, which can be installed on <strong>Ubuntu 16.04 </strong>by running</p><pre>apt install awscli jq</pre><p>First, make sure that you have a role that allows access to <strong>EFS</strong>, and create a <strong>AWS EC2 </strong>machine which uses that role. Since we’re making a cluster, creating an auto-scaling group with that role seems to be the way to go.</p><p>Make sure that <a href="http://netshare.containx.io/"><strong><em>ContainX’s</em></strong> <strong><em>netshare</em></strong> <strong><em>plugin</em></strong></a> is installed and running. On <strong>Ubuntu 16.04</strong>, this amounts to doing:</p><pre>apt-get update &amp;&amp; apt-get install -y nfs-common<br>wget <a href="https://github.com/ContainX/docker-volume-netshare/releases/download/v0.18/docker-volume-netshare_0.18_amd64.deb">https://github.com/ContainX/docker-volume-netshare/releases/download/v0.18/docker-volume-netshare_0.18_amd64.deb</a><br>dpkg -i docker-volume-netshare_0.18_amd64.deb<br>echo &#39;DKV_NETSHARE_OPTS=&quot;efs&quot;&#39; &gt; /etc/default/docker-volume-netshare<br>systemctl enable docker-volume-netshare<br>systemctl start docker-volume-netshare</pre><p>Since we’re on an instance that has a role which allows it to access <strong>EFS</strong>, no further configuration of netshare is needed.</p><p>We then create an<strong> EFS</strong>, using the <strong><em>aws-cli</em></strong>. This can also be done using the console, but showing how to do it in the cli is easier. Make sure you’ve run</p><pre>aws config efs.preview true</pre><p>first, since the cli for <strong>EFS</strong> is not yet stable.</p><p>To create the file system, we do</p><pre>aws efs create-file-system --creation-token docker-volume-test</pre><p>The creation token is to make this command idempotent, i.e. trying to create two file systems with the same token is an error.</p><p>The output of this command includes the file system id, which is what we use in netshare to mount the file system as a volume. However, since we are command line hackers, we’ll fetch the id directly from the cli</p><pre>aws efs describe-file-systems | jq -r &#39;.FileSystems[]|select(.CreationToken == &quot;docker-volume-test&quot;)|.FileSystemId&#39;</pre><p>This returns the Id directly, which will be something like</p><pre>fs-f3efc785</pre><p>To be able to mount this <strong>EFS</strong>, we’ll need to create a mount point. We’ll want to know the <strong><em>subnet-id</em></strong> and <strong><em>availability zone</em></strong> of the current instance, to create a mount point there. Luckily, we can do this with a simple <strong><em>curl</em></strong> request, with dynamic data retrieval. So to create the mount point, you run:</p><pre>aws efs create-mount-target --file-system-id fs-f3efc785 --subnet-id $(curl http://169.254.169.254/latest/meta-data/network/interfaces/macs/$(curl http://169.254.169.254/latest/meta-data/mac)/subnet-id)</pre><p>Though you’ll have to make sure that <strong><em>aws-cli</em></strong> is configured to the right region. We’ll have to wait a bit until the mount point is ready before we can proceed.</p><p>So now we have an <strong>EFS</strong> ready to use as a volume in a container. A problem arises though: We need to create this container on each node, so that all of them will point to the same <strong>EFS</strong>.</p><p>Docker-in-Docker to the rescue! What we really need is to run</p><pre>docker volume create --driver=efs --name=fs-f3efc785</pre><p>on every node in the cluster. But using <strong><em>docker</em></strong> <strong><em>1.12</em></strong> (currently rc3), we can create a <em>global</em> service which runs on every node in the cluster, even if they join the cluster later. To do this we use docker-in-docker, to connect to the node’s docker socket and run the volume create command as if we had run it on the node itself. To create the global service, we run:</p><pre>docker service create --name volume-creator --mount type=bind,source=/var/run/docker.sock,target=/var/run/docker.sock --mode global --restart-condition none docker:rc volume create --driver=efs --name=fs-f3efc785</pre><p>This defines a <em>global</em> service whose name is <em>volume-creator. </em>Since it is a <em>global </em>service, it gets run on every node in the cluster. We tell it to never restart, since we only want it run once, and we mount the node’s docker socket to be able to send a command to docker on the node itself.</p><p>Let’s test it out, and see if it worked.</p><p>For this part, we’ll create a simple <strong><em>php</em></strong> script and place it in the shared volume, to see that it is really there on all the nodes.</p><p>Mount the volume in an alpine <em>container</em> with the following:</p><pre>docker run -it --rm -v fs-f3efc785:/mount alpine ash</pre><p>Now that we’re in a container with the shared volume mounted, let’s put a simple <strong><em>php</em></strong> script into it:</p><pre>echo &#39;&lt;?php echo &quot;Shared volume success!\n&quot; . &quot;This nodes ip: &quot; . $_SERVER[&quot;SERVER_ADDR&quot;] . &quot;\n&quot;; ?&gt;&#39; &gt; /mount/index.php</pre><p>Now, let’s see if it worked:</p><p>Create a new service:</p><pre>docker service create --replicas=3 -p 80:80 --name php --mount type=bind,source=fs-f3efc785,target=/var/www/html php:apache</pre><p>This creates a new service with <strong><em>3</em></strong> replicas, which in my case means one for every node in the test cluster. We bind it to port <strong><em>80</em></strong>, which docker swarm will then load-balance request on. We mount the shared volume so that it is in the <strong><em>/var/www/html</em></strong> directory, which is the default for <strong><em>php:apache</em></strong>, and we run the <em>apache</em> version of the <em>php</em> container to get a web service.</p><p>Then we run:</p><pre>curl localhost</pre><p>To see whether it worked. This sends a request to port 80, which docker load-balances between each of the nodes. In my case, I get:</p><pre>ubuntu@ip-xxx-xxx-xx-xx:~$ curl localhost<br>Shared volume success!<br>This nodes ip: 10.255.0.6<br>ubuntu@ip-xxx-xxx-xx-xx:~$ curl localhost<br>Shared volume success!<br>This nodes ip: 10.255.0.8<br>ubuntu@ip-xxx-xxx-xx-xx:~$ curl localhost<br>Shared volume success!<br>This nodes ip: 10.255.0.7</pre><p>Success!</p><p>So in summary:</p><ul><li>We used the <strong><em>aws-cli</em></strong> to create an <strong>EFS</strong> and a mount point for it;</li><li>We used the <strong><em>netshare</em></strong> volume plugin to be able to mount <strong>EFS</strong> as a docker volume;</li><li>We used a <em>global</em> service and docker-in-docker to create the volume on every node in the cluster;</li><li>And finally, we created a simple service to demonstrate it working as intended.</li></ul><p>I hope this helps!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f85ffdd96d3e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Proper tail-recursion in Python and Hy]]></title>
            <link>https://medium.com/@tritlo/proper-tail-recursion-in-python-and-hy-f27b1c8e6f58?source=rss-9904c930b05d------2</link>
            <guid isPermaLink="false">https://medium.com/p/f27b1c8e6f58</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[hy]]></category>
            <category><![CDATA[functional-programming]]></category>
            <dc:creator><![CDATA[Matthías Páll Gissurarson]]></dc:creator>
            <pubDate>Mon, 22 Feb 2016 17:32:00 GMT</pubDate>
            <atom:updated>2016-02-22T01:33:32.686Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*fGYzq2VvZ_7Ng5pkkSD2Sw.png" /></figure><p><a href="https://www.blogger.com/hylang.org">Hy</a> is a lisp dialect of Python. It has full cross-language support, meaning that any code you write in Hy can be imported into Python, and vice versa. This means that you can add functional goodies to your existing app, without having to go all in. You also have access to the full extent of Python, which has many great packages and support all over.</p><p>However, Hy is missing proper tail-recursion! Sure, you can use loop-recur and trampoline (much like in Clojure), but it’s not the same! Then you have to explicitly worry about tail-recursion, which comes down hard on its usability. Personally, I utilize tail-recursion a lot when programming in the functional paradigm, and hitting an maximum recursion depth exception is something I have to keep in mind.</p><p>As proper tail-recursion is missing in Python, We first have to <a href="https://gist.github.com/Tritlo/2a0e429f2b0037d8c2eb#file-tailrec-py">implement tail-recursion in Python </a>. This was done using a decorator and an exception. The exception, <em>TailCall</em> allows us to escape the functions stack without returning anything, and the decorator allows us to catch these exceptions, unwrap the function which should be called next and call it. This is done until there is no exception thrown, and thus no recursive call at the end. That’s when we return from the function proper. This turns the tail-recursion into a while loop structure, which is exactly what we want.</p><p>The con is though that this is slower than doing it in the interpreter itself, but you gain proper tail-recursion, which is a huge pro in itself. This should also be about as efficient as using the loop-recur or trampoline macros/functions, so it’s not too bad.</p><p>Having implemented proper tail-recursion in Python, we then turn to the problem of using it in Hy, so that is done automatically, without having <a href="http://blog.mpg.is/2014/12/proper-tail-recursion-in-python.html">to explicity throw a <em>TailCall</em> exception and wrapping it with a decorator</a>.</p><p>This is done by using a <em>__future__</em> import, so as to make it optional (seeing as it would cause performance and debugging issues). There has been a <a href="https://github.com/hylang/hy/pull/728">pending pull request</a> for some time now for the change. The implementation adds a new import,</p><pre>(import [__future__ [TailRec]])</pre><p>which the Hy compiler recognizes and turns on tail recursion during the compilation. This adds the decorator around every function that uses tail recursion, and switches the return statements for raising of the <em>TailCall</em> exception. This effectively gives us proper tail recursion in Hy! <br> This allows us to do things like:</p><pre>(defn tcodd [n]<br>  (if (= n 0)<br>      False<br>      (tceven (- n 1))))</pre><pre>(defn tceven [n]<br>  (if (= n 0)<br>      True<br>      (tcodd (- n 1))))</pre><pre>(print (tceven 1000))</pre><p>and</p><pre>(defn fact [n]<br>  (defn facthelper [n acc]<br>    (if (= n 0)<br>      acc<br>      (facthelper (- n 1) (* n acc))))<br>  (facthelper n 1))</pre><pre>(print (fact 1000))</pre><p>without ever hitting the <em>recursion depth exceeded </em>exception! Woo!</p><p>Edit: I posted this originally on my blog, but I’ve decided to republish it on medium. The pull request has not received much attention, as seems to be common with non-trivial pull requests on open source projects. I do most of my functional programming in <em>Haskell, </em>so I’ve not been motivated to pursue it’s acceptance further. If anyone is interested in bringing attention to the request and/or contribute to it, feel free to comment on it on GitHub.</p><p><em>Originally published at </em><a href="http://blog.mpg.is/2014/12/proper-tail-recursion-in-python.html"><em>blog.mpg.is</em></a><em> on February 22, 2016.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f27b1c8e6f58" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>