script

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2026 License: AGPL-3.0 Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package script runs an operator-configured capacity provider: an executable named by a `script:<name>` provider in the server's configuration. It imports no other sqi package: the standard library, and golang.org/x/sys on Windows to hold the script's processes in a job.

Wire contract (version 1)

The server starts the configured command once per action. The request is one JSON object on the child's standard input, and the child answers with one JSON object on standard output.

A request carries `version` (always 1), `action`, `group` (`id`, `name`, `kind`) and, by action:

  • `wake`: the `members` to power on. Each member names its `worker_id` and, when known, its `hostname`, `ip_address`, `instance_id` and `interfaces` (`name`, `mac`, `addrs`).
  • `grow`: `desired`, the total number of machines the group should have, and `current`, the size sqi last asked for (or its registered members, when more). The request is absolute: set the backend's desired size to `desired`. A request repeated after a failover carries `current` equal to `desired`.
  • `retire`: the `members` to remove. A member that never registered (an orphaned instance) is named by its `instance_id` alone, with no `worker_id`. A script for a sized group must also lower the backend's desired size by the members it removes (AWS: `--should-decrement-desired-capacity`), or the backend starts replacements.
  • `list`: nothing further. The script reports the instances the group's backend currently runs.

A response carries `result`, `message` and, for `list`, `instance_ids`. `result` is one of:

  • `ok`: the action was carried out (or, for wake, started).
  • `unsupported`: the script does not implement the action. Not a failure: the caller records it and carries on.
  • `error`: the action failed. `message` says why.

Rules the runner applies to the child:

  • Empty standard output with exit status 0 is `ok`.
  • A non-zero exit status is `error`, carrying the exit status and the last line of standard error, whatever standard output held.
  • Standard output that is not one JSON object, or whose `result` is none of the three values, is `error`.
  • Standard output is limited to 1 MiB. A script that writes more is killed and the result is `error`.
  • Standard error is the script's log. It is logged at Info line by line, up to 64 KiB per run, carrying the provider, action and group name; it is never parsed.
  • The environment is the server's own minus every variable named `SQI_*`, plus the provider's configured `env`. Only the `SQI_` variables are removed: every other variable (PG*, cloud credentials, proxies) is inherited, so an operator who needs the script isolated from them wraps the command (`env -i`, a container, a service account).
  • Each configured `env` value of four bytes or more is replaced by `<redacted>` wherever the script's own output carries it: in logged standard-error lines, in the last standard-error line quoted in a failure message and in the response `message`. Values are replaced longest first. A value split across the 8 KiB chunks of one very long line is not caught.
  • A response `message` is cut to 2 KiB on a character boundary, with `...` marking the cut, and has its control characters (newlines included) replaced by spaces, so a script cannot forge log lines through it.
  • The run is bounded by the provider's timeout, two minutes when none is set; a script still running at the deadline is killed and Run returns an error.

Run returns a non-nil error only for a command that cannot start and for a timeout or a canceled context; every other failure is an `error` Response.

Index

Constants

View Source
const (
	// Version is the request version this package writes.
	Version = 1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type GroupRef

type GroupRef struct {
	ID   string `json:"id"`
	Name string `json:"name"`
	Kind string `json:"kind"`
}

GroupRef identifies the capacity group an action concerns.

type Member

type Member struct {
	WorkerID   string         `json:"worker_id,omitempty"`
	Hostname   string         `json:"hostname,omitempty"`
	IPAddress  string         `json:"ip_address,omitempty"`
	InstanceID string         `json:"instance_id,omitempty"`
	Interfaces []NetInterface `json:"interfaces,omitempty"`
}

Member is a group member as a script sees it. A retire of an instance that never registered names only its InstanceID.

type NetInterface

type NetInterface struct {
	Name  string   `json:"name"`
	MAC   string   `json:"mac"`
	Addrs []string `json:"addrs,omitempty"`
}

NetInterface is this package's own copy of protocol.NetInterface's JSON shape, so the package imports no other sqi package.

type Provider

type Provider struct {
	// Name is the provider's configured name, used only for logging and error messages.
	Name string
	// Command is the executable and its arguments. It is never run through a shell.
	Command []string
	// Env is applied over the server's environment after the SQI_ variables are removed.
	Env map[string]string
	// Timeout bounds one run; zero or negative means two minutes.
	Timeout time.Duration
}

Provider is one configured script provider.

func (Provider) MaxRunTime

func (p Provider) MaxRunTime() time.Duration

MaxRunTime is the longest one Run of p can take: its timeout, and then the wait for a killed script's pipes to close.

type Request

type Request struct {
	Version int      `json:"version"`
	Action  string   `json:"action"` // "wake", "grow", "retire" or "list"
	Group   GroupRef `json:"group"`
	Desired int      `json:"desired,omitempty"` // grow
	Current int      `json:"current,omitempty"` // grow
	Members []Member `json:"members,omitempty"` // wake, retire
}

Request is the JSON object a script reads on standard input.

type Response

type Response struct {
	Result      string   `json:"result"` // "ok", "unsupported" or "error"
	Message     string   `json:"message,omitempty"`
	InstanceIDs []string `json:"instance_ids,omitempty"` // list
}

Response is the JSON object a script writes on standard output.

func Run

func Run(ctx context.Context, p Provider, req Request, logger *slog.Logger) (Response, error)

Run executes the provider for one request. It returns a non-nil error only when the command cannot start, the run times out, or ctx ends; any other failure is a Response with Result "error". A nil logger discards the script's standard error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL