mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
Merge pull request #599 in YUNIONIO/onecloud from ~WANYAOQI/onecloud:feature/wyq/v2-add-misc-handler to release/2.4.0
* commit 'bcaa0bf4f66195297f53cc52a38f3da21a5f5093': add configure rbd storage add misc handler, register baremetal handler; change the monitor request to POST
This commit is contained in:
@@ -350,7 +350,7 @@ func init() {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ret, err := modules.Servers.GetSpecific(s, opts.ID, "monitor", params)
|
||||
ret, err := modules.Servers.PerformAction(s, opts.ID, "monitor", params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db/quotas"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
|
||||
"yunion.io/x/onecloud/pkg/compute/capabilities"
|
||||
"yunion.io/x/onecloud/pkg/compute/misc"
|
||||
"yunion.io/x/onecloud/pkg/compute/models"
|
||||
"yunion.io/x/onecloud/pkg/compute/specs"
|
||||
"yunion.io/x/onecloud/pkg/compute/sshkeys"
|
||||
@@ -22,6 +23,7 @@ func InitHandlers(app *appsrv.Application) {
|
||||
specs.AddSpecHandler("", app)
|
||||
sshkeys.AddSshKeysHandler("", app)
|
||||
taskman.AddTaskHandler("", app)
|
||||
misc.AddMiscHandler("", app)
|
||||
|
||||
for _, manager := range []db.IModelManager{
|
||||
taskman.TaskManager,
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
package misc // import "yunion.io/x/onecloud/pkg/compute/misc"
|
||||
@@ -0,0 +1,70 @@
|
||||
package misc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/onecloud/pkg/appsrv"
|
||||
"yunion.io/x/onecloud/pkg/compute/models"
|
||||
"yunion.io/x/onecloud/pkg/compute/options"
|
||||
"yunion.io/x/onecloud/pkg/httperrors"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/auth"
|
||||
"yunion.io/x/pkg/tristate"
|
||||
)
|
||||
|
||||
func AddMiscHandler(prefix string, app *appsrv.Application) {
|
||||
prefix = fmt.Sprintf("%s/misc", prefix)
|
||||
addHandler("GET", fmt.Sprintf("%s/bm-agent-url", prefix), getBmAgentUrl, app)
|
||||
addHandler("GET", fmt.Sprintf("%s/bm-prepare-script", prefix), getBmPrepareScript, app)
|
||||
}
|
||||
|
||||
func addHandler(method, prefix string, f appsrv.FilterHandler, app *appsrv.Application) {
|
||||
handler := auth.Authenticate(f)
|
||||
app.AddHandler(method, prefix, handler)
|
||||
}
|
||||
|
||||
func getBmAgentUrl(ctx context.Context, w http.ResponseWriter, r *http.Request) {
|
||||
body, _ := appsrv.FetchJSON(r)
|
||||
if body == nil || !body.Contains("ip") {
|
||||
httperrors.InputParameterError(w, "Missing ip ?")
|
||||
return
|
||||
}
|
||||
ipAddr, _ := body.GetString("ip")
|
||||
n, _ := models.NetworkManager.GetNetworkOfIP(ipAddr, "baremetal", tristate.None)
|
||||
if n == nil {
|
||||
httperrors.NotFoundError(w, "Network not found")
|
||||
return
|
||||
}
|
||||
zoneId := n.GetWire().ZoneId
|
||||
bmAgentUrl, err := auth.GetServiceURL("baremetal", options.Options.Region, zoneId, "")
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
httperrors.InternalServerError(w, err.Error())
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(w, bmAgentUrl)
|
||||
}
|
||||
|
||||
func getBmPrepareScript(ctx context.Context, w http.ResponseWriter, r *http.Request) {
|
||||
if len(options.Options.BaremetalPreparePackageUrl) == 0 {
|
||||
httperrors.NotAcceptableError(w, "Baremetal package not prepared")
|
||||
return
|
||||
}
|
||||
regionUrl, err := auth.GetServiceURL("compute_v2", options.Options.Region, "", "")
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
httperrors.InternalServerError(w, err.Error())
|
||||
return
|
||||
}
|
||||
userCred := auth.FetchUserCredential(ctx)
|
||||
var script string
|
||||
script += fmt.Sprintf("curl -fsSL -k -o ./baremetal_prepare.tar.gz %s;",
|
||||
options.Options.BaremetalPreparePackageUrl)
|
||||
script += "mkdir ./baremetal_prepare;"
|
||||
script += "tar -zxf ./baremetal_prepare.tar.gz -C ./baremetal_prepare;"
|
||||
script += fmt.Sprintf("./baremetal_prepare/prepare.sh %s %s",
|
||||
userCred.GetTokenString(), regionUrl)
|
||||
fmt.Fprintf(w, script)
|
||||
}
|
||||
@@ -50,13 +50,17 @@ func (self *SGuest) GetDetailsVnc(ctx context.Context, userCred mcclient.TokenCr
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SGuest) AllowGetDetailsMonitor(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) bool {
|
||||
func (self *SGuest) AllowPerformMonitor(ctx context.Context,
|
||||
userCred mcclient.TokenCredential,
|
||||
query jsonutils.JSONObject,
|
||||
data jsonutils.JSONObject) bool {
|
||||
return self.IsOwner(userCred)
|
||||
}
|
||||
|
||||
func (self *SGuest) GetDetailsMonitor(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
||||
func (self *SGuest) PerformMonitor(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject,
|
||||
data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
||||
if utils.IsInStringArray(self.Status, []string{VM_RUNNING, VM_BLOCK_STREAM}) {
|
||||
cmd, err := query.GetString("command")
|
||||
cmd, err := data.GetString("command")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -564,12 +564,12 @@ func (self *SHostManager) AllowGetPropertyBmStartRegisterScript(ctx context.Cont
|
||||
}
|
||||
|
||||
func (self *SHostManager) GetPropertyBmStartRegisterScript(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
||||
regionUri, err := auth.GetServiceURL("compute", options.Options.Region, "", "")
|
||||
regionUri, err := auth.GetServiceURL("compute_v2", options.Options.Region, "", "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var script string
|
||||
script += fmt.Sprintf("curl -fsSL -H 'X-Auth-Token: %s' %s/misc/bm-prepare-script", userCred.GetTokenString(), regionUri)
|
||||
script += fmt.Sprintf("curl -k -fsSL -H 'X-Auth-Token: %s' %s/misc/bm-prepare-script", userCred.GetTokenString(), regionUri)
|
||||
res := jsonutils.NewDict()
|
||||
res.Add(jsonutils.NewString(script), "script")
|
||||
return res, nil
|
||||
|
||||
@@ -225,24 +225,66 @@ func (self *SStorage) ValidateDeleteCondition(ctx context.Context) error {
|
||||
|
||||
func (self *SStorage) PostCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerProjId string, query jsonutils.JSONObject, data jsonutils.JSONObject) {
|
||||
self.SStandaloneResourceBase.PostCreate(ctx, userCred, ownerProjId, query, data)
|
||||
storageConf, _ := data.Get("storage_conf")
|
||||
if storageConf != nil {
|
||||
_, err := self.GetModelManager().TableSpec().Update(self, func() error {
|
||||
self.StorageConf = storageConf
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
if self.StorageType == STORAGE_RBD {
|
||||
// TODO
|
||||
var storages = make([]SStorage, 0)
|
||||
err := StorageManager.Query().Equals("storage_type", STORAGE_RBD).All(&storages)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
return
|
||||
}
|
||||
nMonHost, _ := storageConf.GetString("mon_host")
|
||||
nKey, _ := storageConf.GetString("key")
|
||||
for i := 0; i < len(storages); i++ {
|
||||
monHost, _ := storages[i].StorageConf.GetString("mon_host")
|
||||
key, _ := storages[i].StorageConf.GetString("key")
|
||||
if monHost == nMonHost && nKey == key {
|
||||
_, err := self.GetModelManager().TableSpec().Update(self, func() error {
|
||||
self.StoragecacheId = storages[i].StoragecacheId
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
return
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
if len(self.StoragecacheId) == 0 {
|
||||
sc := &SStoragecache{}
|
||||
sc.SetModelManager(StoragecacheManager)
|
||||
sc.Name = fmt.Sprintf("imagecache-%s", self.Id)
|
||||
pool, _ := storageConf.GetString("pool")
|
||||
sc.Path = fmt.Sprintf("rbd:%s", pool)
|
||||
err := StorageManager.TableSpec().Insert(sc)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
}
|
||||
}
|
||||
} else if self.StorageType == STORAGE_NFS {
|
||||
sc := &SStoragecache{}
|
||||
sc.Path = options.Options.NfsDefaultImageCacheDir
|
||||
sc.ExternalId = self.Id
|
||||
sc.Name = "nfs-" + self.Name + time.Now().Format("2006-01-02 15:04:05")
|
||||
err := StoragecacheManager.TableSpec().Insert(sc)
|
||||
if err != nil {
|
||||
if err := StoragecacheManager.TableSpec().Insert(sc); err != nil {
|
||||
log.Errorln(err)
|
||||
return
|
||||
}
|
||||
err = StoragecacheManager.Query().Equals("external_id", self.Id).First(sc)
|
||||
if err != nil {
|
||||
if err := StoragecacheManager.Query().Equals("external_id", self.Id).First(sc); err != nil {
|
||||
log.Errorln(err)
|
||||
return
|
||||
}
|
||||
_, err = self.GetModelManager().TableSpec().Update(self, func() error {
|
||||
_, err := self.GetModelManager().TableSpec().Update(self, func() error {
|
||||
self.StoragecacheId = sc.Id
|
||||
self.Status = STORAGE_ONLINE
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user