Lifecycle Hooks

An extension’s lifecycle: what it creates when it is installed, and removes when it is uninstalled.

Most of an extension’s lifecycle is declared, and the platform carries it out:

  • settings ([[tool.smarter.settings]]) are configured in the admin console;

  • waffle switches ([[tool.smarter.switches]]) are created as ext_<slug>_<name> on install, and deleted on uninstall;

  • SAM resources, the manifests in the package’s manifests/ directory, are applied on install and upgrade, and deleted on uninstall. An upgrade deletes the resources that the new version no longer ships.

For anything else, an extension names a subclass of SmarterExtensionLifecycle in [tool.smarter] lifecycle, and overrides the hooks it needs:

from smarter.apps.llmtool.extension import LifecycleContext, SmarterExtensionLifecycle

class Lifecycle(SmarterExtensionLifecycle):
    def post_install(self, context: LifecycleContext) -> None:
        context.apply_manifest(SECRET_MANIFEST)

    def pre_upgrade(self, context: LifecycleContext) -> None:
        if context.previous_version == "1.0.0":
            ...

Hooks run once per cluster, in the process that carries out the administrator’s action, not in every pod. A pre_* hook that raises cancels the action, and nothing changes. A post_* hook that raises leaves the extension installed but disabled, with the error shown in the admin console.

class smarter.apps.llmtool.extension.lifecycle.LifecycleContext(extension, version, settings, user_profile, previous_version=None, logger=None)[source]

Bases: object

What a lifecycle hook knows, and the helpers it may use.

Parameters:
  • extension (Extension) – The extension.

  • version (str) – The version being installed, upgraded to, enabled, or uninstalled.

  • previous_version (str | None) – On an upgrade, the version being replaced; otherwise None.

  • settings (BaseModel) – The extension’s settings, as its tools see them.

  • user_profile (UserProfile) – The superuser who is carrying out the action, who owns the resources it creates.

__init__(extension, version, settings, user_profile, previous_version=None, logger=None)
apply_manifest(manifest)[source]

Apply a SAM manifest, owned by user_profile, and record it as created by this extension.

The resource is deleted when the extension is uninstalled.

Parameters:

manifest (str) – The manifest, as YAML or JSON.

Return type:

tuple[str, str]

Returns:

The resource’s kind and name.

Raises:

SmarterExtensionInstallError – If the manifest cannot be applied.

delete_resource(kind, name)[source]

Delete a SAM resource that this extension created.

Parameters:
  • kind (str) – The resource’s kind, e.g. Secret.

  • name (str) – The resource’s name.

Return type:

None

extension: Extension
logger: Logger | WaffleSwitchedLoggerWrapper = None
previous_version: str | None = None
settings: BaseModel
property slug: str

The extension’s slug.

user_profile: UserProfile
version: str
class smarter.apps.llmtool.extension.lifecycle.SmarterExtensionLifecycle[source]

Bases: object

The base class of an extension’s lifecycle hooks.

Every hook does nothing unless overridden.

A new instance is created for each action.

on_disable(context)[source]

Run when an administrator disables the extension.

Return type:

None

on_enable(context)[source]

Run when an administrator enables the extension.

Raise to keep it disabled.

Return type:

None

post_install(context)[source]

Run after an extension is first installed, its switches created and its manifests applied.

Return type:

None

post_uninstall(context)[source]

Run after an extension’s resources and switches are deleted, just before its rows are.

Return type:

None

post_upgrade(context)[source]

Run, from the new version’s code, after it replaced LifecycleContext.previous_version.

Return type:

None

pre_install(context)[source]

Run before an extension is first installed.

Raise to cancel the install.

Return type:

None

pre_uninstall(context)[source]

Run before an extension is uninstalled.

Raise to cancel the uninstall.

Return type:

None

pre_upgrade(context)[source]

Run, from the new version’s code, before it replaces LifecycleContext.previous_version.

Raise to cancel.

Return type:

None