spec

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 23 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 spec defines the Anyserve service definition format: a declarative description of how to download, configure and run one server or service.

Definitions are data, not code. They can name files to download, files to write from templates, a fixed set of built-in step types and the command to run, but they can never run arbitrary shell. Every path they mention must stay inside the instance directory.

Index

Constants

View Source
const (
	// KindServer is one program Anyserve installs and supervises itself.
	KindServer = "server"
	// KindStack is a group of container images that run together, such as a
	// web app with its database and proxy. Only container targets run them.
	KindStack = "stack"
)

Definition kinds.

View Source
const SchemaVersion = 1

SchemaVersion is the only definition schema this build understands.

Variables

View Source
var FileFormats = []string{"ini", "lines", "json"}

FileFormats are the formats `set` understands.

View Source
var KnownOS = []string{"linux", "windows", "darwin"}

Known operating systems, using Go's GOOS names.

View Source
var Launchers = map[string]Launcher{
	"xvfb":   {Program: "xvfb-run", Args: []string{"-a"}, Install: "a virtual display; on Debian or Ubuntu: sudo apt install xvfb"},
	"dotnet": {Program: "dotnet", Install: "the .NET runtime; see https://learn.microsoft.com/dotnet/core/install/linux"},
	"java":   {Program: "java", Install: "a Java runtime; on Debian or Ubuntu: sudo apt install default-jre-headless"},
	"mono":   {Program: "mono", Install: "the Mono runtime; on Debian or Ubuntu: sudo apt install mono-runtime"},
}

Launchers are the host launchers definitions may name in run.via.

View Source
var StepTypes = map[string]string{
	"copy_dir_if_missing":  "copy directory `from` to `to` unless `to` exists",
	"copy_file_if_missing": "copy file `from` to `to` unless `to` exists",
	"copy_file":            "copy file `from` to `to`, replacing it",
	"touch":                "create an empty file at `path` unless it exists",
	"mkdir":                "create directory `path`",
	"chmod_exec":           "mark `path` as executable (no-op on Windows)",
	"steamcmd_file":        "copy file `from` (relative to Anyserve's SteamCMD folder, e.g. linux32/steamclient.so) to `to`",
	"steam_sdk":            "copy SteamCMD's steamclient libraries into .steam/sdk32 and .steam/sdk64, where many Steam servers look for them",
	"exec":                 "run the server's own `command` with `args` once, after config files are written, unless `creates` exists (e.g. to create a first world)",
}

StepTypes are the built-in step types a definition may use.

Functions

func AppliesTo

func AppliesTo(list []string, goos string) bool

AppliesTo reports whether an OS list (empty = all) includes goos.

func ArchiveURL

func ArchiveURL(u string) bool

ArchiveURL reports whether a URL names a supported archive type.

func HostFolder

func HostFolder(v string) (string, error)

HostFolder checks a folder setting's value: an existing folder on this machine, given as an absolute path or starting with ~. It returns the cleaned absolute path with symlinks resolved.

func RandomPassword

func RandomPassword(n int) string

RandomPassword returns a random password made of unambiguous characters.

func Render

func Render(name, tmpl string, ctx Context) (string, error)

Render executes one template string against ctx.

Types

type Archive

type Archive struct {
	URL    string `yaml:"url" json:"url"`
	SHA256 string `yaml:"sha256,omitempty" json:"sha256,omitempty"`
	// Subdir picks the folder inside the archive to install, per OS.
	// "*" applies to every OS without its own entry.
	Subdir map[string]string `yaml:"subdir,omitempty" json:"subdir,omitempty"`
	// Format names the archive type when the URL doesn't end in one:
	// zip, tar.gz or tar.xz.
	Format string `yaml:"format,omitempty" json:"format,omitempty"`
	// Keep lists files (relative to the server folder) that aren't
	// replaced if they already exist, such as config files the archive
	// ships with.
	Keep []string `yaml:"keep,omitempty" json:"keep,omitempty"`
	// To is the folder (relative to the server folder) to extract into.
	// Empty means the server folder itself.
	To string `yaml:"to,omitempty" json:"to,omitempty"`
}

Archive downloads a .zip, .tar.gz or .tar.xz and extracts part of it.

type Backup

type Backup struct {
	Paths []string `yaml:"paths,omitempty" json:"paths,omitempty"`
	// StopFirst stops a running service for the backup and starts it again
	// afterwards. Databases need this, because copying their files while
	// they run can give a backup that won't restore.
	StopFirst bool `yaml:"stop_first,omitempty" json:"stop_first,omitempty"`
}

