Repository Loader

Load a GitHub repository of Smarter manifests: find them, order them by their dependencies, and apply them.

RepositoryLoader holds everything that loading a repository does, one manifest at a time, and two callers use it:

A repository may also give its folders owners, in its owners.yaml (smarter.apps.account.repositories.owners), and hold scripts, in its scripts/ folder (smarter.apps.account.repositories.scripts).

smarter.apps.account.repositories.loader.IGNORE_FILE = '.smarterignore'

A repository’s list of the files that are never applied, as gitignore-style patterns, at its root.

For example, /scripts/ keeps the repository’s scripts folder out, /tutorials/ the manifests of a tutorial, which its readers build themselves, and *.example.yaml examples. See load_ignore_spec().

smarter.apps.account.repositories.loader.MANIFEST_EXTENSIONS = ('.yaml', '.yml', '.yaml.tpl')

The extensions of the files that may hold a manifest.

Other files, e.g. a README, are ignored.

('.yaml', '.yml', '.yaml.tpl')
class smarter.apps.account.repositories.loader.RepositoryLoader(*args, **kwargs)[source]

Bases: SmarterCommand

Find, order and apply the manifests of a repository’s clone, as a user, through the same brokers as the apply api.

It is a management command class, without arguments or a handle(), because it reports its progress the way a command does, to self.stdout and self.stderr, in the command’s styles. manage.py load_from_github writes it to the console; the Apply page captures it for each manifest.

The repository’s folders are only for people: the manifests are applied in the order of their kinds’ dependencies, SAMKinds.apply_order(), so that a Connection exists before the plugin that uses it, a plugin before the LLMClient that lists it, and so on. Within a kind, they are applied in the order of their paths. See the command’s docstring for what is skipped, kept and annotated.

UNVERIFIED_DNS_STATUSES = (LLMClient.DnsVerificationStatusChoices.NOT_VERIFIED, LLMClient.DnsVerificationStatusChoices.FAILED)

The domain statuses of a deployed LLMClient that no deploy task is going to change.

__init__(*args, **kwargs)[source]
add_source_annotations(manifest, path)[source]

Return a copy of a manifest whose annotations also say where it came from.

The manifest’s own annotations are kept, in either form that SAM accepts, and returned as a map. A manifest whose annotations are invalid is returned unchanged, so that applying it reports them.

Parameters:
  • manifest (dict[str, Any]) – The manifest.

  • path (str) – Its file, relative to the repository’s root.

Returns:

The manifest, with source_annotations() added to metadata.annotations.

Return type:

dict[str, Any]

apply(filespec, user_profile, manifest=None)[source]

Apply one manifest, and report, rather than raise, a failure.

The manifest is applied in a transaction, so that one that fails leaves nothing behind. A manifest that names a resource that doesn’t exist is skipped, rather than failed, with --skip_missing_references: its file is added to skipped.

Parameters:
  • filespec (str) – The manifest’s path.

  • user_profile (UserProfile) – The owner of the resource.

  • manifest (str | None) – The manifest to apply instead of the file’s, e.g. a template’s, filled in.

Returns:

The error, or None if the manifest was applied or skipped.

Return type:

str | None

apply_manifest(item, user_profile, deploy)[source]

Apply one manifest, as its owner, and deploy it if it is an LLMClient and deploy.

Parameters:
  • item (RepositoryManifest) – The manifest.

  • user_profile (UserProfile) – Its owner: load_from_github’s own user, or its folder’s owner in owners.yaml.

  • deploy (bool) – Deploy an LLMClient that is not deployed.

Returns:

A description of the failure, if it failed to apply or deploy, else nothing.

Return type:

list[str]

apply_manifests(manifests, user_profile, deploy=False)[source]

Apply the manifests, in order, and deploy the LLMClients if deploy.

Parameters:
Returns:

A description of each manifest that failed to apply or deploy.

Return type:

list[str]

apply_order_key(item)[source]

Return a manifest’s place in the order of application: its kind’s rank, then its path.

A manifest of a folder that owners.yaml gives an owner belongs to a user, so it comes after the Users, even if its kind ranks before them, e.g. a department’s Secret: the Users are its owners. Everything else keeps SAMKinds.apply_order().

Parameters:

item (RepositoryManifest) – The manifest.

Returns:

(rank, 0 or 1, path): 1 for an owned manifest moved after the Users.

Return type:

tuple[int, int, str]

clone_repo(local_path)[source]

Clone the repository’s default branch, without its history.

Parameters:

local_path (str) – The folder to clone into.

Raises:

subprocess.CalledProcessError – If git fails.

Return type:

None

commit: str | None = None

The commit of the clone, from head_commit(), or None if it is not known.

deploy_applied_llmclient(item, manifest, user_profile)[source]

Deploy an LLMClient that apply_manifest just applied, unless deploy_skipped_reason() gives a reason.

Parameters:
Returns:

A description of the failure, if it failed to deploy, else nothing.

Return type:

list[str]

deploy_llmclient(name, user_profile)[source]

Deploy the LLMClient of a user, if it is not deployed.

An LLMClient that is deployed is left as it is, unless its domain was never verified, or failed to verify, e.g. because the DNS service wasn’t ready. It is then deployed again, with deploy_default_api: otherwise nothing ever retries it, and it stays unreachable until someone undeploys and redeploys it by hand.

Parameters:
  • name (str) – The LLMClient’s name.

  • user_profile (UserProfile) – Its owner.

Returns:

False if the LLMClient does not exist, or failed to deploy.

Return type:

bool

deploy_skipped_reason(manifest, user_profile)[source]

