stratum-node-cli 0.1.0-alpha.4

The `stratum` client for Stratum pod nodes: operator key files, bootstrap, config, backup, invitations and members over the loopback API; join, pod access and export for tenants through the gate
Documentation
//! The process exit codes of `stratum`, stable across releases.
//!
//! | Code | Meaning |
//! |---|---|
//! | 0 | Success; for `status`, the node is `ready` |
//! | 1 | Local failure: a key, token or resume file, the clock, or writing output |
//! | 2 | The node could not be reached |
//! | 3 | `status`: the node is `unconfigured` |
//! | 4 | `status`: the node is `degraded` |
//! | 5 | The node sent a response that does not follow the API contract |
//! | 6 | Changes were previewed but not applied: no confirmation |
//! | 7 | A local check refused the node's answer (claim body, manifest signature, admission notice, probe, an offered grant the manifest does not render), a profile card that does not name its owner, or an ACL that would give an application `Control` |
//! | 8 | A bounded wait ran out: `join` (the pod was not provisioned within `--wait-secs`) or `member suspend --wait` (the suspension was not yet enforced) |
//! | 9 | `export`: the archive was written, but an entry changed, vanished or was skipped during the walk |
//! | 10 | Server error `bad_request` |
//! | 11 | Server error `unauthorized` |
//! | 12 | Server error `forbidden` |
//! | 13 | Server error `not_found` |
//! | 14 | Server error `conflict` |
//! | 15 | Server error `payload_too_large` |
//! | 16 | Server error `unsupported_media_type` |
//! | 17 | Server error `invalid`, or a preview that reports validation errors |
//! | 18 | Server error `too_many_challenges` |
//! | 19 | Server error `backup_refused` |
//! | 20 | Server error `internal` |
//! | 21 | A server error code this client does not know |
//! | 22 | Server error `request_timeout` |
//! | 23 | Server error `capacity_exhausted` |
//! | 24 | Server error `invitation_refused` |
//! | 25 | Server error `invitations_disabled` |
//! | 26 | Server error `gate_unavailable` |
//! | 27 | Server error `too_many_requests` |
//! | 28 | Server error `misdirected_request` |
//! | 29 | Server error `method_not_allowed` |
//! | 30 | Server error `insufficient_storage` |
//! | 31 | `precondition_failed`: a conditional pod write found another version |
//! | 32 | Server error `bad_gateway` (the gate got no valid answer upstream) |
//! | 33 | Server error `gateway_timeout` (the gate's upstream did not answer in time) |
//! | 34 | Server error `suspended` (the administrator suspended the membership the request depends on) |
//! | 35 | Server error `archive_required` (`member remove` needs an archive from the current suspension) |
//! | 36 | Server error `pod_data_present` (the key's earlier pod data has not been purged) |
//! | 38 | Server error `pod_unavailable` (the pod server cannot be reached, or the node has no pods) |
//! | 39 | Server error `workspace_unavailable` (an installation asked for a workspace and no slot is available) |
//! | 40 | Server error `application_unavailable` (the application is not `approved`) |
//! | 41 | Server error `image_absent` or `image_has_volumes` (approval found the image missing from the tenant daemon, or declaring a volume) |
//! | 42 | `app install`: a container's ACL did not read back as written, or consent was refused (by the node, or because the installation stopped being `pending_consent`); consent was not sent or not recorded, and what was written was removed |
//! | 43 | `app withdraw` or `app cleanup`: the grant's ACL cleanup is still pending for at least one container |
//! | 44 | An ACL the client would have to rewrite holds a statement it does not evaluate; nothing was written to that ACL |
//! | 45 | Server error `runner_unavailable` (the node's runner could not be reached or refused; an application's approval stored nothing) |
//! | 46 | Server error `signer_unavailable` (the node committed an installation's create or consent, but the signer has not yet made or enabled its key; a rerun of the install, or `app list`, finishes a create, and the node itself finishes a consent) |
//! | 64 | Invalid command line |
//!
//! Code 37 is unassigned. An earlier pre-release gave it to `lease_expired`,
//! when that was only the gate's log name for a decision. `lease_expired` is
//! now the pod proxy's 503 to a job, one of the [`JOB_ONLY_CODES`] no person
//! meets, and like them exits 21 if it ever reaches the client.

