Manage tenants from code
Create, deploy, limit, copy and delete tenants from a luna with the tenants client, as your customers sign up.
A platform adds a tenant each time a customer signs up, and deploys the customer’s code to it each time they publish. Running luna tenants by hand does not keep up, so a luna of yours does it with tenants.
Give a luna a token
The tenants client needs an API token with the tenants:write scope. Make one and store it as a secret of the environment, in one command.
luna tokens create --name platform --scope tenants:write --project <ORG>/<PROJECT> --env production | luna secrets set PLATFORM_TOKEN --env production
The token goes from one command to the other without being shown. A token for an environment reaches the tenants added to it as well.
Create a tenant
In a luna of the default tenant, pass the token and ctx.tenant as the parent. create takes the new tenant’s name and returns its full name and its two keys.
import { defineLuna, t, tenants } from "luna";
export const User = defineLuna({
name: "User",
key: { id: t.string() },
handlers: (h) => ({
open: h({}, async (ctx) => {
const platform = tenants({ token: ctx.env.PLATFORM_TOKEN, parent: ctx.tenant });
const made = await platform.create(ctx.key.id, { labels: { plan: "free" } });
return { publishableKey: made.publishableKey };
}),
}),
});
The client calls Luna from inside your luna, so the handler needs no network access. made.apiToken is the tenant’s secret token: keep it in your lunas, never in a reply to an app.
Deploy code to a tenant
deploy takes source files as text, keyed by path. Luna builds them, checks the change the way luna deploy does, and deploys it.
publish: h({ args: { code: t.string() } }, async (ctx, { code }) => {
const platform = tenants({ token: ctx.env.PLATFORM_TOKEN, parent: ctx.tenant });
const result = await platform.deploy(ctx.key.id, { files: { "index.ts": code } });
if (!result.deployed) return { deployed: false, preview: result.preview };
return { deployed: true, version: result.version };
}),
Name the entry module in entry, such as app.py for Python. Without one, Luna reads luna.config.ts when the files include it, or else looks for a module such as index.ts, main.ts or src/lunas.ts. The files can be at most 48 MiB together.
A deploy Luna refuses returns deployed: false, and preview lists why. The old version keeps running. Code that fails to load throws, with its error.
Set a tenant’s limits
configure sets a tenant’s quotas, its host names, the services it may call and its outbound handler.
await platform.configure(ctx.key.id, {
quotas: { requestsPerSecond: 50, storageBytes: 1_000_000_000 },
grants: { wallet: { luna: "Wallet" } },
});
| Quota | Does |
|---|---|
requestsPerSecond |
Turns calls past this rate away with status 429 |
storageBytes |
Turns writes away once the tenant stores this many bytes. Reads still work |
cpuPercent |
Caps the tenant’s CPU, as a percent of one core |
sandboxes |
Caps how many sandboxes run at once |
A quota left out falls back to your plan’s limit. Grants are in Call across tenants.
Check what a tenant sends out
An outbound handler sees each request the tenant’s code makes to the internet, and can refuse it. Name a luna of yours and one of its handlers.
await platform.configure(ctx.key.id, {
outbound: { luna: "Gate", key: { id: "main" }, handler: "check" },
});
The handler takes tenant, method, url, host and port as its arguments, and answers { allow: true } to let the request through. Any other answer, an error, or no answer within 5 seconds refuses it. The tenant’s fetch then gets status 403. The default tenant cannot have an outbound handler.
Find tenants
list returns a page of the environment’s tenants, the default tenant among them. Pass labels to keep only the tenants that have all of them.
const page = await platform.list({ labels: { plan: "pro" }, limit: 100 });
Each entry has the tenant’s full name, its version and its labels. Pass page.cursor as after to read the next page, until it is null. get(name) returns one tenant, with its URL, version, settings and luna types.
Copy a tenant
fork copies every luna of one tenant into another, a page at a time. Pass at to copy them as they were at an earlier moment, in milliseconds since the epoch.
const copied = await platform.fork(ctx.key.id, "acme-staging", { at: Date.now() - 3_600_000 });
A destination that does not exist yet is created. One with nothing deployed gets the source’s code and settings first. Copies start with no schedules, unless you pass schedules: true.
fork returns how many lunas it copied, and how many it skipped because they already exist in the destination or did not exist yet at at. It needs a token with data:read and data:write.
Delete a tenant
delete deletes a tenant with its lunas, data, versions, keys and secrets.
await platform.delete(ctx.key.id);
Next steps
- Call across tenants lets the platform and its tenants call each other.
- Read a tenant’s API describes what a tenant’s code exposes.
- Write lunas in Python shows the same client from Python.