def test_callbacks(self): a = mock.Mock() b = mock.Mock() c = mock.Mock() flags.register_trigger(when='a', callback=a) flags.register_trigger(when_not='b', callback=b) flags.set_flag('a') assert a.call_count == 1 assert b.call_count == 0 assert c.call_count == 0 flags.clear_flag('a') assert a.call_count == 1 assert b.call_count == 0 assert c.call_count == 0 flags.set_flag('b') assert a.call_count == 1 assert b.call_count == 0 assert c.call_count == 0 flags.clear_flag('b') assert a.call_count == 1 assert b.call_count == 1 assert c.call_count == 0 flags.set_flag('c') assert a.call_count == 1 assert b.call_count == 1 assert c.call_count == 0 flags.clear_flag('c') assert a.call_count == 1 assert b.call_count == 1 assert c.call_count == 0
def test_when_not_set_clear(self): flags.register_trigger(when_not='foo', set_flag='bar', clear_flag='qux') flags.set_flag('noop') flags.clear_flag('noop') assert not flags.is_flag_set('bar') flags.set_flag('foo') flags.set_flag('qux') assert not flags.is_flag_set('bar') flags.clear_flag('foo') assert flags.is_flag_set('bar') assert not flags.is_flag_set('qux')
def test_triggers(self, kv): kv.return_value = MockKV() assert not flags.any_flags_set('foo', 'bar', 'qux') flags.set_flag('foo') assert not flags.any_flags_set('bar', 'qux') flags.clear_flag('foo') assert not flags.any_flags_set('foo', 'bar', 'qux') flags.register_trigger(when='foo', set_flag='bar') flags.set_flag('foo') assert flags.is_flag_set('bar') flags.clear_flag('foo') assert flags.is_flag_set('bar') flags.clear_flag('bar') flags.register_trigger(when='foo', clear_flag='qux') flags.set_flag('qux') flags.set_flag('foo') assert not flags.is_flag_set('qux')
from charmhelpers.core.host import service_stop from charmhelpers.core.host import service_pause from charmhelpers.core.host import service_start from charmhelpers.core.host import service_restart from charmhelpers.core.host import service_running from charmhelpers.core.hookenv import config from charmhelpers.core.hookenv import status_set from charmhelpers.core.hookenv import storage_get from charmhelpers.core.hookenv import log from charms.slurm.node import get_inventory import charms.reactive as reactive import charms.reactive.flags as flags flags.register_trigger(when='endpoint.slurm-cluster.active.changed', clear_flag='slurm-node.configured') @reactive.only_once() @reactive.when('slurm.installed') def initial_setup(): status_set('maintenance', 'Initial setup of slurm-node') # Disable slurmctld on node service_pause(SLURMCTLD_SERVICE) @reactive.when_not('endpoint.slurm-cluster.joined') def missing_controller(): status_set('blocked', 'Missing a relation to slurm-controller') # Stop slurmd service_stop(SLURMD_SERVICE)
from charmhelpers.fetch import apt_install import charms.leadership as leadership import charms.reactive as reactive import charms.reactive.flags as flags import charms.munge.munge as munge import charmhelpers.core.hookenv as hookenv import charmhelpers.core.host as host MUNGE_PACKAGE = 'munge' # This trigger does not work. Only config.changed works. # Moved to config_changed() below #flags.register_trigger(when='config.changed.munge_key', # clear_flag='munge.configured') flags.register_trigger(when='leadership.changed.munge_key', clear_flag='munge.configured') flags.register_trigger(when='leadership.changed.munge_key', clear_flag='munge.exposed') @reactive.when_not('munge.installed') def install_munge(): hookenv.status_set('maintenance', 'installing munge package') hookenv.log('install_munge(): installing munge package') packages = [MUNGE_PACKAGE] apt_install(packages) flags.set_flag('munge.installed')
import copy import socket import charms.leadership as leadership import charms.reactive as reactive import charms.reactive.flags as flags import charmhelpers.core.hookenv as hookenv import charmhelpers.core.host as host import charms.reactive.relations as relations import charms.slurm.helpers as helpers import charms.slurm.controller as controller flags.register_trigger(when='config.changed.munge_key', clear_flag='slurm-controller.configured') flags.register_trigger(when='config.changed.munge_key', clear_flag='munge.configured') @reactive.when_not('endpoint.slurm-cluster.joined') def missing_nodes(): hookenv.status_set('blocked', 'Missing relation to slurm-node') flags.clear_flag('slurm-controller.configured') host.service_stop(helpers.SLURMCTLD_SERVICE) @reactive.when('leadership.is_leader') @reactive.when_not('leadership.set.active_controller') def set_active_controller(): '''Elects an active controller unit. This is only done once until an operator decides to relocate an active controller to a different node via an action or doing a juju run --unit <leader-unit> "leader-set active_controller=''"
from charms.layer.caas_base import pod_spec_set from charms.reactive import when, when_not, when_any from charms.reactive.flags import set_flag, register_trigger from charmhelpers.core.hookenv import ( log, metadata, config, application_name, ) from charms import layer register_trigger(when='layer.docker-resource.ubuntu-image.changed', clear_flag='k8scharm.configured') @when_any('layer.docker-resource.ubuntu-image.failed') def waiting_for_image(): """Set status blocked Conditions: - ubuntu-image.failed """ layer.status.blocked('Failed fetching Ubuntu image') @when('layer.docker-resource.ubuntu-image.available') @when_not('k8scharm.configured') def configure(): """Configure Ubuntu pod
from charms.reactive.flags import register_trigger from charms.reactive import ( clear_flag, set_flag, when, when_not, when_any, ) import charms.sshproxy import os import subprocess # Register a trigger so that we can respond to config.changed, even if # it's being cleared by another handler register_trigger(when='config.changed', set_flag='sshproxy.reconfigure') @when_any('config.changed', 'sshproxy.reconfigure') def ssh_configured(): """Check if charm is properly configured. Check to see if the charm is configured with SSH credentials. If so, set a state flag that can be used to execute ssh-only actions. For example: @when('sshproxy.configured') def run_remote_command(cmd): ...
import charms.reactive as reactive import charms.reactive.flags as flags import charmhelpers.core.unitdata as unitdata from charms.reactive.relations import ( endpoint_from_flag, ) charm.use_defaults( 'charm.installed', 'update-status') # if config has been changed we need to re-evaluate flags # config.changed is set and cleared (atexit) in layer-basic flags.register_trigger(when='config.changed', clear_flag='config.rendered') flags.register_trigger(when='upgraded', clear_flag='config.rendered') flags.register_trigger(when='config.changed', clear_flag='config.complete') flags.register_trigger( when='endpoint.keystone-fid-service-provider.changed', clear_flag='keystone-data.complete' ) @reactive.hook('upgrade-charm') def default_upgrade_charm(): """Default handler for the 'upgrade-charm' hook. This calls the charm.singleton.upgrade_charm() function as a default. """ reactive.set_state('upgraded')
import copy import socket import charms.leadership as leadership import charms.reactive as reactive import charms.reactive.flags as flags import charmhelpers.core.hookenv as hookenv import charmhelpers.core.host as host import charms.reactive.relations as relations import charms.slurm.helpers as helpers import charms.slurm.controller as controller flags.register_trigger(when='munge.configured', set_flag='slurm-controller.needs_restart') @reactive.when('slurm.installed') @reactive.when('slurm-controller.configured') @reactive.when('munge.configured') @reactive.when('slurm-controller.needs_restart') def handle_munge_change(): ''' A trigger sets needs_restart when munge.configured goes from unset to set after a change. Need to handle this by restarting slurmctld service. ''' hookenv.status_set('maintenance', 'Munge key changed, restarting service') host.service_restart(helpers.SLURMCTLD_SERVICE) flags.clear_flag('slurm-controller.needs_restart') @reactive.hook('upgrade-charm')
local_unit, status_set, ) from charmhelpers.core.templating import render from charms.layer.django_base import ( SU_CONF_DIR, LOG_DIR, render_settings_py, pip_install, ) kv = unitdata.kv() register_trigger(when='config.changed.celery.config', clear_flag='django.celery.settings.available') register_trigger(when='config.changed.custom-config', clear_flag='django.custom.settings.available') register_trigger(when='config.changed.email-config', clear_flag='django.email.settings.available') register_trigger(when='redis.broken', clear_flag='django.redis.available') @when_not('s3.storage.checked') def check_for_django_aws_s3_storage_config(): status_set('maintenance', 'Checking S3 storage configs') if not config('aws-access-key') and \ not config('aws-secret-key') and \
import socket import copy import charms.leadership as leadership import charms.reactive as reactive import charms.reactive.flags as flags import charmhelpers.core.hookenv as hookenv import charmhelpers.core.host as host import charms.reactive.relations as relations import charms.slurm.dbd as dbd import charmhelpers.fetch as ch_fetch flags.register_trigger(when='munge.configured', set_flag='slurm-dbd.needs_restart') flags.register_trigger(when='endpoint.slurm-dbd-ha.joined', clear_flag='slurm-dbd.standalone_startup') @reactive.when_not('slurmdbd.installed') def install_slurm(): hookenv.status_set('maintenance', 'installing slurmdbd packages') packages = [dbd.SLURMDBD_PACKAGE] ch_fetch.apt_install(packages) hookenv.application_version_set( ch_fetch.get_upstream_version(dbd.SLURMDBD_PACKAGE)) flags.set_flag('slurmdbd.installed') @reactive.when_not('endpoint.slurm-dbd-ha.joined') @reactive.when_not('slurm-dbd.configured') def standalone_mode(): flags.set_flag('slurm-dbd.standalone_startup')
import helpers @hook('upgrade-charm') def upgrade_charm(): reactive.clear_flag('cassandra.installed') reactive.clear_flag('cassandra.swapoff.done') reactive.clear_flag('cassandra.kernelsettings.done') reactive.clear_flag("cassandra.limits.done") reactive.clear_flag('cassandra.crontab.installed') reactive.clear_flag('cassandra.io_schedulers.done') reactive.clear_flag('cassandra.version.set') cassandra.config()['last_version_update'] = 0 register_trigger(when='config.changed', clear_flag='cassandra.installed') register_trigger(when='config.changed', clear_flag='cassandra.crontab.installed') register_trigger(when='config.changed', clear_flag='cassandra.etchosts.done') register_trigger(when='config.changed', clear_flag='cassandra.io_schedulers.done') @when_not('cassandra.swapoff.done') def swapoff(fstab_path='/etc/fstab'): '''Turn off swapping on the machine, permanently.''' # Turn off swap in the current session if host.is_container(): hookenv.log("In a container, not touching swap.") else: try: hookenv.log("Turning off swap (swapoff -a)") subprocess.check_call(['swapoff', '-a'])
import time # import copy import socket import charms.leadership as leadership import charms.reactive as reactive import charms.reactive.flags as flags import charmhelpers.core.hookenv as hookenv import charmhelpers.core.host as host import charms.reactive.relations as relations import charms.slurm.helpers as helpers import charms.slurm.controller as controller from charms.reactive import endpoint_from_flag flags.register_trigger(when='munge.configured', set_flag='slurm-controller.munge_updated') #Unnecessary for now, we still need to run the configure_controller() to send munge_key to nodes #@reactive.when('slurm.installed') #@reactive.when('slurm-controller.configured') #@reactive.when('munge.configured') #@reactive.when('slurm-controller.needs_restart') #def handle_munge_change(): # ''' # A trigger sets needs_restart when munge.configured goes from unset to set # after a change. Need to handle this by restarting slurmctld service. # ''' # hookenv.status_set('maintenance', 'Munge key changed, restarting service') # host.service_restart(helpers.SLURMCTLD_SERVICE) # flags.clear_flag('slurm-controller.needs_restart')
from charmhelpers.core.hookenv import DEBUG from charms import ( coordinator, leadership, reactive, ) from charms.layer import cassandra from charms.reactive import ( when, when_any, when_not, ) from charms.reactive.flags import register_trigger register_trigger(when='leadership.changed.seeds', clear_flag='cassandra.configured') register_trigger(when='leadership.changed.seeds', set_flag='cassandra.needs_restart') register_trigger(when='endpoint.cluster.changed.bootstrapped', clear_flag='cassandra.seeds.done') register_trigger(when='endpoint.cluster.departed', clear_flag='cassandra.seeds.done') register_trigger(when='config.changed', clear_flag='cassandra.bootstrapped.published') @when('leadership.is_leader') @when('cassandra.config.validated') @when_not('leadership.set.seeds') def initial_seeds(): leadership.leader_set(seeds=cassandra.listen_ip_address()) reactive.set_flag('cassandra.seeds.done')
#!/usr/bin/env python from charmhelpers.contrib.ansible import apply_playbook from charmhelpers.core.hookenv import application_version_set from charmhelpers.core.hookenv import hook_name from charmhelpers.core.hookenv import log from charmhelpers.core.hookenv import status_set from charms.reactive.decorators import hook from charms.reactive.decorators import when from charms.reactive.decorators import when_not from charms.reactive.flags import clear_flag from charms.reactive.flags import register_trigger from charms.reactive.flags import set_flag register_trigger(when='config.changed', clear_flag='gateway.configured') @when_not('gateway.version') def set_version(): try: with open(file='repo-info') as f: for line in f: if line.startswith('commit-short'): commit_short = line.split(':')[-1].strip() application_version_set(commit_short) except IOError: log('Cannot set application version. Missing repo-info file.') set_flag('gateway.version') @when_not('config.set.api_key')
# import to trigger openstack charm metaclass init import charm.openstack.keystone_ldap # noqa import charms_openstack.charm as charm import charms.reactive as reactive import charms.reactive.flags as flags import charmhelpers.core.hookenv as hookenv charm.use_defaults('charm.installed', 'update-status') # if config has been changed we need to re-evaluate flags # config.changed is set and cleared (atexit) in layer-basic flags.register_trigger(when='config.changed', clear_flag='config.rendered') flags.register_trigger(when='config.changed', clear_flag='config.complete') @reactive.when('domain-backend.connected') @reactive.when_not('domain-name-configured') @reactive.when('config.complete') def configure_domain_name(domain): domain.domain_name(hookenv.config('domain-name') or hookenv.service_name()) flags.set_flag('domain-name-configured') @reactive.when_not('domain-backend.connected') @reactive.when('domain-name-configured') def keystone_departed(): """
#!/usr/bin/env python from charmhelpers.contrib.ansible import apply_playbook from charmhelpers.core.hookenv import status_set from charms.reactive.decorators import hook from charms.reactive.decorators import when from charms.reactive.decorators import when_not from charms.reactive.flags import clear_flag from charms.reactive.flags import register_trigger from charms.reactive.flags import set_flag register_trigger(when='config.changed', clear_flag='thruk.configured') @when_not('thruk.installed') def install_thruk(): status_set('maintenance', 'installing thruk') apply_playbook(playbook='ansible/playbook.yaml') status_set('active', 'ready') set_flag('thruk.configured') set_flag('thruk.installed') @when('thruk.installed') @when_not('thruk.configured') def configure_thruk(): status_set('maintenance', 'configuring thruk') apply_playbook(playbook='ansible/playbook.yaml', tags=['config']) status_set('active', 'ready') set_flag('thruk.configured')
#!/usr/bin/env python from charmhelpers.contrib.ansible import apply_playbook from charmhelpers.contrib.charmsupport import nrpe from charmhelpers.core.hookenv import config from charmhelpers.core.hookenv import open_port from charmhelpers.core.hookenv import status_set from charms.layer.nagios import install_nagios_plugin_from_file from charms.reactive.decorators import hook from charms.reactive.decorators import when from charms.reactive.decorators import when_not from charms.reactive.flags import clear_flag from charms.reactive.flags import register_trigger from charms.reactive.flags import set_flag register_trigger(when='config.changed', clear_flag='netbox.configured') register_trigger(when='config.changed.check_docker_params', clear_flag='netbox.nrpe.configured') register_trigger(when='db.master.changed', clear_flag='netbox.configured') @when_not('db.connected') @when_not('netbox.db.requested') def missing_pgsql(): status_set('blocked', 'missing postgresql relation') @when('db.connected') @when_not('netbox.db.created') def request_db(pgsql): status_set('maintenance', 'setting up postgresql db')
def test_when_set(self): flags.register_trigger(when='foo', set_flag='bar') flags.set_flag('foo') assert flags.is_flag_set('bar') flags.clear_flag('foo') assert flags.is_flag_set('bar')
from charmhelpers.core.hookenv import application_version_set from charmhelpers.core.hookenv import config from charmhelpers.core.hookenv import hook_name from charmhelpers.core.hookenv import log from charmhelpers.core.hookenv import status_set from charms.layer.nagios import install_nagios_plugin_from_file from charms.reactive.decorators import hook from charms.reactive.decorators import when from charms.reactive.decorators import when_not from charms.reactive.flags import clear_flag from charms.reactive.flags import register_trigger from charms.reactive.flags import set_flag PLUGIN_NAME = 'check_lsi_raid' register_trigger(when='config.changed.check_parameters', clear_flag='megaraid.configured') register_trigger(when='config.changed.check_repo', clear_flag='megaraid.installed') register_trigger(when='config.changed.check_version', clear_flag='megaraid.installed') register_trigger(when='config.changed.storcli_path', clear_flag=['megaraid.installed', 'megaraid.configured']) register_trigger(when='config.changed.storcli_url', clear_flag='megaraid.installed') @when_not('megaraid.version') def set_version(): try: with open(file='repo-info') as f: for line in f:
def test_when_clear(self): flags.register_trigger(when='foo', clear_flag='qux') flags.set_flag('qux') flags.set_flag('foo') assert not flags.is_flag_set('qux')
import charms.reactive as reactive import charms_openstack.charm import charm.openstack.cinder_backup_swift import charms.reactive.flags as flags from charms.reactive.relations import ( endpoint_from_flag, ) charms_openstack.charm.defaults.use_defaults('charm.installed', 'update-status') # if config has been changed we need to re-evaluate flags # config.changed is set and cleared (atexit) in layer-basic flags.register_trigger(when='config.changed', clear_flag='config.complete') flags.register_trigger(when='upgraded', clear_flag='config.complete') flags.register_trigger(when='endpoint.backup-backend.changed', clear_flag='config.complete') @reactive.when_any('endpoint.backup-backend.available') @reactive.when_not('config.complete') def configure_cinder_backup(): # don't always have a relation context - obtain from the flag endp = endpoint_from_flag('endpoint.backup-backend.joined') with charm.provide_charm_instance() as charm_instance: # publish config options for all remote units of a given rel name, config = charm_instance.get_swift_backup_config() endp.publish(name, config) charm_instance.configure_ca() charm_instance.restart_service()
def register_triggers(self): register_trigger( when_not=self.expand_name('endpoint.{endpoint_name}.joined'), clear_flag=self.expand_name('{endpoint_name}.available'))