Backup lists what to back up, relative to the instance directory.

type Choice

type Choice struct {
	Value string `yaml:"value" json:"value"`
	Label string `yaml:"label" json:"label"`
}

Choice is one option of a choice setting.

type Console

type Console struct {
	// Say is a template for broadcasting a message, e.g. "say {{.Message}}".
	Say string `yaml:"say,omitempty" json:"say,omitempty"`
	// Save is a command that saves the world, used before backups.
	Save string `yaml:"save,omitempty" json:"save,omitempty"`
	// RCON describes the server's remote console. When set, `anyserve
	// console` uses it, so commands get real replies, and other RCON tools
	// can connect with the details `anyserve console --info` shows.
	RCON *RCON `yaml:"rcon,omitempty" json:"rcon,omitempty"`
	// Exec gives a stack a console: a program run inside one container,
	// such as a database shell.
	Exec *ConsoleExec `yaml:"exec,omitempty" json:"exec,omitempty"`
}

Console describes the server's stdin console.

type ConsoleExec

type ConsoleExec struct {
	Service string   `yaml:"service" json:"service"`
	Command []string `yaml:"command" json:"command"`
	// Description says what the console is, e.g. "PostgreSQL shell".
	Description string `yaml:"description,omitempty" json:"description,omitempty"`
}

ConsoleExec runs a program inside one of a stack's containers.

type Container

type Container struct {
	// Image is the base image the server runs in. Server files are mounted
	// into it, never baked in, so nothing non-redistributable is published.
	Image string `yaml:"image" json:"image"`
	// Packages to install on top of the image (Debian package names).
	Packages []string `yaml:"packages,omitempty" json:"packages,omitempty"`
}

Container holds hints for the container and Kubernetes targets.

type Context

type Context struct {
	// Name is the instance name, e.g. "my-rust".
	Name string
	// Dir is the absolute instance directory, in the host's path format.
	Dir string
	// OS is the host OS (linux, windows, darwin).
	OS       string
	Settings map[string]any
	Ports    map[string]int
	// Message is set only when rendering console.say.
	Message string
	// UID and GID are the user and group the instance runs as (stack
	// containers run as this user too).
	UID, GID int
}

Context is the data available to every template in a definition.

type Definition

type Definition struct {
	Schema      int    `yaml:"schema" json:"schema"`
	ID          string `yaml:"id" json:"id"`
	Name        string `yaml:"name" json:"name"`
	Description string `yaml:"description" json:"description"`
	Homepage    string `yaml:"homepage,omitempty" json:"homepage,omitempty"`
	// Version is the definition's own version. Bump it when the definition
	// changes in a way installed instances should pick up.
	Version string `yaml:"version" json:"version"`
	// Requires is the oldest Anyserve release that can install this
	// definition (e.g. "0.2.0"), for changes older releases would read
	// but get wrong. Older releases skip the definition and say to update.
	Requires string `yaml:"requires,omitempty" json:"requires,omitempty"`
	// Kind is "server" (the default) or "stack".
	Kind string `yaml:"kind,omitempty" json:"kind,omitempty"`
	// Platforms lists the operating systems with a native server build.
	// Stacks leave it out: they run wherever a container target runs.
	Platforms []string `yaml:"platforms,omitempty" json:"platforms,omitempty"`

	Install   Install        `yaml:"install" json:"install"`
	Steps     []Step         `yaml:"steps,omitempty" json:"steps,omitempty"`
	Settings  []Setting      `yaml:"settings,omitempty" json:"settings,omitempty"`
	Ports     []Port         `yaml:"ports" json:"ports"`
	Files     []File         `yaml:"files,omitempty" json:"files,omitempty"`
	Run       map[string]Run `yaml:"run" json:"run"`
	Stop      Stop           `yaml:"stop,omitempty" json:"stop,omitempty"`
	Console   Console        `yaml:"console,omitempty" json:"console,omitempty"`
	Backup    Backup         `yaml:"backup,omitempty" json:"backup,omitempty"`
	Container *Container     `yaml:"container,omitempty" json:"container,omitempty"`
	Stack     *Stack         `yaml:"stack,omitempty" json:"stack,omitempty"`
	LinuxGSM  *LinuxGSM      `yaml:"linuxgsm,omitempty" json:"linuxgsm,omitempty"`
	Notes     []string       `yaml:"notes,omitempty" json:"notes,omitempty"`
	Extra     map[string]any `yaml:",inline" json:"-"`
}

Definition is one service definition file.

func LoadFile

func LoadFile(p string) (*Definition, error)

LoadFile reads and validates a definition file.

