Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-pkg-server

Source of release 0.6.1

0.6.1 / src / admin.lucb

## Local registry operator commands. The store token is accepted only through
## the process environment; it is never an argument or public HTTP credential.
from luce_auth import auth
import c
from luce_crypto import native as crypto
from luce_db import db
import interop
import os
import strings
from luce_std import files, paths
import repositories
import memory
from luce_prism import prism
import migrate as migration
import site as site_files

func store_token() -> str!:
    let token = os.env((c.str)"LUCE_REGISTRY_STORE_TOKEN") else error(auth.invalid, "LUCE_REGISTRY_STORE_TOKEN is required")
    if token.length == 0: error(auth.invalid, "empty store token")
    return token

func generate_token() -> i32!:
    var secret = try crypto.random_secret(32)
    defer secret.close()
    let bytes = try secret.bytes()
    let digits = b"0123456789abcdef"
    var output: u8[64]
    for i in 0..<bytes.length:
        output[i * 2] = digits[(usize)(bytes[i] >> 4)]
        output[i * 2 + 1] = digits[(usize)(bytes[i] & 15)]
    print((str)output)
    return 0

func initialize(path: str) -> i32!:
    let database = try db.Database.open(path, try store_token())
    defer database.release()
    let authority = try auth.Authority.attach(try database.value().store())
    defer authority.release()
    try repositories.initialize(database.value())
    _ = try database.value().bake(wait_ms = 60000)
    print("initialized")
    return 0

## Invitation issuance uses the running owner's Unix socket. This keeps the
## database lock single-owner and makes the invitation durable before printing.
func invite(socket: str) -> i32!:
    let database = try db.Database.connect(socket, try store_token())
    defer database.release()
    let authority = try auth.Authority.attach(try database.value().store())
    defer authority.release()
    let code = try authority.value().invite()
    defer code.release()
    print(try code.value().text_at())
    return 0

## Checkpoint (bake) the database. Given the running registry's `<database>.sock`, it
## asks the owner to bake online, queueing behind any writer; the deployment's timer
## does this. Given the database path, the registry must be stopped so this process
## can take the exclusive Prism owner lock; backup tooling does that before copying.
func checkpoint(path: str) -> i32!:
    if strings.ends_with(path, ".sock"):
        let online = try db.Database.connect(path, try store_token())
        defer online.release()
        let generation = try online.value().bake(wait_ms = 60000)
        print(f"checkpoint {generation}")
        return 0
    let database = try db.Database.open(path, try store_token())
    defer database.release()
    let authority = try auth.Authority.attach(try database.value().store())
    defer authority.release()
    try repositories.initialize(database.value())
    let generation = try database.value().bake(wait_ms = 60000)
    print(f"checkpoint {generation}")
    return 0

## Copy a stopped registry's database into a fresh one whose Git objects live in files
## (see migrate.lucb). The source is only read; the target and `<target>.objects` are
## created or resumed. Both use the store token from the environment.
func migrate(source: str, target: str) -> i32!:
    if source == target: error(auth.invalid, "migrate into a different database path")
    let source_name = try strings.copy(source)
    defer strings.release(source_name)
    if not files.exists((c.str)source_name): error(auth.invalid, "the source database does not exist")
    let token = try store_token()
    let old = try db.Database.open(source, token)
    defer old.release()
    let fresh = try db.Database.open(target, token)
    defer fresh.release()
    try repositories.use_objects(target)
    var source_buffer: u8[1100]
    var target_buffer: u8[1100]
    let summary = try migration.migrate(old.value(), try strings.format(source_buffer, f"{source}.objects"),
                                        fresh.value(), try strings.format(target_buffer, f"{target}.objects"))
    print(f"migrated {summary.elements} elements, {summary.objects} objects ({summary.object_bytes} bytes), wrote {summary.written} object files, removed {summary.removed}")
    return 0