/// Success. For `status`, the node is `ready`.
pub const OK: u8 = 0;
/// A local failure: a key, token or resume file, the clock, or output.
pub const FAILURE: u8 = 1;
/// The node could not be reached.
pub const UNREACHABLE: u8 = 2;
/// `status`: the node is `unconfigured`.
pub const UNCONFIGURED: u8 = 3;
/// `status`: the node is `degraded`.
pub const DEGRADED: u8 = 4;
/// The node's response does not follow the API contract.
pub const PROTOCOL: u8 = 5;
/// Changes were previewed but not applied because nothing confirmed them.
pub const NOT_CONFIRMED: u8 = 6;
/// A local check refused the node's answer (including an offered grant the
/// manifest does not render), or the client refused to write an ACL that
/// would give an application `Control`.
pub const REFUSED: u8 = 7;
/// A bounded wait ran out. For `join`, the pod was not provisioned within
/// the wait; rerun `join` to continue. For `member suspend --wait`, the
/// suspension is still `enforcing`; `member show` reports when it is
/// `enforced`.
pub const TIMED_OUT: u8 = 8;
/// `export`: the archive and manifest were written, but at least one entry
/// changed, vanished or was skipped during the walk; the manifest lists
/// them.
pub const EXPORT_INCOMPLETE: u8 = 9;
/// Server error `bad_request` (HTTP 400).
pub const BAD_REQUEST: u8 = 10;
/// Server error `unauthorized` (HTTP 401).
pub const UNAUTHORIZED: u8 = 11;
/// Server error `forbidden` (HTTP 403).
pub const FORBIDDEN: u8 = 12;
/// Server error `not_found` (HTTP 404).
pub const NOT_FOUND: u8 = 13;
/// Server error `conflict` (HTTP 409): stale revision, already bound or
/// operation conflict.
pub const CONFLICT: u8 = 14;
/// Server error `payload_too_large` (HTTP 413).
pub const PAYLOAD_TOO_LARGE: u8 = 15;
/// Server error `unsupported_media_type` (HTTP 415).
pub const UNSUPPORTED_MEDIA_TYPE: u8 = 16;
/// Server error `invalid`, or a preview with validation errors. The node
/// sends it with HTTP 422 on the configuration routes and with HTTP 400 for
/// an application's manifest (with `error.field` naming the member); the
/// exit code follows the code, not the status.
pub const INVALID: u8 = 17;
/// Server error `too_many_challenges` (HTTP 429).
pub const TOO_MANY_CHALLENGES: u8 = 18;
/// Server error `backup_refused` (HTTP 409): no backup recipient configured.
pub const BACKUP_REFUSED: u8 = 19;
/// Server error `internal` (HTTP 500).
pub const INTERNAL: u8 = 20;
/// A server error code this client does not know.
pub const UNKNOWN_SERVER_ERROR: u8 = 21;
/// Server error `request_timeout` (HTTP 408): the node gave up waiting for
/// the request to arrive.
pub const REQUEST_TIMEOUT: u8 = 22;
/// Server error `capacity_exhausted` (HTTP 409): every tenant slot is
/// taken; the invitation is still `issued`.
pub const CAPACITY_EXHAUSTED: u8 = 23;
/// Server error `invitation_refused` (HTTP 403): the invitation is unknown,
/// expired, revoked, used or for another identity.
pub const INVITATION_REFUSED: u8 = 24;
/// Server error `invitations_disabled` (HTTP 403):
/// `membership.invitations_enabled` is off.
pub const INVITATIONS_DISABLED: u8 = 25;
/// Server error `gate_unavailable` (HTTP 503). From the node's API: the gate
/// could not be paused and drained for a backup. From the gate: pod writes
/// are paused for a backup or archive, or the gate has not heard from the
/// node's controller in time (its directory lease ran out). The gate
/// forwarded nothing, so the request can be retried shortly.
pub const GATE_UNAVAILABLE: u8 = 26;
/// Server error `too_many_requests` (HTTP 429): over the admission rate
/// limit.
pub const TOO_MANY_REQUESTS: u8 = 27;
/// Server error `misdirected_request` (HTTP 421): `Host` is not the node's
/// canonical authority, so `--origin` is wrong.
pub const MISDIRECTED_REQUEST: u8 = 28;
/// Server error `method_not_allowed` (HTTP 405), including a `DELETE` of
/// the root ACL.
pub const METHOD_NOT_ALLOWED: u8 = 29;
/// Server error `insufficient_storage` (HTTP 507): the pod volume is low
/// and writes are stopped.
pub const INSUFFICIENT_STORAGE: u8 = 30;
/// `precondition_failed` (HTTP 412): `--if-match` named another version,
/// or `--create-only` found the resource already there.
pub const PRECONDITION_FAILED: u8 = 31;
/// Server error `bad_gateway` (HTTP 502): the gate got no valid answer
/// from the pod server or the controller.
pub const BAD_GATEWAY: u8 = 32;
/// Server error `gateway_timeout` (HTTP 504): the pod server or the
/// controller did not answer the gate in time. A write may or may not have
/// happened; read before writing again.
pub const GATEWAY_TIMEOUT: u8 = 33;
/// Server error `suspended` (HTTP 403): the administrator has suspended the
/// membership the request depends on, the caller's own or the pod owner's.
/// Signed requests by or to a suspended member are refused until the
/// administrator resumes it; an existing public profile stays readable.
pub const SUSPENDED: u8 = 34;
/// Server error `archive_required` (HTTP 409): `member remove` needs an
/// archive taken during the member's current suspension. Run
/// `member archive` first, or waive it with `--no-archive`.
pub const ARCHIVE_REQUIRED: u8 = 35;
/// Server error `pod_data_present` (HTTP 409): an invitation or redemption
/// for a removed or cancelled key whose earlier pod data has not been
/// purged yet.
pub const POD_DATA_PRESENT: u8 = 36;
/// Server error `pod_unavailable` (HTTP 503): the pod server could not be
/// reached or broke off (from the gate), or the node runs without pods or
/// has no canonical origin yet (from the controller).
pub const POD_UNAVAILABLE: u8 = 38;
/// Server error `workspace_unavailable` (HTTP 409): the installation asked
/// for a workspace and the node has no slot for it. In Step 3b-2 every
/// workspace mode other than `none` gets this answer.
pub const WORKSPACE_UNAVAILABLE: u8 = 39;
/// Server error `application_unavailable` (HTTP 409): the application is
/// withdrawn or disabled, so it cannot be installed.
pub const APPLICATION_UNAVAILABLE: u8 = 40;
/// Server error `image_absent` or `image_has_volumes` (HTTP 409): approval
/// found the manifest's image ID missing from the tenant daemon, or found
/// that the image declares a volume.
pub const IMAGE_REFUSED: u8 = 41;
/// `app install` did not complete its consent: a container's ACL did not
/// read back as written, the installation stopped being `pending_consent`
/// before a write, or the node refused the consent. Consent was not sent,
/// or not recorded, and the client removed what it had written (the error
/// lists anything it could not remove).
pub const GRANT_NOT_CONFIRMED: u8 = 42;
/// `app withdraw` or `app cleanup`: the withdrawn grant's ACL cleanup is
/// still pending for at least one container; run `stratum app cleanup`
/// again.
pub const CLEANUP_PENDING: u8 = 43;
/// An ACL the client would have to rewrite holds a statement it does not
/// evaluate (an `acl:agentGroup`, an `acl:origin`, an agent class other than
/// `foaf:Agent`, an unknown predicate or type, an ACL that is not Turtle);
/// nothing was written to that ACL.
pub const ACL_NOT_EVALUATED: u8 = 44;
/// Server error `runner_unavailable` (HTTP 503): an application's approval
/// needs the node's runner to read the image, and the runner is not
/// configured, did not answer, or refused. Nothing was stored; approve
/// again once the runner answers.
pub const RUNNER_UNAVAILABLE: u8 = 45;
/// Server error `signer_unavailable` (HTTP 503): the node committed an
/// installation's create or consent, but the signer did not finish its step
/// (making the application key, or confirming it enabled). The commit
/// stays. After a create, `app install` again with the same
/// `--operation-id`, or `app list`, finishes it (and so does the node's
/// next start); after a consent the node's key sync keeps trying, and
/// `app show` reports when the installation is `active`.
pub const SIGNER_UNAVAILABLE: u8 = 46;
/// The command line could not be parsed.
pub const USAGE: u8 = 64;

