Beispiel #1
0
def activate_venv():
    """
    Activate the venv if enabled in ``layer.yaml``.

    This is handled automatically for normal hooks, but actions might
    need to invoke this manually, using something like:

        # Load modules from $JUJU_CHARM_DIR/lib
        import sys
        sys.path.append('lib')

        from charms.layer.basic import activate_venv
        activate_venv()

    This will ensure that modules installed in the charm's
    virtual environment are available to the action.
    """
    from charms.layer import options
    venv = os.path.abspath('../.venv')
    vbin = os.path.join(venv, 'bin')
    vpy = os.path.join(vbin, 'python')
    use_venv = options.get('basic', 'use_venv')
    if use_venv and '.venv' not in sys.executable:
        # activate the venv
        os.environ['PATH'] = ':'.join([vbin, os.environ['PATH']])
        reload_interpreter(vpy)
    layer.patch_options_interface()
    layer.import_layer_libs()
import re
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)