## The database an operator command works on: through the running owner's socket for a
## `.sock` path, else opened directly, which needs the registry stopped. Git objects
## are found beside the database either way.
func operate(path: str) -> interop.Reference[db.Database]!:
    let online = strings.ends_with(path, ".sock")
    var database_buffer: u8[1100]
    let database_path = try strings.format(database_buffer, f"{(str)path.bytes[..<path.length - 5]}") if online else path
    try repositories.use_objects(database_path)
    return try db.Database.connect(path, try store_token()) if online else try db.Database.open(path, try store_token())

## Remove the package `owner/name`: its repository (record, refs, object listings and the
## object files no other repository lists) and everything the site holds for it (index
## line, release files, versions, package page, source browser), then the front pages.
## Refuses while another package's latest release depends on it, unless `force`. Like
## `checkpoint`, a `.sock` path goes through the running owner; a database path needs
## the registry stopped. The site is `LUCE_REGISTRY_SITE`.
func remove(path: str, coordinate: str, force: bool) -> i32!:
    let (owner, name) = strings.split_once(coordinate, "/") else error(auth.invalid, "name the package as owner/name")
    if not repositories.valid_name(name) or owner.length == 0: error(auth.invalid, "invalid package name")
    let site = os.env((c.str)"LUCE_REGISTRY_SITE") else error(auth.invalid, "LUCE_REGISTRY_SITE is required")
    if site.length == 0: error(auth.invalid, "empty LUCE_REGISTRY_SITE")
    let dependents = try new u8[65536] --- in memory.heap
    defer free(dependents) in memory.heap
    let dependents_size = try site_files.dependents(site, owner, name, dependents)
    if dependents_size > 0:
        print(f"{coordinate} is a dependency of the latest release of:")
        print((str)dependents[..<dependents_size - 1])
        if not force:
            print("refusing to remove it; pass --force to remove it anyway")
            return 1
    let online = strings.ends_with(path, ".sock")
    let database = try operate(path)
    defer database.release()
    var removed: repositories.Removal
    var in_database = true
    removed = repositories.remove(database.value(), owner, name) catch failure:
        if failure.code != prism.missing: error(failure.code, failure.message)
        in_database = false
        recover repositories.Removal()
    if not online: _ = try database.value().bake(wait_ms = 60000)
    let on_site = try site_files.remove_package(site, owner, name)
    if not in_database and not on_site.listed and not on_site.directory:
        print(f"{coordinate}: no such package")
        return 1
    if in_database:
        print(f"removed repository {coordinate}: {removed.releases} release tags, {removed.objects} objects listed ({removed.deleted} object files deleted, {removed.shared} shared with other repositories kept)")
    else:
        print(f"{coordinate} had no repository")
    let listed = "index entry" if on_site.listed else "no index entry"
    if on_site.directory:
        print(f"removed from the site: {listed}, its directory with {on_site.releases} releases; front pages rewritten")
    else:
        print(f"removed from the site: {listed}, no package directory; front pages rewritten")
    return 0

## Write the whole public site into `site` from the database alone (see
## `repositories.rebuild_site`). Backups hold only the database, so a restore runs this;
## so does pruning, after `withdraw`. `site` is new or an empty directory the operator
## prepared with the site's owner, group and mode; one holding a site is refused.
## Through `<database>.sock` it reads the running registry; a database path needs the
## registry stopped. The site's stylesheets and scripts come from the release bundle.
func rebuild_site(path: str, site: str) -> i32!:
    try files.ensure_directory(site)
    let index = try paths.join(site, "index")
    defer index.release()
    if files.exists((c.str)index.value): error(auth.invalid, "this directory already holds a site; rebuild into a new or empty one")
    let database = try operate(path)
    defer database.release()
    let rebuilt = try repositories.rebuild_site(database.value(), site)
    print(f"rebuilt {site}: {rebuilt.packages} packages, {rebuilt.releases} releases, {rebuilt.failed} failed")
    return 1 if rebuilt.failed > 0 else 0

