/
migrate_instance.go
284 lines (234 loc) · 8.44 KB
/
migrate_instance.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package main
import (
"context"
"fmt"
"io"
"net/url"
"os/exec"
"strings"
"time"
"github.com/lxc/lxd/lxd/instance"
"github.com/lxc/lxd/lxd/instance/instancetype"
"github.com/lxc/lxd/lxd/instance/operationlock"
"github.com/lxc/lxd/lxd/migration"
"github.com/lxc/lxd/lxd/operations"
"github.com/lxc/lxd/lxd/state"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/api"
"github.com/lxc/lxd/shared/logger"
)
func newMigrationSource(inst instance.Instance, stateful bool, instanceOnly bool, allowInconsistent bool, clusterMoveSourceName string, pushTarget *api.InstancePostTarget) (*migrationSourceWs, error) {
ret := migrationSourceWs{
migrationFields: migrationFields{
instance: inst,
allowInconsistent: allowInconsistent,
},
clusterMoveSourceName: clusterMoveSourceName,
}
if pushTarget != nil {
ret.pushCertificate = pushTarget.Certificate
ret.pushOperationURL = pushTarget.Operation
ret.pushSecrets = pushTarget.Websockets
}
ret.instanceOnly = instanceOnly
secretNames := []string{api.SecretNameControl, api.SecretNameFilesystem}
if stateful && inst.IsRunning() {
if inst.Type() == instancetype.Container {
_, err := exec.LookPath("criu")
if err != nil {
return nil, migration.ErrNoLiveMigrationSource
}
}
ret.live = true
secretNames = append(secretNames, api.SecretNameState)
}
ret.conns = make(map[string]*migrationConn, len(secretNames))
for _, connName := range secretNames {
if ret.pushOperationURL != "" {
if ret.pushSecrets[connName] == "" {
return nil, fmt.Errorf("Expected %q connection secret missing from migration source target request", connName)
}
dialer, err := setupWebsocketDialer(ret.pushCertificate)
if err != nil {
return nil, fmt.Errorf("Failed setting up websocket dialer for migration source %q connection: %w", connName, err)
}
u, err := url.Parse(fmt.Sprintf("wss://%s/websocket", strings.TrimPrefix(ret.pushOperationURL, "https://")))
if err != nil {
return nil, fmt.Errorf("Failed parsing websocket URL for migration source %q connection: %w", connName, err)
}
ret.conns[connName] = newMigrationConn(ret.pushSecrets[connName], dialer, u)
} else {
secret, err := shared.RandomCryptoString()
if err != nil {
return nil, fmt.Errorf("Failed creating migration source secret for %q connection: %w", connName, err)
}
ret.conns[connName] = newMigrationConn(secret, nil, nil)
}
}
return &ret, nil
}
func (s *migrationSourceWs) Do(state *state.State, migrateOp *operations.Operation) error {
l := logger.AddContext(logger.Ctx{"project": s.instance.Project().Name, "instance": s.instance.Name(), "live": s.live, "clusterMoveSourceName": s.clusterMoveSourceName, "push": s.pushOperationURL != ""})
ctx, cancel := context.WithTimeout(context.TODO(), time.Second*10)
defer cancel()
l.Info("Waiting for migration control connection on source")
_, err := s.conns[api.SecretNameControl].WebSocket(ctx)
if err != nil {
return fmt.Errorf("Failed waiting for migration control connection on source: %w", err)
}
l.Info("Migration control connection established on source")
defer l.Info("Migration channels disconnected on source")
defer s.disconnect()
stateConnFunc := func(ctx context.Context) (io.ReadWriteCloser, error) {
conn := s.conns[api.SecretNameState]
if conn == nil {
return nil, fmt.Errorf("Migration source control connection not initialized")
}
wsConn, err := conn.WebsocketIO(ctx)
if err != nil {
return nil, fmt.Errorf("Failed getting migration source control connection: %w", err)
}
return wsConn, nil
}
filesystemConnFunc := func(ctx context.Context) (io.ReadWriteCloser, error) {
conn := s.conns[api.SecretNameFilesystem]
if conn == nil {
return nil, fmt.Errorf("Migration source filesystem connection not initialized")
}
wsConn, err := conn.WebsocketIO(ctx)
if err != nil {
return nil, fmt.Errorf("Failed getting migration source filesystem connection: %w", err)
}
return wsConn, nil
}
s.instance.SetOperation(migrateOp)
err = s.instance.MigrateSend(instance.MigrateSendArgs{
MigrateArgs: instance.MigrateArgs{
ControlSend: s.send,
ControlReceive: s.recv,
StateConn: stateConnFunc,
FilesystemConn: filesystemConnFunc,
Snapshots: !s.instanceOnly,
Live: s.live,
Disconnect: func() {
for connName, conn := range s.conns {
if connName != api.SecretNameControl {
conn.Close()
}
}
},
ClusterMoveSourceName: s.clusterMoveSourceName,
},
AllowInconsistent: s.allowInconsistent,
})
if err != nil {
l.Error("Failed migration on source", logger.Ctx{"err": err})
return fmt.Errorf("Failed migration on source: %w", err)
}
return nil
}
func newMigrationSink(args *migrationSinkArgs) (*migrationSink, error) {
sink := migrationSink{
migrationFields: migrationFields{
instance: args.Instance,
instanceOnly: args.InstanceOnly,
live: args.Live,
},
url: args.URL,
clusterMoveSourceName: args.ClusterMoveSourceName,
push: args.Push,
refresh: args.Refresh,
}
secretNames := []string{api.SecretNameControl, api.SecretNameFilesystem}
if sink.live {
if sink.instance.Type() == instancetype.Container {
_, err := exec.LookPath("criu")
if err != nil {
return nil, migration.ErrNoLiveMigrationTarget
}
}
secretNames = append(secretNames, api.SecretNameState)
}
sink.conns = make(map[string]*migrationConn, len(secretNames))
for _, connName := range secretNames {
if !sink.push {
if args.Secrets[connName] == "" {
return nil, fmt.Errorf("Expected %q connection secret missing from migration sink target request", connName)
}
u, err := url.Parse(fmt.Sprintf("wss://%s/websocket", strings.TrimPrefix(args.URL, "https://")))
if err != nil {
return nil, fmt.Errorf("Failed parsing websocket URL for migration sink %q connection: %w", connName, err)
}
sink.conns[connName] = newMigrationConn(args.Secrets[connName], args.Dialer, u)
} else {
secret, err := shared.RandomCryptoString()
if err != nil {
return nil, fmt.Errorf("Failed creating migration sink secret for %q connection: %w", connName, err)
}
sink.conns[connName] = newMigrationConn(secret, nil, nil)
}
}
return &sink, nil
}
func (c *migrationSink) Do(state *state.State, instOp *operationlock.InstanceOperation) error {
l := logger.AddContext(logger.Ctx{"project": c.instance.Project().Name, "instance": c.instance.Name(), "live": c.live, "clusterMoveSourceName": c.clusterMoveSourceName, "push": c.push})
ctx, cancel := context.WithTimeout(context.TODO(), time.Second*10)
defer cancel()
l.Info("Waiting for migration control connection on target")
_, err := c.conns[api.SecretNameControl].WebSocket(ctx)
if err != nil {
return fmt.Errorf("Failed waiting for migration control connection on target: %w", err)
}
l.Info("Migration control connection established on target")
defer l.Info("Migration channels disconnected on target")
if c.push {
defer c.disconnect()
}
stateConnFunc := func(ctx context.Context) (io.ReadWriteCloser, error) {
conn := c.conns[api.SecretNameState]
if conn == nil {
return nil, fmt.Errorf("Migration target control connection not initialized")
}
wsConn, err := conn.WebsocketIO(ctx)
if err != nil {
return nil, fmt.Errorf("Failed getting migration target control connection: %w", err)
}
return wsConn, nil
}
filesystemConnFunc := func(ctx context.Context) (io.ReadWriteCloser, error) {
conn := c.conns[api.SecretNameFilesystem]
if conn == nil {
return nil, fmt.Errorf("Migration target filesystem connection not initialized")
}
wsConn, err := conn.WebsocketIO(ctx)
if err != nil {
return nil, fmt.Errorf("Failed getting migration target filesystem connection: %w", err)
}
return wsConn, nil
}
err = c.instance.MigrateReceive(instance.MigrateReceiveArgs{
MigrateArgs: instance.MigrateArgs{
ControlSend: c.send,
ControlReceive: c.recv,
StateConn: stateConnFunc,
FilesystemConn: filesystemConnFunc,
Snapshots: !c.instanceOnly,
Live: c.live,
Disconnect: func() {
for connName, conn := range c.conns {
if connName != api.SecretNameControl {
conn.Close()
}
}
},
ClusterMoveSourceName: c.clusterMoveSourceName,
},
InstanceOperation: instOp,
Refresh: c.refresh,
})
if err != nil {
l.Error("Failed migration on target", logger.Ctx{"err": err})
return fmt.Errorf("Failed migration on target: %w", err)
}
return nil
}