Skip to content

Commit

Permalink
fix(region): convert to esxi fill network params (#20494)
Browse files Browse the repository at this point in the history
Deploy converted guest after guest created
  • Loading branch information
wanyaoqi authored Jun 16, 2024
1 parent ddee083 commit 4a59da5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
19 changes: 19 additions & 0 deletions pkg/compute/models/guest_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ func (self *SGuest) ConvertCloudpodsToKvm(ctx context.Context, userCred mcclient
createInput.Networks[i].Network = data.Networks[i].Network
createInput.Networks[i].Address = data.Networks[i].Address
createInput.Networks[i].Schedtags = data.Networks[i].Schedtags
} else {
createInput.Networks[i].Network = ""
createInput.Networks[i].Address = ""
createInput.Networks[i].Schedtags = nil
}
}

Expand Down Expand Up @@ -133,6 +137,21 @@ func (self *SGuest) ConvertEsxiToKvm(ctx context.Context, userCred mcclient.Toke
if err != nil {
return nil, errors.Wrap(err, "create converted server")
}

if data.Networks != nil && len(data.Networks) != len(createInput.Networks) {
return nil, httperrors.NewInputParameterError("input network configs length must equal guestnetworks length")
}

for i := 0; i < len(createInput.Networks); i++ {
createInput.Networks[i].Network = ""
createInput.Networks[i].Wire = ""
if data.Networks != nil {
createInput.Networks[i].Network = data.Networks[i].Network
createInput.Networks[i].Address = data.Networks[i].Address
createInput.Networks[i].Schedtags = data.Networks[i].Schedtags
}
}

return nil, self.StartConvertToKvmTask(ctx, userCred, "GuestConvertEsxiToKvmTask", preferHost, newGuest, createInput)
}

Expand Down
41 changes: 36 additions & 5 deletions pkg/compute/tasks/guest_convert_esxi_to_kvm_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/util/httputils"
"yunion.io/x/pkg/utils"

Expand All @@ -46,6 +47,13 @@ func (task *GuestConvertEsxiToKvmTask) OnInit(ctx context.Context, obj db.IStand
func (task *GuestConvertEsxiToKvmTask) GetSchedParams() (*schedapi.ScheduleInput, error) {
obj := task.GetObject()
guest := obj.(*models.SGuest)

input := new(api.ServerCreateInput)
err := task.Params.Unmarshal(input, "input")
if err != nil {
return nil, errors.Wrap(err, "failed unmarshal create input")
}

schedDesc := guest.ToSchedDesc()
if task.Params.Contains("prefer_host_id") {
preferHostId, _ := task.Params.GetString("prefer_host_id")
Expand All @@ -56,6 +64,7 @@ func (task *GuestConvertEsxiToKvmTask) GetSchedParams() (*schedapi.ScheduleInput
schedDesc.Disks[i].Medium = ""
schedDesc.Disks[i].Storage = ""
}
schedDesc.Networks = input.Networks
schedDesc.Hypervisor = api.HYPERVISOR_KVM
return schedDesc, nil
}
Expand Down Expand Up @@ -129,6 +138,19 @@ func (task *GuestConvertEsxiToKvmTask) SaveScheduleResult(ctx context.Context, o
input = guest.ToCreateInput(ctx, task.UserCred)
}

err = task.Params.Unmarshal(input, "input")
if err != nil {
log.Errorf("fail to unmarshal params input")
task.taskFailed(ctx, guest, jsonutils.NewString(fmt.Sprintf("umarshal create input %s", err)))
return
}

err = targetGuest.CreateNetworksOnHost(ctx, task.UserCred, host, input.Networks, nil, nil, target.Nets)
if err != nil {
task.taskFailed(ctx, guest, jsonutils.NewString(fmt.Sprintf("guest create networks %s", err)))
return
}

//pendingUsage.Storage = guest.GetDisksSize()
err = targetGuest.CreateDisksOnHost(ctx, task.UserCred, host, input.Disks, nil,
true, true, target.Disks, nil, true)
Expand Down Expand Up @@ -185,11 +207,11 @@ func (task *GuestConvertEsxiToKvmTask) OnHostCreateGuest(
}
db.OpsLog.LogEvent(disk, db.ACT_ALLOCATE, disk.GetShortDesc(ctx), task.UserCred)
}
if err := guest.ConvertEsxiNetworks(targetGuest); err != nil {
task.taskFailed(ctx, guest, jsonutils.NewString(err.Error()))
return
}
task.TaskComplete(ctx, guest, targetGuest)

task.SetStage("OnGuestConvertDoDeployGuest", nil)
deployParams := jsonutils.NewDict()
deployParams.Set("reset_password", jsonutils.JSONFalse)
targetGuest.StartGuestDeployTask(ctx, task.UserCred, deployParams, "deploy", task.GetTaskId())
}

func (task *GuestConvertEsxiToKvmTask) OnHostCreateGuestFailed(
Expand All @@ -198,6 +220,15 @@ func (task *GuestConvertEsxiToKvmTask) OnHostCreateGuestFailed(
task.taskFailed(ctx, guest, data)
}

func (task *GuestConvertEsxiToKvmTask) OnGuestConvertDoDeployGuest(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
targetGuest := task.getTargetGuest()
task.TaskComplete(ctx, guest, targetGuest)
}

func (task *GuestConvertEsxiToKvmTask) OnGuestConvertDoDeployGuestFailed(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
task.taskFailed(ctx, guest, data)
}

func (task *GuestConvertEsxiToKvmTask) TaskComplete(ctx context.Context, guest, targetGuest *models.SGuest) {
guest.SetMetadata(ctx, api.SERVER_META_CONVERTED_SERVER, targetGuest.Id, task.UserCred)
guest.SetStatus(ctx, task.UserCred, api.VM_CONVERTED, "")
Expand Down
6 changes: 0 additions & 6 deletions pkg/compute/tasks/guest_delete_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,6 @@ func (self *GuestDeleteTask) OnStartEipDissociate(ctx context.Context, guest *mo
sourceGuest := models.GuestManager.FetchGuestById(sourceGuestId)
if sourceGuest != nil &&
sourceGuest.GetMetadata(ctx, api.SERVER_META_CONVERTED_SERVER, self.UserCred) == guest.Id {
err := guest.ConvertEsxiNetworks(sourceGuest)
if err != nil {
log.Errorf("Convert networks failed %s", err)
self.OnFailed(ctx, guest, jsonutils.NewString(err.Error()))
return
}
sourceGuest.RemoveMetadata(ctx, api.SERVER_META_CONVERTED_SERVER, self.UserCred)
sourceGuest.StartSyncstatus(ctx, self.UserCred, "")
}
Expand Down

0 comments on commit 4a59da5

Please sign in to comment.