## Withdraw releases of `owner/name`: their tags leave the database, so the next
## `rebuild-site` leaves them out. The newest release always stays.
func withdraw(path: str, coordinate: str, versions: str[]) -> i32!:
    let (owner, name) = strings.split_once(coordinate, "/") else error(auth.invalid, "name the package as owner/name")
    if not repositories.valid_name(name) or owner.length == 0: error(auth.invalid, "invalid package name")
    let online = strings.ends_with(path, ".sock")
    let database = try operate(path)
    defer database.release()
    for version in versions:
        try repositories.withdraw(database.value(), owner, name, version)
        print(f"withdrew {coordinate} {version}")
    if not online: _ = try database.value().bake(wait_ms = 60000)
    return 0

## Trim packages to their releases (see `repositories.trim`): the refs other than release
## tags and the objects no release reaches leave the database, and the object files no
## repository lists any more are deleted. The registry keeps what installs and updates
## use; history is GitHub's. `--all` trims every package in the site's index
## (`LUCE_REGISTRY_SITE`). Needs the registry stopped (a database path, not `.sock`).
func trim(path: str, coordinates: str[]) -> i32!:
    if strings.ends_with(path, ".sock"): error(auth.invalid, "trim runs with the registry stopped: give the database path")
    let database = try operate(path)
    defer database.release()
    var failed = 0
    if coordinates.length == 1 and coordinates[0] == "--all":
        let site = os.env((c.str)"LUCE_REGISTRY_SITE") else error(auth.invalid, "LUCE_REGISTRY_SITE is required")
        let index = try paths.join(site, "index")
        defer index.release()
        let bytes = try files.read((c.str)index.value)
        defer bytes.release()
        let lines = try strings.split(strings.trim((str)bytes.value), '\n')
        defer free(lines)
        for line in lines:
            let (coordinate, _) = strings.split_once(line, "\t") else error(auth.invalid, "unreadable site index line")
            if not try trim_one(database.value(), coordinate): failed += 1
    else:
        for coordinate in coordinates:
            if not try trim_one(database.value(), coordinate): failed += 1
    _ = try database.value().bake(wait_ms = 60000)
    return 1 if failed > 0 else 0

## Trim one `owner/name` and say what went; false when it could not be trimmed.
func trim_one(database: db.Database*, coordinate: str) -> bool!:
    let (owner, name) = strings.split_once(coordinate, "/") else error(auth.invalid, "name the package as owner/name")
    if not repositories.valid_name(name) or owner.length == 0: error(auth.invalid, "invalid package name")
    let trimmed = repositories.trim(database, owner, name) catch failure:
        print(f"{coordinate}: not trimmed: {failure.message}")
        return false
    print(f"{coordinate}: kept {trimmed.releases} release tags and the {trimmed.kept} objects they reach; removed {trimmed.refs} other refs and {trimmed.unlisted} object listings ({trimmed.deleted} files deleted, {trimmed.shared} shared kept)")
    return true

pub func main(arguments: str[]) -> i32!:
    if arguments.length == 2 and arguments[1] == "token": return try generate_token()
    if arguments.length == 4 and arguments[1] == "rebuild-site": return try rebuild_site(arguments[2], arguments[3])
    if arguments.length >= 5 and arguments[1] == "withdraw": return try withdraw(arguments[2], arguments[3], arguments[4..])
    if arguments.length == 4 and arguments[1] == "migrate": return try migrate(arguments[2], arguments[3])
    if arguments.length >= 4 and arguments[1] == "trim": return try trim(arguments[2], arguments[3..])
    if arguments.length == 4 and arguments[1] == "remove": return try remove(arguments[2], arguments[3], false)
    if arguments.length == 5 and arguments[1] == "remove" and arguments[4] == "--force": return try remove(arguments[2], arguments[3], true)
    if arguments.length != 3:
        return 2
    if arguments[1] == "init": return try initialize(arguments[2])
    if arguments[1] == "invite": return try invite(arguments[2])
    if arguments[1] == "checkpoint": return try checkpoint(arguments[2])
    return 2