This commit is contained in:
wanyaoqi
2019-01-22 16:56:47 +08:00
committed by Zexi Li
parent 2671ae6892
commit 1451b1aeb8
4 changed files with 96 additions and 27 deletions
+24 -13
View File
@@ -19,6 +19,8 @@ import (
"yunion.io/x/onecloud/pkg/cloudcommon/workmanager"
"yunion.io/x/onecloud/pkg/hostman/guestfs"
"yunion.io/x/onecloud/pkg/httperrors"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/mcclient/auth"
"yunion.io/x/pkg/util/regutils"
"yunion.io/x/pkg/util/seclib"
)
@@ -338,16 +340,6 @@ func (m *SGuestManager) GetFreeVncPort() int64 {
return port
}
func initGuestManager(serversPath string) {
if guestManger == nil {
guestManger = NewGuestManager(serversPath)
}
}
func GetGuestManager() *SGuestManager {
return guestManger
}
func Stop() {
// guestManger.ExitGuestCleanup()
}
@@ -356,13 +348,32 @@ func Init(serversPath string) {
initGuestManager(serversPath)
}
var guestManger *SGuestManager
var wm *workmanager.SWorkManager
var session *mcclient.ClientSession
func SetClientSession() *mcclient.ClientSession {
session = auth.GetAdminSession(o.Options.Region, "v2")
}
func GetClinetSession() *mcclient.ClientSession {
return session
}
func GetGuestManager() *SGuestManager {
return guestManger
}
func initGuestManager(serversPath string) {
if guestManger == nil {
guestManger = NewGuestManager(serversPath)
}
}
func GetWorkManager() *workmanager.SWorkManager {
return wm
}
var guestManger *SGuestManager
var wm *workmanager.SWorkManager
func init() {
wm = workmanager.NewWorkManger()
}
+54
View File
@@ -0,0 +1,54 @@
package guestman
import (
"context"
"time"
"yunion.io/x/onecloud/pkg/cloudcommon"
"yunion.io/x/onecloud/pkg/cloudcommon/httpclients"
)
type SGuestStopTask struct {
*SKVMGuestInstance
ctx context.Context
timeout int64
startPowerdown *time.Time
}
func NewGuestStopTask(guest *SKVMGuestInstance, ctx context.Context, timeout int64) *SGuestStopTask {
return &SGuestStopTask{
SKVMGuestInstance: guest,
ctx: ctx,
timeout: timeout,
startPowerdown: nil,
}
}
func (s *SGuestStopTask) Start() {
if s.IsRunning() && s.IsMonitorAlive() {
// Do Powerdown,
s.monitor.SimpleCommand("system_powerdown", s.onPowerdownGuest)
} else {
s.CheckGuestRunningLater()
}
}
func (s *SGuestStopTask) onPowerdownGuest(results string) {
s.ExitCleanup(true)
s.startPowerdown = &time.Now()
s.checkGuestRunning()
}
func (s *SGuestStopTask) checkGuestRunning() {
if !s.IsRunning() || time.Now().Sub(*s.startPowerdown) > (s.timeout*time.Duration) {
// force stop
s.Stop()
httpclients.TaskComplete(s.ctx, nil)
} else {
s.CheckGuestRunningLater()
}
}
func (s *SGuestStopTask) CheckGuestRunningLater() {
cloudcommon.AddTimeout(time.Second*1, s.checkGuestRunning())
}
+18 -7
View File
@@ -503,13 +503,24 @@ func (s *SKVMGuestInstance) Delete(ctx context.Context, migrated bool) error {
return exec.Command("rm", "-rf", s.HomeDir()).Run()
}
// ================ Guest Stop Task ==================
func (s *SKVMGuestInstance) ExecStopTask(ctx context.Context, timeout int64) {
if s.IsRunning() && s.IsMonitorAlive() {
// Do Powerdown,
s.monitor.SimpleCommand("system_powerdown", s.OnPowerdownGuest)
func (s *SKVMGuestInstance) Stop() bool {
s.ExitCleanup(true)
if s.scriptStop() {
return true
} else {
s.CheckGuestRunningLater()
return false
}
}
func (s *SKVMGuestInstance) scriptStop() bool {
err := exec.Command("sh", s.GetStopScriptPath()).Run()
if err != nil {
log.Errorln(err)
return false
}
return true
}
func (s *SKVMGuestInstance) ExecStopTask(ctx context.Context, timeout int64) {
NewGuestStopTask(s, ctx, timeout).Start()
}
-7
View File
@@ -1,7 +0,0 @@
package guestman
import "yunion.io/x/jsonutils"
func DoTask(guest *SKVMGuestInstance, kwargs jsonutils.JSONObject) {
}