def main(relation_name=None): """ This is the main entry point for the reactive framework. It calls :func:`~bus.discover` to find and load all reactive handlers (e.g., :func:`@when <decorators.when>` decorated blocks), and then :func:`~bus.dispatch` to trigger hook and state handlers until the state settles out. Finally, :meth:`unitdata.kv().flush <charmhelpers.core.unitdata.Storage.flush>` is called to persist the state. :param str relation_name: Optional name of the relation which is being handled. """ hookenv.log('Reactive main running for hook %s' % hookenv.hook_name(), level=hookenv.INFO) # work-around for https://bugs.launchpad.net/juju-core/+bug/1503039 # ensure that external handlers can tell what hook they're running in if 'JUJU_HOOK_NAME' not in os.environ: os.environ['JUJU_HOOK_NAME'] = os.path.basename(sys.argv[0]) def flush_kv(): if unitdata._KV: unitdata._KV.flush() hookenv.atexit(flush_kv) try: bus.discover() hookenv._run_atstart() bus.dispatch() except SystemExit as x: if x.code is None or x.code == 0: hookenv._run_atexit() raise hookenv._run_atexit()
def main(args): hookenv._run_atstart() action_name = os.path.basename(args[0]) try: action = ACTIONS[action_name] except KeyError: return "Action %s undefined" % action_name else: try: action(args) except Exception as e: hookenv.action_fail(str(e)) hookenv._run_atexit()
def manage(self): """ Handle the current hook by doing The Right Thing with the registered services. """ hookenv._run_atstart() try: hook_name = hookenv.hook_name() if hook_name == 'stop': self.stop_services() else: self.reconfigure_services() self.provide_data() except SystemExit as x: if x.code is None or x.code == 0: hookenv._run_atexit() hookenv._run_atexit()
def main(relation_name=None): """ This is the main entry point for the reactive framework. It calls :func:`~bus.discover` to find and load all reactive handlers (e.g., :func:`@when <decorators.when>` decorated blocks), and then :func:`~bus.dispatch` to trigger hook and state handlers until the state settles out. Finally, :meth:`unitdata.kv().flush <charmhelpers.core.unitdata.Storage.flush>` is called to persist the state. :param str relation_name: Optional name of the relation which is being handled. """ hookenv.log('Reactive main running for hook %s' % hookenv.hook_name(), level=hookenv.INFO) # work-around for https://bugs.launchpad.net/juju-core/+bug/1503039 # ensure that external handlers can tell what hook they're running in if 'JUJU_HOOK_NAME' not in os.environ: os.environ['JUJU_HOOK_NAME'] = os.path.basename(sys.argv[0]) # update data to be backwards compatible after fix for issue 28 relations._migrate_conversations() def flush_kv(): if unitdata._KV: unitdata._KV.flush() hookenv.atexit(flush_kv) if hookenv.hook_name().endswith('-relation-departed'): def depart_conv(): rel = RelationBase.from_name(hookenv.relation_type()) rel.conversation().depart() hookenv.atexit(depart_conv) try: bus.discover() hookenv._run_atstart() bus.dispatch() except SystemExit as x: if x.code is None or x.code == 0: hookenv._run_atexit() raise hookenv._run_atexit()
def main(relation_name=None): """ This is the main entry point for the reactive framework. It calls :func:`~bus.discover` to find and load all reactive handlers (e.g., :func:`@when <decorators.when>` decorated blocks), and then :func:`~bus.dispatch` to trigger handlers until the queue settles out. Finally, :meth:`unitdata.kv().flush <charmhelpers.core.unitdata.Storage.flush>` is called to persist the flags and other data. :param str relation_name: Optional name of the relation which is being handled. """ restricted_mode = hookenv.hook_name() in [ 'meter-status-changed', 'collect-metrics' ] hookenv.log('Reactive main running for hook %s' % hookenv.hook_name(), level=hookenv.INFO) if restricted_mode: hookenv.log('Restricted mode.', level=hookenv.INFO) # work-around for https://bugs.launchpad.net/juju-core/+bug/1503039 # ensure that external handlers can tell what hook they're running in if 'JUJU_HOOK_NAME' not in os.environ: os.environ['JUJU_HOOK_NAME'] = os.path.basename(sys.argv[0]) try: bus.discover() if not restricted_mode: # limit what gets run in restricted mode hookenv._run_atstart() bus.dispatch(restricted=restricted_mode) except Exception: tb = traceback.format_exc() hookenv.log('Hook error:\n{}'.format(tb), level=hookenv.ERROR) raise except SystemExit as x: if x.code not in (None, 0): raise if not restricted_mode: # limit what gets run in restricted mode hookenv._run_atexit() unitdata._KV.flush()
import sys from base64 import b64decode from charmhelpers.core import hookenv from charmhelpers.core.hookenv import ( action_get, action_set, action_fail, action_name ) from charms import layer os.environ['PATH'] += os.pathsep + os.path.join(os.sep, 'snap', 'bin') # Import charm layers and start reactive layer.import_layer_libs() hookenv._run_atstart() def protect_resources(name): '''Do not allow the action to operate on names used by Charmed Kubernetes.''' protected_names = ['admin', 'system:kube-controller-manager', 'kube-controller-manager', 'system:kube-proxy', 'kube-proxy', 'system:kube-scheduler', 'kube-scheduler', 'system:monitoring'] if name.startswith('kubelet') or name in protected_names: action_fail('Not allowed to {} "{}".'.format(action, name)) sys.exit(0) def user_list():
from subprocess import check_call from subprocess import check_output from subprocess import CalledProcessError from subprocess import Popen from subprocess import PIPE from datetime import datetime from uuid import uuid4 import hashlib import os import sys import time import yaml # Import charm layers and start reactive layer.import_layer_libs() _run_atstart() opts = layer.options('etcd') DATESTAMP = datetime.strftime(datetime.now(), '%Y%m%d-%H%M%S') ARCHIVE = "etcd-data-{}.tar.gz".format(DATESTAMP) unit_name = os.getenv('JUJU_UNIT_NAME').replace('/', '') ETCD_DATA_DIR = '{}/{}.etcd'.format(opts['etcd_data_dir'], unit_name) if not os.path.isdir(ETCD_DATA_DIR): ETCD_DATA_DIR = opts['etcd_data_dir'] ETCD_PORT = config('management_port') CLUSTER_ADDRESS = get_ingress_address('cluster') SKIP_BACKUP = action_get('skip-backup') SNAPSHOT_ARCHIVE = resource_get('snapshot')