~jorge/juju-core/review-configs-for-bundles

« back to all changes in this revision

Viewing changes to htmldocs/authors-charm-writing.html

  • Committer: Nick Veitch
  • Date: 2013-11-13 14:28:36 UTC
  • Revision ID: nick.veitch@canonical.com-20131113142836-a4gnub9xi83ojo58
rewrite of charm writing tutorial

Show diffs side-by-side

added added

removed removed

Lines of Context:
72
72
          <article>
73
73
<h1>Your First charm starts here!</h1>
74
74
<p>Okay, so you have read up all the background info on what a charm is, how it works, and what parts of the charm do what, now it is time to dust off your favourite code editor (no arguments please) and get charming!</p>
 
75
<p>Although it is possible to create a charm without using anything other than a text editor, we are also going to make use of 
 
76
the very excellent timesaving plugin for Juju, Charm Tools. <a href="/tools-charm-tools.html"> Find out how to get and install charm tools here</a>. Then hurry back.
75
77
<p>For this example, we are imagining that we want to create a charm for <a href="http://vanillaforums.org/" >the Vanilla forum software</a></p>
76
78
 
77
79
<span class="number"> 1 </span>
79
81
          <h2>Prepare yourself</h2>
80
82
          <p>As we are writing a charm, it makes sense to create it in a local charm repository (see how to deploy from a local repository <a href="./charms-deploying.html" target="#local">here</a>) to make it easy to test in your Juju environment.</p>
81
83
          <p>Go to your home directory (or wherever is appropriate and make the appropriate file structure:</p>
82
 
          <pre>cd ~<br>mkdir -p charms/precise/vanilla </pre>
 
84
          <pre class="prettyprint lang-bash">
 
85
cd ~
 
86
mkdir -p charms/precise/vanilla 
 
87
cd charms/precise
 
88
</pre>
 
89
 
83
90
          
84
91
          </div>
85
92
 
86
93
<span class="number" > 2 </span>
87
94
          <div class="step" id="step02">
 
95
          <h2>Create a barebones charm with Charm Tools</h2>
 
96
          <p>Using the charm tools plugin, we can create the directory structure we need for our charm quickly and easily:</p>
 
97
          <pre class="prettyprint lang-bash">juju charm create vanilla</pre>
 
98
          <p>This not only creates the directory structure, it also prepopulates it with template files for you to edit. Your directory will now look like this</p>
 
99
<img src="./media/author-charm-writing-01.png" width='600' alt="directory tree"> 
 
100
 
 
101
          </div>
 
102
<span class="number" > 3 </span>
 
103
          <div class="step" id="step03">
88
104
          <h2>Create the README file</h2>
89
 
          <p>Fire up your text editor and create a readme file.</p>
 
105
          <p>Fire up your text editor and load/edit the readme file.</p>
90
106
          <p>This step is especially important if you intend making your charm public, but it is very useful even if your charm will only ever be seen by you. The README is a good place to make nots about how the charm works, what information it expects to communicate and how.</p>
91
107
          <p>Although a plain text file is fine, you can also write your README file using Markdown (in which case use the .md suffix). The advantage of this, other than it looks quite neat, is that the Charm Store will render Mardown properly online, so your README will look and read better.</p>
92
108
          <p>Here is a quick example README file for our Vanilla charm:</p>
113
129
          <p>Obviously, you can include any useful info you wish.</p>
114
130
          </div>
115
131
          
116
 
<span class="number" > 3 </span>
117
 
          <div class="step" id="step03">
 
132
<span class="number" > 4 </span>
 
133
          <div class="step" id="step04">
118
134
          <h2>Make some metadata.yaml</h2>
119
135
          <p>The <strong>metadata.yaml</strong> file is really important. This is the file that Juju reads to find out what a charm is, what it does and what it needs to do it.</p>
120
136
          <p>The YAML syntax is at once simple, but exact, so if you have any future problems with Juju not recognising your charm, this is the first port of call! The information is stored in simple <span class="pre">&LT;key&GT; : &LT;value&GT;</span> associations. The first four are pretty self explanatory:</p>
166
182
          
167
183
 
168
184
          </div>         
169
 
<span class="number" > 4 </span>
170
 
          <div class="step" id="step04">
 
185
<span class="number" > 5 </span>
 
186
          <div class="step" id="step05">
171
187
          <h2>Writing hooks</h2>
172
188
          <p>As you will know from your thorough reading of the <a href="./authors-charm-components.html">charm components</a>, the hooks are the important scripts that actually do things. You can write hooks in whatever language you can reasonably expect to execute on an Ubuntu server.</p>
173
189
          <p>For our charm, the hooks we will need to create are:</p>
299
315
<p>So, any connecting charm will be able to ask for the values 'hostname' and 'port'. Remember, once you have finished writing your hooks make sure you 'chmod +x' them.</p>
300
316
<p>For our simplistic charm, that is all the hooks we need for the moment, so now we can test it out!</p>
301
317
</div>
302
 
<span class="number"> 5 </span>
303
 
          <div class="step" id="step05">
 
318
 
 
319
<span class="number"> 6 </span>
 
320
          <div class="step" id="step06">
 
321
          <h2>Run the charm proof tool</h2>
 
322
          <p>Another part of the Charm Tools plugin is a useful lint-like tool which will check for 
 
323
          errors in the files of your charm. Run it like this:</p>
 
324
<pre class="prettyprint lang-bash">juju charm proof [CHARM_DIRECTORY]
 
325
</pre>
 
326
<p> The output classifies messages as:</p>
 
327
<ul><li>I - for information</li>
 
328
<li>W - A warning; something which should be looked at but won't necessarily stop the charm working.</li>
 
329
<li>E - An error; these are blocker which must be fixed for the charm to be used.</li>
 
330
<p>some example output might be:</p>
 
331
<pre class="prettyprint lang-bash">
 
332
E: no copyright file
 
333
E: README.ex Includes boilerplate README.ex line 1
 
334
I: relation peer-relation has no hooks
 
335
</pre>
 
336
<p>Which tells you that you forgot to add a <code>copyright</code> file, you have left some default text in the README, and one of your relations has no hooks. All useful stuff.</p>
 
337
 
 
338
</div>
 
339
<span class="number"> 7 </span>
 
340
          <div class="step" id="step07">
304
341
          <h2>Testing</h2>
305
342
          <p>Before we congratulate ourselves too much, we should check that the charm actually works. To help with this, we should open a new terminal window and run the following command:</p>
306
343
          <pre>juju debug-log</pre>
320
357
          
321
358
          </div>
322
359
 
323
 
<span class="number"> 6 </span>
324
 
          <div class="step" id="step06">
 
360
<span class="number"> 8 </span>
 
361
          <div class="step" id="step08">
325
362
          <h2>Tidying up</h2>
326
363
          <p>With the charm working properly, you may consider everything a job well done. If your charm is really great and you want to share it, particularly on the charm store, then there are a couple of things you ought to add.</p>
327
364
          <ol>