remote: restore sessions with remote= reconnect to remote host
This commit is contained in:
parent
208db7ba68
commit
c4e1950f8c
|
|
@ -802,6 +802,27 @@ func (s *Manager) restoreSession(ps *agent.PersistedSession) error {
|
|||
}
|
||||
|
||||
uname := s.nextUname()
|
||||
var newDisp func() tools.Dispatcher
|
||||
var promptEnv []string
|
||||
remoteTarget := ps.Remote
|
||||
|
||||
if remoteTarget != "" {
|
||||
rsrv, dialErr := remote.Dial(context.Background(), remote.Config{
|
||||
SSHTarget: remoteTarget,
|
||||
CWD: cwd,
|
||||
})
|
||||
if dialErr != nil {
|
||||
return fmt.Errorf("remote dial: %w", dialErr)
|
||||
}
|
||||
newDisp = tools.NewDispatcherFunc(map[string]func() tools.Server{
|
||||
"execute": func() tools.Server { return rsrv },
|
||||
})
|
||||
promptEnv = []string{
|
||||
"PRIME_CWD=" + cwd,
|
||||
"PRIME_PLATFORM=" + rsrv.Info.Platform,
|
||||
"PRIME_IS_GIT_REPO=" + fmt.Sprintf("%v", rsrv.Info.IsGitRepo),
|
||||
}
|
||||
} else {
|
||||
var execOpts []execute.Option
|
||||
if !s.cfg.NoMount {
|
||||
execOpts = append(execOpts, WithMount9P())
|
||||
|
|
@ -812,11 +833,14 @@ func (s *Manager) restoreSession(ps *agent.PersistedSession) error {
|
|||
if s.cfg.Yolo {
|
||||
execOpts = append(execOpts, execute.WithYolo())
|
||||
}
|
||||
newDisp := tools.NewDispatcherFunc(map[string]func() tools.Server{
|
||||
newDisp = tools.NewDispatcherFunc(map[string]func() tools.Server{
|
||||
"execute": execute.Decl(cwd, execOpts...),
|
||||
})
|
||||
promptEnv = agent.PromptEnv(cwd)
|
||||
}
|
||||
|
||||
env := []string{"OLLIE_SESSION_ID=" + sessID, "OLLIE_UNAME=" + uname, "OLLIE=" + MountPathFor(uname)}
|
||||
env = append(env, agent.PromptEnv(cwd)...)
|
||||
env = append(env, promptEnv...)
|
||||
rt := agent.BuildRuntime(cfg, newDisp(), cwd, env)
|
||||
|
||||
restoredSession := agent.RestoreSession(ps.Messages)
|
||||
|
|
@ -825,7 +849,6 @@ func (s *Manager) restoreSession(ps *agent.PersistedSession) error {
|
|||
}
|
||||
|
||||
var sessPtr *Session
|
||||
promptEnv := agent.PromptEnv(cwd)
|
||||
core := agent.NewAgentCore(agent.AgentCoreConfig{
|
||||
Backend: be,
|
||||
AgentName: agentName,
|
||||
|
|
@ -834,6 +857,7 @@ func (s *Manager) restoreSession(ps *agent.PersistedSession) error {
|
|||
SessionID: sessID,
|
||||
Uname: uname,
|
||||
CWD: cwd,
|
||||
Remote: remoteTarget,
|
||||
Session: restoredSession,
|
||||
Runtime: rt,
|
||||
NewDispatcher: newDisp,
|
||||
|
|
@ -855,6 +879,14 @@ func (s *Manager) restoreSession(ps *agent.PersistedSession) error {
|
|||
sess := NewSession(sessID, core, ctx, cancel)
|
||||
sessPtr = sess
|
||||
sess.uname = uname
|
||||
sess.remote = remoteTarget
|
||||
|
||||
// Start mount for remote sessions (not managed by local execute.Server).
|
||||
if remoteTarget != "" && !s.cfg.NoMount {
|
||||
ms := &mountState{envExtra: make(map[string]string)}
|
||||
startMount(ms, uname)
|
||||
sess.mount = ms
|
||||
}
|
||||
|
||||
// Replay tail of persisted messages into the chat log so the GUI
|
||||
// and `chat` file show recent history on restore.
|
||||
|
|
@ -1093,6 +1125,7 @@ func (s *Manager) CreateSession(args []string) (string, error) {
|
|||
SessionID: sessID,
|
||||
Uname: uname,
|
||||
CWD: cwd,
|
||||
Remote: remoteTarget,
|
||||
Runtime: rt,
|
||||
NewDispatcher: newDisp,
|
||||
PromptEnvExtra: promptEnv,
|
||||
|
|
|
|||
Reference in New Issue