Pack a hook based charm with Charmcraft

Availability notice: This Charmcraft feature is currently available only in the edge channel. The tutorial will be updated as needed to reflect availability in candidate or stable.

Charms can be created in different ways. By default, Charmcraft will use the Charmed Operator SDK, but by defining the appropriate parts it can be also used to pack other types of charms.

Nightly builds of Charmcraft can now be used to create charms using the hooks mechanism. In this short tutorial we will pack tiny-bash, a tiny hooks-only charm, using part definitions. To follow the tutorial you should have Charmcraft installed from the edge channel, git, and a local juju installation to deploy to.

Step 1: Obtain the charm source

Clone the tiny-bash git repository using the following command:

$ git clone https://github.com/erik78se/tiny-bash

Step 2: Create charmcraft.yaml

After the repository is cloned to the local filesystem, change to the tiny-bash directory and create a charmcraft.yaml file with the part definition for the hooks-based charm. The file contents should be:

type: charm

bases:
  - build-on:
    - name: "ubuntu"
      channel: "20.04"
    run-on:
    - name: "ubuntu"
      channel: "20.04"

parts:
  tiny-bash:
    plugin: dump
    source: .
    prime:
      - LICENSE
      - README.md
      - config.yaml
      - copyright
      - hooks
      - icon.svg
      - metadata.yaml

Step 3: Pack

Charmcraft will now create the charm payload based on the parts defined in charmcraft.yaml:

$ charmcraft pack
Charms packed:
    tiny-bash_ubuntu-20.04-amd64.charm

The charm file should contain all the files listed in the prime section of the tiny-bash part and the charm manifest:

$ unzip -l tiny-bash_ubuntu-20.04-amd64.charm
Archive:  tiny-bash_ubuntu-20.04-amd64.charm
  Length      Date    Time    Name
---------  ---------- -----   ----
      423  2021-11-12 19:37   metadata.yaml
      431  2021-11-12 19:37   README.md
       12  2021-11-12 19:37   config.yaml
     3693  2021-11-12 19:37   icon.svg
       38  2021-11-12 19:37   copyright
      261  2021-11-12 20:08   manifest.yaml
    34523  2021-11-12 19:37   LICENSE
      381  2021-11-12 19:37   hooks/update-status
      346  2021-11-12 19:37   hooks/start
     1294  2021-11-12 19:37   hooks/shared-fs-relation-changed
      563  2021-11-12 19:37   hooks/stop
      497  2021-11-12 19:37   hooks/leader-elected
      447  2021-11-12 19:37   hooks/install
      417  2021-11-12 19:37   hooks/leader-settings-changed
      811  2021-11-12 19:37   hooks/upgrade-charm
      625  2021-11-12 19:37   hooks/config-changed
---------                     -------
    44762                     16 files

Step 4: Deploy!

The charm can now be published or deployed with juju:

$ juju deploy ./tiny-bash_ubuntu-20.04-amd64.charm
Located local charm "tiny-bash", revision 0
Deploying "tiny-bash" from local charm "tiny-bash", revision 0

Wait for the deployment to finish and you can see the application status:

$ juju status
Model    Controller           Cloud/Region         Version  SLA          Timestamp
default  localhost-localhost  localhost/localhost  2.9.12   unsupported  17:23:23-03:00

App        Version  Status  Scale  Charm      Store  Channel  Rev  OS      Message
tiny-bash           active      1  tiny-bash  local             0  ubuntu  update-status ran: 20:22

Unit          Workload  Agent  Machine  Public address  Ports  Message
tiny-bash/0*  active    idle   0        10.2.17.31             update-status ran: 20:22

Machine  State    DNS         Inst id        Series  AZ  Message
0        started  10.2.17.31  juju-55481c-0  focal       Running
2 Likes

I should probably update this tutorial/repo to make some adjustments to the charmcraft tool…

@cmatsuoka I think I hit a bug when adding your suggestion and trying to charmcraft build on version 1.3.1 of charmcraft.

charmcraft build fails.

2021-11-22 23:36:00,821  charmcraft.providers._logs               DEBUG    :: mm 2021-11-22 22:36:00,006  charmcraft                               ERROR    Parts processing error: Failed to copy '/root/stage/src': no such file or directory. (full execution logs in '/tmp/charmcraft.log')
2021-11-22 23:36:00,834  craft_providers.lxd.lxc                  DEBUG    Executing on host: lxc --project charmcraft config device show local:charmcraft-tiny-bash-6559631-0-0-amd64
2021-11-22 23:36:00,915  craft_providers.lxd.lxc                  DEBUG    Executing on host: lxc --project charmcraft config device remove local:charmcraft-tiny-bash-6559631-0-0-amd64 disk-/root/project
2021-11-22 23:36:01,134  craft_providers.lxd.lxc                  DEBUG    Executing on host: lxc --project charmcraft stop local:charmcraft-tiny-bash-6559631-0-0-amd64
2021-11-22 23:36:03,601  charmcraft                               ERROR    Failed to build charm for bases index '0'. (full execution logs in '/home/erik/snap/charmcraft/common/charmcraft-log-m8cxkcx3')

@erik-lonroth This is a strange error, is it reproducible? If it builds correctly after you run charmcraft clean, I suspect it is a manifestation of issue 501. Did you do anything different that may have triggered this error?

erik@pixel:~/PycharmProjects/tiny-bash$ charmcraft clean
Cleaned project 'tiny-bash'.
erik@pixel:~/PycharmProjects/tiny-bash$ charmcraft build
Packing charm 'tiny-bash_ubuntu-20.04-amd64_ubuntu-18.04-amd64.charm'...
Parts processing error: Failed to copy '/root/stage/src': no such file or directory. (full execution logs in '/tmp/charmcraft.log')
Failed to build charm for bases index '0'. (full execution logs in '/home/erik/snap/charmcraft/common/charmcraft-log-ig7ib2l6')

Yes.

Logs:

2021-11-23 22:37:12,268  charmcraft.providers._logs               DEBUG    :: mm 2021-11-23 21:37:11,595  craft_parts.executor.executor            DEBUG    execute action charm:Action(part_name='charm', step=Step.PRIME, action_type=ActionType.RUN, reason=None)
2021-11-23 22:37:12,268  charmcraft.providers._logs               DEBUG    :: mm 2021-11-23 21:37:11,601  charmcraft                               ERROR    Parts processing error: Failed to copy '/root/stage/src': no such file or directory. (full execution logs in '/tmp/charmcraft.log')
2021-11-23 22:37:12,269  craft_providers.lxd.lxc                  DEBUG    Executing on host: lxc --project charmcraft config device show local:charmcraft-tiny-bash-6559631-0-0-amd64
2021-11-23 22:37:12,386  craft_providers.lxd.lxc                  DEBUG    Executing on host: lxc --project charmcraft config device remove local:charmcraft-tiny-bash-6559631-0-0-amd64 disk-/root/project
2021-11-23 22:37:12,563  craft_providers.lxd.lxc                  DEBUG    Executing on host: lxc --project charmcraft stop local:charmcraft-tiny-bash-6559631-0-0-amd64
2021-11-23 22:37:14,364  charmcraft                               ERROR    Failed to build charm for bases index '0'. (full execution logs in '/home/erik/snap/charmcraft/common/charmcraft-log-ig7ib2l6')

What filesystem is this host (pixel) on for ~/PycharmProjects/tiny-bash?

cat /etc/mtab | grep sda

/dev/sda2 / ext4 rw,relatime,errors=remount-ro 0 0