<p><em>In the previous part, we started to nail down what it is that we’re going to do to try and accomplish our task. We’re going to write a charm - a package that represents a way to deploy a piece of software repeatably to the cloud - which does all that’s needed to host a WSGI app. It will be able to talk to several different services and use any WSGI server. If you haven’t yet, <ahref="/posts/tech/how-charming/2016/12/13/part-1.html">check that post out first</a>!</em></p>
<p>“Hey! I think I have deployment all figured out for Honeycomb!” I blurted out in the Guild chat, sharing the link to the previous post.</p>
<p>Blank stares, then finally, “You wrote about the furry website you’re building for work<supid="fnref:disclaimer"><aclass="footnote-ref"href="#fn:disclaimer">1</a></sup>..?”</p>
<p>Anyhow, I digress. The goal of Honeycomb is not to be a furry website but a writing one, and besides, none of the guild really cared about deployment strategies; that’s something for me to worry about. And as a developer with a fascination with the Ops side of things, I <em>do</em> care about deployment strategies.</p>
<p>Chat aside, Juju is a delightful step in making things much easier for us DevOpsy critters, because it allows us to write general and repeatable solutions that can be used within a ecosystem of other solutions to accomplish a specific goal. In our case, we want to be able to deploy any WSGI compliant application that can connect to a variety of other services and be hosted in the cloud.</p>
<p>These solutions, charms, are what we’ll need to have to get our stack up and running, so let’s get started in making our <code>wsgi-app</code> charm.</p>
<p>As Juju is a Canonical offering (and I’m a Canonical employee), I’ll be working in Ubuntu, so if you’re following along, you’ll need that running on a machine or VM at your disposal (you’ll need 15.10 Wily or 16.04 Xenial, to be specific). Once you’ve got your system up and running, installation of juju and the tools we need is fairly easy:</p>
<p>This will install just about everything we need: we’ll get juju itself, of course, as well as LXD, which is used for local and development environments, and <code>charm-tools</code>, which contains a few applications we’ll be using for creating, building, and testing our charm.</p>
<p>Getting our charm set up after this point is fairly easy, but it does require deciding on how you’re going to work with your charm. I have a work directory in which I keep all of my projects, and when developing a charm, it’s often easiest to have all of your charm related work in its own directory structure. With that in mind, I’m going to set up the following directory structure for working with <code>wsgi-app</code>:</p>
<p>We’ll get into what layers are in just a second and interfaces in a later article, but for now, that’s the basic structure that one needs to get started with building a charm. This is because the various charm tools expect to find a certain structure when they do their work. This is all controlled through environment variables:</p>
<p>Now that we’ve got our basic directory structure in place, we can start to actually build out our charm. Thankfully, the charm tools give us a way to do so with a simple command:</p>
INFO: Using default charm template <spanclass="o">(</span>reactive-python<spanclass="o">)</span>. To <spanclass="k">select</span> a different template, use the -t option.
<p><code>charm create</code> will do a few things, as shown above, but in short, it will create a <code>wsgi-app</code> directory for us, and populate it with a basic template upon which we’ll build our charm. This winds up giving you a directory like so:</p>
<p>There’s a lot going on here, so it’s time we take a step back and talk about what goes into building a charm, and the two important concepts to learn about: hooks and layers.</p>
<p>Charms, as deployment solutions, are fundamentally reactive. When you deploy a charm, it goes through several stages, and at each stage, the charm has a chance to react to what’s going on during the deployment process. The same with other things going on around the charm: when configuration values are changed, or a relation (a means of communication between two services) is added or removed or changed, the charm can react to that as well.</p>
<p>As with many other reactive frameworks, we call these sorts of reactions ‘hooks’. For example, when you first deploy a charm with juju, the charm receives several hooks from juju once the machine on which it is to be deployed is up and running. It will start with the <code>install</code> hook, which is usually when the charm has a chance to install all of its software. After that, it will receive the <code>config-changed</code> hook, which is when the charm will learn about all of the configuration options that the user has specified and can react accordingly. Finally, it will receive a <code>start</code> hook, which is when any long-running processes such as servers will be started. <code>stop</code> is another hook, as well, of course, wherein one might back-up any charm data.</p>
<p>The hook-based lifecycle of a charm looks something like the following<supid="fnref:rr-diagrams"><aclass="footnote-ref"href="#fn:rr-diagrams">3</a></sup>:</p>
<p>{% include tech/how-charming/part-2-hooks.svg %}</p>
<p>In reality, most of the work that is done within the charm happens during the <code>config-changed</code> hook, while <code>install</code> is used only for installing ancillary software and <code>start</code> is often just ignored. This is because that is the hook which will always occur after startup and any configuration changes (such as when the user runs <code>juju set</code>). This way, you can just restart all tasks during <code>config-changed</code>, as this will cause config files changed during the hook to be reloaded and so on.</p>
<p>We’ll get more into relations in a later part, but for now, it’s enough to understand that relations are, primarily, ways in which one service can expose information to another. Contrary to their names or how they appear in visualizations, they’re not means or representations of <em>communication</em> between services. Rather, one can think of them as the juju controller allowing the two services to acknowledge that they exist to each other. In our instance, this will mean that, when a relation is created between <code>wsgi-app</code> and PostGres, <code>wsgi-app</code> will receive connection information for the PostGres database server, and networking will be restructured such that the two instances <em>can</em> talk to each other.</p>
<p>Relations follow their own cycle of hooks, through creation, change, and deletion, but again, we’ll be diving into them later - they’re a topic of their own!</p>
<p>Charms can be written in most any language that’s available on a base ubuntu server image, such as bash or python, or any language installed during the charm hook, which covers just about everything. This works by having a hook directory in the charm with an executable file named for each hook:</p>
<p>Many charms are still written by hand in this fashion, as it’s quite easy to do. In most cases, all code lives in, for instance, a <code>hooks.py</code> file with all hook files being symlinks to that. Pertinent functions are run through the use of <code>@hook</code> decorators, such as <code>@hook('config-changed')</code>.</p>
<p>Although you can always write charms more directly like this, one winds up writing a lot of boilerplate code. If you’ve written charms before, you’ll be well versed in this, and if you haven’t, you may find yourself stealing from other charmers more often than not.</p>
<p>In the spirit of Don’t Repeat Yourself, there’s another way of writing charms: layers.</p>
<p>Layers are composable packages that allow one to include functionality in the final charm without having to write it yourself. They’re the libraries that the charm ecosystem really needed. As yet, they only exist for python charms, but as we’re charming up python applications, we should be good to go in using them!</p>
<p>So what can one do with layers? Well, quite a bit. Say one needs to install some software on installation - Honeycomb requires the use of <code>pandoc</code>, for instance, which allows writers to upload many different file types - in which case one require the <code>apt</code> layer, which will allow one to specify in one’s layer configuration what one expects to have installed on the machine by the time we get to running our service. Similarly, we may want to use a git layer which will fetch our website repository as specified in a configuration and use that to deploy our application.</p>
<p><em>Hooks</em> respond to what is happening in the juju environment and are the basis of how charms work, while <em>layers</em> compose oft-repeated bits of functionality into a charm and are the basis of making it easier to conceptualize and compartmentalize different actions that a charm may make.</p>
<p>Sort of. For the most part. Ish.</p>
<p>Layers also introduce a bit of complexity in that they offer hook-like functionality through a state mechanism. For instance, an apache layer may have a state <code>apache.available</code> that is set when Apache is up and running and ready to serve your page. That layer may call <code>set_state('apache.available')</code>, which will mean that your charm will get notified through a state change. If you need to do something when Apache is made available, you can set up a function to do so based on a decorator: <code>@when('apache.available')</code>.</p>
<p>You can think of it like so:</p>
<ul>
<li><em>Hooks</em> are fired when something changes in Juju</li>
<li><em>Layers</em> compose repeatable functionality into libraries and set state, and</li>
<li><em>State changes</em> happen when something happens within the charm itself as one of the hooks is called.</li>
<p>There’s a lot going on here, and I’ve already thrown a lot of words at it, with a few more posts to go. I learn best by seeing how things are implemented, though, so let’s get onto the charm and see how this all fits together.</p>
<p>Well, not actually. We created a <code>wsgi-app</code><em>layer</em>, which can be built into a charm. What we need to do is start working on what the layer will do, so that when we run <code>charm build</code> in a bit, we’ll wind up with a complete charm that serves up a WSGI application.</p>
<p>Here’s what we need <code>wsgi-app</code> to do:</p>
<p>Remember, though, that we’re going to save relations for a later step, so let’s just focus on the first two steps.</p>
<p>For installing stuff, our best bet is to use the <code>apt</code> layer. Remember that layers act as libraries; in this case, we’re aiming to install the library that will let us install packages through apt.</p>
<p>This is a fairly simple thing to do, if perhaps a little magical: open <code>layer.yaml</code>; you’ll see the boilerplate code, which looks like so:</p>
<divclass="codehilite"><pre><span></span><code><spanclass="nt">includes</span><spanclass="p">:</span><spanclass="w"></span><spanclass="p p-Indicator">[</span><spanclass="s">'layer:basic'</span><spanclass="p p-Indicator">]</span><spanclass="w"></span><spanclass="c1"># if you have any interfaces, add them here</span><spanclass="w"></span>
<p>Good! That was easy! Of course, we can also tell the apt layer what to install here rather than doing so programmatically (such as <code>pip</code>, perhaps even more down the line), so our <code>layer.yaml</code> will look like the following:</p>
<p>Now we can start testing things out, though lets stick with just the build step for now. When we run <code>charm build</code>, our layers will be composed together and hopefully we’ll wind up with a built charm in our build dir!</p>
<p>Well that looks…promising! There’s a lot of green, at least. There’s a few warnings we can address, though. I hadn’t initialized the charm as a git repo yet, because it was created through <code>charm create</code>, so let’s do that now:</p>
<p>Our next step will be to clean up some of the code created by bootstrap. For starters, we can make <code>metadata.yaml</code> agree with reality as best we can for now - we’ll leave the relations section as is until we get to that point - and to specify which series we want the charm to support (trusty and xenial, in our case):</p>
<spanclass="nt">summary</span><spanclass="p">:</span><spanclass="w"></span><spanclass="l l-Scalar l-Scalar-Plain">charm for running any WSGI application</span><spanclass="w"></span>
<p>We’ll also need to update <code>config.yaml</code> for some configuration settings that <code>layer:apt</code> requires, and to get rid of the placeholders. Our resulting file should look like this:</p>
<p>Again, while we might come up with more configuration values in the future, this will do for now. Running <code>charm build</code> again gives us another successful output, though it’s worth noting that, since we changed the charm from a single series to a multi-series charm, it is now built into <code>$JUJU_REPOSITORY/builds</code>.</p>
<p>It’s getting late and that was a lot of information to dump, so I’ll pause here for now. We still have a ways to go - I’ve got at least three future posts lined up for this project - but we’re getting closer with each step. We’ve started work on our <code>wsgi-app</code> layer, which, when built, produces a functional skeleton of the charm we eventually want.</p>
<p>Here are some resources for the time being:</p>
<ul>
<li><ahref="https://github.com/makyo/wsgi-app/tree/710b7efe21440e49b78cb99f2f4d7b83feff938a">Source code for the <code>wsgi-app</code> layer</a> as it was at the point reached at the end of this article.</li>
<li><ahref="https://jujucharms.com/docs/stable/developer-layers">Documentation on charm layers</a></li>
<li><ahref="http://interfaces.juju.solutions">List of charm layers and interfaces</a></li>
<li><ahref="https://jujucharms.com/docs/stable/reference-charm-hooks">Documentation on charm hooks</a></li>
<p>As always, views are my own, not my employer’s. <aclass="footnote-backref"href="#fnref:disclaimer"title="Jump back to footnote 1 in the text">↩</a></p>
<p>Seriously. After students (54%), nearly 10% of furries are employed in the tech industry, making it the most common occupation within the subculture. (The Furry Poll, conducted March-December 2015, <em>n=11831</em>) <aclass="footnote-backref"href="#fnref:furries-tech"title="Jump back to footnote 2 in the text">↩</a></p>
<p>Credit where it’s due, railroad diagrams created with the help of <ahref="http://bottlecaps.de/rr/ui">this tool</a> by Gunther Rademacher. <aclass="footnote-backref"href="#fnref:rr-diagrams"title="Jump back to footnote 3 in the text">↩</a></p>