This repository has been archived by the owner on Feb 27, 2020. It is now read-only.
/
sandbox.go
189 lines (162 loc) · 4.49 KB
/
sandbox.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package qemuengine
import (
"errors"
"io"
"net/http"
"strings"
"github.com/Sirupsen/logrus"
"github.com/taskcluster/taskcluster-worker/engines"
"github.com/taskcluster/taskcluster-worker/engines/qemu/image"
"github.com/taskcluster/taskcluster-worker/engines/qemu/metaservice"
"github.com/taskcluster/taskcluster-worker/engines/qemu/network"
"github.com/taskcluster/taskcluster-worker/engines/qemu/vm"
"github.com/taskcluster/taskcluster-worker/runtime"
"github.com/taskcluster/taskcluster-worker/runtime/atomics"
)
type sandbox struct {
engines.SandboxBase
vm *vm.VirtualMachine
context *runtime.TaskContext
engine *engine
proxies map[string]http.Handler
metaService *metaservice.MetaService
resolve atomics.Once // Must wrap access mutation of resultXXX/done
resultSet engines.ResultSet // ResultSet for WaitForResult
resultError error // Error for WaitForResult
resultAbort error // Error for Abort
log *logrus.Entry // System log
sessions *sessionManager
}
// newSandbox will create a new sandbox and start it.
func newSandbox(
command []string,
env map[string]string,
proxies map[string]http.Handler,
image *image.Instance,
network *network.Network,
c *runtime.TaskContext,
e *engine,
) (*sandbox, error) {
log := e.Log.WithField("taskId", c.TaskID).WithField("runId", c.RunID)
vm, err := vm.NewVirtualMachine(
e.engineConfig.MachineOptions,
image, network, e.engineConfig.SocketFolder, "", "",
log.WithField("component", "vm"),
)
if err != nil {
return nil, err
}
// Create sandbox
s := &sandbox{
vm: vm,
context: c,
engine: e,
proxies: proxies,
log: log,
}
// Setup meta-data service
s.metaService = metaservice.New(command, env, c.LogDrain(), s.result, e.Environment)
// Create session manager
s.sessions = newSessionManager(s.metaService, s.vm)
// Setup network handler
s.vm.SetHTTPHandler(http.HandlerFunc(s.handleRequest))
// Start the VM
debug("Starting virtual machine")
s.vm.Start()
// Resolve when VM is closed
go s.waitForCrash()
return s, nil
}
func (s *sandbox) handleRequest(w http.ResponseWriter, r *http.Request) {
// Sanity checks and identifiation of name/hostname/virtualhost/folder
if r.URL.Path[0] != '/' {
w.WriteHeader(http.StatusNotFound)
return
}
p := strings.SplitN(r.URL.Path[1:], "/", 2)
if len(p) != 2 {
w.WriteHeader(http.StatusNotFound)
return
}
name, path := p[0], "/"+p[1]
// If name is engine, we pass it to meta-data
if name == "engine" {
s.metaService.ServeHTTP(w, r)
return
}
h := s.proxies[name]
if h == nil {
w.WriteHeader(http.StatusNotFound)
return
}
r.URL.Path = path
r.URL.RawPath = "" // TODO: implement this if we ever need it
h.ServeHTTP(w, r)
}
func (s *sandbox) result(success bool) {
// Wait for all sessions to be finished and stop issuing new sessions
s.sessions.WaitAndTerminate()
s.resolve.Do(func() {
s.resultSet = newResultSet(success, s.vm, s.metaService)
s.resultAbort = engines.ErrSandboxTerminated
})
}
// waitForCrash will wait for a VM crash and resolve
func (s *sandbox) waitForCrash() {
// Wait for the VM to finish
<-s.vm.Done
s.resolve.Do(func() {
// Kill all sessions
s.sessions.AbortSessions()
// TODO: Read s.vm.Error and handle the error
s.resultError = errors.New("QEMU crashed unexpected")
s.resultAbort = engines.ErrSandboxTerminated
})
}
func (s *sandbox) WaitForResult() (engines.ResultSet, error) {
s.resolve.Wait()
return s.resultSet, s.resultError
}
func (s *sandbox) Abort() error {
s.resolve.Do(func() {
// Kill all shells
s.sessions.AbortSessions()
// Abort the VM
s.vm.Kill()
s.resultError = engines.ErrSandboxAborted
})
s.resolve.Wait()
return s.resultAbort
}
func (s *sandbox) NewShell(command []string, tty bool) (engines.Shell, error) {
return s.sessions.NewShell(command, tty)
}
const qemuDisplayName = "screen"
func (s *sandbox) ListDisplays() ([]engines.Display, error) {
select {
case <-s.vm.Done:
return nil, engines.ErrSandboxTerminated
default:
img, _ := s.vm.Screenshot()
w := 0
h := 0
if img != nil {
w = img.Bounds().Size().X
h = img.Bounds().Size().Y
}
return []engines.Display{
{
Name: qemuDisplayName,
Description: "Primary screen attached to the virtual machine",
Width: w,
Height: h,
},
}, nil
}
}
func (s *sandbox) OpenDisplay(name string) (io.ReadWriteCloser, error) {
if name != qemuDisplayName {
return nil, engines.ErrNoSuchDisplay
}
return s.sessions.OpenDisplay()
}