func Parse

func Parse(data []byte) (*Definition, error)

Parse reads a definition from YAML. It rejects unknown fields so typos in definitions fail loudly instead of being ignored.

func (*Definition) IsFileVolume

func (d *Definition) IsFileVolume(p string) bool

IsFileVolume reports whether a volume path is one of the definition's files or downloads, so it's mounted as a single file rather than a folder.

func (*Definition) IsStack

func (d *Definition) IsStack() bool

IsStack reports whether the definition is a container stack.

func (*Definition) Port

func (d *Definition) Port(name string) (Port, bool)

Port returns the port with the given name.

func (*Definition) RenderRun

func (d *Definition) RenderRun(goos string, ctx Context) (Run, error)

RenderRun renders the run entry for one OS.

func (*Definition) RenderStack

func (d *Definition) RenderStack(ctx Context) ([]RenderedService, error)

RenderStack fills in every template in the stack.

func (*Definition) ResolvePorts

func (d *Definition) ResolvePorts(overrides map[string]int) (map[string]int, error)

ResolvePorts merges user overrides with the definition's default ports.

func (*Definition) ResolveSettings

func (d *Definition) ResolveSettings(raw map[string]string) (map[string]any, error)

ResolveSettings merges user-supplied raw string values with defaults, converts them to their declared types and generates secrets. It returns typed values ready for templates.

func (*Definition) Setting

func (d *Definition) Setting(key string) (Setting, bool)

Setting returns the setting with the given key.

func (*Definition) SupportsOS

func (d *Definition) SupportsOS(goos string) bool

SupportsOS reports whether the service has a native build for an OS.

func (*Definition) Validate

func (d *Definition) Validate() error

Validate checks the definition and returns every problem it finds.

type Download

type Download struct {
	URL    string `yaml:"url" json:"url"`
	SHA256 string `yaml:"sha256" json:"sha256"`
	To     string `yaml:"to" json:"to"`
	// Always replaces the file on every update. By default an existing
	// file is kept, because users often replace these with their own.
	Always bool `yaml:"always,omitempty" json:"always,omitempty"`
}

Download is one extra file to fetch into the server directory.

type File

type File struct {
	Path     string `yaml:"path" json:"path"`
	Template string `yaml:"template,omitempty" json:"template,omitempty"`
	// Overwrite rewrites a templated file on reconfigure and update.
	// Without it the file is only written when missing, so hand edits
	// survive. Files with set are always updated.
	Overwrite bool `yaml:"overwrite,omitempty" json:"overwrite,omitempty"`
	// Format says how to read the file for set: ini (keys are
	// "Section.key"), lines (keys are line prefixes; the whole line is
	// replaced) or json (keys are dotted paths into objects).
	Format string            `yaml:"format,omitempty" json:"format,omitempty"`
	Set    map[string]string `yaml:"set,omitempty" json:"set,omitempty"`
	OS     []string          `yaml:"os,omitempty" json:"os,omitempty"`
}

File is a config file Anyserve manages. It works in one of two ways:

  • template: Anyserve writes the whole file from a template.
  • set: Anyserve changes only some values in a file, leaving the rest alone. This suits files the game creates or that users hand-edit. Template, if also given, is the starting content when the file is missing.

type HealthCheck

type HealthCheck struct {
	Command []string `yaml:"command,omitempty" json:"command,omitempty"`
	// Disable turns off a health check built into the image, for a
	// container that doesn't do what the image's check expects (a worker
	// started from a server's image, say).
	Disable         bool `yaml:"disable,omitempty" json:"disable,omitempty"`
	IntervalSeconds int  `yaml:"interval_seconds,omitempty" json:"interval_seconds,omitempty"`
	Retries         int  `yaml:"retries,omitempty" json:"retries,omitempty"`
}

HealthCheck runs a command inside the container. Exit status 0 means healthy.

type Install

type Install struct {
	SteamCMD *SteamCMD `yaml:"steamcmd,omitempty" json:"steamcmd,omitempty"`
	Archive  *Archive  `yaml:"archive,omitempty" json:"archive,omitempty"`
	// Downloads are extra single files fetched after the main source, each
	// pinned by checksum.
	Downloads []Download `yaml:"downloads,omitempty" json:"downloads,omitempty"`
}

Install says where the server files come from. Exactly one source is set.

type Launcher

type Launcher struct {
	Program string
	Args    []string
	// Install tells the user how to get the program.
	Install string
}

Launcher is a host program a server can be started through. The list is fixed in Anyserve so definitions can't run arbitrary host commands.

type LinuxGSM

