Files
cloudpods/pkg/compute/tasks/host_guests_migrate_task.go
Qiu Jian 77c4a53059 feature: 1. ak/sk auth support 2. a full feature s3gateway works with
Cyberduck

注意:以下两个文件的修改还未被官方合并,所以下次make
mod的时候会被覆盖,需要注意checkout恢复:

vendor/github.com/Azure/azure-sdk-for-go/storage/blockblob.go
vendor/github.com/tencentyun/cos-go-sdk-v5/object_part.go
2019-08-30 00:12:39 +08:00

94 lines
2.6 KiB
Go

// 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 tasks
import (
"context"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
api "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
"yunion.io/x/onecloud/pkg/compute/models"
)
func init() {
taskman.RegisterTask(HostGuestsMigrateTask{})
}
type HostGuestsMigrateTask struct {
taskman.STask
}
func (self *HostGuestsMigrateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
guests := make([]*api.GuestBatchMigrateParams, 0)
err := self.Params.Unmarshal(&guests, "guests")
if err != nil {
self.SetStageFailed(ctx, err.Error())
return
}
preferHostId, _ := self.Params.GetString("prefer_host_id")
var guestMigrating bool
var migrateIndex int
for i := 0; i < len(guests); i++ {
guest := models.GuestManager.FetchGuestById(guests[i].Id)
if guests[i].LiveMigrate {
err := guest.StartGuestLiveMigrateTask(
ctx, self.UserCred, guests[i].OldStatus, preferHostId, self.Id)
if err != nil {
log.Errorln(err)
continue
} else {
guestMigrating = true
migrateIndex = i
break
}
} else {
err := guest.StartMigrateTask(ctx, self.UserCred, guests[i].RescueMode,
false, guests[i].OldStatus, preferHostId, self.Id)
if err != nil {
log.Errorln(err)
continue
} else {
guestMigrating = true
migrateIndex = i
break
}
}
}
if !guestMigrating {
if jsonutils.QueryBoolean(self.Params, "some_guest_migrate_failed", false) {
self.SetStageFailed(ctx, "some guest migrate failed")
} else {
self.SetStageComplete(ctx, nil)
}
} else {
guests := append(guests[:migrateIndex], guests[migrateIndex+1:]...)
params := jsonutils.NewDict()
params.Set("guests", jsonutils.Marshal(guests))
self.SaveParams(params)
}
}
func (self *HostGuestsMigrateTask) OnInitFailed(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
kwargs := jsonutils.NewDict()
kwargs.Set("some_guest_migrate_failed", jsonutils.JSONTrue)
self.SaveParams(kwargs)
self.OnInit(ctx, obj, data)
}