The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

App::CharmKit::Manual::GettingStarted - Getting started with CharmKit

VERSION

version 0.004

Getting Started

Creating a project

All development happens within src/ and the builtin pack command is used for generating the proper hooks/tests and dependencies within that directory so Juju is able to act upon them. Hooks within hooks/ directory are always overwritten, think of this similar to a dist or release directory.

To start a project:

  $ charmkit init [--with-hooks] <charm-name>

If used --with-hooks then src/hooks/ will be populated with all the default hooks. A few questions will be prompted and then the project is generated with config.yaml, metadata.yaml, LICENSE, and README.md.

Directory Layout

Once a project is created the structure of your project should look similar to:

  charm-project/
    hooks/
      install
      config-changed
      start
      stop
    src/
      hooks/
        install
        config-changed
        start
        stop
      tests/
        00-basic.test
    tests/
      00-basic.test
    config.yaml
    metadata.yaml
    LICENSE
    README.md

Creating hooks

By default CharmKit allows the creation of a set of default unit hooks. Those hooks are install, config-changed, start, upgrade-charm, stop.

  $ charmkit generate upgrade-charm

Or you can generate all known default unit hooks:

  $ charmkit generate -a

A special relation hook can be created with the -r option:

  $ charmkit generate -r database-relation-joined

Writing charm hooks

Hooks are written using perl with automatically imported helpers for convenience. When developing hooks they should reside in src/hooks.

A typical hook starts with

   #!/usr/bin/env perl

   use charm -logging;

   log 'Starting install hook for database';
   my $dbhost = relation_get 'dbhost';
   my $dbuser = relation_get 'dbuser';

Writing charm tests

Tests are written in the same way and should live in src/tests/*.test.

A typical test starts with

   #!/usr/bin/env perl

   use charm -tester, -sys;

   # See if an nginx config file exists
   ok -e '/etc/nginx/sites-enabled/mysite.com';

   my $cmd = ['service', 'nginx', 'status'];
   my $ret = execute($cmd);
   ok $ret->{error} eq 0;

   # finish tests
   done_testing;

Tests are built in a way that the test runner from the charm reviewers will be able to run and validate your charm. The tests can be executed calling them directly (how the test runner does it) or running with:

   $ prove tests/*.test

Packaging a charm fo release

Once development is complete and you have your hooks defined and tests written. Packaging a charm for release to either Git or the Charm Store is a matter of running:

  $ charmkit pack

AUTHOR

Adam Stokes <adamjs@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Adam Stokes.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.