Documentation
¶
Overview ¶
Package ui implements sind ui, a local web UI for sind.
It serves server-rendered pages (html/template) with htmx and one Server-Sent Events stream per tab for live updates, all embedded in the binary. It listens on 127.0.0.1 only, and lets in only the user's own processes holding the per-run token from the URL it prints.
Extending sind ui ¶
A part of sind ui, such as the Clusters page or the task dock, adds itself to the server from files of its own, without editing server.go, sse.go or the layout. Parts built side by side then touch no common file. A part needs up to five things:
- Templates, in web/templates/NAME.tmpl. The server parses every file there into one set, so a block name is global: prefix yours with the part's name. A page defines the block "page-NAME" for its content; a fragment defines a block of its own name. Both are executed with a *View.
- A page, registered from an init function with defaultRegistry.AddPage: a Page has a route ("GET /clusters/{name}"), a title, the key of the rail entry to light, the fragments it keeps live, and a Load that fills View.Data from View.Snap (the snapshot) and View.Params (the route's wildcards and the declared query keys). Load may return ErrNotFound (404), or a *Miss, which says what is missing and offers the closest matches on the Not found page, or an error that wraps ErrBadRequest (400), and only reads.
- Fragments, registered with AddFragment. A fragment is a block of HTML that live updates morph into its element. Its page places it with <div hx-swap="morph:innerHTML" sse-swap="NAME">{{fragment "NAME" .}}</div>, so that the elements that stay keep their animations, focus and open state (idiomorph's htmx extension): the template function fragment runs the fragment's Load on a copy of the view and executes its block, so the page holds what /events would send. /events renders the fragment again when a topic it depends on changes (Topics for exact topics, Prefixes for prefixes such as "run/", Watch for topics that depend on the Params), and sends it only when its HTML differs from what that stream sent before. Fragments that show what one Load fills, as the parts of a page do, name it with Share, and a stream runs it once for all of them. The rail and the two parts of the task dock are part of every page (shellFragments).
- Routes for everything else, registered with AddRoute: dialogs, POST actions, helpers that answer with one fragment. A route is a function of the *Server and the request. A request that changes something has passed the Origin and CSRF checks and has its form parsed (at most maxBody, 4 MiB) when the route runs; use Server.View for its Params, Server.Write and Server.Fragment to answer, Server.Fail for an error, and the template block "csrf-field" in a form that a browser sends without script.
- Background work, registered with AddService: collectors that write the store, runners. A Service has a Setup, which runs before the server is built and hands what routes need to the other parts through Env.Put, and a Run, which runs until the context ends. Every external command goes through Env.Exec.
A service can also learn what the open pages need. Env.AddWatcher, called from Setup, adds a Watcher: each /events stream calls it with the exact topics its fragments depend on (Fragment.Topics and what Fragment.Watch returns for the stream's Params) when it opens, and calls the function it returns when it ends. The collectors count the status topics this way and read the status of a cluster only while a page shows it. The collect service also puts two plain funcs in Env for other parts: ColRefreshKey (func(realm string), read that realm again now) and ColDoctorKey (func(), run the host checks again). A part that finds neither (nil) works without them. A page's Load reads them with View.Env, which looks in the Env of the server that renders the page.
Styles and scripts need no registration: web/static/css/*.css and web/static/js/*.js are linked from the layout, in name order, after app.css and app.js, with content-hashed URLs. No inline script or style works: the Content-Security-Policy allows files of this origin only. No part adds template functions; the counts behind the rail entries come from SetNavCount.
Everything registered lives in a Registry. The init functions fill the default one, which Run uses; tests build their own with NewRegistry.
Index ¶
- Constants
- Variables
- func Run(ctx context.Context, o Options) error
- type Env
- type Fragment
- type LogsTabView
- type Miss
- type MissLink
- type NavCount
- type NavCountFunc
- type NavItem
- type Options
- type Page
- type Params
- type PgRealmScope
- type Registry
- type Route
- type Server
- func (s *Server) Fail(w http.ResponseWriter, r *http.Request, err error)
- func (s *Server) Fragment(w http.ResponseWriter, r *http.Request, name string)
- func (s *Server) Handler() http.Handler
- func (s *Server) View(r *http.Request, keys ...string) (*View, error)
- func (s *Server) Write(w http.ResponseWriter, r *http.Request, status int, name string, v *View)
- type Service
- type View
- type Watcher
Constants ¶
const ( // ColRefreshKey holds a func(realm string) that reads that realm again // now, such as after a task that changed it ended. ColRefreshKey colEnvKey = "collect.refresh" // ColDoctorKey holds a func() that runs the host checks again. ColDoctorKey colEnvKey = "collect.doctor" // ColMonitoringIntervalKey holds the interval at which the monitoring // stacks are read, a value with the methods Get() time.Duration and // Set(time.Duration). ColMonitoringIntervalKey colEnvKey = "collect.monitoring-interval" )
The Env keys that the collectors provide, for the parts that act on them. The values are plain funcs and interfaces, so that no part imports another.
Variables ¶
var ErrBadRequest = errors.New("bad request")
ErrBadRequest is what a Load returns for a parameter that does not pass its check. Wrap it with fmt.Errorf("...: %w", ErrBadRequest); the server answers 400 and says what the error says.
var ErrNotFound = errors.New("not found")
ErrNotFound is what a Load returns for an item that does not exist: the server answers 404 with the "Not found" page.
Functions ¶
Types ¶
type Env ¶
type Env struct {
Store *store.Store
// Exec runs external commands, and is the one that tests replace.
Exec cmdexec.Executor
Log *slog.Logger
// Opts are the options Run was called with, defaults filled in.
Opts Options
// Addr is host:port that the server listens on.
Addr string
// contains filtered or unexported fields
}
Env is what services and routes share: the store, the executor through which every external command runs, and the options.
func (*Env) AddWatcher ¶
AddWatcher adds a Watcher. Call it from Service.Setup, which runs before the server is built.
type Fragment ¶
type Fragment struct {
// Name is the SSE event and the name of the template block.
Name string
// Topics are the topics the fragment depends on: it is rendered again
// when one of them changes.
Topics []store.Topic
// Prefixes are topic prefixes it depends on, such as "run/" for every
// store.RunTopic. The prefix "" is every topic.
Prefixes []string
// Watch returns the topics that depend on the request's Params, such
// as the one run of the run page. It may be nil.
Watch func(p Params) []store.Topic
// Params are the query keys the fragment reads; /events passes on no
// others.
Params []string
// Load fills v.Data from v.Snap and v.Params.
Load func(v *View) error
// the fragments of one page: a stream runs it once for all the
// fragments of a Share that it renders at a time. Empty shares none.
Share string
}
A Fragment is a block of HTML that live updates replace.
type LogsTabView ¶
type LogsTabView struct {
Realm, Cluster string
// Nodes are the nodes of the cluster, the one shown selected.
Nodes []logsNodeOption
Node string
// Source is docker or journal.
Source string
// Units are the units of the node shown, after the entry for all.
Units []logsChoice
Unit string
// Lines are the line counts to start with.
Lines []logsChoice
// Count is the line count that the stream starts with.
Count int
// Query is the filter, and Errors keeps errors and warnings only.
// QueryMax is the longest filter, the longest value of a parameter.
Query string
QueryMax int
Errors bool
// Sind is the command that the terminal line starts with, and Suffix
// what follows a short name in a node argument.
Sind, Suffix string
// Keep is how many lines the tab keeps.
Keep int
// Fixed is set on the Logs tab of a node page, whose node is that of
// the page: the tab has no picker of the node. Ended is what the tab
// says in place of Follow once the log of a node that does not run
// ended, empty for one that runs.
Fixed bool
Ended string
}
LogsTabView is what the Logs tab of the cluster page shows.
func (*LogsTabView) Command ¶
func (d *LogsTabView) Command() string
Command returns the command of the terminal that shows the same log: the container output of the node, the journal of one unit, or the whole journal, which only a command on the node shows. logs.js writes the same.
func (*LogsTabView) KeepText ¶
func (d *LogsTabView) KeepText() string
KeepText is Keep for a sentence: 10,000.
type Miss ¶
type Miss struct {
// What says what is missing, in one sentence.
What string
// Hint is a second sentence, or empty.
Hint string
// Links are the ways forward, the closest match first.
Links []MissLink
}
Miss is an ErrNotFound that says what is missing and where to go instead. The Not found page shows it: a Load or a route returns it in place of a plain ErrNotFound when it knows better than "nothing here". errors.Is(m, ErrNotFound) holds.
type MissLink ¶
type MissLink struct {
Label string
URL string
// Note follows the link as plain text, such as "in realm ci".
Note string
}
MissLink is a way forward of a Miss.
type NavCount ¶
type NavCount struct {
// Class ok.
Text string
// bar of a phone, which has no room for it, leaves it out, as "3" of
// "3 need a look".
Unit string
Class string
// as its tooltip, such as "1 node needs a look".
Label string
}
NavCount is the badge behind a rail entry: a count with its unit, such as "1 warning", or a dot when all is well.
type NavCountFunc ¶
NavCountFunc returns the badge of a rail entry from the snapshot, at the time of the view. realm is the scope of an entry of the Realm group of the rail, which counts only that realm; it is "" for every realm, and for the other entries.
type NavItem ¶
type NavItem struct {
// Key is what Page.Nav names, and the ID of the entry's icon in
// web/static/icons.svg.
Group string
// the others are in the bar's More sheet.
Main bool
// same list.
Chord string
}
NavItem is an entry of the rail.
type Options ¶
type Options struct {
// Port to listen on; 0 picks a free one.
Port int
// NoOpen keeps the browser closed; the URL is printed either way.
NoOpen bool
// RuntimeDir is $XDG_RUNTIME_DIR, where the running UI is recorded so
// that a second sind ui opens it instead of starting another. Empty
// records nothing.
RuntimeDir string
// Exec runs the external commands; nil is the real one.
Exec cmdexec.Executor
// GOOS names the operating system, as runtime.GOOS does; empty is
// runtime.GOOS.
GOOS string
// Getenv reads the variables that tell whether a browser can open
// (SSH_CONNECTION, DISPLAY, WAYLAND_DISPLAY), as os.Getenv does; nil
// reads every variable as unset, so that on Linux no browser opens.
Getenv func(string) string
// Stderr gets the URL and the log; nil discards both.
Stderr io.Writer
// Demo serves made-up clusters and plays the commands of the dialogs:
// no Docker, no child process, nothing written. It does not record
// itself in RuntimeDir, so that it never stands in for a real sind ui.
Demo bool
// Still makes the demo stand at demo.Epoch and play nothing, so that its
// pages are the same on every run. It is for screenshots and needs Demo.
Still bool
// MonitoringInterval is how often the monitoring stacks are read
// while a page is open, between collect.MinInterval and
// collect.MaxInterval (sind ui checks it); 0 is collect.MonitoringEvery.
// The Monitoring page changes it.
MonitoringInterval time.Duration
// contains filtered or unexported fields
}
Options configure Run.
type Page ¶
type Page struct {
// Name identifies the page and names its content block
// "page-NAME". It is lower case, letters, digits and hyphens.
Name string
// Pattern is the route as http.ServeMux reads it, with the method:
// "GET /clusters/{name}". Its wildcards become Params.
Pattern string
// Title is the page title. Load may change it.
Title string
// empty lights none.
Nav string
// Params are the query keys that the page reads, besides the
// wildcards of its Pattern.
Params []string
// Fragments are the fragments of the page that live updates replace.
// The shell fragments (the rail, the task dock) are always included.
Fragments []string
// Load fills v.Data, and may set v.Title. It may return ErrNotFound or
// ErrBadRequest. It runs on each request, so it only reads.
Load func(v *View) error
}
A Page is a screen of sind ui at its own address.
type Params ¶
Params are the values a page or a fragment reads from its request: the wildcards of the route (as in {name}) and the query. The server keeps only the keys that the page or the fragment declares, and no value longer than maxParamLen. A Load checks each value again: the browser sends whatever it likes.
type PgRealmScope ¶
PgRealmScope is what the Overview or Activity says when its address narrows it to one realm, which filters only that page: the realm, and the address of the same page with every realm.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry holds what parts of sind ui register. The init functions fill the default one, before anything reads it.
func (*Registry) AddFragment ¶
AddFragment adds a fragment. It panics for a name that is invalid or already taken.
func (*Registry) AddPage ¶
AddPage adds a page. It panics for a name or a pattern that is invalid or already taken, as the init function that calls it is a program error.
func (*Registry) AddService ¶
AddService adds a service. It panics for a name that is taken.
func (*Registry) SetNavCount ¶
func (r *Registry) SetNavCount(key string, fn NavCountFunc)
SetNavCount sets the count behind the rail entry key.
type Route ¶
type Route struct {
// Pattern is the route as http.ServeMux reads it, with the method:
// "POST /clusters/{name}/delete".
Pattern string
// Handle answers the request. Use s.View, s.Write and s.Fail.
Handle func(s *Server, w http.ResponseWriter, r *http.Request)
// FetchOnly lets in only the requests that a page of this server sent
// itself, as its live parts do, and no navigation from another page. Set
// it on a route that starts work.
FetchOnly bool
}
A Route is a handler for the paths that no page serves.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server serves sind ui's pages and live updates for one store.
func (*Server) Fail ¶
Fail answers a request whose Load or route failed: ErrNotFound with the Not found page and what a Miss in err says; ErrBadRequest with 400 and its message, in the layout when a browser navigated to the address; anything else with 500 and a line in the log.
func (*Server) Fragment ¶
Fragment answers a request with the fragment name: its Load runs on the request's Params (the fragment's own keys) and its block is the response.
func (*Server) Handler ¶
Handler returns the server's handler with every check in front of it. The static files need no session: they hold no data, and the page that asks a browser to sign in uses them.
func (*Server) View ¶
View returns the view of a request: the current snapshot, the session's CSRF token, and as Params the wildcards of the route that matched and the values of the named keys, from the query and from the form. A value that is too long is an ErrBadRequest.
type Service ¶
type Service struct {
// Name orders the services: Setup and start go by name.
Name string
// Setup runs once before the server is built, to make what routes
// need and to put it in env (Env.Put). It may be nil.
Setup func(env *Env) error
// Run runs in its own goroutine until ctx ends, and may be nil.
Run func(ctx context.Context, env *Env)
}
A Service is background work of sind ui.
type View ¶
type View struct {
// Page is the name of the page, the empty string in a fragment that
// /events renders and on the Not found page.
Page string
// Title is the page's title, set by Page.Title and changed by Load.
Title string
// and /events passes it on to the rail fragment.
Nav string
// Snap is the snapshot at the start of the render. Load reads it and
// never changes it.
Snap store.Snapshot
// Now is the time to measure from.
Now time.Time
// Still says that the clock of the server stands still (the still demo),
// so that the clocks of the page do not count on.
Still bool
// Params are the values of the request that Load reads.
Params Params
// Scope is the realm that the session works in, "" for every realm:
// the realm that the browser remembers (pg_realm.go), or the one that a
// page of one realm or all of them shows. The rail shows it, scopes its
// Realm group to it, and /events renders with the scope of the page.
Scope string
// Data is what Load sets, for the template.
Data any
// CSRF is the session's token, for forms: {{template "csrf-field" .}}.
CSRF string
// Addr is host:port that sind ui listens on.
Addr string
// Events is the URL of the page's live updates, empty for none.
Events string
// Content is the name of the block that holds the page's content.
Content string
// Asset returns the URL of a static file by its path below
// web/static, such as "app.css".
Asset func(string) string
// Vendor returns the URL of a script of go-clikit's localweb/htmx by
// its name, such as htmx.Script.
Vendor func(string) string
// HTMXConfig is the configuration of htmx that the layout puts in the
// page, htmx.Config.
HTMXConfig string
// Styles and Scripts are the URLs of the extra files that every page
// links: web/static/css/*.css and web/static/js/*.js.
Styles []string
Scripts []string
// contains filtered or unexported fields
}
View is what a template renders from.
func (*View) Env ¶ added in v0.18.0
Env returns what a service put under key in the Env of the server that renders the view (Env.Get), nil when none did or the view has no server. A page's Load reads the values of the services through it.
func (*View) LogsTab ¶
func (v *View) LogsTab() *LogsTabView
LogsTab returns what the Logs tab of the view's cluster shows, from the view's Params. A value that does not fit, such as a node that the cluster does not have, falls back to the default.
func (*View) NodeLogsTab ¶ added in v0.16.0
func (v *View) NodeLogsTab() *LogsTabView
NodeLogsTab returns what the Logs tab of the view's node page shows: LogsTab, with the node of the page.
func (*View) PgRealmScope ¶
func (v *View) PgRealmScope() PgRealmScope
PgRealmScope returns the scope of the view of a page that shows one realm or all of them; Realm is empty when it shows every realm. The page is the one of the lit rail entry, which a fragment that /events renders knows too.
type Watcher ¶
A Watcher is told which topics a live stream (/events) watches: when the stream opens it is called with the topics of the stream's fragments, and the function it returns is called when the stream ends. A collector uses it to read more often what an open page shows, such as the status of a cluster whose page is open. The topics are the exact ones of the fragments (Fragment.Topics) and those their Watch functions return for the stream's Params; a fragment that depends on a prefix names none. The function may be called concurrently and must not block.
Source Files
¶
- action.go
- action_dialog.go
- action_images.go
- action_page.go
- action_plan.go
- action_power.go
- action_retry.go
- action_workers.go
- action_workers_pick.go
- assets.go
- collect.go
- demo_services.go
- doc.go
- dock.go
- help.go
- jump.go
- jump_verbs.go
- logs_stream.go
- logs_tab.go
- miss.go
- more_common.go
- more_doctor.go
- more_mesh.go
- more_monitoring.go
- more_topology.go
- overview.go
- overview_fleet.go
- pg_cluster.go
- pg_files.go
- pg_fix.go
- pg_funcs.go
- pg_life.go
- pg_lists.go
- pg_listview.go
- pg_miss.go
- pg_model.go
- pg_needs.go
- pg_node.go
- pg_ready.go
- pg_realm.go
- pg_slurm.go
- rail.go
- registry.go
- run_activity.go
- run_facts.go
- run_gantt.go
- run_model.go
- run_outcome.go
- run_outcome_fail.go
- run_outcome_stop.go
- run_page.go
- run_result.go
- run_task.go
- server.go
- signin_landing.go
- sse.go
- ui.go
- views.go
Directories
¶
| Path | Synopsis |
|---|---|
|
Package action turns the forms of sind ui's dialogs into the sind commands they run.
|
Package action turns the forms of sind ui's dialogs into the sind commands they run. |
|
Package activity follows sind's event log, the go-clikit event log (v1) that every sind command appends to and that sind ui reads its progress display from, and reduces its lines to store.Run values: each run is one command with the tree of its spans.
|
Package activity follows sind's event log, the go-clikit event log (v1) that every sind command appends to and that sind ui reads its progress display from, and reduces its lines to store.Run values: each run is one command with the tree of its spans. |
|
Package collect fills the store of sind ui with what Docker and the host know: the realms, clusters, nodes, networks and volumes, the mesh, the status of the clusters, the realm locks, the monitoring stacks and the host checks.
|
Package collect fills the store of sind ui with what Docker and the host know: the realms, clusters, nodes, networks and volumes, the mesh, the status of the clusters, the realm locks, the monitoring stacks and the host checks. |
|
Package demo is the made-up Docker host that sind ui --demo shows: two realms with clusters of different shapes and states, a mesh, a monitoring stack, the host checks with one warning, and a few runs of the activity log.
|
Package demo is the made-up Docker host that sind ui --demo shows: two realms with clusters of different shapes and states, a mesh, a monitoring stack, the host checks with one warning, and a few runs of the activity log. |
|
Package nodefold folds the names of nodes into node sets as sind's node arguments take them.
|
Package nodefold folds the names of nodes into node sets as sind's node arguments take them. |
|
Package run starts the sind commands that the dialogs of sind ui make, and keeps their tasks in the store while they run.
|
Package run starts the sind commands that the dialogs of sind ui make, and keeps their tasks in the store while they run. |
|
Package store holds what sind ui shows, in memory: the collectors write it, the pages and the live updates read it.
|
Package store holds what sind ui shows, in memory: the collectors write it, the pages and the live updates read it. |