-
Notifications
You must be signed in to change notification settings - Fork 544
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17671 from IRONICBo/feature/implement-vm-rescue
[WIP]feat(region): add guest rescue api
- Loading branch information
Showing
19 changed files
with
854 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// 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 compute | ||
|
||
// Rescue constants are used for rescue mode | ||
const ( | ||
GUEST_RESCUE_RELATIVE_PATH = "rescue" // serverxxx/rescue | ||
|
||
GUEST_RESCUE_INITRAMFS = "initramfs" | ||
GUEST_RESCUE_KERNEL = "kernel" | ||
GUEST_RESCUE_INITRAMFS_ARM64 = "initramfs_aarch64" | ||
GUEST_RESCUE_KERNEL_ARM64 = "kernel_aarch64" | ||
|
||
GUEST_RESCUE_SYS_DISK_NAME = "sys_img" | ||
GUEST_RESCUE_SYS_DISK_SIZE = 500 // MB | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
// 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 models | ||
|
||
import ( | ||
"context" | ||
|
||
"yunion.io/x/jsonutils" | ||
"yunion.io/x/pkg/errors" | ||
"yunion.io/x/pkg/utils" | ||
|
||
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/httperrors" | ||
"yunion.io/x/onecloud/pkg/mcclient" | ||
) | ||
|
||
func (self *SGuest) PerformStartRescue(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, | ||
data jsonutils.JSONObject) (jsonutils.JSONObject, error) { | ||
if !utils.IsInStringArray(self.Status, []string{api.VM_READY, api.VM_RUNNING}) { | ||
return nil, httperrors.NewInvalidStatusError("guest status must be ready or running") | ||
} | ||
|
||
// Check vmem size, need to be greater than 2G | ||
if self.VmemSize < 2048 { | ||
return nil, httperrors.NewInvalidStatusError("vmem size must be greater than 2G") | ||
} | ||
|
||
// Reset index | ||
disks, err := self.GetGuestDisks() | ||
if err != nil || len(disks) < 1 { | ||
return nil, httperrors.NewInvalidStatusError("guest.GetGuestDisks: %s", err.Error()) | ||
} | ||
for i := 0; i < len(disks); i++ { | ||
if disks[i].BootIndex >= 0 { | ||
// Move to next index, and easy to rollback | ||
err = disks[i].SetBootIndex(disks[i].BootIndex + 1) | ||
if err != nil { | ||
return nil, httperrors.NewInvalidStatusError("guest.SetBootIndex: %s", err.Error()) | ||
} | ||
} | ||
} | ||
|
||
// Get baremetal agent | ||
host, err := self.GetHost() | ||
if err != nil { | ||
return nil, httperrors.NewInvalidStatusError("guest.GetHost: %s", err.Error()) | ||
} | ||
bmAgent := BaremetalagentManager.GetAgent(api.AgentTypeBaremetal, host.ZoneId) | ||
if bmAgent == nil { | ||
return nil, httperrors.NewInvalidStatusError("BaremetalagentManager.GetAgent: %s", "Baremetal agent not found") | ||
} | ||
|
||
// Set available baremetal agent managerURi to data | ||
dataDict := data.(*jsonutils.JSONDict) | ||
dataDict.Add(jsonutils.NewString(bmAgent.ManagerUri), "manager_uri") | ||
|
||
// Start rescue vm task | ||
err = self.StartRescueTask(ctx, userCred, dataDict, "") | ||
if err != nil { | ||
return nil, httperrors.NewInvalidStatusError("guest.StartGuestRescueTask: %s", err.Error()) | ||
} | ||
|
||
// Now it only support kvm guest os rescue | ||
return nil, nil | ||
} | ||
|
||
func (self *SGuest) PerformStopRescue(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, | ||
data jsonutils.JSONObject) (jsonutils.JSONObject, error) { | ||
if !self.RescueMode { | ||
return nil, httperrors.NewInvalidStatusError("guest is not in rescue mode") | ||
} | ||
|
||
// Recover index | ||
disks, err := self.GetGuestDisks() | ||
if err != nil || len(disks) < 1 { | ||
return nil, httperrors.NewInvalidStatusError("guest.GetGuestDisks: %s", err.Error()) | ||
} | ||
for i := 0; i < len(disks); i++ { | ||
if disks[i].BootIndex >= 0 { | ||
// Rollback index | ||
err = disks[i].SetBootIndex(disks[i].BootIndex - 1) | ||
if err != nil { | ||
return nil, httperrors.NewInvalidStatusError("guest.SetBootIndex: %s", err.Error()) | ||
} | ||
} | ||
} | ||
|
||
// Start rescue vm task | ||
err = self.StopRescueTask(ctx, userCred, data.(*jsonutils.JSONDict), "") | ||
if err != nil { | ||
return nil, httperrors.NewInvalidStatusError("guest.StopGuestRescueTask: %s", err.Error()) | ||
} | ||
|
||
// Now it only support kvm guest os rescue | ||
return nil, nil | ||
} | ||
|
||
func (self *SGuest) UpdateRescueMode(mode bool) error { | ||
_, err := db.Update(self, func() error { | ||
self.RescueMode = mode | ||
return nil | ||
}) | ||
if err != nil { | ||
return errors.Wrap(err, "Update RescueMode") | ||
} | ||
return nil | ||
} | ||
|
||
func (self *SGuest) StartRescueTask(ctx context.Context, userCred mcclient.TokenCredential, data *jsonutils.JSONDict, parentTaskId string) error { | ||
// Now only support KVM | ||
taskName := "StartRescueTask" | ||
task, err := taskman.TaskManager.NewTask(ctx, taskName, self, userCred, data, parentTaskId, "", nil) | ||
if err != nil { | ||
return err | ||
} | ||
err = task.ScheduleRun(nil) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (self *SGuest) StopRescueTask(ctx context.Context, userCred mcclient.TokenCredential, data *jsonutils.JSONDict, parentTaskId string) error { | ||
taskName := "StopRescueTask" | ||
task, err := taskman.TaskManager.NewTask(ctx, taskName, self, userCred, data, parentTaskId, "", nil) | ||
if err != nil { | ||
return err | ||
} | ||
err = task.ScheduleRun(nil) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.