Our example charm isn’t really doing anything fun yet. Let’s update it to install the hello package and set a Hello World
message for Juju once it’s done.
Installing packages is a very common charm requirement and layer:apt has all the functionality we need for installing packages from apt repositories. We’ll now use apt to install the hello package.
Modify the ~/charms/layers/layer-example/layer.yaml to look like this:
includes:
- 'layer:basic'
- 'layer:apt'
options:
apt:
packages:
- hello
Modify ~/charms/layers/layer-example/reactive/layer_example.py to look like this:
from charms.reactive import set_flag, when, when_not
from charmhelpers.core.hookenv import application_version_set, status_set
from charmhelpers.fetch import get_upstream_version
import subprocess as sp
@when_not('example.installed')
def install_example():
set_flag('example.installed')
@when('apt.installed.hello')
def set_message_hello():
# Set the upstream version of hello for juju status.
application_version_set(get_upstream_version('hello'))
# Run hello and get the message
message = sp.check_output('hello', stderr=sp.STDOUT)
# Set the active status with the message
status_set('active', message )
# Signal that we know the version of hello
set_flag('hello.version.set')
Let’s build again with our changes.
cd ~/charms/layers/
charm build layer-example
The charm will now be built and the final charm assembled, ending up in ~/charms/layers/trusty/examplee
.
We can now deploy it with Juju:
juju deploy example
After some time, juju status
will show the “Hello World” message.
Congratulations, you have just created and deployed your first charm!