type LinuxGSM struct {
	// Status is one of: upstream (LinuxGSM already supports it),
	// contributed (we sent it upstream), missing (not in LinuxGSM yet).
	Status string `yaml:"status" json:"status"`
	Name   string `yaml:"name,omitempty" json:"name,omitempty"`
	Notes  string `yaml:"notes,omitempty" json:"notes,omitempty"`
}

LinuxGSM records how this service relates to LinuxGSM.

type Port

type Port struct {
	Name        string `yaml:"name" json:"name"`
	Port        int    `yaml:"port" json:"port"`
	Protocol    string `yaml:"protocol" json:"protocol"` // tcp, udp, both
	Description string `yaml:"description,omitempty" json:"description,omitempty"`
	// Public says players need this port forwarded. Admin ports like RCON
	// are usually not public.
	Public bool `yaml:"public" json:"public"`
}

Port is a network port the server listens on.

type RCON

type RCON struct {
	// Protocol is source or webrcon.
	Protocol string `yaml:"protocol" json:"protocol"`
	// Port names the entry in ports that RCON listens on.
	Port string `yaml:"port" json:"port"`
	// Password is a template, usually {{.Settings.rcon_password}}.
	Password string `yaml:"password" json:"password"`
}

RCON is a server's remote console.

type RenderedService

type RenderedService struct {
	StackService
}

RenderedService is a stack service with its templates filled in.

type Run

type Run struct {
	// Command is a file in the server directory: the server binary, or the
	// program a launcher in Via runs (such as a .dll for dotnet).
	Command string `yaml:"command" json:"command"`
	// Via lists host launchers to start Command through, outermost first,
	// from the built-in Launchers list (e.g. [xvfb], [dotnet]).
	Via []string `yaml:"via,omitempty" json:"via,omitempty"`
	// ViaArgs go to the launcher, before Command: JVM options and -jar
	// for java, for example. Templates allowed.
	ViaArgs []string          `yaml:"via_args,omitempty" json:"via_args,omitempty"`
	Args    []string          `yaml:"args,omitempty" json:"args,omitempty"`
	Workdir string            `yaml:"workdir,omitempty" json:"workdir,omitempty"`
	Env     map[string]string `yaml:"env,omitempty" json:"env,omitempty"`
}

Run says how to start the server on one OS.

func (Run) Requires

func (r Run) Requires() []Launcher

Requires lists the host programs a run entry needs, for install checks.

type ServicePort

type ServicePort struct {
	// Port names an entry in the definition's ports.
	Port string `yaml:"port" json:"port"`
	// Target is the port inside the container.
	Target int `yaml:"target" json:"target"`
}

ServicePort publishes one of the definition's ports from a container.

type Setting

type Setting struct {
	Key         string   `yaml:"key" json:"key"`
	Prompt      string   `yaml:"prompt" json:"prompt"`
	Description string   `yaml:"description,omitempty" json:"description,omitempty"`
	Type        string   `yaml:"type" json:"type"` // string, int, bool, choice, folder
	Default     any      `yaml:"default,omitempty" json:"default,omitempty"`
	Choices     []Choice `yaml:"choices,omitempty" json:"choices,omitempty"`
	// Secret hides the value in prompts and output.
	Secret bool `yaml:"secret,omitempty" json:"secret,omitempty"`
	// Generate fills an empty value with a random password.
	Generate bool `yaml:"generate,omitempty" json:"generate,omitempty"`
	// Optional allows an empty value.
	Optional bool `yaml:"optional,omitempty" json:"optional,omitempty"`
	// MustAccept makes a bool setting a required agreement, such as a
	// licence: installing fails until the user sets it to true.
	MustAccept bool `yaml:"must_accept,omitempty" json:"must_accept,omitempty"`
	Min        *int `yaml:"min,omitempty" json:"min,omitempty"`
	Max        *int `yaml:"max,omitempty" json:"max,omitempty"`
}

Setting is a value the user chooses at install time.

func (Setting) Convert

func (s Setting) Convert(v string) (any, error)

Convert parses a raw string into the setting's type and checks limits.

func (Setting) DefaultString

func (s Setting) DefaultString() string

DefaultString renders the default as the raw string a user would type. Defaults decoded from JSON arrive as float64, so whole numbers are printed without an exponent or decimal point.

type Stack

type Stack struct {
	Services []StackService `yaml:"services" json:"services"`
}

Stack lists the containers in a stack definition.

Stacks stay declarative like everything else: images must be pinned by digest, containers always run as the invoking user with every Linux capability dropped, and they can only mount folders and files inside the instance directory. There's no way to ask for privileged mode, the host network, host paths or the Docker socket.

