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-07-26 22:30:11 +02:00
|
|
|
"crypto/rand"
|
2024-05-15 01:53:03 +02:00
|
|
|
"encoding/base64"
|
2024-07-26 22:30:11 +02:00
|
|
|
"encoding/hex"
|
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-06-24 23:34:31 +02:00
|
|
|
"io/fs"
|
2024-05-15 07:37:04 +02:00
|
|
|
"log"
|
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-24 23:34:31 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/wavebase"
|
2024-06-12 02:42:10 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/waveobj"
|
2024-07-26 22:30:11 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/wshrpc"
|
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
|
|
|
)
|
|
|
|
|
2024-07-18 00:24:43 +02:00
|
|
|
// set by main-server.go (for dependency inversion)
|
2024-07-26 22:30:11 +02:00
|
|
|
var WshServerFactoryFn func(inputCh chan []byte, outputCh chan []byte, initialCtx wshrpc.RpcContext) = nil
|
2024-07-18 00:24:43 +02:00
|
|
|
|
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-06-24 23:34:31 +02:00
|
|
|
const (
|
|
|
|
Status_Init = "init"
|
|
|
|
Status_Running = "running"
|
|
|
|
Status_Done = "done"
|
|
|
|
)
|
|
|
|
|
2024-06-21 01:01:55 +02:00
|
|
|
const (
|
|
|
|
DefaultTermMaxFileSize = 256 * 1024
|
|
|
|
DefaultHtmlMaxFileSize = 256 * 1024
|
|
|
|
)
|
|
|
|
|
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
|
2024-06-22 00:15:38 +02:00
|
|
|
ControllerType string
|
2024-06-21 01:01:55 +02:00
|
|
|
TabId string
|
2024-06-14 08:54:04 +02:00
|
|
|
BlockId string
|
|
|
|
BlockDef *wstore.BlockDef
|
|
|
|
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-06-24 23:34:31 +02:00
|
|
|
ShellProcStatus string
|
2024-07-18 00:24:43 +02:00
|
|
|
StopCh chan bool
|
2024-05-14 22:34:41 +02:00
|
|
|
}
|
|
|
|
|
2024-06-24 23:34:31 +02:00
|
|
|
type BlockControllerRuntimeStatus struct {
|
|
|
|
BlockId string `json:"blockid"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
ShellProcStatus string `json:"shellprocstatus,omitempty"`
|
|
|
|
}
|
|
|
|
|
2024-05-24 23:08:24 +02:00
|
|
|
func (bc *BlockController) WithLock(f func()) {
|
|
|
|
bc.Lock.Lock()
|
|
|
|
defer bc.Lock.Unlock()
|
|
|
|
f()
|
|
|
|
}
|
|
|
|
|
2024-06-24 23:34:31 +02:00
|
|
|
func (bc *BlockController) GetRuntimeStatus() *BlockControllerRuntimeStatus {
|
|
|
|
var rtn BlockControllerRuntimeStatus
|
|
|
|
bc.WithLock(func() {
|
|
|
|
rtn.BlockId = bc.BlockId
|
|
|
|
rtn.Status = bc.Status
|
|
|
|
rtn.ShellProcStatus = bc.ShellProcStatus
|
|
|
|
})
|
|
|
|
return &rtn
|
|
|
|
}
|
|
|
|
|
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) 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-06-24 23:34:31 +02:00
|
|
|
func (bc *BlockController) UpdateControllerAndSendUpdate(updateFn func() bool) {
|
|
|
|
var sendUpdate bool
|
|
|
|
bc.WithLock(func() {
|
|
|
|
sendUpdate = updateFn()
|
|
|
|
})
|
|
|
|
if sendUpdate {
|
|
|
|
log.Printf("sending blockcontroller update %#v\n", bc.GetRuntimeStatus())
|
|
|
|
go eventbus.SendEvent(eventbus.WSEventType{
|
2024-06-25 04:04:08 +02:00
|
|
|
EventType: eventbus.WSEvent_BlockControllerStatus,
|
2024-06-24 23:34:31 +02:00
|
|
|
ORef: waveobj.MakeORef(wstore.OType_Block, bc.BlockId).String(),
|
|
|
|
Data: bc.GetRuntimeStatus(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func HandleTruncateBlockFile(blockId string, blockFile string) error {
|
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
|
|
|
|
defer cancelFn()
|
|
|
|
err := filestore.WFS.WriteFile(ctx, blockId, blockFile, nil)
|
|
|
|
if err == fs.ErrNotExist {
|
|
|
|
return nil
|
2024-05-16 22:40:23 +02:00
|
|
|
}
|
2024-06-24 23:34:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error truncating blockfile: %w", err)
|
|
|
|
}
|
|
|
|
eventbus.SendEvent(eventbus.WSEventType{
|
2024-06-25 04:04:08 +02:00
|
|
|
EventType: eventbus.WSEvent_BlockFile,
|
2024-06-24 23:34:31 +02:00
|
|
|
ORef: waveobj.MakeORef(wstore.OType_Block, blockId).String(),
|
|
|
|
Data: &eventbus.WSFileEventData{
|
|
|
|
ZoneId: blockId,
|
|
|
|
FileName: blockFile,
|
|
|
|
FileOp: eventbus.FileOp_Truncate,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
|
2024-05-16 22:40:23 +02:00
|
|
|
}
|
|
|
|
|
2024-06-21 01:01:55 +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-25 04:04:08 +02:00
|
|
|
EventType: eventbus.WSEvent_BlockFile,
|
2024-06-14 08:54:04 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-05-29 09:28:25 +02:00
|
|
|
func (bc *BlockController) resetTerminalState() {
|
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
|
|
|
|
defer cancelFn()
|
2024-06-24 23:34:31 +02:00
|
|
|
var shouldTruncate bool
|
|
|
|
blockData, getBlockDataErr := wstore.DBMustGet[*wstore.Block](ctx, bc.BlockId)
|
|
|
|
if getBlockDataErr == nil {
|
2024-07-30 21:33:28 +02:00
|
|
|
shouldTruncate = blockData.Meta.GetBool(wstore.MetaKey_CmdClearOnRestart, false)
|
2024-06-24 23:34:31 +02:00
|
|
|
}
|
|
|
|
if shouldTruncate {
|
|
|
|
err := HandleTruncateBlockFile(bc.BlockId, BlockFile_Main)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error truncating main blockfile: %v\n", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// controller type = "shell"
|
2024-05-29 09:28:25 +02:00
|
|
|
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-07-26 22:30:11 +02:00
|
|
|
// every byte is 4-bits of randomness
|
|
|
|
func randomHexString(numHexDigits int) (string, error) {
|
|
|
|
numBytes := (numHexDigits + 1) / 2 // Calculate the number of bytes needed
|
|
|
|
bytes := make([]byte, numBytes)
|
|
|
|
if _, err := rand.Read(bytes); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
hexStr := hex.EncodeToString(bytes)
|
|
|
|
return hexStr[:numHexDigits], nil // Return the exact number of hex digits
|
|
|
|
}
|
|
|
|
|
2024-07-30 21:33:28 +02:00
|
|
|
func (bc *BlockController) DoRunShellCommand(rc *RunShellOpts, blockMeta waveobj.MetaMapType) 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})
|
2024-06-24 23:34:31 +02:00
|
|
|
if err != nil && err != fs.ErrExist {
|
|
|
|
err = fs.ErrExist
|
2024-05-29 06:44:47 +02:00
|
|
|
return fmt.Errorf("error creating blockfile: %w", err)
|
|
|
|
}
|
2024-06-24 23:34:31 +02:00
|
|
|
if err == fs.ErrExist {
|
2024-05-29 09:28:25 +02:00
|
|
|
// reset the terminal state
|
|
|
|
bc.resetTerminalState()
|
|
|
|
}
|
2024-06-24 23:34:31 +02:00
|
|
|
err = nil
|
2024-05-15 08:25:21 +02:00
|
|
|
if bc.getShellProc() != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-24 23:34:31 +02:00
|
|
|
var shellProcErr error
|
|
|
|
bc.WithLock(func() {
|
|
|
|
if bc.ShellProc != nil {
|
|
|
|
shellProcErr = fmt.Errorf("shell process already running")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if shellProcErr != nil {
|
|
|
|
return shellProcErr
|
|
|
|
}
|
2024-07-26 22:30:11 +02:00
|
|
|
var remoteDomainSocketName string
|
2024-07-30 21:33:28 +02:00
|
|
|
remoteName := blockMeta.GetString(wstore.MetaKey_Connection, "")
|
2024-07-26 22:30:11 +02:00
|
|
|
isRemote := remoteName != ""
|
|
|
|
if isRemote {
|
|
|
|
randStr, err := randomHexString(16) // 64-bits of randomness
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error generating random string: %w", err)
|
|
|
|
}
|
|
|
|
remoteDomainSocketName = fmt.Sprintf("/tmp/waveterm-%s.sock", randStr)
|
|
|
|
}
|
2024-06-24 23:34:31 +02:00
|
|
|
var cmdStr string
|
2024-07-23 22:16:53 +02:00
|
|
|
cmdOpts := shellexec.CommandOptsType{
|
|
|
|
Env: make(map[string]string),
|
|
|
|
}
|
2024-07-30 21:33:28 +02:00
|
|
|
if !blockMeta.GetBool(wstore.MetaKey_CmdNoWsh, false) {
|
2024-07-26 22:30:11 +02:00
|
|
|
if isRemote {
|
|
|
|
jwtStr, err := wshutil.MakeClientJWTToken(wshrpc.RpcContext{TabId: bc.TabId, BlockId: bc.BlockId}, remoteDomainSocketName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error making jwt token: %w", err)
|
|
|
|
}
|
2024-08-10 02:46:52 +02:00
|
|
|
cmdOpts.Env[wshutil.WaveJwtTokenVarName] = jwtStr
|
2024-07-26 22:30:11 +02:00
|
|
|
} else {
|
|
|
|
jwtStr, err := wshutil.MakeClientJWTToken(wshrpc.RpcContext{TabId: bc.TabId, BlockId: bc.BlockId}, wavebase.GetDomainSocketName())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error making jwt token: %w", err)
|
|
|
|
}
|
2024-08-10 02:46:52 +02:00
|
|
|
cmdOpts.Env[wshutil.WaveJwtTokenVarName] = jwtStr
|
2024-07-26 22:30:11 +02:00
|
|
|
}
|
|
|
|
}
|
2024-06-24 23:34:31 +02:00
|
|
|
if bc.ControllerType == BlockController_Shell {
|
2024-07-23 22:16:53 +02:00
|
|
|
cmdOpts.Interactive = true
|
|
|
|
cmdOpts.Login = true
|
2024-07-30 21:33:28 +02:00
|
|
|
cmdOpts.Cwd = blockMeta.GetString(wstore.MetaKey_CmdCwd, "")
|
2024-07-26 09:48:12 +02:00
|
|
|
if cmdOpts.Cwd != "" {
|
|
|
|
cmdOpts.Cwd = wavebase.ExpandHomeDir(cmdOpts.Cwd)
|
|
|
|
}
|
2024-06-24 23:34:31 +02:00
|
|
|
} else if bc.ControllerType == BlockController_Cmd {
|
2024-07-30 21:33:28 +02:00
|
|
|
cmdStr = blockMeta.GetString(wstore.MetaKey_Cmd, "")
|
|
|
|
if cmdStr == "" {
|
2024-06-24 23:34:31 +02:00
|
|
|
return fmt.Errorf("missing cmd in block meta")
|
|
|
|
}
|
2024-07-30 21:33:28 +02:00
|
|
|
cmdOpts.Cwd = blockMeta.GetString(wstore.MetaKey_CmdCwd, "")
|
|
|
|
if cmdOpts.Cwd != "" {
|
|
|
|
cmdOpts.Cwd = wavebase.ExpandHomeDir(cmdOpts.Cwd)
|
2024-06-24 23:34:31 +02:00
|
|
|
}
|
2024-07-30 21:33:28 +02:00
|
|
|
cmdOpts.Interactive = blockMeta.GetBool(wstore.MetaKey_CmdInteractive, false)
|
|
|
|
cmdOpts.Login = blockMeta.GetBool(wstore.MetaKey_CmdLogin, false)
|
|
|
|
cmdEnv := blockMeta.GetMap(wstore.MetaKey_CmdEnv)
|
|
|
|
for k, v := range cmdEnv {
|
|
|
|
if v == nil {
|
|
|
|
continue
|
2024-06-24 23:34:31 +02:00
|
|
|
}
|
2024-07-30 21:33:28 +02:00
|
|
|
if _, ok := v.(string); ok {
|
|
|
|
cmdOpts.Env[k] = v.(string)
|
2024-06-24 23:34:31 +02:00
|
|
|
}
|
2024-07-30 21:33:28 +02:00
|
|
|
if _, ok := v.(float64); ok {
|
|
|
|
cmdOpts.Env[k] = fmt.Sprintf("%v", v)
|
2024-06-24 23:34:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("unknown controller type %q", bc.ControllerType)
|
2024-05-15 08:25:21 +02:00
|
|
|
}
|
2024-07-16 03:00:10 +02:00
|
|
|
var shellProc *shellexec.ShellProc
|
2024-07-26 22:30:11 +02:00
|
|
|
if remoteName != "" {
|
2024-07-16 03:00:10 +02:00
|
|
|
shellProc, err = shellexec.StartRemoteShellProc(rc.TermSize, cmdStr, cmdOpts, remoteName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
shellProc, err = shellexec.StartShellProc(rc.TermSize, cmdStr, cmdOpts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-15 08:25:21 +02:00
|
|
|
}
|
2024-06-24 23:34:31 +02:00
|
|
|
bc.UpdateControllerAndSendUpdate(func() bool {
|
|
|
|
bc.ShellProc = shellProc
|
|
|
|
bc.ShellProcStatus = Status_Running
|
|
|
|
return true
|
|
|
|
})
|
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-07-18 00:24:43 +02:00
|
|
|
messageCh := make(chan []byte, 32)
|
2024-06-17 18:58:28 +02:00
|
|
|
ptyBuffer := wshutil.MakePtyBuffer(wshutil.WaveOSCPrefix, bc.ShellProc.Pty, messageCh)
|
2024-07-18 00:24:43 +02:00
|
|
|
outputCh := make(chan []byte, 32)
|
2024-07-26 22:30:11 +02:00
|
|
|
WshServerFactoryFn(messageCh, outputCh, wshrpc.RpcContext{BlockId: bc.BlockId, TabId: bc.TabId})
|
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() {
|
2024-06-24 23:34:31 +02:00
|
|
|
log.Printf("[shellproc] pty-read loop done\n")
|
|
|
|
|
2024-05-15 08:25:21 +02:00
|
|
|
// 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-21 01:01:55 +02:00
|
|
|
err := HandleAppendBlockFile(bc.BlockId, BlockFile_Main, buf[:nr])
|
2024-06-14 23:43:47 +02:00
|
|
|
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-24 23:34:31 +02:00
|
|
|
defer func() {
|
|
|
|
log.Printf("[shellproc] shellInputCh loop done\n")
|
|
|
|
}()
|
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-07-19 01:56:00 +02:00
|
|
|
err = bc.ShellProc.Cmd.SetSize(ic.TermSize.Rows, ic.TermSize.Cols)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error setting remote SIGWINCH: %v\n", err)
|
|
|
|
}
|
2024-05-15 08:25:21 +02:00
|
|
|
}
|
2024-06-17 18:58:28 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
// handles outputCh -> shellInputCh
|
2024-07-18 00:24:43 +02:00
|
|
|
for msg := range outputCh {
|
|
|
|
encodedMsg := wshutil.EncodeWaveOSCBytes(wshutil.WaveServerOSC, msg)
|
|
|
|
shellInputCh <- &BlockInputUnion{InputData: encodedMsg}
|
2024-05-15 08:25:21 +02:00
|
|
|
}
|
|
|
|
}()
|
2024-06-24 23:34:31 +02:00
|
|
|
go func() {
|
|
|
|
// wait for the shell to finish
|
|
|
|
defer func() {
|
|
|
|
bc.UpdateControllerAndSendUpdate(func() bool {
|
|
|
|
bc.ShellProcStatus = Status_Done
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
log.Printf("[shellproc] shell process wait loop done\n")
|
|
|
|
}()
|
|
|
|
waitErr := shellProc.Cmd.Wait()
|
|
|
|
shellProc.SetWaitErrorAndSignalDone(waitErr)
|
|
|
|
exitCode := shellexec.ExitCodeFromWaitErr(waitErr)
|
|
|
|
termMsg := fmt.Sprintf("\r\nprocess finished with exit code = %d\r\n\r\n", exitCode)
|
|
|
|
HandleAppendBlockFile(bc.BlockId, BlockFile_Main, []byte(termMsg))
|
|
|
|
}()
|
2024-05-15 08:25:21 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-24 23:34:31 +02:00
|
|
|
func getBoolFromMeta(meta map[string]any, key string, def bool) bool {
|
|
|
|
ival, found := meta[key]
|
|
|
|
if !found || ival == nil {
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
if val, ok := ival.(bool); ok {
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bc *BlockController) run(bdata *wstore.Block, blockMeta map[string]any) {
|
2024-05-14 22:34:41 +02:00
|
|
|
defer func() {
|
2024-06-24 23:34:31 +02:00
|
|
|
bc.UpdateControllerAndSendUpdate(func() bool {
|
|
|
|
if bc.Status == Status_Running {
|
|
|
|
bc.Status = Status_Done
|
|
|
|
return true
|
2024-05-16 09:29:58 +02:00
|
|
|
}
|
2024-06-24 23:34:31 +02:00
|
|
|
return false
|
2024-05-14 22:34:41 +02:00
|
|
|
})
|
|
|
|
globalLock.Lock()
|
|
|
|
defer globalLock.Unlock()
|
|
|
|
delete(blockControllerMap, bc.BlockId)
|
|
|
|
}()
|
2024-06-24 23:34:31 +02:00
|
|
|
bc.UpdateControllerAndSendUpdate(func() bool {
|
|
|
|
bc.Status = Status_Running
|
|
|
|
return true
|
2024-05-16 09:29:58 +02:00
|
|
|
})
|
2024-07-30 21:33:28 +02:00
|
|
|
controllerName := bdata.Meta.GetString(wstore.MetaKey_Controller, "")
|
|
|
|
if controllerName != BlockController_Shell && controllerName != BlockController_Cmd {
|
|
|
|
log.Printf("unknown controller %q\n", controllerName)
|
2024-06-24 23:34:31 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if getBoolFromMeta(blockMeta, wstore.MetaKey_CmdClearOnStart, false) {
|
|
|
|
err := HandleTruncateBlockFile(bc.BlockId, BlockFile_Main)
|
2024-05-16 09:29:58 +02:00
|
|
|
if err != nil {
|
2024-06-24 23:34:31 +02:00
|
|
|
log.Printf("error truncating main blockfile: %v\n", err)
|
2024-05-16 09:29:58 +02:00
|
|
|
}
|
2024-06-24 23:34:31 +02:00
|
|
|
}
|
|
|
|
runOnStart := getBoolFromMeta(blockMeta, wstore.MetaKey_CmdRunOnStart, true)
|
|
|
|
if runOnStart {
|
|
|
|
go func() {
|
|
|
|
err := bc.DoRunShellCommand(&RunShellOpts{TermSize: bdata.RuntimeOpts.TermSize}, bdata.Meta)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error running shell: %v\n", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2024-07-18 00:24:43 +02:00
|
|
|
<-bc.StopCh
|
|
|
|
}
|
2024-05-14 22:34:41 +02:00
|
|
|
|
2024-07-18 00:24:43 +02:00
|
|
|
func (bc *BlockController) SendInput(inputUnion *BlockInputUnion) error {
|
|
|
|
if bc.ShellInputCh == nil {
|
|
|
|
return fmt.Errorf("no shell input chan")
|
|
|
|
}
|
|
|
|
bc.ShellInputCh <- inputUnion
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-24 23:34:31 +02:00
|
|
|
|
2024-07-18 00:24:43 +02:00
|
|
|
func (bc *BlockController) RestartController() error {
|
|
|
|
// TODO: if shell command is already running
|
|
|
|
// we probably want to kill it off, wait, and then restart it
|
|
|
|
bdata, err := wstore.DBMustGet[*wstore.Block](context.Background(), bc.BlockId)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting block: %w", err)
|
2024-05-14 22:34:41 +02:00
|
|
|
}
|
2024-07-18 00:24:43 +02:00
|
|
|
err = bc.DoRunShellCommand(&RunShellOpts{TermSize: bdata.RuntimeOpts.TermSize}, bdata.Meta)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error running shell command: %v\n", err)
|
|
|
|
}
|
|
|
|
return nil
|
2024-05-14 22:34:41 +02:00
|
|
|
}
|
|
|
|
|
2024-07-18 00:24:43 +02:00
|
|
|
func StartBlockController(ctx context.Context, tabId string, blockId string) error {
|
|
|
|
log.Printf("start blockcontroller %q\n", blockId)
|
2024-05-28 00:44:57 +02:00
|
|
|
blockData, err := wstore.DBMustGet[*wstore.Block](ctx, blockId)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting block: %w", err)
|
|
|
|
}
|
2024-07-30 21:33:28 +02:00
|
|
|
controllerName := blockData.Meta.GetString(wstore.MetaKey_Controller, "")
|
|
|
|
if controllerName == "" {
|
2024-05-28 00:44:57 +02:00
|
|
|
// nothing to start
|
|
|
|
return nil
|
|
|
|
}
|
2024-07-30 21:33:28 +02:00
|
|
|
if controllerName != BlockController_Shell && controllerName != BlockController_Cmd {
|
|
|
|
return fmt.Errorf("unknown controller %q", controllerName)
|
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-06-24 23:34:31 +02:00
|
|
|
Lock: &sync.Mutex{},
|
2024-07-30 21:33:28 +02:00
|
|
|
ControllerType: controllerName,
|
2024-06-24 23:34:31 +02:00
|
|
|
TabId: tabId,
|
|
|
|
BlockId: blockId,
|
|
|
|
Status: Status_Init,
|
|
|
|
ShellProcStatus: Status_Init,
|
2024-07-18 00:24:43 +02:00
|
|
|
StopCh: make(chan bool),
|
2024-05-14 22:34:41 +02:00
|
|
|
}
|
|
|
|
blockControllerMap[blockId] = bc
|
2024-06-24 23:34:31 +02:00
|
|
|
go bc.run(blockData, blockData.Meta)
|
2024-05-28 00:44:57 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func StopBlockController(blockId string) {
|
|
|
|
bc := GetBlockController(blockId)
|
|
|
|
if bc == nil {
|
|
|
|
return
|
|
|
|
}
|
2024-06-24 23:34:31 +02:00
|
|
|
if bc.getShellProc() != nil {
|
|
|
|
bc.ShellProc.Close()
|
|
|
|
}
|
2024-07-18 00:24:43 +02:00
|
|
|
close(bc.StopCh)
|
2024-05-14 22:34:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetBlockController(blockId string) *BlockController {
|
|
|
|
globalLock.Lock()
|
|
|
|
defer globalLock.Unlock()
|
|
|
|
return blockControllerMap[blockId]
|
|
|
|
}
|