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:
manage.py load_from_github, whose command is aRepositoryLoaderwith arguments: seesmarter.apps.account.management.commands.load_from_github.manage.py initialize_platformandmanage.py initialize_accountload the example manifests and Stackademy with it.The web console’s Apply page, which applies a repository as a user may: see
smarter.apps.account.repositories.apply.
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.yamlexamples. Seeload_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:
SmarterCommandFind, order and apply the manifests of a repository’s clone, as a user, through the same brokers as the
applyapi.It is a management command class, without arguments or a
handle(), because it reports its progress the way a command does, toself.stdoutandself.stderr, in the command’s styles.manage.py load_from_githubwrites 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.
- 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.
- 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 toskipped.
- 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 inowners.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:
- apply_manifests(manifests, user_profile, deploy=False)[source]
Apply the manifests, in order, and deploy the LLMClients if
deploy.- Parameters:
manifests (
list[RepositoryManifest]) – The manifests, fromfind_manifests().user_profile (
UserProfile) – The owner of the resources.deploy (
bool) – Deploy each LLMClient that is not deployed.
- Returns:
A description of each manifest that failed to apply or deploy.
- Return type:
- 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.yamlgives 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 keepsSAMKinds.apply_order().- Parameters:
item (
RepositoryManifest) – The manifest.- Returns:
(rank, 0 or 1, path): 1 for an owned manifest moved after the Users.- Return type:
- 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:
- 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_manifestjust applied, unlessdeploy_skipped_reason()gives a reason.- Parameters:
item (
RepositoryManifest) – The LLMClient’s manifest, as the repository holds it.user_profile (
UserProfile) – Its owner.
- Returns:
A description of the failure, if it failed to deploy, else nothing.
- Return type:
- 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:
- deploy_skipped_reason(manifest, user_profile)[source]
Return why
--deployleaves 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.yamlgives 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.
- 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:
- 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:
- 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 byinitialize_account, and never deployed.
- static keep_deployed(manifest, user_profile)[source]
Return a copy of an LLMClient manifest whose
deployedis 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.
- 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_extensionsinstalls them, from a registry that may not be reachable.
- owner_profile_for(item, user_profile)[source]
Return the profile that applies a manifest: its folder’s owner in
owners.yaml, elseuser_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. byinitialize_account: they belong to their owner, not to that account. Seesmarter.apps.account.repositories.owners.- Parameters:
item (
RepositoryManifest) – The manifest.user_profile (
UserProfile) –load_from_github’s own user.
- Returns:
The profile to apply it as, or None to skip it.
- Return type:
- Raises:
SmarterValueError – If its owner doesn’t exist, or doesn’t belong to the account that
owners.yamlnames.
- 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']) –preorpost.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:
- select_manifests(manifests, included, excluded)[source]
Return the manifests of the kinds to apply, and say which were skipped.
- Parameters:
manifests (
list[RepositoryManifest]) – The manifests, fromfind_manifests().included (
list[str]) – Apply only these kinds, or every kind if it is empty.
- Returns:
The manifests to apply, in the same order.
- Return type:
- skipped: set[str]
The manifests, by file, that
apply()skipped, because they name a resource that doesn’t exist.
- property source_repository: str
The repository’s url, without a trailing slash or
.git, as resources are annotated with it.
- user_profile: UserProfile | None = None
- class smarter.apps.account.repositories.loader.RepositoryManifest(path, filespec, kind, name, manifest)[source]
Bases:
objectA manifest of the repository: its file, relative to the repository’s root, and its content.
- __init__(path, filespec, kind, name, manifest)
- 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.