forked from hyperhq/hyperd
/
attach.go
67 lines (61 loc) · 1.37 KB
/
attach.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package daemon
import (
"fmt"
"hyper/engine"
"hyper/hypervisor"
"hyper/lib/glog"
"hyper/types"
)
func (daemon *Daemon) CmdAttach(job *engine.Job) (err error) {
if len(job.Args) == 0 {
return fmt.Errorf("Can not execute 'attach' command without any container/pod ID!")
}
if len(job.Args) == 1 {
return fmt.Errorf("Can not execute 'attach' command without any command!")
}
typeKey := job.Args[0]
typeVal := job.Args[1]
tag := job.Args[2]
var podName string
// We need find the vm id which running POD, and stop it
if typeKey == "pod" {
podName = typeVal
} else {
container := typeVal
podName, err = daemon.GetPodByContainer(container)
if err != nil {
return
}
}
vmid, err := daemon.GetPodVmByName(podName)
if err != nil {
return err
}
var (
ttyIO hypervisor.TtyIO
qemuCallback = make(chan *types.QemuResponse, 1)
)
ttyIO.Stdin = job.Stdin
ttyIO.Stdout = job.Stdout
ttyIO.ClientTag = tag
ttyIO.Callback = qemuCallback
var attachCommand = &hypervisor.AttachCommand{
Streams: &ttyIO,
Size: nil,
}
if typeKey == "pod" {
attachCommand.Container = ""
} else {
attachCommand.Container = typeVal
}
qemuEvent, _, _, err := daemon.GetQemuChan(vmid)
if err != nil {
return err
}
qemuEvent.(chan hypervisor.VmEvent) <- attachCommand
<-qemuCallback
defer func() {
glog.V(2).Info("Defer function for exec!")
}()
return nil
}