/// The server error codes no person meets, which therefore have no exit code
/// of their own and fall to [`UNKNOWN_SERVER_ERROR`] (21) if they ever reach
/// the client.
///
/// The first ten are the answers a job sees on its lease socket: the pod
/// proxy's own refusals (`lease_revoked`, `outside_grant`, `lease_starting`,
/// `lease_expired`, `busy`, `pod_auth_refused`) and the signer's refusals the
/// proxy relays (`key_disabled`, `no_window`, `outside_pod`,
/// `payload_rule`). The last two are the runner's refusals, which only the
/// controller sees (`container_identity_conflict`, `create_expired`). No
/// route a person calls answers any of them: a person signs with their own
/// key, never through a lease.
///
/// # Examples
///
/// ```
/// use stratum_node_cli::exit;
///
/// for code in exit::JOB_ONLY_CODES {
///     assert_eq!(exit::for_server_code(code), exit::UNKNOWN_SERVER_ERROR);
/// }
/// ```
pub const JOB_ONLY_CODES: [&str; 12] = [
    "lease_revoked",
    "outside_grant",
    "lease_starting",
    "lease_expired",
    "busy",
    "pod_auth_refused",
    "key_disabled",
    "no_window",
    "outside_pod",
    "payload_rule",
    "container_identity_conflict",
    "create_expired",
];

