Esempio n. 1
0
def main():
    action_name = hookenv.action_name()
    try:
        action = ACTIONS[action_name]
    except KeyError:
        return "Action '{}' undefined".format(action_name)

    try:
        action()
    except Exception as e:
        details = {"traceback": traceback.format_exc()}
        hookenv.action_set(details)
        hookenv.action_fail(str(e))
Esempio n. 2
0
    """
    Applies a user defined manifest with kubectl
    """
    _, apply_path = tempfile.mkstemp(suffix=".json")
    try:
        manifest = json.loads(action_get("json"))
        with open(apply_path, "w") as manifest_file:
            json.dump(manifest, manifest_file)
        output = _kubectl(["apply", "-f", apply_path])

        action_set({
            "summary": "Manifest applied.",
            "output": output.decode("utf-8"),
        })
    except subprocess.CalledProcessError as e:
        action_fail("kubectl failed with exit code {} and message: {}".format(
            e.returncode, e.output))
    except json.JSONDecodeError as e:
        action_fail("Failed to parse JSON manifest: {}".format(str(e)))
    except Exception as e:
        action_fail("Failed to apply manifest: {}".format(str(e)))
    finally:
        os.unlink(apply_path)


action = action_name()
if action == "get-kubeconfig":
    get_kubeconfig()
elif action == "apply-manifest":
    apply_manifest()
    fetch_cmd = 'juju scp {}:{} .'.format(hookenv.local_unit(), kubeconfig_path)
    action_set({'msg': 'User "{}" created.'.format(user)})
    action_set({'users': ', '.join(list(users) + [user])})
    action_set({'kubeconfig': fetch_cmd})


def user_delete():
    user = action_get('name')
    protect_resources(user)

    users = user_list()
    if user not in list(users):
        action_fail('User "{}" does not exist.'.format(user))
        return

    # Delete the secret
    secret_id = users[user]
    layer.kubernetes_master.delete_secret(secret_id)

    action_set({'msg': 'User "{}" deleted.'.format(user)})
    action_set({'users': ', '.join(u for u in list(users) if u != user)})


action = action_name().replace('user-', '')
if action == 'create':
    user_create()
elif action == 'list':
    user_list()
elif action == 'delete':
    user_delete()
Esempio n. 4
0
                                          kubeconfig_path)
    action_set({"msg": 'User "{}" created.'.format(user)})
    action_set({"users": ", ".join(list(users) + [user])})
    action_set({"kubeconfig": fetch_cmd})


def user_delete():
    user = action_get("name")
    protect_resources(user)

    users = user_list()
    if user not in list(users):
        action_fail('User "{}" does not exist.'.format(user))
        return

    # Delete the secret
    secret_id = users[user]
    layer.kubernetes_control_plane.delete_secret(secret_id)

    action_set({"msg": 'User "{}" deleted.'.format(user)})
    action_set({"users": ", ".join(u for u in list(users) if u != user)})


action = action_name().replace("user-", "")
if action == "create":
    user_create()
elif action == "list":
    user_list()
elif action == "delete":
    user_delete()
Esempio n. 5
0
def dump_environment():
    log("HookName: %s" % hook_name())
    log("ActionName: %s" % action_name())
    log("ActionTag: %s" % action_tag())
    log(os.environ)