Allow non-instance specific socket actions
This commit is contained in:
parent
25c3af3516
commit
78b5a96294
2 changed files with 47 additions and 39 deletions
|
|
@ -2,7 +2,6 @@ package admin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -17,9 +16,32 @@ type InstanceAction struct {
|
||||||
NumParams int
|
NumParams int
|
||||||
|
|
||||||
HandleInteractive func(ctx context.Context, info *Admin, instance *wisski.WissKI, out io.Writer, params ...string) error
|
HandleInteractive func(ctx context.Context, info *Admin, instance *wisski.WissKI, out io.Writer, params ...string) error
|
||||||
HandleResult func(ctx context.Context, info *Admin, instance *wisski.WissKI, params ...string) (value any, err error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ia *InstanceAction) AsGenericAction() GenericAction {
|
||||||
|
return GenericAction{
|
||||||
|
NumParams: ia.NumParams + 1,
|
||||||
|
HandleInteractive: func(ctx context.Context, info *Admin, out io.Writer, params ...string) error {
|
||||||
|
instance, err := info.Dependencies.Instances.WissKI(ctx, params[0])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return ia.HandleInteractive(ctx, info, instance, out, params[1:]...)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type GenericAction struct {
|
||||||
|
NumParams int
|
||||||
|
|
||||||
|
HandleInteractive func(ctx context.Context, info *Admin, out io.Writer, params ...string) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// non-instance specific actions
|
||||||
|
var genericActions = map[string]GenericAction{}
|
||||||
|
|
||||||
|
// socket specific actions
|
||||||
var socketInstanceActions = map[string]InstanceAction{
|
var socketInstanceActions = map[string]InstanceAction{
|
||||||
"snapshot": {
|
"snapshot": {
|
||||||
HandleInteractive: func(ctx context.Context, admin *Admin, instance *wisski.WissKI, out io.Writer, params ...string) error {
|
HandleInteractive: func(ctx context.Context, admin *Admin, instance *wisski.WissKI, out io.Writer, params ...string) error {
|
||||||
|
|
@ -67,6 +89,14 @@ var socketInstanceActions = map[string]InstanceAction{
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var socketGenericActions = func() map[string]GenericAction {
|
||||||
|
generics := make(map[string]GenericAction, len(socketInstanceActions))
|
||||||
|
for n, a := range socketInstanceActions {
|
||||||
|
generics[n] = a.AsGenericAction()
|
||||||
|
}
|
||||||
|
return generics
|
||||||
|
}()
|
||||||
|
|
||||||
func (admin *Admin) serveSocket(conn httpx.WebSocketConnection) {
|
func (admin *Admin) serveSocket(conn httpx.WebSocketConnection) {
|
||||||
// read the next message to act on
|
// read the next message to act on
|
||||||
message, ok := <-conn.Read()
|
message, ok := <-conn.Read()
|
||||||
|
|
@ -74,31 +104,23 @@ func (admin *Admin) serveSocket(conn httpx.WebSocketConnection) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// perform an action if it exists!
|
name := string(message.Bytes)
|
||||||
if action, ok := socketInstanceActions[string(message.Bytes)]; ok {
|
|
||||||
admin.handleInstanceAction(conn, action)
|
// perform a generic action first
|
||||||
|
if action, ok := genericActions[name]; ok {
|
||||||
|
admin.handleGenericAction(conn, action)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// then do the socket actions
|
||||||
|
if action, ok := socketGenericActions[name]; ok {
|
||||||
|
admin.handleGenericAction(conn, action)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var instanceParamsTimeout = time.Second
|
var instanceParamsTimeout = time.Second
|
||||||
|
|
||||||
func (admin *Admin) handleInstanceAction(conn httpx.WebSocketConnection, action InstanceAction) {
|
func (admin *Admin) handleGenericAction(conn httpx.WebSocketConnection, action GenericAction) {
|
||||||
|
|
||||||
// read the slug
|
|
||||||
slug, ok := <-conn.Read()
|
|
||||||
if !ok {
|
|
||||||
<-conn.WriteText("Error reading slug")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// resolve the instance
|
|
||||||
instance, err := admin.Dependencies.Instances.WissKI(conn.Context(), string(slug.Bytes))
|
|
||||||
if err != nil {
|
|
||||||
<-conn.WriteText("Instance not found")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// read the parameters
|
// read the parameters
|
||||||
params := make([]string, action.NumParams)
|
params := make([]string, action.NumParams)
|
||||||
for i := range params {
|
for i := range params {
|
||||||
|
|
@ -126,28 +148,11 @@ func (admin *Admin) handleInstanceAction(conn httpx.WebSocketConnection, action
|
||||||
|
|
||||||
// handle the interactive action
|
// handle the interactive action
|
||||||
if action.HandleInteractive != nil {
|
if action.HandleInteractive != nil {
|
||||||
err := action.HandleInteractive(conn.Context(), admin, instance, writer, params...)
|
err := action.HandleInteractive(conn.Context(), admin, writer, params...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(writer, err)
|
fmt.Fprintln(writer, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Fprintln(writer, "done")
|
fmt.Fprintln(writer, "done")
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle the result computation
|
|
||||||
if action.HandleResult != nil {
|
|
||||||
result, err := action.HandleResult(conn.Context(), admin, instance, params...)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintln(writer, "false")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
data, err := json.Marshal(result)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintln(writer, "false")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Fprintln(writer, "true")
|
|
||||||
fmt.Fprintln(writer, data)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
3
internal/status/core.go
Normal file
3
internal/status/core.go
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
package status
|
||||||
|
|
||||||
|
type ImageVersion string
|
||||||
Loading…
Add table
Add a link
Reference in a new issue