mirror of
https://github.com/wavetermdev/backup.git
synced 2026-08-05 13:57:07 -07:00
reinit updates (#500)
* working on re-init when you create a tab. some refactoring of existing reinit to make the messaging clearer. auto-connect, etc. * working to remove the 'default' shell states out of MShellProc. each tab should have its own state that gets set on open. * refactor newtab settings into individual components (and move to a new file) * more refactoring of tab settings -- use same control in settings and newtab * have screensettings use the same newtab settings components * use same conn dropdown, fix classes, update some of the confirm messages to be less confusing (replace screen with tab) * force a cr on a new tab to initialize state in a new line. poc right now, need to add to new workspace workflow as well * small fixups * remove nohist from GetRawStr, make const * update hover behavior for tabs * fix interaction between change remote dropdown, cmdinput, error handling, and selecting a remote * only switch screen remote if the activemainview is session (new remote flow). don't switch it if we're on the connections page which is confusing. also make it interactive * fix wording on tos modal * allow empty workspaces. also allow the last workspace to be deleted. (prep for new startup sequence where we initialize the first session after tos modal) * add some dead code that might come in use later (when we change how we show connection in cmdinput) * working a cople different angles. new settings tab-pulldown (likely orphaned). and then allowing null activeScreen and null activeSession in workspaceview (show appropriate messages, and give buttons to create new tabs/workspaces). prep for new startup flow * don't call initActiveShells anymore. also call ensureWorkspace() on TOS close * trying to use new pulldown screen settings * experiment with an escape keybinding * working on tab settings close triggers * close tab settings on tab switch * small updates to tos popup, reorder, update button text/size, small wording updates * when deleting a screen, send SIGHUP to all running commands * not sure how this happened, lineid should not be passed to setLineFocus * remove context timeouts for ReInit (it is now interactive, so it gets canceled like a normal command -- via ^C, and should not timeout on its own) * deal with screen/session tombstones updates (ignore to quite warning) * remove defaultfestate from remote * fix issue with removing default ris * remove dead code * open the settings pulldown for new screens * update prompt to show when the shell is still initializing (or if it failed) * switch buttons to use wave button class, update messages, and add warning for no shell state * all an override of rptr for dyncmds. needed for the 'connect' command (we need to set the rptr to the *new* connection rather than the old one) * remove old commented out code
This commit is contained in:
@@ -214,38 +214,6 @@ func (msh *MShellProc) GetStatus() string {
|
||||
return msh.Status
|
||||
}
|
||||
|
||||
func (msh *MShellProc) GetDefaultState(shellType string) *packet.ShellState {
|
||||
_, state := msh.StateMap.GetCurrentState(shellType)
|
||||
return state
|
||||
}
|
||||
|
||||
func (msh *MShellProc) EnsureShellType(ctx context.Context, shellType string) error {
|
||||
if msh.StateMap.HasShell(shellType) {
|
||||
return nil
|
||||
}
|
||||
// try to reinit the shell
|
||||
_, err := msh.ReInit(ctx, base.CommandKey(""), shellType, nil, false)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error trying to initialize shell %q: %v", shellType, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (msh *MShellProc) GetDefaultStatePtr(shellType string) *sstore.ShellStatePtr {
|
||||
msh.Lock.Lock()
|
||||
defer msh.Lock.Unlock()
|
||||
hash, _ := msh.StateMap.GetCurrentState(shellType)
|
||||
if hash == "" {
|
||||
return nil
|
||||
}
|
||||
return &sstore.ShellStatePtr{BaseHash: hash}
|
||||
}
|
||||
|
||||
func (msh *MShellProc) GetDefaultFeState(shellType string) map[string]string {
|
||||
state := msh.GetDefaultState(shellType)
|
||||
return sstore.FeStateFromShellState(state)
|
||||
}
|
||||
|
||||
func (msh *MShellProc) GetRemoteId() string {
|
||||
msh.Lock.Lock()
|
||||
defer msh.Lock.Unlock()
|
||||
@@ -489,6 +457,26 @@ func ResolveRemoteRef(remoteRef string) *RemoteRuntimeState {
|
||||
return nil
|
||||
}
|
||||
|
||||
func SendSignalToCmd(ctx context.Context, cmd *sstore.CmdType, sig string) error {
|
||||
msh := GetRemoteById(cmd.Remote.RemoteId)
|
||||
if msh == nil {
|
||||
return fmt.Errorf("no connection found")
|
||||
}
|
||||
if !msh.IsConnected() {
|
||||
return fmt.Errorf("not connected")
|
||||
}
|
||||
cmdCk := base.MakeCommandKey(cmd.ScreenId, cmd.LineId)
|
||||
if !msh.IsCmdRunning(cmdCk) {
|
||||
// this could also return nil (depends on use case)
|
||||
// settled on coded error so we can check for this error
|
||||
return base.CodedErrorf(packet.EC_CmdNotRunning, "cmd not running")
|
||||
}
|
||||
sigPk := packet.MakeSpecialInputPacket()
|
||||
sigPk.CK = cmdCk
|
||||
sigPk.SigName = sig
|
||||
return msh.ServerProc.Input.SendPacket(sigPk)
|
||||
}
|
||||
|
||||
func unquoteDQBashString(str string) (string, bool) {
|
||||
if len(str) < 2 {
|
||||
return str, false
|
||||
@@ -588,6 +576,7 @@ func (msh *MShellProc) GetRemoteRuntimeState() RemoteRuntimeState {
|
||||
InstallStatus: msh.InstallStatus,
|
||||
NeedsMShellUpgrade: msh.NeedsMShellUpgrade,
|
||||
Local: msh.Remote.Local,
|
||||
IsSudo: msh.Remote.IsSudo(),
|
||||
NoInitPk: msh.ErrNoInitPk,
|
||||
AuthType: sstore.RemoteAuthTypeNone,
|
||||
ShellPref: msh.Remote.ShellPref,
|
||||
@@ -661,11 +650,6 @@ func (msh *MShellProc) GetRemoteRuntimeState() RemoteRuntimeState {
|
||||
vars["besthost"] = vars["remotehost"]
|
||||
vars["bestshorthost"] = vars["remoteshorthost"]
|
||||
}
|
||||
_, curState := msh.StateMap.GetCurrentState(shellPref)
|
||||
if curState != nil {
|
||||
state.DefaultFeState = sstore.FeStateFromShellState(curState)
|
||||
vars["cwd"] = curState.Cwd
|
||||
}
|
||||
if msh.Remote.Local && msh.Remote.IsSudo() {
|
||||
vars["bestuser"] = "sudo"
|
||||
} else if msh.Remote.IsSudo() {
|
||||
@@ -687,7 +671,6 @@ func (msh *MShellProc) GetRemoteRuntimeState() RemoteRuntimeState {
|
||||
varsCopy[key] = value
|
||||
}
|
||||
state.RemoteVars = varsCopy
|
||||
state.ActiveShells = msh.StateMap.GetShells()
|
||||
return state
|
||||
}
|
||||
|
||||
@@ -932,9 +915,9 @@ func (msh *MShellProc) writeToPtyBuffer_nolock(strFmt string, args ...interface{
|
||||
realStr = realStr + "\r\n"
|
||||
}
|
||||
if strings.HasPrefix(realStr, "*") {
|
||||
realStr = "\033[0m\033[31mprompt>\033[0m " + realStr[1:]
|
||||
realStr = "\033[0m\033[31mwave>\033[0m " + realStr[1:]
|
||||
} else {
|
||||
realStr = "\033[0m\033[32mprompt>\033[0m " + realStr
|
||||
realStr = "\033[0m\033[32mwave>\033[0m " + realStr
|
||||
}
|
||||
barr := msh.PtyBuffer.Bytes()
|
||||
if len(barr) > 0 && barr[len(barr)-1] != '\n' {
|
||||
@@ -1509,18 +1492,18 @@ func (msh *MShellProc) ReInit(ctx context.Context, ck base.CommandKey, shellType
|
||||
}
|
||||
|
||||
func makeShellInitOutputMsg(verbose bool, state *packet.ShellState, stats *packet.ShellStateStats, dur time.Duration, ptyMsg bool) string {
|
||||
waveStr := fmt.Sprintf("%swave>%s", utilfn.AnsiGreenColor(), utilfn.AnsiResetColor())
|
||||
if !verbose || ptyMsg {
|
||||
if ptyMsg {
|
||||
return fmt.Sprintf("initialized state shell:%s statehash:%s %dms\n", state.GetShellType(), state.GetHashVal(false), dur.Milliseconds())
|
||||
} else {
|
||||
return fmt.Sprintf("initialized connection state (shell:%s)\r\n", state.GetShellType())
|
||||
return fmt.Sprintf("%s initialized connection state (shell:%s)\r\n", waveStr, state.GetShellType())
|
||||
}
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
buf.WriteString("-----\r\n")
|
||||
buf.WriteString(fmt.Sprintf("initialized connection shell:%s statehash:%s %dms\r\n", state.GetShellType(), state.GetHashVal(false), dur.Milliseconds()))
|
||||
buf.WriteString(fmt.Sprintf("%s initialized connection shell:%s statehash:%s %dms\r\n", waveStr, state.GetShellType(), state.GetHashVal(false), dur.Milliseconds()))
|
||||
if stats != nil {
|
||||
buf.WriteString(fmt.Sprintf(" outsize:%s size:%s env:%d, vars:%d, aliases:%d, funcs:%d\r\n", scbase.NumFormatDec(stats.OutputSize), scbase.NumFormatDec(stats.StateSize), stats.EnvCount, stats.VarCount, stats.AliasCount, stats.FuncCount))
|
||||
buf.WriteString(fmt.Sprintf("%s outsize:%s size:%s env:%d, vars:%d, aliases:%d, funcs:%d\r\n", waveStr, scbase.NumFormatDec(stats.OutputSize), scbase.NumFormatDec(stats.StateSize), stats.EnvCount, stats.VarCount, stats.AliasCount, stats.FuncCount))
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
@@ -1762,7 +1745,7 @@ func (msh *MShellProc) Launch(interactive bool) {
|
||||
msh.WriteToPtyBuffer("*disconnected exitcode=%d\n", exitCode)
|
||||
}()
|
||||
go msh.ProcessPackets()
|
||||
msh.initActiveShells()
|
||||
// msh.initActiveShells()
|
||||
go msh.NotifyRemoteUpdate()
|
||||
}
|
||||
|
||||
@@ -1780,7 +1763,7 @@ func (msh *MShellProc) initActiveShells() {
|
||||
wg.Add(1)
|
||||
go func(shellType string) {
|
||||
defer wg.Done()
|
||||
reinitCtx, cancelFn := context.WithTimeout(context.Background(), shellapi.ReInitTimeout)
|
||||
reinitCtx, cancelFn := context.WithTimeout(context.Background(), 12*time.Second)
|
||||
defer cancelFn()
|
||||
_, err = msh.ReInit(reinitCtx, base.CommandKey(""), shellType, nil, false)
|
||||
if err != nil {
|
||||
@@ -1894,25 +1877,6 @@ func (msh *MShellProc) removePendingStateCmd(screenId string, rptr sstore.Remote
|
||||
}
|
||||
}
|
||||
|
||||
func ResolveCurrentScreenStatePtr(ctx context.Context, sessionId string, screenId string, remotePtr sstore.RemotePtrType) (*sstore.ShellStatePtr, error) {
|
||||
statePtr, err := sstore.GetRemoteStatePtr(ctx, sessionId, screenId, remotePtr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot get current connection stateptr: %w", err)
|
||||
}
|
||||
if statePtr == nil {
|
||||
msh := GetRemoteById(remotePtr.RemoteId)
|
||||
err := msh.EnsureShellType(ctx, msh.GetShellPref()) // make sure shellType is initialized
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
statePtr = msh.GetDefaultStatePtr(msh.GetShellPref())
|
||||
if statePtr == nil {
|
||||
return nil, fmt.Errorf("no valid default connection stateptr")
|
||||
}
|
||||
}
|
||||
return statePtr, nil
|
||||
}
|
||||
|
||||
type RunCommandOpts struct {
|
||||
SessionId string
|
||||
ScreenId string
|
||||
@@ -1990,10 +1954,13 @@ func RunCommand(ctx context.Context, rcOpts RunCommandOpts, runPacket *packet.Ru
|
||||
statePtr = rcOpts.StatePtr
|
||||
} else {
|
||||
var err error
|
||||
statePtr, err = ResolveCurrentScreenStatePtr(ctx, sessionId, screenId, remotePtr)
|
||||
statePtr, err = sstore.GetRemoteStatePtr(ctx, sessionId, screenId, remotePtr)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("cannot run command: %w", err)
|
||||
}
|
||||
if statePtr == nil {
|
||||
return nil, nil, fmt.Errorf("cannot run command: no valid shell state found")
|
||||
}
|
||||
}
|
||||
currentState, err := sstore.GetFullState(ctx, *statePtr)
|
||||
if err != nil || currentState == nil {
|
||||
@@ -2002,10 +1969,6 @@ func RunCommand(ctx context.Context, rcOpts RunCommandOpts, runPacket *packet.Ru
|
||||
runPacket.State = addScVarsToState(currentState)
|
||||
runPacket.StateComplete = true
|
||||
runPacket.ShellType = currentState.GetShellType()
|
||||
err = msh.EnsureShellType(ctx, runPacket.ShellType) // make sure shellType is initialized
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
// start cmdwait. must be started before sending the run packet
|
||||
// this ensures that we don't process output, or cmddone packets until we set up the line, cmd, and ptyout file
|
||||
@@ -2144,7 +2107,7 @@ func (msh *MShellProc) HandleFeInput(inputPk *scpacket.FeInputPacketType) error
|
||||
msh.Lock.Unlock()
|
||||
if sink == nil {
|
||||
// no sink and no running command
|
||||
return fmt.Errorf("cannot send input, cmd is not running")
|
||||
return fmt.Errorf("cannot send input, cmd is not running (%s)", inputPk.CK)
|
||||
}
|
||||
return sink.HandleInput(inputPk)
|
||||
}
|
||||
@@ -2461,7 +2424,7 @@ func (msh *MShellProc) processSinglePacket(pk packet.PacketType) {
|
||||
msh.WriteToPtyBuffer("stderr> [remote %s] %s\n", msh.GetRemoteName(), rawPk.Data)
|
||||
return
|
||||
}
|
||||
msh.WriteToPtyBuffer("MSH> [remote %s] unhandled packet %s\n", msh.GetRemoteName(), packet.AsString(pk))
|
||||
msh.WriteToPtyBuffer("*[remote %s] unhandled packet %s\n", msh.GetRemoteName(), packet.AsString(pk))
|
||||
}
|
||||
|
||||
func (msh *MShellProc) ProcessPackets() {
|
||||
|
||||
Reference in New Issue
Block a user