type StackService

type StackService struct {
	// Name is the container's name in the stack, and its host name on the
	// stack's private network.
	Name string `yaml:"name" json:"name"`
	// Image must be pinned by digest: name:tag@sha256:<64 hex>.
	Image string `yaml:"image" json:"image"`
	// Command replaces the image's command. Each item is a template.
	Command []string          `yaml:"command,omitempty" json:"command,omitempty"`
	Env     map[string]string `yaml:"env,omitempty" json:"env,omitempty"`
	Volumes []Volume          `yaml:"volumes,omitempty" json:"volumes,omitempty"`
	Ports   []ServicePort     `yaml:"ports,omitempty" json:"ports,omitempty"`
	// DependsOn starts these services first, waiting until they're healthy
	// if they have a health check.
	DependsOn []string     `yaml:"depends_on,omitempty" json:"depends_on,omitempty"`
	Health    *HealthCheck `yaml:"health,omitempty" json:"health,omitempty"`
	// ShmMB sets the shared memory size, which databases often need raised.
	ShmMB int `yaml:"shm_mb,omitempty" json:"shm_mb,omitempty"`
	// Once marks a job that runs to completion on every start, such as
	// database migrations, instead of a long-running service. Services
	// that depend on it wait until it has finished successfully.
	Once bool `yaml:"once,omitempty" json:"once,omitempty"`
}

StackService is one container in a stack.

type SteamCMD

type SteamCMD struct {
	AppID int `yaml:"app_id" json:"app_id"`
	// Platform asks SteamCMD for a specific OS's build, per host OS.
	// Usually left empty, which means "the host's own OS".
	Platform map[string]string `yaml:"platform,omitempty" json:"platform,omitempty"`
	// Beta installs a beta branch instead of the public one.
	Beta string `yaml:"beta,omitempty" json:"beta,omitempty"`
	// Login is "anonymous" (the default) or "required" for apps that only
	// download with a Steam account. Anyserve never stores the password:
	// `anyserve steam login` lets SteamCMD cache the session itself.
	Login string `yaml:"login,omitempty" json:"login,omitempty"`
}

SteamCMD installs an app anonymously through Valve's SteamCMD.

type Step

type Step struct {
	// Type is one of the StepTypes.
	Type string `yaml:"type" json:"type"`
	From string `yaml:"from,omitempty" json:"from,omitempty"`
	To   string `yaml:"to,omitempty" json:"to,omitempty"`
	Path string `yaml:"path,omitempty" json:"path,omitempty"`
	// Command, Args and Creates are for exec steps: run Command (a file in
	// the server folder) with Args once, unless Creates already exists.
	Command string   `yaml:"command,omitempty" json:"command,omitempty"`
	Args    []string `yaml:"args,omitempty" json:"args,omitempty"`
	Creates string   `yaml:"creates,omitempty" json:"creates,omitempty"`
	// OS limits the step to some operating systems. Empty means all.
	OS []string `yaml:"os,omitempty" json:"os,omitempty"`
}

Step is one built-in install step. Steps run in order after the files are downloaded, and again after every update.

type Stop

type Stop struct {
	// Command is written to the server's console (stdin) to stop it.
	Command string `yaml:"command,omitempty" json:"command,omitempty"`
	// TimeoutSeconds is how long to wait before killing the process.
	TimeoutSeconds int `yaml:"timeout_seconds,omitempty" json:"timeout_seconds,omitempty"`
	// RCON sends Command over the console's RCON instead of stdin, for
	// servers that don't read their standard input.
	RCON bool `yaml:"rcon,omitempty" json:"rcon,omitempty"`
}

Stop says how to shut the server down cleanly.

type ValidationError

type ValidationError struct{ Problems []string }

ValidationError collects every problem found in a definition.

func (*ValidationError) Error

func (e *ValidationError) Error() string

type Volume

type Volume struct {
	Path string `yaml:"path,omitempty" json:"path,omitempty"`
	// Setting mounts the host folder the user chose in a folder setting
	// (a media library, say) instead of a path inside the instance. If the
	// setting is optional and left empty, the mount is skipped.
	Setting  string `yaml:"setting,omitempty" json:"setting,omitempty"`
	Target   string `yaml:"target" json:"target"`
	ReadOnly bool   `yaml:"read_only,omitempty" json:"read_only,omitempty"`
}

Volume mounts a folder or file from the instance's server directory. Paths listed in the definition's files are mounted as files; anything else is a folder, created before the stack starts.

Jump to

Keyboard shortcuts

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