/// Returns the exit code for a server error code from the API contract.
///
/// # Examples
///
/// ```
/// use stratum_node_cli::exit;
///
/// assert_eq!(exit::for_server_code("conflict"), exit::CONFLICT);
/// assert_eq!(exit::for_server_code("request_timeout"), exit::REQUEST_TIMEOUT);
/// assert_eq!(exit::for_server_code("capacity_exhausted"), exit::CAPACITY_EXHAUSTED);
/// assert_eq!(exit::for_server_code("suspended"), exit::SUSPENDED);
/// assert_eq!(exit::for_server_code("pod_unavailable"), exit::POD_UNAVAILABLE);
/// assert_eq!(exit::for_server_code("image_has_volumes"), exit::IMAGE_REFUSED);
/// assert_eq!(exit::for_server_code("runner_unavailable"), exit::RUNNER_UNAVAILABLE);
/// assert_eq!(exit::for_server_code("signer_unavailable"), exit::SIGNER_UNAVAILABLE);
/// assert_eq!(exit::for_server_code("something_new"), exit::UNKNOWN_SERVER_ERROR);
/// // `lease_expired` is the pod proxy's answer to a job; no person meets it.
/// assert_eq!(exit::for_server_code("lease_expired"), exit::UNKNOWN_SERVER_ERROR);
/// ```
pub fn for_server_code(code: &str) -> u8 {
    match code {
        "bad_request" => BAD_REQUEST,
        "unauthorized" => UNAUTHORIZED,
        "forbidden" | "app_forbidden" => FORBIDDEN,
        "not_found" => NOT_FOUND,
        "conflict" => CONFLICT,
        "payload_too_large" => PAYLOAD_TOO_LARGE,
        "unsupported_media_type" => UNSUPPORTED_MEDIA_TYPE,
        "invalid" => INVALID,
        "too_many_challenges" => TOO_MANY_CHALLENGES,
        "backup_refused" => BACKUP_REFUSED,
        "internal" => INTERNAL,
        "request_timeout" => REQUEST_TIMEOUT,
        "capacity_exhausted" => CAPACITY_EXHAUSTED,
        "invitation_refused" => INVITATION_REFUSED,
        "invitations_disabled" => INVITATIONS_DISABLED,
        "gate_unavailable" => GATE_UNAVAILABLE,
        "too_many_requests" | "rate_limited" => TOO_MANY_REQUESTS,
        "misdirected_request" => MISDIRECTED_REQUEST,
        "method_not_allowed" => METHOD_NOT_ALLOWED,
        "insufficient_storage" => INSUFFICIENT_STORAGE,
        "precondition_failed" => PRECONDITION_FAILED,
        "bad_gateway" => BAD_GATEWAY,
        "gateway_timeout" => GATEWAY_TIMEOUT,
        "suspended" => SUSPENDED,
        "archive_required" => ARCHIVE_REQUIRED,
        "pod_data_present" => POD_DATA_PRESENT,
        "pod_unavailable" => POD_UNAVAILABLE,
        "workspace_unavailable" => WORKSPACE_UNAVAILABLE,
        "application_unavailable" => APPLICATION_UNAVAILABLE,
        "image_absent" | "image_has_volumes" => IMAGE_REFUSED,
        "runner_unavailable" => RUNNER_UNAVAILABLE,
        "signer_unavailable" => SIGNER_UNAVAILABLE,
        _ => UNKNOWN_SERVER_ERROR,
    }
}