/
virtualization.go
339 lines (303 loc) · 12.1 KB
/
virtualization.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package guestdrivers
import (
"context"
"fmt"
"regexp"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/util/netutils"
api "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/cloudcommon/db/quotas"
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
"yunion.io/x/onecloud/pkg/compute/models"
"yunion.io/x/onecloud/pkg/httperrors"
"yunion.io/x/onecloud/pkg/mcclient"
)
type SVirtualizedGuestDriver struct {
SBaseGuestDriver
}
func (d *SVirtualizedGuestDriver) DoScheduleSKUFilter() bool {
return false
}
func (self *SVirtualizedGuestDriver) GetMaxVCpuCount() int {
return 128
}
func (self *SVirtualizedGuestDriver) GetMaxVMemSizeGB() int {
return 512
}
func (self *SVirtualizedGuestDriver) PrepareDiskRaidConfig(userCred mcclient.TokenCredential, host *models.SHost, params []*api.BaremetalDiskConfig, disks []*api.DiskConfig) ([]*api.DiskConfig, error) {
// do nothing
return nil, nil
}
func (self *SVirtualizedGuestDriver) GetNamedNetworkConfiguration(guest *models.SGuest, ctx context.Context, userCred mcclient.TokenCredential, host *models.SHost, netConfig *api.NetworkConfig) (*models.SNetwork, []models.SNicConfig, api.IPAllocationDirection, bool) {
net, _ := host.GetNetworkWithId(netConfig.Network, netConfig.Reserved)
nicConfs := []models.SNicConfig{
{
Mac: netConfig.Mac,
Index: -1,
Ifname: netConfig.Ifname,
},
}
if netConfig.RequireTeaming || netConfig.TryTeaming {
nicConfs = append(nicConfs, models.SNicConfig{
Mac: "",
Index: -1,
Ifname: "",
})
}
return net, nicConfs, api.IPAllocationStepdown, false
}
func (self *SVirtualizedGuestDriver) GetRandomNetworkTypes() []string {
return []string{api.NETWORK_TYPE_GUEST}
}
func (self *SVirtualizedGuestDriver) wireAvaiableForGuest(guest *models.SGuest, wire *models.SWire) (bool, error) {
if guest.BackupHostId == "" {
return true, nil
} else {
backupHost := models.HostManager.FetchHostById(guest.BackupHostId)
count, err := backupHost.GetWiresQuery().Equals("wire_id", wire.Id).CountWithError()
if err != nil {
return false, errors.Wrap(err, "query host wire")
}
return count > 0, nil
}
}
func (self *SVirtualizedGuestDriver) Attach2RandomNetwork(guest *models.SGuest, ctx context.Context, userCred mcclient.TokenCredential, host *models.SHost, netConfig *api.NetworkConfig, pendingUsage quotas.IQuota) ([]models.SGuestnetwork, error) {
var wirePattern *regexp.Regexp
if len(netConfig.Wire) > 0 {
wirePattern = regexp.MustCompile(netConfig.Wire)
}
hostwires := host.GetHostwires()
netsAvaiable := make([]models.SNetwork, 0)
netTypes := guest.GetDriver().GetRandomNetworkTypes()
if len(netConfig.NetType) > 0 {
netTypes = []string{netConfig.NetType}
}
for i := 0; i < len(hostwires); i += 1 {
hostwire := hostwires[i]
wire := hostwire.GetWire()
if ok, err := self.wireAvaiableForGuest(guest, wire); err != nil {
return nil, err
} else if !ok {
continue
}
if wire == nil {
log.Errorf("host wire is nil?????")
continue
}
// !!
if wirePattern != nil && !wirePattern.MatchString(wire.Id) && !wirePattern.MatchString(wire.Name) {
continue
}
var net *models.SNetwork
if netConfig.Private {
net, _ = wire.GetCandidatePrivateNetwork(userCred, models.NetworkManager.AllowScope(userCred), netConfig.Exit, netTypes)
} else {
net, _ = wire.GetCandidateAutoAllocNetwork(userCred, models.NetworkManager.AllowScope(userCred), netConfig.Exit, netTypes)
}
if net != nil {
netsAvaiable = append(netsAvaiable, *net)
}
}
if len(netsAvaiable) == 0 {
return nil, fmt.Errorf("No appropriate host virtual network...")
}
if len(netConfig.Address) > 0 {
addr, _ := netutils.NewIPV4Addr(netConfig.Address)
netsAvaiableForAddr := make([]models.SNetwork, 0)
for i := range netsAvaiable {
if netsAvaiable[i].IsAddressInRange(addr) {
netsAvaiableForAddr = append(netsAvaiableForAddr, netsAvaiable[i])
}
}
if len(netsAvaiableForAddr) == 0 {
if netConfig.RequireDesignatedIP {
return nil, fmt.Errorf("No virtual network for IP %s", netConfig.Address)
}
} else {
netsAvaiable = netsAvaiableForAddr
}
}
selNet := models.ChooseCandidateNetworks(netsAvaiable, netConfig.Exit, netTypes)
if selNet == nil {
return nil, fmt.Errorf("Not enough address in virtual network")
}
nicConfs := make([]models.SNicConfig, 1)
nicConfs[0] = models.SNicConfig{
Mac: netConfig.Mac,
Index: -1,
Ifname: netConfig.Ifname,
}
if netConfig.RequireTeaming || netConfig.TryTeaming {
nicConf := models.SNicConfig{
Mac: "",
Index: -1,
Ifname: "",
}
nicConfs = append(nicConfs, nicConf)
}
gn, err := guest.Attach2Network(ctx, userCred, selNet, pendingUsage, netConfig.Address, netConfig.Driver, netConfig.BwLimit, netConfig.Vip, netConfig.Reserved, api.IPAllocationDefault, netConfig.RequireDesignatedIP, false, nicConfs)
return gn, err
}
func (self *SVirtualizedGuestDriver) GetStorageTypes() []string {
return nil
}
func (self *SVirtualizedGuestDriver) ChooseHostStorage(host *models.SHost, backend string, storageIds []string) *models.SStorage {
if len(storageIds) == 0 {
return host.GetLeastUsedStorage(backend)
}
return models.StorageManager.FetchStorageById(storageIds[0])
}
func (self *SVirtualizedGuestDriver) RequestGuestCreateInsertIso(ctx context.Context, imageId string, guest *models.SGuest, task taskman.ITask) error {
return guest.StartInsertIsoTask(ctx, imageId, true, guest.HostId, task.GetUserCred(), task.GetTaskId())
}
func (self *SVirtualizedGuestDriver) StartGuestStopTask(guest *models.SGuest, ctx context.Context, userCred mcclient.TokenCredential, params *jsonutils.JSONDict, parentTaskId string) error {
taskName := "GuestStopTask"
if guest.BackupHostId != "" {
taskName = "HAGuestStopTask"
}
task, err := taskman.TaskManager.NewTask(ctx, taskName, guest, userCred, params, parentTaskId, "", nil)
if err != nil {
return err
}
task.ScheduleRun(nil)
return nil
}
func (self *SVirtualizedGuestDriver) StartGuestResetTask(guest *models.SGuest, ctx context.Context, userCred mcclient.TokenCredential, isHard bool, parentTaskId string) error {
var taskName = "GuestSoftResetTask"
if isHard {
taskName = "GuestHardResetTask"
}
task, err := taskman.TaskManager.NewTask(ctx, taskName, guest, userCred, nil, parentTaskId, "", nil)
if err != nil {
return err
}
task.ScheduleRun(nil)
return nil
}
func (self *SVirtualizedGuestDriver) RequestDeleteDetachedDisk(ctx context.Context, disk *models.SDisk, task taskman.ITask, isPurge bool) error {
return disk.StartDiskDeleteTask(ctx, task.GetUserCred(), task.GetTaskId(), isPurge,
jsonutils.QueryBoolean(task.GetParams(), "override_pending_delete", false), false)
}
func (self *SVirtualizedGuestDriver) StartGuestSyncstatusTask(guest *models.SGuest, ctx context.Context, userCred mcclient.TokenCredential, parentTaskId string) error {
return models.StartResourceSyncStatusTask(ctx, userCred, guest, "GuestSyncstatusTask", parentTaskId)
}
func (self *SVirtualizedGuestDriver) RequestStopGuestForDelete(ctx context.Context, guest *models.SGuest,
host *models.SHost, task taskman.ITask) error {
if host == nil {
host = guest.GetHost()
}
if host != nil && host.GetEnabled() && host.HostStatus == api.HOST_ONLINE {
return guest.StartGuestStopTask(ctx, task.GetUserCred(), true, task.GetTaskId())
}
if host != nil && !jsonutils.QueryBoolean(task.GetParams(), "purge", false) {
return fmt.Errorf("fail to contact host")
}
task.ScheduleRun(nil)
return nil
}
func (self *SVirtualizedGuestDriver) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, input *api.ServerCreateInput) (*api.ServerCreateInput, error) {
return input, nil
}
func (self *SVirtualizedGuestDriver) ValidateCreateDataOnHost(ctx context.Context, userCred mcclient.TokenCredential, bmName string, host *models.SHost, input *api.ServerCreateInput) (*api.ServerCreateInput, error) {
if host.HostStatus != api.HOST_ONLINE {
return nil, httperrors.NewInvalidStatusError("Host %s is not online", bmName)
}
input.PreferHost = host.Id
if host.IsPrepaidRecycle() {
input.VmemSize = host.MemSize
input.VcpuCount = int(host.CpuCount)
cnt, err := host.GetGuestCount()
if err != nil {
return nil, httperrors.NewInternalServerError("GetGuestCount fail %s", err)
}
if cnt >= 1 {
return nil, httperrors.NewInsufficientResourceError("host has been occupied")
}
}
return input, nil
}
func (self *SVirtualizedGuestDriver) PerformStart(ctx context.Context, userCred mcclient.TokenCredential, guest *models.SGuest, data *jsonutils.JSONDict) error {
return guest.StartGueststartTask(ctx, userCred, data, "")
}
func (self *SVirtualizedGuestDriver) CheckDiskTemplateOnStorage(ctx context.Context, userCred mcclient.TokenCredential, imageId string, format string, storageId string, task taskman.ITask) error {
storage := models.StorageManager.FetchStorageById(storageId)
if storage == nil {
return fmt.Errorf("No such storage?? %s", storageId)
}
cache := storage.GetStoragecache()
if cache == nil {
return fmt.Errorf("Cache is missing from storage")
}
return cache.StartImageCacheTask(ctx, userCred, imageId, format, false, task.GetTaskId())
}
func (self *SVirtualizedGuestDriver) CanKeepDetachDisk() bool {
return true
}
func (self *SVirtualizedGuestDriver) StartGuestDetachdiskTask(ctx context.Context, userCred mcclient.TokenCredential, guest *models.SGuest, params *jsonutils.JSONDict, parentTaskId string) error {
task, err := taskman.TaskManager.NewTask(ctx, "GuestDetachDiskTask", guest, userCred, params, parentTaskId, "", nil)
if err != nil {
return err
}
task.ScheduleRun(nil)
return nil
}
func (self *SVirtualizedGuestDriver) StartGuestAttachDiskTask(ctx context.Context, userCred mcclient.TokenCredential, guest *models.SGuest, params *jsonutils.JSONDict, parentTaskId string) error {
task, err := taskman.TaskManager.NewTask(ctx, "GuestAttachDiskTask", guest, userCred, params, parentTaskId, "", nil)
if err != nil {
return err
}
task.ScheduleRun(nil)
return nil
}
func (self *SVirtualizedGuestDriver) StartSuspendTask(ctx context.Context, userCred mcclient.TokenCredential, guest *models.SGuest, params *jsonutils.JSONDict, parentTaskId string) error {
task, err := taskman.TaskManager.NewTask(ctx, "GuestSuspendTask", guest, userCred, params, parentTaskId, "", nil)
if err != nil {
return err
}
task.ScheduleRun(nil)
return nil
}
func (self *SVirtualizedGuestDriver) StartResumeTask(ctx context.Context, userCred mcclient.TokenCredential,
guest *models.SGuest, params *jsonutils.JSONDict, parentTaskId string) error {
task, err := taskman.TaskManager.NewTask(ctx, "GuestResumeTask", guest, userCred, params, parentTaskId, "", nil)
if err != nil {
return err
}
task.ScheduleRun(nil)
return nil
}
func (self *SVirtualizedGuestDriver) StartGuestSaveImage(ctx context.Context, userCred mcclient.TokenCredential, guest *models.SGuest, params *jsonutils.JSONDict, parentTaskId string) error {
guest.SetStatus(userCred, api.VM_START_SAVE_DISK, "")
if task, err := taskman.TaskManager.NewTask(ctx, "GuestSaveImageTask", guest, userCred, params, parentTaskId, "", nil); err != nil {
return err
} else {
task.ScheduleRun(nil)
}
return nil
}
func (self *SVirtualizedGuestDriver) StartGuestSaveGuestImage(ctx context.Context, userCred mcclient.TokenCredential,
guest *models.SGuest, params *jsonutils.JSONDict, parentTaskId string) error {
guest.SetStatus(userCred, api.VM_START_SAVE_DISK, "")
if task, err := taskman.TaskManager.NewTask(ctx, "GuestSaveGuestImageTask", guest, userCred, params, parentTaskId,
"", nil); err != nil {
return err
} else {
task.ScheduleRun(nil)
}
return nil
}