Return why --deploy leaves an LLMClient undeployed, or None to deploy it.

  • A staff-only LLMClient: its staff use it in the workbench, and a deployed url would only answer them anyway.

  • An LLMClient of a folder that owners.yaml gives an owner, e.g. a department’s assistant over its own records: deploying it gives it a public url, which its owner decides, not the platform’s load.

Parameters:
Returns:

The reason, or None.

Return type:

str | None

find_manifests(local_path)[source]

Return the repository’s manifests, in the order in which to apply them.

Parameters:

local_path (str) – The repository’s clone.

Returns:

The manifests, sorted by SAMKinds.apply_order(), then by path.

Return type:

list[RepositoryManifest]

head_commit(local_path)[source]

Return the commit that the clone checked out.

Parameters:

local_path (str) – The repository’s clone.

Returns:

The commit’s full SHA, or None if git can’t tell, in which case the resources are annotated without it.

Return type:

str | None

is_existing_detached_budget(item)[source]

Whether a manifest is of a Budget that exists, and lists no resources.

Such a Budget is left as it is: applying its manifest would detach it from the resources that a superuser has attached it to since it was created.

Parameters:

item (RepositoryManifest) – The manifest.

Returns:

True if it is to be skipped.

Return type:

bool

static is_staff_only(manifest)[source]

Return whether an LLMClient manifest is staff-only, spec.config.prompt.staffOnly.

A staff-only LLMClient, e.g. smarter_sysadmin, is applied to the Smarter account only, never copied into another account by initialize_account, and never deployed.

Parameters:

manifest (dict[str, Any]) – The LLMClient manifest, as a dict.

Return type:

bool

Returns:

True if it is staff-only.

static keep_deployed(manifest, user_profile)[source]

Return a copy of an LLMClient manifest whose deployed is that of the LLMClient that exists, if any.

A manifest is not deployed, so applying it as it is would undeploy an LLMClient that is deployed, whose undeploy task destroys its A record, only for the next deploy to create it again. Resolvers that query the domain in between cache the NXDOMAIN for minutes.

Parameters:
  • manifest (dict[str, Any]) – The LLMClient manifest.

  • user_profile (UserProfile) – The owner of the LLMClient.

Returns:

The manifest, with spec.config.prompt.deployed set.

Return type:

dict[str, Any]

static missing_extensions(slugs)[source]

Return the slugs, of those that an LLMClient manifest lists, of the extensions that are not installed.

manage.py install_official_extensions installs them, from a registry that may not be reachable.

Parameters:

slugs (list[str]) – The extensions that the manifest lists.

Returns:

Those that are not installed.

Return type:

list[str]

owner_profile_for(item, user_profile)[source]

Return the profile that applies a manifest: its folder’s owner in owners.yaml, else user_profile.

Account and User manifests are always applied by user_profile: they declare the organization that owns the rest. An owned folder’s manifests are applied only when the repository is loaded into the Smarter account, and skipped otherwise, e.g. by initialize_account: they belong to their owner, not to that account. See smarter.apps.account.repositories.owners.

Parameters:
Returns:

The profile to apply it as, or None to skip it.

Return type:

UserProfile | None

Raises:

SmarterValueError – If its owner doesn’t exist, or doesn’t belong to the account that owners.yaml names.

owners_config: RepositoryOwnersConfig | None = None

The repository’s owners.yaml, or None if it has none.

run_scripts(local_path, config, stage, user_profile)[source]

Run the repository’s scripts of one stage, in the order of its scripts/scripts.yaml.

Parameters:
  • local_path (str) – The repository’s clone.

  • config (RepositoryScriptsConfig | None) – The repository’s scripts configuration, or None if it has none.

  • stage (Literal['pre', 'post']) – pre or post.

  • user_profile (UserProfile) – The owner of the resources, whose account and username the scripts receive.

Returns:

A description of each script that failed.

Return type:

list[str]

select_manifests(manifests, included, excluded)[source]

Return the manifests of the kinds to apply, and say which were skipped.

Parameters:
Returns:

The manifests to apply, in the same order.

Return type:

list[RepositoryManifest]

skip_missing_references: bool = False
skipped: set[str]

The manifests, by file, that apply() skipped, because they name a resource that doesn’t exist.

source_annotations(path)[source]

Return the annotations that say where a resource came from.

Parameters:

path (str) – The manifest’s file, relative to the repository’s root.

Returns:

The repository, the file and, if it is known, the commit.

Return type:

dict[str, str]

property source_repository: str

The repository’s url, without a trailing slash or .git, as resources are annotated with it.

property url: str

The repository’s url.

user_profile: UserProfile | None = None
verbose: bool = False
class smarter.apps.account.repositories.loader.RepositoryManifest(path, filespec, kind, name, manifest)[source]

Bases: object

A manifest of the repository: its file, relative to the repository’s root, and its content.

__init__(path, filespec, kind, name, manifest)
filespec: str

The file’s absolute path in the clone.

kind: str

The manifest’s kind, e.g. SqlPlugin.

manifest: dict[str, Any]

The manifest, with a template’s placeholders filled in.

name: str

The manifest’s metadata.name.

path: str

The file, relative to the repository’s root, e.g. plugins/sql/stackademy-sql.yaml.

property rank: int

The position of the manifest’s kind in SAMKinds.apply_order().

smarter.apps.account.repositories.loader.load_ignore_spec(local_path)[source]

Return the patterns of a repository’s .smarterignore, if it has one.

They are gitignore’s patterns, with its rules, which pathspec implements: a pattern without a slash matches at any depth, a leading slash anchors it to the repository’s root, a trailing slash matches a folder and everything in it, ** matches any number of folders, ! includes again what an earlier pattern excluded, and # starts a comment.

Parameters:

local_path (str) – The repository’s clone.

Return type:

GitIgnoreSpec | None

Returns:

The patterns, or None if the repository has no .smarterignore.