def test_cannot_run_unregistered_function(self): foo = MagicMock() hooks = hookenv.Hooks() hooks.register('foo', foo) self.assertRaises(hookenv.UnregisteredHookError, hooks.execute, ['bar'])
def test_runs_a_registered_function(self): foo = MagicMock() hooks = hookenv.Hooks() hooks.register('foo', foo) hooks.execute(['foo', 'some', 'other', 'args']) foo.assert_called_with()
def test_config_save_disabled(self): config = hookenv.config() config.implicit_save = True foo = MagicMock() hooks = hookenv.Hooks(config_save=False) hooks.register('foo', foo) hooks.execute(['foo', 'some', 'other', 'args']) self.assertFalse(config.save.called)
def test_can_run_a_decorated_function_as_itself(self): execs = [] hooks = hookenv.Hooks() @hooks.hook() def func(): execs.append(True) hooks.execute(['func']) self.assertRaises(hookenv.UnregisteredHookError, hooks.execute, ['brew']) self.assertEqual(execs, [True])
def test_can_run_a_decorated_function_as_one_or_more_hooks(self): execs = [] hooks = hookenv.Hooks() @hooks.hook('bar', 'baz') def func(): execs.append(True) hooks.execute(['bar']) hooks.execute(['baz']) self.assertRaises(hookenv.UnregisteredHookError, hooks.execute, ['brew']) self.assertEqual(execs, [True, True])
def test_magic_underscores(self): # Juju hook names use hypens as separators. Python functions use # underscores. If explicit names have not been provided, hooks # are registered with both the function name and the function # name with underscores replaced with hypens for convenience. execs = [] hooks = hookenv.Hooks() @hooks.hook() def call_me_maybe(): execs.append(True) hooks.execute(['call-me-maybe']) hooks.execute(['call_me_maybe']) self.assertEqual(execs, [True, True])
import json import httplib import os import time import socket import subprocess import sys import urlparse from charmhelpers.core import hookenv, host from kubernetes_installer import KubernetesInstaller from path import path from lib.registrator import Registrator hooks = hookenv.Hooks() @hooks.hook('api-relation-changed') def api_relation_changed(): """ On the relation to the api server, this function determines the appropriate architecture and the configured version to copy the kubernetes binary files from the kubernetes-master charm and installs it locally on this machine. """ hookenv.log('Starting api-relation-changed') charm_dir = path(hookenv.charm_dir()) # Get the package architecture, rather than the from the kernel (uname -m). arch = subprocess.check_output(['dpkg', '--print-architecture']).strip() kubernetes_bin_dir = path('/opt/kubernetes/bin') # Get the version of kubernetes to install.
# Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. """Charm event hooks.""" import json import sys from charmhelpers.core import hookenv from charmhelpers.payload import execd import cinder_contexts HOOKS = hookenv.Hooks() @HOOKS.hook('install') def install(): """Perform ``install`` handling.""" execd.execd_preinstall() @HOOKS.hook('config-changed', 'upgrade-charm') def upgrade_charm(): """Perform ``config-changed`` or ``upgrade-charm`` handling.""" for rid in hookenv.relation_ids('storage-backend'): storage_backend(rid)
import os import charms_openstack.charm as ldap_charm import charmhelpers.core.hookenv as ch_hookenv # noqa #https://github.com/juju-solutions/charms.templating.jinja2 from charms.templating.jinja2 import render #https://charm-helpers.readthedocs.io/en/latest/examples/config.html hooks = ch_hookenv.Hooks() ldap_charm.use_defaults('charm.default-select-release') LDAP_CONFIG_FILE_DIR = "/etc/keystone/domains/" LDAP_CONFIG_FILE = LDAP_CONFIG_FILE_DIR + "keystone.{}.conf" TEMPLATE = "keystone_ldap.j2" class CharmKeystoneLDAP(): name = 'keystone ldap configuration' version = 'v0.0.1' def config_ldap(self): """ Define the destination LDAP server in the /etc/keystone/keystone.conf or /etc/keystone/domains/keystone.DOMAIN_NAME.conf : [ldap] url = ldap://localhost user = dc=Manager,dc=example,dc=org password = samplepassword suffix = dc=example,dc=org """