localmode

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2026 License: Apache-2.0 Imports: 13 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 localmode holds the pieces shared by the CLI's dev verb and the control plane's local-instance behavior.

Index

Constants

View Source
const (
	// EnvEnabled turns a control plane into a loopback-only local instance.
	EnvEnabled = "APP_LOCAL_MODE"
	// EnvSupervised tells the control plane a parent will relaunch it after a reset exit.
	EnvSupervised = "APP_RESTART_SUPERVISED"
)
View Source
const (
	EnvDashboardPort     = "APP_LOCAL_DASHBOARD_PORT"
	EnvHTTPPort          = "APP_LOCAL_HTTP_PORT"
	EnvHTTPSPort         = "APP_LOCAL_HTTPS_PORT"
	EnvAgentPort         = "APP_LOCAL_AGENT_PORT"
	EnvHTTPFallbackPort  = "APP_LOCAL_HTTP_FALLBACK_PORT"
	EnvHTTPSFallbackPort = "APP_LOCAL_HTTPS_FALLBACK_PORT"
	EnvPortScan          = "APP_LOCAL_PORT_SCAN"

	// EnvPublicHTTPPort is the port browsers use for apps when a forward sits in front of the ingress.
	EnvPublicHTTPPort = "APP_LOCAL_PUBLIC_HTTP_PORT"
)

Env names. They seed a first start; ports saved afterwards win.

View Source
const (
	// DefaultDashboardPort matches the CLI's default API address.
	DefaultDashboardPort = 8080

	// MaxPort is the highest TCP port.
	MaxPort = 65535
)
View Source
const (
	RoleDashboard = "dashboard"
	RoleHTTP      = "app_http"
	RoleHTTPS     = "app_https"
	RoleAgent     = "agent"
)

Port roles.

Variables

View Source
var ErrDockerUnreachable = errors.New("docker is not reachable")

ErrDockerUnreachable is returned when no candidate socket accepts a connection.

Functions

func AdminFilePath

func AdminFilePath(dataDir string) string

AdminFilePath is where the generated admin credential is kept, mode 0600.

func AppURL

func AppURL(host string, httpPort int) string

AppURL is the address of an app served on host by an HTTP ingress listening on httpPort.

func ClearPortExchange

func ClearPortExchange(dataDir string)

ClearPortExchange drops requests and results left by an earlier run.

func ClearState

func ClearState(dataDir string)

ClearState removes the state file.

func DashboardURL

func DashboardURL(port int) string

DashboardURL is the dashboard address for a loopback port.

func DetectDocker

func DetectDocker(env DockerEnv) (string, error)

DetectDocker returns the first socket that exists and accepts a connection.

func DockerFix

func DockerFix(goos string, tried []string) string

DockerFix is the one-line next step for an operator whose Docker is not reachable.

func DockerSocketCandidates

func DockerSocketCandidates(env DockerEnv) []string

DockerSocketCandidates lists engine sockets in preference order: explicit settings first, then Docker Desktop's per-user sockets, then the system default.

func Enabled

func Enabled(getenv func(string) string) bool

Enabled reports whether getenv says this process is a local instance.

func LoopbackFree

func LoopbackFree(port int) bool

LoopbackFree is the real PortFree: a listen-and-close probe.

func PortsFilePath

func PortsFilePath(dataDir string) string

PortsFilePath is where a local instance keeps its chosen ports.

func PublicHTTPPort

func PublicHTTPPort(getenv func(string) string) int

PublicHTTPPort is the forwarded app port from EnvPublicHTTPPort, or 0 when unset or invalid.

func RequestReset

func RequestReset(dataDir string) error

RequestReset drops the reset marker.

func ResetMarkerPath

func ResetMarkerPath(dataDir string) string

ResetMarkerPath is the file whose presence asks the supervisor to wipe the data dir on the next relaunch.

func ResetRequested

func ResetRequested(dataDir string) bool

ResetRequested reports whether a reset marker is present.

func RoleLabel

func RoleLabel(role string) string

RoleLabel is the name people see for a role.

func SavePorts

func SavePorts(dataDir string, p Ports) error

SavePorts records the chosen ports.

func StateFilePath

func StateFilePath(dataDir string) string

StateFilePath is where the running supervisor records its pid and URLs.

func ValidateListenAddr

func ValidateListenAddr(addr string) error

ValidateListenAddr rejects a dashboard listen address that is reachable beyond this machine.

func Wipe

func Wipe(dataDir string) error

Wipe removes everything inside dataDir but keeps the directory itself; it refuses a non-empty directory a local instance did not create.

func WithdrawPortRequest

func WithdrawPortRequest(dataDir string) bool

WithdrawPortRequest removes a request nobody picked up and reports whether it was still there.

func WritePortRequest

func WritePortRequest(dataDir string, r PortRequest) error

WritePortRequest leaves a request for the supervisor of dataDir.

func WritePortResult

func WritePortResult(dataDir string, r PortResult) error

WritePortResult records the outcome of a request.

func WriteState

func WriteState(st State) error

WriteState records st in its data dir.

Types

type Admin

type Admin struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

Admin is the generated sign-in for a local instance.

