mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-01 15:07:17 +08:00
remove httpclient
This commit is contained in:
@@ -1,90 +0,0 @@
|
||||
package httpclients
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/appctx"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/httpclients"
|
||||
"yunion.io/x/onecloud/pkg/hostman/options"
|
||||
)
|
||||
|
||||
type SComputeClient struct {
|
||||
SServiceClient
|
||||
}
|
||||
|
||||
func NewComputeClient(region, service, version string) *SComputeClient {
|
||||
return &SComputeClient{SServiceClient: *NewServiceClient(region, service, version)}
|
||||
}
|
||||
|
||||
var computeClients map[string]*SComputeClient
|
||||
|
||||
func init() {
|
||||
computeClients = make(map[string]*SComputeClient, 0)
|
||||
}
|
||||
|
||||
func GetComputeClient(region, version string) *SComputeClient {
|
||||
if len(version) == 0 {
|
||||
version = DEFAULT_VERSION
|
||||
}
|
||||
if len(region) == 0 {
|
||||
region = options.HostOptions.Region
|
||||
}
|
||||
if len(region) == 0 {
|
||||
return nil
|
||||
}
|
||||
cli, ok := computeClients[region+"-"+version]
|
||||
if !ok {
|
||||
log.Infof("Compute service client for region %s-%s initialized", region, version)
|
||||
cli = NewComputeClient(region, "compute", version)
|
||||
computeClients[region+"-"+version] = cli
|
||||
return cli
|
||||
}
|
||||
return cli
|
||||
}
|
||||
|
||||
func GetDefaultComputeClient() *SComputeClient {
|
||||
if len(options.HostOptions.Region) == 0 {
|
||||
return nil
|
||||
}
|
||||
cli, ok := computeClients[options.HostOptions.Region+"-"+DEFAULT_VERSION]
|
||||
if !ok {
|
||||
log.Infof("Compute service client for region %s-%s initialized",
|
||||
options.HostOptions.Region, DEFAULT_VERSION)
|
||||
cli = NewComputeClient(options.HostOptions.Region, "compute", DEFAULT_VERSION)
|
||||
|
||||
}
|
||||
return cli
|
||||
}
|
||||
|
||||
func (c *SComputeClient) UpdateServerStatus(sid, status string) {
|
||||
var url = fmt.Sprintf("/servers/%s/status", sid)
|
||||
var body = jsonutils.NewDict()
|
||||
var stus = jsonutils.NewDict()
|
||||
stus.Set("status", jsonutils.NewString(status))
|
||||
body.Set("server", stus)
|
||||
c.Request(context.Background(), "POST", url, nil, body, false)
|
||||
}
|
||||
|
||||
func TaskFailed(ctx context.Context, reason string) error {
|
||||
if taskId := ctx.Value(appctx.APP_CONTEXT_KEY_TASK_ID); taskId != nil {
|
||||
httpclients.GetDefaultComputeClient().TaskFail(ctx, taskId.(string), reason)
|
||||
return nil
|
||||
} else {
|
||||
log.Errorln("Reqeuest task failed missing task id, with reason(%s)", reason)
|
||||
return fmt.Errorf("Reqeuest task failed missing task id")
|
||||
}
|
||||
}
|
||||
|
||||
func TaskComplete(ctx context.Context, data jsonutils.JSONObject) error {
|
||||
if taskId := ctx.Value(appctx.APP_CONTEXT_KEY_TASK_ID); taskId != nil {
|
||||
httpclients.GetDefaultComputeClient().TaskComplete(ctx, taskId.(string), data, 0)
|
||||
return nil
|
||||
} else {
|
||||
log.Errorln("Reqeuest task complete missing task id")
|
||||
return fmt.Errorf("Reqeuest task complete missing task id")
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
package httpclients // import "yunion.io/x/onecloud/pkg/cloudcommon/httpclients"
|
||||
@@ -1,81 +0,0 @@
|
||||
package httpclients
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/auth"
|
||||
"yunion.io/x/onecloud/pkg/util/httputils"
|
||||
)
|
||||
|
||||
const (
|
||||
DEFAULT_VERSION = "v1"
|
||||
MAX_TRY_TIMES = 3
|
||||
)
|
||||
|
||||
type SServiceClient struct {
|
||||
client *http.Client
|
||||
region, service, version string
|
||||
}
|
||||
|
||||
func NewServiceClient(region, service, version string) *SServiceClient {
|
||||
return &SServiceClient{
|
||||
client: httputils.GetDefaultClient(),
|
||||
region: region,
|
||||
service: service,
|
||||
version: version,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *SServiceClient) Request(ctx context.Context, method string, urlStr string, header http.Header, body jsonutils.JSONObject, debug bool) (http.Header, jsonutils.JSONObject, error) {
|
||||
if _, ok := header["X-Auth-Token"]; !ok {
|
||||
token := auth.GetTokenString()
|
||||
if len(token) == 0 {
|
||||
return nil, nil, fmt.Errorf("Missing Auth Token")
|
||||
}
|
||||
header.Set("X-Auth-Token", token)
|
||||
}
|
||||
baseUrl, err := c.GetUrl()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
urlStr = baseUrl + urlStr
|
||||
return httputils.JSONRequest(c.client, ctx, method, urlStr, header, body, debug)
|
||||
}
|
||||
|
||||
func (c *SServiceClient) GetUrl() (string, error) {
|
||||
var service = c.service
|
||||
if c.version != DEFAULT_VERSION {
|
||||
service = fmt.Sprintf("%s_%s", service, c.version)
|
||||
}
|
||||
return auth.GetServiceURL(service, c.region, "", "")
|
||||
}
|
||||
|
||||
func (c *SServiceClient) TaskFail(ctx context.Context, taskId string, reason jsonutils.JSONObject) {
|
||||
body := jsonutils.NewDict()
|
||||
body.Set("__status__", jsonutils.NewString("error"))
|
||||
body.Set("__reason__", reason)
|
||||
c.TaskComplete(ctx, taskId, body, 0)
|
||||
}
|
||||
|
||||
func (c *SServiceClient) TaskComplete(ctx context.Context, taskId string, data jsonutils.JSONObject, tried int) {
|
||||
url := fmt.Sprintf("/tasks/%s", taskId)
|
||||
_, res, err := c.Request(ctx, "POST", url, nil, data, false)
|
||||
if err != nil {
|
||||
log.Errorf("Sync task complete fail %s", err)
|
||||
if tried < MAX_TRY_TIMES {
|
||||
time.Sleep(time.Second * 5)
|
||||
c.TaskComplete(ctx, taskId, data, tried+1)
|
||||
}
|
||||
} else {
|
||||
var output string
|
||||
if res != nil {
|
||||
output = res.String()
|
||||
}
|
||||
log.Infof("Sync task complete succ %s", output)
|
||||
}
|
||||
}
|
||||
@@ -7,13 +7,19 @@ import (
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/httpclients"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/appctx"
|
||||
)
|
||||
|
||||
type DelayTaskFunc func(ctx context.Context, params interface{}) (jsonutils.JSONObject, error)
|
||||
type DelayTaskFunc func(context.Context, interface{}) (jsonutils.JSONObject, error)
|
||||
type OnTaskFailed func(context.Context, string)
|
||||
type OnTaskCompleted func(context.Context, jsonutils.JSONObject)
|
||||
|
||||
type SWorkManager struct {
|
||||
curCount int32
|
||||
|
||||
onFailed OnTaskFailed
|
||||
onCompleted OnTaskCompleted
|
||||
}
|
||||
|
||||
func (w *SWorkManager) add() {
|
||||
@@ -28,63 +34,65 @@ func (w *SWorkManager) done() {
|
||||
// task complete will be called, otherwise called task failed
|
||||
// Params is interface for receive any type, task func should do type assert
|
||||
func (w *SWorkManager) DelayTask(ctx context.Context, task DelayTaskFunc, params interface{}) {
|
||||
if ctx == nil || ctx.Value(APP_CONTEXT_KEY_TASK_ID) == nil {
|
||||
w.DelayTaskWithoutTaskid(task, params)
|
||||
if ctx == nil || ctx.Value(appctx.APP_CONTEXT_KEY_TASK_ID) == nil {
|
||||
w.DelayTaskWithoutTask(ctx, task, params)
|
||||
return
|
||||
}
|
||||
|
||||
w.add()
|
||||
go func() {
|
||||
defer w.done()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Errorln("DelayTask panic: ", r)
|
||||
switch val := r.(type) {
|
||||
case string:
|
||||
httpclients.TaskFailed(ctx, val)
|
||||
case error:
|
||||
httpclients.TaskFailed(ctx, val.Error())
|
||||
default:
|
||||
httpclients.TaskFailed(ctx, "Unknown panic")
|
||||
} else {
|
||||
w.add()
|
||||
go func() {
|
||||
defer w.done()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Errorln("DelayTask panic: ", r)
|
||||
switch val := r.(type) {
|
||||
case string:
|
||||
w.onFailed(ctx, val)
|
||||
case error:
|
||||
w.onFailed(ctx, val.Error())
|
||||
default:
|
||||
w.onFailed(ctx, "Unknown panic")
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
res, err := task(ctx, params)
|
||||
if err != nil {
|
||||
log.Debugf("DelayTask failed: %s", err)
|
||||
w.onFailed(ctx, err.Error())
|
||||
} else {
|
||||
log.Debugf("DelayTask complete: %v", res)
|
||||
w.onCompleted(ctx, res)
|
||||
}
|
||||
}()
|
||||
res, err := task(ctx, params)
|
||||
if err != nil {
|
||||
log.Debugf("DelayTask failed: %s", err)
|
||||
httpclients.TaskFailed(ctx, err.Error())
|
||||
} else {
|
||||
log.Debugf("DelayTask complete: %v", res)
|
||||
httpclients.TaskComplete(ctx, res)
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
func StartWorker()
|
||||
|
||||
func (w *SWorkManager) DelayTaskWithoutTaskid(task DelayTaskFunc, params interface{}) {
|
||||
func (w *SWorkManager) DelayTaskWithoutTask(ctx context.Context, task DelayTaskFunc, params interface{}) {
|
||||
w.add()
|
||||
go func() {
|
||||
defer w.done()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Errorln("DelayTaskWithoutTaskid panic: ", r)
|
||||
log.Errorln("DelayTaskWithoutTask panic: ", r)
|
||||
}
|
||||
}()
|
||||
if _, err := task(ctx, params); err != nil {
|
||||
log.Errorln("DelayTaskWithoutTaskid", err)
|
||||
log.Errorln("DelayTaskWithoutTask error:", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (w *SWorkManager) Stop() {
|
||||
log.Infof("WorkManager To stop, wait for workers ...")
|
||||
log.Infof("WorkManager stop, waitting for workers ...")
|
||||
for w.curCount > 0 {
|
||||
log.Warningf("Busy workers count %d, waiting stopped", w.curCount)
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func NewWorkManger() *SWorkManager {
|
||||
return &SWorkManager{}
|
||||
func NewWorkManger(onFailed OnTaskFailed, onCompleted OnTaskCompleted) *SWorkManager {
|
||||
return &SWorkManager{
|
||||
onFailed: onFailed,
|
||||
onCompleted: onCompleted,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
package hostman
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"yunion.io/x/log"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/appctx"
|
||||
"yunion.io/x/onecloud/pkg/hostman/options"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/auth"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules"
|
||||
)
|
||||
|
||||
func GetComputeSession(ctx context.Context) *mcclient.ClientSession {
|
||||
return auth.GetAdminSession(ctx, options.HostOptions.Region, "v2")
|
||||
}
|
||||
|
||||
func TaskFailed(ctx context.Context, reason string) error {
|
||||
if taskId := ctx.Value(appctx.APP_CONTEXT_KEY_TASK_ID); taskId != nil {
|
||||
modules.ComputeTasks.TaskFailed2(GetComputeSession(ctx), taskId.(string), reason)
|
||||
return nil
|
||||
} else {
|
||||
log.Errorln("Reqeuest task failed missing task id, with reason(%s)", reason)
|
||||
return fmt.Errorf("Reqeuest task failed missing task id")
|
||||
}
|
||||
}
|
||||
@@ -92,7 +92,7 @@ func doCreate(ctx context.Context, sid string, body jsonutils.JSONObject) (inter
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
wm.DelayTask(ctx, guestManger.DoDeploy, &SGuestDeploy{sid, body, true})
|
||||
wm.DelayTask(ctx, guestManger.GuestDeploy, &SGuestDeploy{sid, body, true})
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -101,7 +101,7 @@ func doDeploy(ctx context.Context, sid string, body jsonutils.JSONObject) (inter
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
wm.DelayTask(ctx, guestManger.DoDeploy, &SGuestDeploy{sid, body, false})
|
||||
wm.DelayTask(ctx, guestManger.GuestDeploy, &SGuestDeploy{sid, body, false})
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -118,6 +118,10 @@ func doStop(ctx context.Context, sid string, body jsonutils.JSONObject) (interfa
|
||||
}
|
||||
|
||||
func doMonitor(ctx context.Context, sid string, body jsonutils.JSONObject) (interface{}, error) {
|
||||
if !guestManger.IsGuestExist(sid) {
|
||||
return nil, httperrors.NewNotFoundError("Guest %s not found", sid)
|
||||
}
|
||||
|
||||
if body.Contains("cmd") {
|
||||
var c = make(chan string)
|
||||
cb := func(res string) {
|
||||
@@ -136,6 +140,22 @@ func doMonitor(ctx context.Context, sid string, body jsonutils.JSONObject) (inte
|
||||
}
|
||||
}
|
||||
|
||||
func doSync(ctx context.Context, sid string, body jsonutils.JSONObject) (interface{}, error) {
|
||||
if !guestManger.IsGuestExist(sid) {
|
||||
return nil, httperrors.NewNotFoundError("Guest %s not found", sid)
|
||||
}
|
||||
wm.DelayTask(ctx, guestManger.GuestSync, &SGuestSync{sid, body})
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func doSuspend(ctx context.Context, sid string, body jsonutils.JSONObject) (interface{}, error) {
|
||||
if !guestManger.IsGuestExist(sid) {
|
||||
return nil, httperrors.NewNotFoundError("Guest %s not found", sid)
|
||||
}
|
||||
wm.DelayTaskWithoutTask(ctx, guestManger.GuestSuspend, sid)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var actionFuncs = map[string]actionFunc{
|
||||
"create": doCreate,
|
||||
"deploy": doDeploy,
|
||||
|
||||
@@ -3,7 +3,12 @@ package guestman
|
||||
import "yunion.io/x/jsonutils"
|
||||
|
||||
type SGuestDeploy struct {
|
||||
sid string
|
||||
body jsonutils.JSONObject
|
||||
isInit bool
|
||||
Sid string
|
||||
Body jsonutils.JSONObject
|
||||
IsInit bool
|
||||
}
|
||||
|
||||
type SGuestSync struct {
|
||||
Sid string
|
||||
Body jsonutils.JSONObject
|
||||
}
|
||||
|
||||
@@ -16,11 +16,12 @@ import (
|
||||
"yunion.io/x/pkg/util/regutils"
|
||||
"yunion.io/x/pkg/util/seclib"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/httpclients"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/sshkeys"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/workmanager"
|
||||
"yunion.io/x/onecloud/pkg/hostman"
|
||||
"yunion.io/x/onecloud/pkg/hostman/guestfs"
|
||||
"yunion.io/x/onecloud/pkg/httperrors"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules"
|
||||
"yunion.io/x/onecloud/pkg/util/timeutils2"
|
||||
)
|
||||
|
||||
@@ -77,13 +78,11 @@ func (m *SGuestManager) VerifyExistingGuests(pendingDelete bool) {
|
||||
}
|
||||
params.Set("filter.1", strings.Join(keys, ","))
|
||||
}
|
||||
urlStr := fmt.Sprintf("/servers?%s", params.Encode())
|
||||
// TODO: get default context not use background context
|
||||
_, res, err := httpclients.GetDefaultComputeClient().Request(context.Background(), "GET", urlStr, nil, nil, false)
|
||||
res, err := modules.Servers.List(hostman.GetComputeSession(context.Background()), id, params)
|
||||
if err != nil {
|
||||
m.OnVerifyExistingGuestsFail(err, pendingDelete)
|
||||
} else {
|
||||
m.OnVerifyExistingGuestsSucc(res, pendingDelete)
|
||||
m.OnVerifyExistingGuestsSucc(res.Data, pendingDelete)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,32 +91,26 @@ func (m *SGuestManager) OnVerifyExistingGuestsFail(err error, pendingDelete bool
|
||||
timeutils2.AddTimeout(30*time.Second, func() { m.VerifyExistingGuests(false) })
|
||||
}
|
||||
|
||||
func (m *SGuestManager) OnVerifyExistingGuestsSucc(res jsonutils.JSONObject, pendingDelete bool) {
|
||||
iServers, err := res.Get("servers")
|
||||
if err != nil {
|
||||
m.OnVerifyExistingGuestsFail(err, pendingDelete)
|
||||
} else {
|
||||
servers := iServers.(*jsonutils.JSONArray)
|
||||
for _, v := range servers.Value() {
|
||||
id, _ := v.GetString("id")
|
||||
server, ok := m.CandidateServers[id]
|
||||
if !ok {
|
||||
log.Errorf("verify_existing_guests return unknown server %s ???????", id)
|
||||
} else {
|
||||
server.ImportServer(pendingDelete)
|
||||
}
|
||||
}
|
||||
if !pendingDelete {
|
||||
m.VerifyExistingGuests(true)
|
||||
func (m *SGuestManager) OnVerifyExistingGuestsSucc(servers []jsonutils.JSONObject, pendingDelete bool) {
|
||||
for _, v := range servers {
|
||||
id, _ := v.GetString("id")
|
||||
server, ok := m.CandidateServers[id]
|
||||
if !ok {
|
||||
log.Errorf("verify_existing_guests return unknown server %s ???????", id)
|
||||
} else {
|
||||
var unknownServerrs = make([]*SKVMGuestInstance, 0)
|
||||
for _, server := range m.CandidateServers {
|
||||
log.Errorf("Server %s not found on this host", server.GetName())
|
||||
unknownServerrs = append(unknownServerrs, server)
|
||||
}
|
||||
for _, server := range unknownServerrs {
|
||||
m.RemoveCandidateServer(server)
|
||||
}
|
||||
server.ImportServer(pendingDelete)
|
||||
}
|
||||
}
|
||||
if !pendingDelete {
|
||||
m.VerifyExistingGuests(true)
|
||||
} else {
|
||||
var unknownServerrs = make([]*SKVMGuestInstance, 0)
|
||||
for _, server := range m.CandidateServers {
|
||||
log.Errorf("Server %s not found on this host", server.GetName())
|
||||
unknownServerrs = append(unknownServerrs, server)
|
||||
}
|
||||
for _, server := range unknownServerrs {
|
||||
m.RemoveCandidateServer(server)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -153,6 +146,14 @@ func (m *SGuestManager) IsGuestDir(f os.FileInfo) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (m *SGuestManager) IsGuestExist(sid string) bool {
|
||||
if _, ok := guestManger.Servers[sid]; !ok {
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
func (m *SGuestManager) LoadExistingGuests() {
|
||||
files, err := ioutil.ReadDir(m.ServersPath)
|
||||
if err != nil {
|
||||
@@ -214,30 +215,30 @@ func (m *SGuestManager) Monitor(sid, cmd string, callback func(string)) error {
|
||||
}
|
||||
|
||||
// Delay process
|
||||
func (m *SGuestManager) DoDeploy(ctx context.Context, params interface{}) (jsonutils.JSONObject, error) {
|
||||
func (m *SGuestManager) GuestDeploy(ctx context.Context, params interface{}) (jsonutils.JSONObject, error) {
|
||||
deployParams, ok := params.(*SGuestDeploy)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("Unknown params")
|
||||
}
|
||||
guest, ok := m.Servers[deployParams.sid]
|
||||
guest, ok := m.Servers[deployParams.Sid]
|
||||
if ok {
|
||||
desc, _ := deployParams.body.Get("desc")
|
||||
desc, _ := deployParams.Body.Get("desc")
|
||||
if desc != nil {
|
||||
guest.SaveDesc(desc)
|
||||
}
|
||||
if jsonutils.QueryBoolean(deployParams.body, "k8s_pod", false) {
|
||||
if jsonutils.QueryBoolean(deployParams.Body, "k8s_pod", false) {
|
||||
return nil, nil
|
||||
}
|
||||
publicKey := sshkeys.GetKeys(deployParams.body)
|
||||
deploys, _ := deployParams.body.GetArray("deploys")
|
||||
password, _ := deployParams.body.GetString("password")
|
||||
resetPassword := jsonutils.QueryBoolean(deployParams.body, "reset_password", false)
|
||||
publicKey := sshkeys.GetKeys(deployParams.Body)
|
||||
deploys, _ := deployParams.Body.GetArray("deploys")
|
||||
password, _ := deployParams.Body.GetString("password")
|
||||
resetPassword := jsonutils.QueryBoolean(deployParams.Body, "reset_password", false)
|
||||
if resetPassword && len(password) == 0 {
|
||||
password = seclib.RandomPassword(12)
|
||||
}
|
||||
|
||||
guestInfo, err := guest.DeployFs(&guestfs.SDeployInfo{
|
||||
publicKey, deploys, password, deployParams.isInit})
|
||||
publicKey, deploys, password, deployParams.IsInit})
|
||||
if err != nil {
|
||||
log.Errorf("Deploy guest fs error: %s", err)
|
||||
return nil, err
|
||||
@@ -319,6 +320,31 @@ func (m *SGuestManager) GuestStop(ctx context.Context, sid string, timeout int64
|
||||
}
|
||||
}
|
||||
|
||||
func (m *SGuestManager) GuestSync(ctx context.Context, params interface{}) (jsonutils.JSONObject, error) {
|
||||
syncParams, ok := params.(*SGuestSync)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("Unknown params")
|
||||
}
|
||||
guest := m.Servers[syncParams.Sid]
|
||||
if syncParams.Body.Contains("desc") {
|
||||
desc, _ := syncParams.Body.Get("desc")
|
||||
fwOnly := jsonutils.QueryBoolean(syncParams.Body, "fw_only", false)
|
||||
// TODO :SyncConfig
|
||||
return guest.SyncConfig(ctx, desc, fwOnly)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (m *SGuestManager) GuestSuspend(ctx context.Context, params interface{}) (jsonutils.JSONObject, error) {
|
||||
sid, ok := params.(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("Unknown params")
|
||||
}
|
||||
guest := m.Servers[sid]
|
||||
guest.ExecSuspendTask()
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (m *SGuestManager) GetFreeVncPort() int64 {
|
||||
vncPorts := make(map[int]struct{}, 0)
|
||||
for _, guest := range m.Servers {
|
||||
@@ -340,31 +366,27 @@ func (m *SGuestManager) GetFreeVncPort() int64 {
|
||||
return port
|
||||
}
|
||||
|
||||
var guestManger *SGuestManager
|
||||
var wm *workmanager.SWorkManager
|
||||
|
||||
func Stop() {
|
||||
// guestManger.ExitGuestCleanup()
|
||||
}
|
||||
|
||||
func Init(serversPath string) {
|
||||
initGuestManager(serversPath)
|
||||
}
|
||||
|
||||
var guestManger *SGuestManager
|
||||
var wm *workmanager.SWorkManager
|
||||
|
||||
func GetGuestManager() *SGuestManager {
|
||||
return guestManger
|
||||
}
|
||||
|
||||
func initGuestManager(serversPath string) {
|
||||
if guestManger == nil {
|
||||
guestManger = NewGuestManager(serversPath)
|
||||
}
|
||||
}
|
||||
|
||||
func GetGuestManager() *SGuestManager {
|
||||
return guestManger
|
||||
}
|
||||
|
||||
func GetWorkManager() *workmanager.SWorkManager {
|
||||
return wm
|
||||
}
|
||||
|
||||
func init() {
|
||||
wm = workmanager.NewWorkManger()
|
||||
wm = workmanager.NewWorkManger(hostman.TaskFailed, hostman.TaskComplete)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/httpclients"
|
||||
"yunion.io/x/onecloud/pkg/hostman"
|
||||
"yunion.io/x/onecloud/pkg/util/timeutils2"
|
||||
)
|
||||
|
||||
@@ -42,7 +42,7 @@ func (s *SGuestStopTask) onPowerdownGuest(results string) {
|
||||
func (s *SGuestStopTask) checkGuestRunning() {
|
||||
if !s.IsRunning() || time.Now().Sub(*s.startPowerdown) > (s.timeout*time.Duration) {
|
||||
s.Stop() // force stop
|
||||
httpclients.TaskComplete(s.ctx, nil)
|
||||
hostman.TaskComplete(s.ctx, nil)
|
||||
} else {
|
||||
s.CheckGuestRunningLater()
|
||||
}
|
||||
|
||||
@@ -16,12 +16,13 @@ import (
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/util/regutils"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/httpclients"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/storagetypes"
|
||||
"yunion.io/x/onecloud/pkg/hostman"
|
||||
"yunion.io/x/onecloud/pkg/hostman/guestfs"
|
||||
"yunion.io/x/onecloud/pkg/hostman/hostinfo"
|
||||
"yunion.io/x/onecloud/pkg/hostman/monitor"
|
||||
"yunion.io/x/onecloud/pkg/hostman/options"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules"
|
||||
"yunion.io/x/onecloud/pkg/util/fileutils2"
|
||||
"yunion.io/x/onecloud/pkg/util/timeutils2"
|
||||
)
|
||||
@@ -159,13 +160,12 @@ func (s *SKVMGuestInstance) IsDirtyShotdown() bool {
|
||||
}
|
||||
|
||||
func (s *SKVMGuestInstance) DirtyServerRequestStart() {
|
||||
var url = "/servers/dirty-server-start"
|
||||
hostId, _ := s.Desc.GetString("host_id")
|
||||
var body = jsonutils.NewDict()
|
||||
body.Set("guest_id", jsonutils.NewString(s.Id))
|
||||
body.Set("host_id", jsonutils.NewString(hostId))
|
||||
_, _, err := httpclients.GetDefaultComputeClient().
|
||||
Request(context.Background(), "POST", url, nil, body, false)
|
||||
_, err := modules.Servers.PerformClassAction(
|
||||
hostman.GetComputeSession(context.Background()), "dirty-server-start", body)
|
||||
if err != nil {
|
||||
log.Errorf("Dirty server request start error: %s", err)
|
||||
}
|
||||
@@ -329,7 +329,7 @@ func (s *SKVMGuestInstance) onGetQemuVersion(ctx context.Context, version string
|
||||
migratePort, _ := s.Desc.Get("live_migrate_dest_port")
|
||||
body := jsonutils.NewDict(
|
||||
jsonutils.JSONPair{"live_migrate_dest_port", migratePort})
|
||||
httpclients.TaskComplete(ctx, body)
|
||||
hostman.TaskComplete(ctx, body)
|
||||
} else if jsonutils.QueryBoolean(s.Desc, "is_slave", false) {
|
||||
// TODO
|
||||
} else if jsonutils.QueryBoolean(s.Desc, "is_master", false) && ctx == nil {
|
||||
@@ -387,7 +387,8 @@ func (s *SKVMGuestInstance) SyncStatus() {
|
||||
if s.IsSuspend() {
|
||||
status = "suspend"
|
||||
}
|
||||
httpclients.GetDefaultComputeClient().UpdateServerStatus(s.GetId(), status)
|
||||
|
||||
hostman.UpdateServerStatus(context.Background(), s.GetId(), status)
|
||||
}
|
||||
|
||||
func (s *SKVMGuestInstance) SaveDesc(desc jsonutils.JSONObject) error {
|
||||
@@ -525,3 +526,7 @@ func (s *SKVMGuestInstance) scriptStop() bool {
|
||||
func (s *SKVMGuestInstance) ExecStopTask(ctx context.Context, timeout int64) {
|
||||
NewGuestStopTask(s, ctx, timeout).Start()
|
||||
}
|
||||
|
||||
func (s *SKVMGuestInstance) ExecSuspendTask(ctx context.Context) {
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package hostman
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/appctx"
|
||||
"yunion.io/x/onecloud/pkg/hostman/options"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/auth"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules"
|
||||
)
|
||||
|
||||
func GetComputeSession(ctx context.Context) *mcclient.ClientSession {
|
||||
return auth.GetAdminSession(ctx, options.HostOptions.Region, "v2")
|
||||
}
|
||||
|
||||
func TaskFailed(ctx context.Context, reason string) {
|
||||
if taskId := ctx.Value(appctx.APP_CONTEXT_KEY_TASK_ID); taskId != nil {
|
||||
modules.ComputeTasks.TaskFailed2(GetComputeSession(ctx), taskId.(string), reason)
|
||||
} else {
|
||||
log.Errorln("Reqeuest task failed missing task id, with reason(%s)", reason)
|
||||
}
|
||||
}
|
||||
|
||||
func TaskComplete(ctx context.Context, params jsonutils.JSONObject) {
|
||||
if taskId := ctx.Value(appctx.APP_CONTEXT_KEY_TASK_ID); taskId != nil {
|
||||
modules.ComputeTasks.TaskComplete(GetComputeSession(ctx), taskId.(string), params)
|
||||
} else {
|
||||
log.Errorln("Reqeuest task complete missing task id")
|
||||
}
|
||||
}
|
||||
|
||||
func UpdateServerStatus(ctx context.Context, sid, status string) (jsonutils.JSONObject, error) {
|
||||
var body = jsonutils.NewDict()
|
||||
var stus = jsonutils.NewDict()
|
||||
stus.Set("status", jsonutils.NewString(status))
|
||||
body.Set("server", stus)
|
||||
return modules.Servers.PerformAction(GetComputeSession(ctx), sid, "status", body)
|
||||
}
|
||||
Reference in New Issue
Block a user