Documentation
¶
Overview ¶
Package modulecmd is the module assistant: `casaos module list|plan|install|remove|status`. It is the one place a module is installed and removed; the sha256 check, the rules of what an archive may write and the reading of manifests and catalogues are the code of osmodule, and this package is what runs them against a box: it asks the box what it is (plan), puts a module on it in steps and takes it off again (install, remove).
Everything the commands touch of the world comes through an Env: the file system (a Roots), the commands they run (a Runner), the network (a Downloader), the gateway, the clock, the terminal, and the few calls that only Linux has (a Host). Production wires the real ones (Production); a test wires a temporary directory and stand-ins. The package compiles everywhere and runs for real on Linux only.
Index ¶
- Constants
- Variables
- func Abandon(env Env, name, nonce, kind string, queuedAt, at time.Time, said []string) error
- func Dispatch(args []string) (rest []string, ok bool)
- func JobLogs(env Env) ([]string, error)
- func Main(ctx context.Context, args []string, env Env) (code int)
- func PlanOf(ctx context.Context, env Env, name string) osmodule.PlanResult
- func Queue(env Env, name, nonce, kind string, at time.Time) error
- func QueuedLine(name, nonce, kind string, at time.Time) string
- func RemoveRefusal(ctx context.Context, env Env, name string) string
- func Run(args []string, env Env) int
- func ValidKind(kind string) bool
- type CatalogueInfo
- type Clock
- type Cmd
- type Downloader
- type Env
- type Gateway
- type Host
- type Installed
- type Job
- type ListModule
- type ListResult
- type Output
- type PermanentError
- type Reducer
- type Roots
- type RouteState
- type Runner
- type StatusResult
- type SystemInfo
- type UnitSight
- type UnitState
Constants ¶
const ( ExitOK = 0 ExitRefused = 2 // a code, and nothing was changed ExitFailed = 3 // the work began, failed at a step and was rolled back as said )
The exit codes of the commands.
const ( StepPackages = "packages" StepDownload = "download" StepVerify = "verify" StepUnpack = "unpack" StepUnits = "units" StepManifest = "manifest" StepStart = "start" StepRoute = "route" StepDone = "done" )
The steps of an install, in order. Each is a marker in the log; a failure at one of them is rolled back.
const ( CodeNotRoot = "not_root" CodeBusy = "busy" CodePlanChanged = "plan_changed" CodeConfirmationNeeded = "confirmation_needed" CodeDeclined = "declined" CodeNotInstalled = "not_installed" CodeLogUnwritable = "log_unwritable" CodeLockFailed = "lock_failed" CodeFilesUnreadable = "files_unreadable" CodePackagesFailed = "packages_failed" CodeDpkgBroken = "dpkg_broken" CodeHeldPackage = osmodule.RefusedHeld CodeDownloadFailed = "download_failed" CodeDownloadTooLarge = "download_too_large" CodeChecksumMismatch = "checksum_mismatch" CodeUnpackFailed = "unpack_failed" CodeUnitsFailed = "units_failed" CodeManifestInvalid = "manifest_invalid" CodeManifestMismatch = "manifest_mismatch" CodeManifestWrite = "manifest_write_failed" CodeStartFailed = "start_failed" CodeRouteTimeout = "route_timeout" CodeInterrupted = "interrupted" CodeUnitNotStopped = "unit_not_stopped" CodeRemoveIncomplete = "remove_incomplete" CodeTmpUnusable = "tmp_unusable" CodeStateUnsafe = "state_unsafe" CodeInternal = "internal_error" )
The codes a run is refused or failed with, besides the ones of the plan (osmodule.Check*, osmodule.Refused*) and of the unpack (osmodule.Unpack*, osmodule.FileExists).
const ( JobIdle = "idle" JobRunning = "running" JobSucceeded = "succeeded" JobFailed = "failed" JobInterrupted = "interrupted" JobRefused = "refused" )
The states of a job.
const ( KindInstall = "install" KindRemove = "remove" )
The kinds of a job: what the core started, and said in the first line of the log. The words are those of the verbs of the assistant.
const ( UndoRolledBack = "rolled_back" UndoIncomplete = "rollback_incomplete" )
The words of Job.Undo: the last field of the RESULT line of a job that failed. The empty word is a failure that had nothing to undo or a verb that does not undo.
const ( // InterruptedAfter is how long a unit that is not there is given, from the last sign of life of the job, to have said its RESULT: the unit can be gone just before the last line reached the // log, and it can be gone just after the core queued it and before systemd had it. InterruptedAfter = 30 * time.Second // SilentAfter is the same for systemd that does not answer: it is not known that the unit is gone, and an apt can be quiet for a long time. SilentAfter = 30 * time.Minute // TailLines is how many lines of the end of the log the state carries. TailLines = 20 )
const ( StepStop = "stop" StepFiles = "files" StepManifest2 = "manifest" StepReload = "reload" )
The steps of a remove.
const ( // StopWithin is how long a run needs, from the SIGTERM that asks it to stop, to be over: the command it is in is given killGrace to leave, then the rollback has rollbackTime, and the // command that ends the rollback is given killGrace in its turn. A unit that waits for less before it kills what is left cuts the rollback of an install. StopWithin = 2*killGrace + rollbackTime )
Variables ¶
var ( // ErrLockHeld is the error of Lock for a lock that another process holds. ErrLockHeld = errors.New("the lock is held") // ErrUnsupported is what a host that is not Linux says. ErrUnsupported = errors.New("this system cannot run the module assistant") )
var ErrLogChanged = errors.New("the log is no longer the queued line of the job")
ErrLogChanged is the error of Abandon for a log that is no longer the first line of the job: a unit started, and the log holds what it has written.
var ( // ErrTooLarge is a download or an unpacked archive that has more bytes than allowed. ErrTooLarge = errors.New("more bytes than allowed") )
Functions ¶
func Abandon ¶
Abandon makes the log of a job whose unit could not be started a whole run: the queued line (queuedAt and kind are what Queue was given), what failed, what systemd said (said, as text, behind "| " like the output of a tool) and a RESULT line that says failed, start_failed, with nothing undone because nothing was done. It does not wait for the lock. It writes only while the log is exactly the queued line of this job, or is not there: a unit that did start has replaced the log with its own lines, and they are not for the core to overwrite (ErrLogChanged).
func Dispatch ¶
Dispatch says whether a command line is the module assistant's: `casaos module ...`, with "module" as the first argument after the program and spelled exactly so. It gives what follows. Nothing else reaches the code of the assistant: a flag of the core in front of it, a different case, an abbreviation or a word that only starts with it is the core's own command line, and the core starts as it always did.
func JobLogs ¶
JobLogs are the names of the modules that have a log, in the order the folder gives them. A folder that is not there has none; one that cannot be read is an error.
func PlanOf ¶
PlanOf is the plan of installing the module called name: what the box can say of it, and nothing written.
func Queue ¶
Queue replaces the log of the module called name with the first line of the job whose nonce this is, and whose kind (KindInstall or KindRemove) is said in it: the log is the job's from here on, and a unit that is started next keeps that line, replaces the rest and carries on with the same nonce. It does not wait for the lock (ErrLockHeld when a run has it).
func QueuedLine ¶
QueuedLine is the first line of the log of a job that the core is about to start. It carries the nonce, so that the log is the job's from now on, and the kind of the job (KindInstall or KindRemove).
func RemoveRefusal ¶
RemoveRefusal is the code that a remove of the module called name is refused with, "" when it is not: there is nothing of the module to remove (CodeNotInstalled), or a package manager or the upgrade of the operating system is at work (CodeBusy). It reads and changes nothing.
Types ¶
type CatalogueInfo ¶
type CatalogueInfo struct {
OK bool `json:"ok"`
Error string `json:"error,omitempty"`
Detail string `json:"detail,omitempty"`
}
CatalogueInfo says whether the catalogue of this release was read, and if not why: "none" (there is none), "unreadable" or "invalid" with the place in Detail.
type Cmd ¶
type Cmd struct {
Argv []string
Env []string
Dir string
Stdout io.Writer
Timeout time.Duration
// Finish, when set, is how long a command that is asked to stop (its context ends) is first given to finish what it is doing and leave on its own: it is sent SIGINT, and nothing else
// until the time is up or the command has left. apt-get answers SIGINT by finishing the dpkg run it is in (SIGTERM kills apt-get, and dpkg, whose status pipe it was, dies on SIGPIPE
// between two packages). What is still there when the time is up is stopped as for any command (SIGTERM, the grace, SIGKILL).
Finish time.Duration
}
A Cmd is a command to run: no shell. Env is the whole environment (nil is the clean one of the install); Dir the directory it runs in ("" is the root). Stdout, when set, takes the output as it comes (and Output.Text is empty); when nil the output is kept, bounded, in Output.Text. Timeout, when set, ends the command with its context error.
type Downloader ¶
A Downloader fetches an address into dst and says how many bytes it wrote. An error that retrying cannot mend (a refused address, a refused redirect, too many bytes, a status that is not 200) is a *PermanentError.
func NewHTTPDownloader ¶
func NewHTTPDownloader() Downloader
NewHTTPDownloader is the downloader of production: the archive of a release, at most osmodule.MaxArchiveBytes.
type Env ¶
type Env struct {
Roots Roots
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
Getenv func(string) string
// EUID is the user the assistant runs as; Arch the architecture as the archives name it ("amd64", "arm64"); Version the ReCasaOS release that is running.
EUID func() int
Arch string
Version string
Runner Runner
// Sandbox makes the sandbox the simulation of apt runs in (osplan.Sandbox): the production one is a directory in /var/tmp.
Sandbox func() osplan.Sandbox
Download Downloader
Gateway Gateway
Clock Clock
// Terminal says whether there is someone to ask.
Terminal func() bool
Host Host
// StepHook, when set, is called at the start and at the end of every step of an install and a remove, with the step and "begin" or "done": a test uses it to stop the run at a
// chosen point. Production leaves it nil.
StepHook func(ctx context.Context, step, phase string)
}
An Env is the world of a run of the assistant.
func Production ¶
func Production() Env
Production is the Env of a box: the real file system, commands, network and clock.
type Host ¶
type Host interface {
// Lock takes the lock at path, which is not waited for: ErrLockHeld when another process has it. The lock is the process's, and is gone when the process is.
Lock(path string) (release func(), err error)
// FreeBytes is the room left, for a program that is not root, on the file system that holds the path (the nearest directory that exists).
FreeBytes(path string) (int64, error)
// DpkgLockHeld says whether another process holds a lock on any of the files; an error says it could not be told.
DpkgLockHeld(paths []string) (bool, error)
// NewDest is the file system an archive is unpacked onto, with prefix in front of every path.
NewDest(prefix string) osmodule.Destination
// IsTerminal says whether the file is a terminal.
IsTerminal(f *os.File) bool
// SafeDir says why a directory is not one that root may keep files in: it must be a directory and not a link, owned by the user the assistant runs as (root, since install and remove
// refuse any other) and writable by nobody else.
SafeDir(path string) error
}
A Host is what only the machine has: a lock that dies with its holder, the room on a file system, the question of whether a package manager holds dpkg's lock, and the file system the unpack writes to.
type Installed ¶
type Installed struct {
Version *string `json:"version"`
}
Installed is what the box has of a module: its version, null when the box does not know it.
type Job ¶
type Job struct {
Name string `json:"name"`
// Kind is what the job is: KindInstall or KindRemove, as the core said when it started the job; "" when the log does not say (a job that was not started by the core).
Kind string `json:"kind"`
State string `json:"state"`
// Step is where the job is: the step of the last STEP line (the rollback's steps are those of a remove behind "rollback_"), and for a job that failed the step of the RESULT line,
// which is "" when it failed before its first step. "done" for a job that succeeded; "" for one that was refused or has not begun.
Step string `json:"step"`
// Code is the reason of a job that was refused or failed: the code of the RESULT line.
Code string `json:"code"`
// Undo is UndoRolledBack or UndoIncomplete for an install that failed after it had changed the box, "" otherwise.
Undo string `json:"undo"`
// KeptPackages are the packages of the module that an install that failed left on the box, from the sentence kept_packages that comes before the RESULT line: null when it is not said
// or not known, an empty list when there are none.
KeptPackages []string `json:"kept_packages"`
// StartedAt and EndedAt are the times of the first marker and of the RESULT line, RFC 3339 in UTC; "" when there is none.
StartedAt string `json:"started_at"`
EndedAt string `json:"ended_at"`
// LogTail is the last TailLines lines of the log that are not marker lines, as text: printable ASCII, a URL reduced to its host, cut.
LogTail []string `json:"log_tail"`
}
A Job is how the job of a module is going, or how it went.
func ReadJob ¶
ReadJob says how the job of the module called name is going, from its log and the state of its unit. The unit is looked at BEFORE the log is read (a unit that is gone has written everything it wrote), and now is the time of the core's clock. A module that has no log has no job. The log is a regular file that is not reached through a link, and the first maxLine bytes of a line are all that is held of it.
type ListModule ¶
type ListModule struct {
Name string `json:"name"`
Title string `json:"title"`
Description string `json:"description"`
Tier string `json:"tier"`
Tag string `json:"tag"`
Catalogue bool `json:"catalogue"`
Installed *Installed `json:"installed"`
System SystemInfo `json:"system"`
Broken string `json:"broken,omitempty"`
}
A ListModule is a module of the catalogue or one that is installed. Catalogue says whether the catalogue of this release has it.
type ListResult ¶
type ListResult struct {
Catalogue CatalogueInfo `json:"catalogue"`
Modules []ListModule `json:"modules"`
}
A ListResult is the answer of list.
type Output ¶
Output is what a command printed (its output and error output, in English, cut) and how it ended.
type PermanentError ¶
type PermanentError struct{ Err error }
A PermanentError is a download that is not tried again.
func (*PermanentError) Error ¶
func (e *PermanentError) Error() string
func (*PermanentError) Unwrap ¶
func (e *PermanentError) Unwrap() error
type Reducer ¶
type Reducer struct {
// contains filtered or unexported fields
}
A Reducer reads the log of a module line by line and says how the job it holds is going. The zero value is not usable; NewReducer makes one.
func NewReducer ¶
NewReducer is a reducer for the log of the module called name. nonce is the job's; "" says it is not known, and then it is the nonce of the first marker of the module in the log that carries one.
func (*Reducer) Feed ¶
Feed gives the reducer the next line of the log, without its new line. whole says that the line ended with one and was read to its end: the last line of a log that is being written does not, and a line that is cut is not whole either. Only a whole line can be a marker.
type Roots ¶
type Roots struct {
Prefix string
}
Roots is where the file system of the box is. The zero value is the real one.
type RouteState ¶
type RouteState struct {
Answers bool `json:"answers"`
}
RouteState says whether the gateway answers on the route of the module.
type Runner ¶
A Runner runs commands (apt-get, systemctl). A command that ran and ended with a status is an Output, whatever the status is; one that could not be run, or that was ended by its context or its timeout, is an error.
type StatusResult ¶
type StatusResult struct {
Name string `json:"name"`
Installed bool `json:"installed"`
Version *string `json:"version"`
Unit UnitState `json:"unit"`
Route RouteState `json:"route"`
}
A StatusResult is the answer of status for one module.
type SystemInfo ¶
SystemInfo says whether this system can have the module, and the checks (by code) that say no.