func LoadOrCreateAdmin

func LoadOrCreateAdmin(dataDir string, random io.Reader) (a Admin, created bool, err error)

LoadOrCreateAdmin reads the stored admin or generates one; created is true only for a new credential.

type DockerEnv

type DockerEnv struct {
	Getenv func(string) string
	Exists func(path string) bool
	Dial   func(socketPath string) error
	GOOS   string
}

DockerEnv is the slice of the process environment socket detection reads.

func RealDockerEnv

func RealDockerEnv(goos string) DockerEnv

RealDockerEnv is DockerEnv backed by the running process.

type PortEnv

type PortEnv struct {
	Probe  func(ctx context.Context, port int) PortState
	Holder func(ctx context.Context, port int) string
	// Owned are ports the instance being changed holds now; a restart releases them.
	Owned map[int]bool
	// Min is the lowest port the runtime uses.
	Min  int
	Scan int
}

PortEnv is what planning reads from the machine.

func (PortEnv) Check

func (e PortEnv) Check(ctx context.Context, port int) PortState

Check says whether port may be used, without regard to other roles.

func (PortEnv) Explain

func (e PortEnv) Explain(ctx context.Context, port int, state PortState) (reason, holder string)

Explain turns a state into a sentence, naming the holder when it is known.

type PortFree

type PortFree func(port int) bool

PortFree reports whether port can be bound on loopback right now.

type PortPrefs

type PortPrefs struct {
	Want, From Ports
	Scan       int
}

PortPrefs are the ports a first start asks for and where it searches next.

func PortPrefsFromEnv

func PortPrefsFromEnv(lookup func(string) (string, bool)) (PortPrefs, error)

PortPrefsFromEnv layers the seed env values over the defaults.

func (PortPrefs) Specs

func (p PortPrefs) Specs(want Ports) []RoleSpec

Specs lists the three roles people choose, asking for want.

type PortRequest

type PortRequest struct {
	ID    string `json:"id"`
	Ports Ports  `json:"ports"`
}

PortRequest asks a running supervisor to move to other ports.

func TakePortRequest

func TakePortRequest(dataDir string) (PortRequest, bool)

TakePortRequest consumes a waiting request.

type PortResult

type PortResult struct {
	ID       string `json:"id"`
	Applied  bool   `json:"applied"`
	Restored bool   `json:"restored"`
	Healthy  bool   `json:"healthy"`
	Reason   string `json:"reason,omitempty"`
	Ports    Ports  `json:"ports"`
}

PortResult is the supervisor's answer. Healthy false means nothing is running.

func ReadPortResult

func ReadPortResult(dataDir, id string) (PortResult, bool)

ReadPortResult returns the outcome of the request with id, once it exists.

type PortState

type PortState string

PortState is what probing one port found.

const (
	PortFreeState PortState = "free"
	PortInUse     PortState = "in_use"
	// PortDenied means this account may not listen there.
	PortDenied PortState = "denied"
	// PortTooLow means the runtime itself does not use ports that low.
	PortTooLow PortState = "too_low"
	// PortOwned means the instance being changed already holds it.
	PortOwned PortState = "yours"
)

Port states.

func (PortState) Usable

func (s PortState) Usable() bool

Usable reports whether a role may take a port in this state.

type Ports

type Ports struct {
	Dashboard int `json:"dashboard"`
	HTTP      int `json:"http"`
	HTTPS     int `json:"https"`
	Agent     int `json:"agent"`
}

Ports are the loopback ports a local instance listens on.

func LoadPorts

func LoadPorts(dataDir string) (p Ports, ok bool, err error)

LoadPorts reads the saved ports; ok is false when none were saved.

type RoleReport

type RoleReport struct {
	Role        string    `json:"role"`
	Label       string    `json:"label"`
	Requested   int       `json:"requested"`
	State       PortState `json:"state"`
	Holder      string    `json:"holder,omitempty"`
	Recommended int       `json:"recommended"`
	Reason      string    `json:"reason"`
}

RoleReport is the finding for one role.

func PlanPorts

func PlanPorts(ctx context.Context, e PortEnv, specs []RoleSpec, reserved ...int) ([]RoleReport, error)

PlanPorts reports each role and recommends distinct usable ports. A role that has none gets Recommended 0 and the error names it.

type RoleSpec

type RoleSpec struct {
	Role      string
	Requested int
	ScanFrom  int
}

RoleSpec asks for one role's port; ScanFrom is where the search for another starts.

type State

type State struct {
	PID       int       `json:"pid"`
	DataDir   string    `json:"data_dir"`
	Dashboard string    `json:"dashboard_url"`
	HTTPPort  int       `json:"http_port"`
	HTTPSPort int       `json:"https_port"`
	Started   time.Time `json:"started"`
	// PortChange marks a supervisor that can move ports while running.
	PortChange bool `json:"port_change,omitempty"`
}

State is what a running supervisor records so stop and status can find it.

func ReadState

func ReadState(dataDir string) (st State, ok bool, err error)

ReadState loads the state recorded in dataDir; ok is false when none exists.

Jump to

Keyboard shortcuts

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