2024-05-14 22:34:41 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package blockcontroller
|
|
|
|
|
|
|
|
import (
|
2024-05-29 09:28:25 +02:00
|
|
|
"bytes"
|
2024-05-24 23:08:24 +02:00
|
|
|
"context"
|
2024-05-15 01:53:03 +02:00
|
|
|
"encoding/base64"
|
2024-05-16 09:29:58 +02:00
|
|
|
"encoding/json"
|
2024-05-14 22:34:41 +02:00
|
|
|
"fmt"
|
2024-05-15 08:25:21 +02:00
|
|
|
"io"
|
2024-05-15 07:37:04 +02:00
|
|
|
"log"
|
2024-06-13 23:41:28 +02:00
|
|
|
"strings"
|
2024-05-14 22:34:41 +02:00
|
|
|
"sync"
|
2024-05-24 23:08:24 +02:00
|
|
|
"time"
|
2024-05-14 22:34:41 +02:00
|
|
|
|
2024-05-15 08:25:21 +02:00
|
|
|
"github.com/creack/pty"
|
2024-05-14 22:34:41 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/eventbus"
|
2024-06-03 22:03:21 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/filestore"
|
2024-05-15 07:37:04 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/shellexec"
|
2024-06-12 02:42:10 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/waveobj"
|
2024-06-14 08:54:04 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/wshutil"
|
2024-05-21 00:57:15 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/wstore"
|
2024-05-16 09:29:58 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
BlockController_Shell = "shell"
|
|
|
|
BlockController_Cmd = "cmd"
|
2024-05-14 22:34:41 +02:00
|
|
|
)
|
|
|
|
|
2024-06-14 08:54:04 +02:00
|
|
|
const (
|
|
|
|
BlockFile_Main = "main" // used for main pty output
|
|
|
|
BlockFile_Html = "html" // used for alt html layout
|
|
|
|
)
|
|
|
|
|
2024-05-24 23:08:24 +02:00
|
|
|
const DefaultTimeout = 2 * time.Second
|
|
|
|
|
2024-05-14 22:34:41 +02:00
|
|
|
var globalLock = &sync.Mutex{}
|
|
|
|
var blockControllerMap = make(map[string]*BlockController)
|
|
|
|
|
2024-06-17 18:58:28 +02:00
|
|
|
type BlockInputUnion struct {
|
|
|
|
InputData []byte `json:"inputdata,omitempty"`
|
|
|
|
SigName string `json:"signame,omitempty"`
|
|
|
|
TermSize *shellexec.TermSize `json:"termsize,omitempty"`
|
|
|
|
}
|
|
|
|
|
2024-05-14 22:34:41 +02:00
|
|
|
type BlockController struct {
|
2024-06-14 08:54:04 +02:00
|
|
|
Lock *sync.Mutex
|
|
|
|
BlockId string
|
|
|
|
BlockDef *wstore.BlockDef
|
|
|
|
InputCh chan wshutil.BlockCommand
|
|
|
|
Status string
|
|
|
|
CreatedHtmlFile bool
|
2024-06-14 23:43:47 +02:00
|
|
|
ShellProc *shellexec.ShellProc
|
2024-06-17 18:58:28 +02:00
|
|
|
ShellInputCh chan *BlockInputUnion
|
2024-05-14 22:34:41 +02:00
|
|
|
}
|
|
|
|
|
2024-05-24 23:08:24 +02:00
|
|
|
func (bc *BlockController) WithLock(f func()) {
|
|
|
|
bc.Lock.Lock()
|
|
|
|
defer bc.Lock.Unlock()
|
|
|
|
f()
|
|
|
|
}
|
|
|
|
|
2024-05-16 09:29:58 +02:00
|
|
|
func jsonDeepCopy(val map[string]any) (map[string]any, error) {
|
|
|
|
barr, err := json.Marshal(val)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var rtn map[string]any
|
|
|
|
err = json.Unmarshal(barr, &rtn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return rtn, nil
|
|
|
|
}
|
|
|
|
|
2024-05-15 08:25:21 +02:00
|
|
|
func (bc *BlockController) setShellProc(shellProc *shellexec.ShellProc) error {
|
|
|
|
bc.Lock.Lock()
|
|
|
|
defer bc.Lock.Unlock()
|
|
|
|
if bc.ShellProc != nil {
|
|
|
|
return fmt.Errorf("shell process already running")
|
2024-05-14 22:34:41 +02:00
|
|
|
}
|
2024-05-15 08:25:21 +02:00
|
|
|
bc.ShellProc = shellProc
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bc *BlockController) getShellProc() *shellexec.ShellProc {
|
|
|
|
bc.Lock.Lock()
|
|
|
|
defer bc.Lock.Unlock()
|
|
|
|
return bc.ShellProc
|
2024-05-14 22:34:41 +02:00
|
|
|
}
|
|
|
|
|
2024-05-16 09:29:58 +02:00
|
|
|
type RunShellOpts struct {
|
|
|
|
TermSize shellexec.TermSize `json:"termsize,omitempty"`
|
2024-05-15 07:37:04 +02:00
|
|
|
}
|
|
|
|
|
2024-05-16 22:40:23 +02:00
|
|
|
func (bc *BlockController) Close() {
|
|
|
|
if bc.getShellProc() != nil {
|
|
|
|
bc.ShellProc.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-29 06:44:47 +02:00
|
|
|
const DefaultTermMaxFileSize = 256 * 1024
|
2024-06-14 08:54:04 +02:00
|
|
|
const DefaultHtmlMaxFileSize = 256 * 1024
|
2024-05-29 06:44:47 +02:00
|
|
|
|
2024-06-14 08:54:04 +02:00
|
|
|
func handleAppendBlockFile(blockId string, blockFile string, data []byte) error {
|
2024-05-29 06:44:47 +02:00
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
|
|
|
|
defer cancelFn()
|
2024-06-14 08:54:04 +02:00
|
|
|
err := filestore.WFS.AppendData(ctx, blockId, blockFile, data)
|
2024-05-29 06:44:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error appending to blockfile: %w", err)
|
|
|
|
}
|
2024-06-12 02:42:10 +02:00
|
|
|
eventbus.SendEvent(eventbus.WSEventType{
|
2024-06-14 08:54:04 +02:00
|
|
|
EventType: "blockfile",
|
|
|
|
ORef: waveobj.MakeORef(wstore.OType_Block, blockId).String(),
|
|
|
|
Data: &eventbus.WSFileEventData{
|
|
|
|
ZoneId: blockId,
|
|
|
|
FileName: blockFile,
|
|
|
|
FileOp: eventbus.FileOp_Append,
|
|
|
|
Data64: base64.StdEncoding.EncodeToString(data),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleAppendIJsonFile(blockId string, blockFile string, cmd map[string]any, tryCreate bool) error {
|
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
|
|
|
|
defer cancelFn()
|
|
|
|
if blockFile == BlockFile_Html && tryCreate {
|
|
|
|
err := filestore.WFS.MakeFile(ctx, blockId, blockFile, nil, filestore.FileOptsType{MaxSize: DefaultHtmlMaxFileSize, IJson: true})
|
|
|
|
if err != nil && err != filestore.ErrAlreadyExists {
|
|
|
|
return fmt.Errorf("error creating blockfile[html]: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err := filestore.WFS.AppendIJson(ctx, blockId, blockFile, cmd)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error appending to blockfile(ijson): %w", err)
|
|
|
|
}
|
|
|
|
eventbus.SendEvent(eventbus.WSEventType{
|
|
|
|
EventType: "blockfile",
|
|
|
|
ORef: waveobj.MakeORef(wstore.OType_Block, blockId).String(),
|
|
|
|
Data: &eventbus.WSFileEventData{
|
|
|
|
ZoneId: blockId,
|
|
|
|
FileName: blockFile,
|
|
|
|
FileOp: eventbus.FileOp_Append,
|
|
|
|
Data64: base64.StdEncoding.EncodeToString([]byte("{}")),
|
2024-05-29 06:44:47 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-29 09:28:25 +02:00
|
|
|
func (bc *BlockController) resetTerminalState() {
|
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
|
|
|
|
defer cancelFn()
|
|
|
|
var buf bytes.Buffer
|
|
|
|
// buf.WriteString("\x1b[?1049l") // disable alternative buffer
|
|
|
|
buf.WriteString("\x1b[0m") // reset attributes
|
|
|
|
buf.WriteString("\x1b[?25h") // show cursor
|
|
|
|
buf.WriteString("\x1b[?1000l") // disable mouse tracking
|
|
|
|
buf.WriteString("\r\n\r\n(restored terminal state)\r\n\r\n")
|
2024-06-03 22:03:21 +02:00
|
|
|
err := filestore.WFS.AppendData(ctx, bc.BlockId, "main", buf.Bytes())
|
2024-05-29 09:28:25 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error appending to blockfile (terminal reset): %v\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-17 18:58:28 +02:00
|
|
|
func resolveSimpleId(ctx context.Context, simpleId string) (*waveobj.ORef, error) {
|
|
|
|
if strings.Contains(simpleId, ":") {
|
|
|
|
rtn, err := waveobj.ParseORef(simpleId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error parsing simple id: %w", err)
|
|
|
|
}
|
|
|
|
return &rtn, nil
|
|
|
|
}
|
|
|
|
return wstore.DBResolveEasyOID(ctx, simpleId)
|
|
|
|
}
|
|
|
|
|
|
|
|
func staticHandleGetMeta(ctx context.Context, cmd *wshutil.BlockGetMetaCommand) (map[string]any, error) {
|
|
|
|
oref, err := waveobj.ParseORef(cmd.ORef)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error parsing oref: %w", err)
|
|
|
|
}
|
|
|
|
obj, err := wstore.DBGetORef(ctx, oref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error getting object: %w", err)
|
|
|
|
}
|
|
|
|
if obj == nil {
|
|
|
|
return nil, fmt.Errorf("object not found: %s", oref)
|
|
|
|
}
|
|
|
|
return waveobj.GetMeta(obj), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func staticHandleSetMeta(ctx context.Context, cmd *wshutil.BlockSetMetaCommand, curBlockId string) (map[string]any, error) {
|
|
|
|
var oref *waveobj.ORef
|
|
|
|
if cmd.ORef != "" {
|
|
|
|
orefVal, err := waveobj.ParseORef(cmd.ORef)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error parsing oref: %w", err)
|
|
|
|
}
|
|
|
|
oref = &orefVal
|
|
|
|
} else {
|
|
|
|
orefVal := waveobj.MakeORef(wstore.OType_Block, curBlockId)
|
|
|
|
oref = &orefVal
|
|
|
|
}
|
|
|
|
log.Printf("SETMETA: %s | %v\n", oref, cmd.Meta)
|
|
|
|
obj, err := wstore.DBGetORef(ctx, *oref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error getting object: %w", err)
|
|
|
|
}
|
|
|
|
if obj == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
meta := waveobj.GetMeta(obj)
|
|
|
|
if meta == nil {
|
|
|
|
meta = make(map[string]any)
|
|
|
|
}
|
|
|
|
for k, v := range cmd.Meta {
|
|
|
|
if v == nil {
|
|
|
|
delete(meta, k)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
meta[k] = v
|
|
|
|
}
|
|
|
|
waveobj.SetMeta(obj, meta)
|
|
|
|
err = wstore.DBUpdate(ctx, obj)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error updating block: %w", err)
|
|
|
|
}
|
|
|
|
// send a waveobj:update event
|
|
|
|
updatedBlock, err := wstore.DBGetORef(ctx, *oref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error getting object (2): %w", err)
|
|
|
|
}
|
|
|
|
eventbus.SendEvent(eventbus.WSEventType{
|
|
|
|
EventType: "waveobj:update",
|
|
|
|
ORef: oref.String(),
|
|
|
|
Data: wstore.WaveObjUpdate{
|
|
|
|
UpdateType: wstore.UpdateType_Update,
|
|
|
|
OType: updatedBlock.GetOType(),
|
|
|
|
OID: waveobj.GetOID(updatedBlock),
|
|
|
|
Obj: updatedBlock,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func staticHandleResolveIds(ctx context.Context, cmd *wshutil.ResolveIdsCommand) (map[string]any, error) {
|
|
|
|
rtn := make(map[string]any)
|
|
|
|
for _, simpleId := range cmd.Ids {
|
|
|
|
oref, err := resolveSimpleId(ctx, simpleId)
|
|
|
|
if err != nil || oref == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rtn[simpleId] = oref.String()
|
|
|
|
}
|
|
|
|
return rtn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bc *BlockController) waveOSCMessageHandler(ctx context.Context, cmd wshutil.BlockCommand, respFn wshutil.ResponseFnType) (wshutil.ResponseDataType, error) {
|
|
|
|
if strings.HasPrefix(cmd.GetCommand(), "controller:") {
|
|
|
|
bc.InputCh <- cmd
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
switch cmd.GetCommand() {
|
|
|
|
case wshutil.BlockCommand_GetMeta:
|
|
|
|
return staticHandleGetMeta(ctx, cmd.(*wshutil.BlockGetMetaCommand))
|
|
|
|
case wshutil.Command_ResolveIds:
|
|
|
|
return staticHandleResolveIds(ctx, cmd.(*wshutil.ResolveIdsCommand))
|
2024-06-20 09:00:00 +02:00
|
|
|
case wshutil.Command_CreateBlock:
|
|
|
|
return nil, nil
|
2024-06-17 18:58:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ProcessStaticCommand(bc.BlockId, cmd)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2024-05-16 09:29:58 +02:00
|
|
|
func (bc *BlockController) DoRunShellCommand(rc *RunShellOpts) error {
|
2024-05-29 06:44:47 +02:00
|
|
|
// create a circular blockfile for the output
|
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
defer cancelFn()
|
2024-06-03 22:03:21 +02:00
|
|
|
err := filestore.WFS.MakeFile(ctx, bc.BlockId, "main", nil, filestore.FileOptsType{MaxSize: DefaultTermMaxFileSize, Circular: true})
|
|
|
|
if err != nil && err != filestore.ErrAlreadyExists {
|
2024-05-29 06:44:47 +02:00
|
|
|
return fmt.Errorf("error creating blockfile: %w", err)
|
|
|
|
}
|
2024-06-03 22:03:21 +02:00
|
|
|
if err == filestore.ErrAlreadyExists {
|
2024-05-29 09:28:25 +02:00
|
|
|
// reset the terminal state
|
|
|
|
bc.resetTerminalState()
|
|
|
|
}
|
2024-05-15 08:25:21 +02:00
|
|
|
if bc.getShellProc() != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
shellProc, err := shellexec.StartShellProc(rc.TermSize)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = bc.setShellProc(shellProc)
|
|
|
|
if err != nil {
|
|
|
|
bc.ShellProc.Close()
|
|
|
|
return err
|
|
|
|
}
|
2024-06-17 18:58:28 +02:00
|
|
|
shellInputCh := make(chan *BlockInputUnion, 32)
|
2024-05-15 08:25:21 +02:00
|
|
|
bc.ShellInputCh = shellInputCh
|
2024-06-17 18:58:28 +02:00
|
|
|
messageCh := make(chan wshutil.RpcMessage, 32)
|
|
|
|
ptyBuffer := wshutil.MakePtyBuffer(wshutil.WaveOSCPrefix, bc.ShellProc.Pty, messageCh)
|
|
|
|
_, outputCh := wshutil.MakeWshRpc(wshutil.WaveServerOSC, messageCh, bc.waveOSCMessageHandler)
|
2024-05-15 08:25:21 +02:00
|
|
|
go func() {
|
2024-06-17 18:58:28 +02:00
|
|
|
// handles regular output from the pty (goes to the blockfile and xterm)
|
2024-05-15 08:25:21 +02:00
|
|
|
defer func() {
|
|
|
|
// needs synchronization
|
|
|
|
bc.ShellProc.Close()
|
|
|
|
close(bc.ShellInputCh)
|
|
|
|
bc.ShellProc = nil
|
|
|
|
bc.ShellInputCh = nil
|
|
|
|
}()
|
|
|
|
buf := make([]byte, 4096)
|
|
|
|
for {
|
2024-06-14 23:43:47 +02:00
|
|
|
nr, err := ptyBuffer.Read(buf)
|
2024-05-29 06:44:47 +02:00
|
|
|
if nr > 0 {
|
2024-06-14 23:43:47 +02:00
|
|
|
err := handleAppendBlockFile(bc.BlockId, BlockFile_Main, buf[:nr])
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error appending to blockfile: %v\n", err)
|
2024-05-29 06:44:47 +02:00
|
|
|
}
|
|
|
|
}
|
2024-05-15 08:25:21 +02:00
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error reading from shell: %v\n", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
go func() {
|
2024-06-17 18:58:28 +02:00
|
|
|
// handles input from the shellInputCh, sent to pty
|
2024-05-15 08:25:21 +02:00
|
|
|
for ic := range shellInputCh {
|
2024-06-17 18:58:28 +02:00
|
|
|
if len(ic.InputData) > 0 {
|
|
|
|
bc.ShellProc.Pty.Write(ic.InputData)
|
2024-05-15 08:25:21 +02:00
|
|
|
}
|
|
|
|
if ic.TermSize != nil {
|
2024-05-16 09:29:58 +02:00
|
|
|
log.Printf("SETTERMSIZE: %dx%d\n", ic.TermSize.Rows, ic.TermSize.Cols)
|
2024-05-15 08:25:21 +02:00
|
|
|
err := pty.Setsize(bc.ShellProc.Pty, &pty.Winsize{Rows: uint16(ic.TermSize.Rows), Cols: uint16(ic.TermSize.Cols)})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error setting term size: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|
2024-06-17 18:58:28 +02:00
|
|
|
// TODO signals
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
// handles outputCh -> shellInputCh
|
|
|
|
for out := range outputCh {
|
|
|
|
shellInputCh <- &BlockInputUnion{InputData: out}
|
2024-05-15 08:25:21 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-21 00:57:15 +02:00
|
|
|
func (bc *BlockController) Run(bdata *wstore.Block) {
|
2024-05-14 22:34:41 +02:00
|
|
|
defer func() {
|
2024-05-24 23:08:24 +02:00
|
|
|
bc.WithLock(func() {
|
2024-05-16 09:29:58 +02:00
|
|
|
// if the controller had an error status, don't change it
|
2024-05-24 23:08:24 +02:00
|
|
|
if bc.Status == "running" {
|
|
|
|
bc.Status = "done"
|
2024-05-16 09:29:58 +02:00
|
|
|
}
|
|
|
|
})
|
2024-06-12 02:42:10 +02:00
|
|
|
eventbus.SendEvent(eventbus.WSEventType{
|
|
|
|
EventType: "block:done",
|
|
|
|
ORef: waveobj.MakeORef(wstore.OType_Block, bc.BlockId).String(),
|
|
|
|
Data: nil,
|
2024-05-14 22:34:41 +02:00
|
|
|
})
|
|
|
|
globalLock.Lock()
|
|
|
|
defer globalLock.Unlock()
|
|
|
|
delete(blockControllerMap, bc.BlockId)
|
|
|
|
}()
|
2024-05-24 23:08:24 +02:00
|
|
|
bc.WithLock(func() {
|
|
|
|
bc.Status = "running"
|
2024-05-16 09:29:58 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
// only controller is "shell" for now
|
|
|
|
go func() {
|
|
|
|
err := bc.DoRunShellCommand(&RunShellOpts{TermSize: bdata.RuntimeOpts.TermSize})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error running shell: %v\n", err)
|
|
|
|
}
|
|
|
|
}()
|
2024-05-14 22:34:41 +02:00
|
|
|
|
|
|
|
for genCmd := range bc.InputCh {
|
|
|
|
switch cmd := genCmd.(type) {
|
2024-06-14 08:54:04 +02:00
|
|
|
case *wshutil.BlockInputCommand:
|
2024-06-17 18:58:28 +02:00
|
|
|
if bc.ShellInputCh == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
inputUnion := &BlockInputUnion{
|
|
|
|
SigName: cmd.SigName,
|
|
|
|
TermSize: cmd.TermSize,
|
2024-05-15 08:25:21 +02:00
|
|
|
}
|
2024-06-17 18:58:28 +02:00
|
|
|
if len(cmd.InputData64) > 0 {
|
|
|
|
inputBuf := make([]byte, base64.StdEncoding.DecodedLen(len(cmd.InputData64)))
|
|
|
|
nw, err := base64.StdEncoding.Decode(inputBuf, []byte(cmd.InputData64))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error decoding input data: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
inputUnion.InputData = inputBuf[:nw]
|
|
|
|
}
|
|
|
|
bc.ShellInputCh <- inputUnion
|
2024-05-14 22:34:41 +02:00
|
|
|
default:
|
2024-06-13 04:33:44 +02:00
|
|
|
log.Printf("unknown command type %T\n", cmd)
|
2024-05-14 22:34:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-28 00:44:57 +02:00
|
|
|
func StartBlockController(ctx context.Context, blockId string) error {
|
|
|
|
blockData, err := wstore.DBMustGet[*wstore.Block](ctx, blockId)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting block: %w", err)
|
|
|
|
}
|
|
|
|
if blockData.Controller == "" {
|
|
|
|
// nothing to start
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if blockData.Controller != BlockController_Shell {
|
|
|
|
return fmt.Errorf("unknown controller %q", blockData.Controller)
|
2024-05-16 09:29:58 +02:00
|
|
|
}
|
2024-05-14 22:34:41 +02:00
|
|
|
globalLock.Lock()
|
|
|
|
defer globalLock.Unlock()
|
2024-05-16 09:29:58 +02:00
|
|
|
if _, ok := blockControllerMap[blockId]; ok {
|
2024-05-28 00:44:57 +02:00
|
|
|
// already running
|
|
|
|
return nil
|
2024-05-15 01:53:03 +02:00
|
|
|
}
|
2024-05-14 22:34:41 +02:00
|
|
|
bc := &BlockController{
|
2024-05-15 08:25:21 +02:00
|
|
|
Lock: &sync.Mutex{},
|
2024-05-14 22:34:41 +02:00
|
|
|
BlockId: blockId,
|
2024-05-24 23:08:24 +02:00
|
|
|
Status: "init",
|
2024-06-14 08:54:04 +02:00
|
|
|
InputCh: make(chan wshutil.BlockCommand),
|
2024-05-14 22:34:41 +02:00
|
|
|
}
|
|
|
|
blockControllerMap[blockId] = bc
|
2024-05-28 00:44:57 +02:00
|
|
|
go bc.Run(blockData)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func StopBlockController(blockId string) {
|
|
|
|
bc := GetBlockController(blockId)
|
|
|
|
if bc == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bc.Close()
|
|
|
|
close(bc.InputCh)
|
2024-05-14 22:34:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetBlockController(blockId string) *BlockController {
|
|
|
|
globalLock.Lock()
|
|
|
|
defer globalLock.Unlock()
|
|
|
|
return blockControllerMap[blockId]
|
|
|
|
}
|
2024-05-17 03:01:52 +02:00
|
|
|
|
2024-06-14 08:54:04 +02:00
|
|
|
func ProcessStaticCommand(blockId string, cmdGen wshutil.BlockCommand) error {
|
2024-05-24 23:08:24 +02:00
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
|
|
|
|
defer cancelFn()
|
2024-05-17 03:01:52 +02:00
|
|
|
switch cmd := cmdGen.(type) {
|
2024-06-14 08:54:04 +02:00
|
|
|
case *wshutil.BlockSetViewCommand:
|
2024-05-17 03:01:52 +02:00
|
|
|
log.Printf("SETVIEW: %s | %q\n", blockId, cmd.View)
|
2024-05-26 20:59:14 +02:00
|
|
|
block, err := wstore.DBGet[*wstore.Block](ctx, blockId)
|
2024-05-24 23:08:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting block: %w", err)
|
2024-05-17 03:01:52 +02:00
|
|
|
}
|
2024-05-24 23:08:24 +02:00
|
|
|
block.View = cmd.View
|
2024-05-26 20:59:14 +02:00
|
|
|
err = wstore.DBUpdate(ctx, block)
|
2024-05-24 23:08:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error updating block: %w", err)
|
|
|
|
}
|
2024-06-13 23:41:28 +02:00
|
|
|
// send a waveobj:update event
|
|
|
|
updatedBlock, err := wstore.DBGet[*wstore.Block](ctx, blockId)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting block: %w", err)
|
|
|
|
}
|
|
|
|
eventbus.SendEvent(eventbus.WSEventType{
|
|
|
|
EventType: "waveobj:update",
|
|
|
|
ORef: waveobj.MakeORef(wstore.OType_Block, blockId).String(),
|
|
|
|
Data: wstore.WaveObjUpdate{
|
|
|
|
UpdateType: wstore.UpdateType_Update,
|
|
|
|
OType: wstore.OType_Block,
|
|
|
|
OID: blockId,
|
|
|
|
Obj: updatedBlock,
|
|
|
|
},
|
|
|
|
})
|
2024-05-24 23:08:24 +02:00
|
|
|
return nil
|
2024-06-14 08:54:04 +02:00
|
|
|
case *wshutil.BlockSetMetaCommand:
|
2024-06-17 18:58:28 +02:00
|
|
|
_, err := staticHandleSetMeta(ctx, cmd, blockId)
|
2024-05-24 23:08:24 +02:00
|
|
|
if err != nil {
|
2024-06-17 18:58:28 +02:00
|
|
|
return err
|
2024-06-13 23:41:28 +02:00
|
|
|
}
|
|
|
|
return nil
|
2024-06-17 18:58:28 +02:00
|
|
|
|
2024-06-14 08:54:04 +02:00
|
|
|
case *wshutil.BlockMessageCommand:
|
2024-06-13 23:41:28 +02:00
|
|
|
log.Printf("MESSAGE: %s | %q\n", blockId, cmd.Message)
|
2024-05-24 23:08:24 +02:00
|
|
|
return nil
|
2024-06-14 08:54:04 +02:00
|
|
|
|
|
|
|
case *wshutil.BlockAppendFileCommand:
|
|
|
|
log.Printf("APPENDFILE: %s | %q | len:%d\n", blockId, cmd.FileName, len(cmd.Data))
|
|
|
|
err := handleAppendBlockFile(blockId, cmd.FileName, cmd.Data)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error appending blockfile: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case *wshutil.BlockAppendIJsonCommand:
|
|
|
|
log.Printf("APPENDIJSON: %s | %q\n", blockId, cmd.FileName)
|
|
|
|
err := handleAppendIJsonFile(blockId, cmd.FileName, cmd.Data, true)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error appending blockfile(ijson): %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
2024-05-24 23:08:24 +02:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown command type %T", cmdGen)
|
2024-05-17 03:01:52 +02:00
|
|
|
}
|
|
|
|
}
|