mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
Merge pull request #10794 from rainzm/devtool/script
Devtool script optimization and fix
This commit is contained in:
@@ -42,6 +42,23 @@ type ScriptApplyRecoredListInput struct {
|
||||
// description: Id of Script
|
||||
// example: cc2e2ba6-e33d-4be3-8e2d-4d2aa843dd03
|
||||
ScriptId string
|
||||
// description: Id of Server
|
||||
// example: a4b3n2c9-dbb7-4c51-8e1a-d2d4b331ccec
|
||||
ServerId string
|
||||
// description: Id of script apply
|
||||
// example: a70eb6e6-dbb7-4c51-8e1a-d2d4b331ccec
|
||||
ScriptApplyId string
|
||||
}
|
||||
|
||||
type ScriptApplyRecordDetails struct {
|
||||
apis.StandaloneResourceDetails
|
||||
SScriptApplyRecord
|
||||
// description: Id of Script
|
||||
// example: cc2e2ba6-e33d-4be3-8e2d-4d2aa843dd03
|
||||
ScriptId string
|
||||
// description: Id of Server
|
||||
// example: a4b3n2c9-dbb7-4c51-8e1a-d2d4b331ccec
|
||||
ServerId string
|
||||
}
|
||||
|
||||
type ScriptCreateInput struct {
|
||||
|
||||
@@ -59,11 +59,10 @@ type SScriptApply struct {
|
||||
// SScriptApplyRecord is an autogenerated struct via yunion.io/x/onecloud/pkg/devtool/models.SScriptApplyRecord.
|
||||
type SScriptApplyRecord struct {
|
||||
apis.SStatusStandaloneResourceBase
|
||||
ScriptId string `json:"script_id"`
|
||||
ServerId string `json:"server_id"`
|
||||
StartTime time.Time `json:"start_time"`
|
||||
EndTime time.Time `json:"end_time"`
|
||||
Reason string `json:"reason"`
|
||||
ScriptApplyId string `json:"script_apply_id"`
|
||||
StartTime time.Time `json:"start_time"`
|
||||
EndTime time.Time `json:"end_time"`
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
|
||||
// SVSCronjob is an autogenerated struct via yunion.io/x/onecloud/pkg/devtool/models.SVSCronjob.
|
||||
|
||||
@@ -103,7 +103,7 @@ func (sa *SScriptApply) StartApply(ctx context.Context, userCred mcclient.TokenC
|
||||
err = sa.startApplyScriptTask(ctx, userCred, "")
|
||||
if err != nil {
|
||||
f := false
|
||||
_, err = ScriptApplyRecordManager.createRecordWithResult(ctx, sa.ScriptId, sa.GuestId, &f, fmt.Sprintf("unabel to start ApplyScriptTask: %v", err))
|
||||
_, err = ScriptApplyRecordManager.createRecordWithResult(ctx, sa.GetId(), &f, fmt.Sprintf("unabel to start ApplyScriptTask: %v", err))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "unable to record")
|
||||
}
|
||||
|
||||
@@ -19,21 +19,22 @@ import (
|
||||
"time"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/sqlchemy"
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/devtool"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/util/rbacutils"
|
||||
"yunion.io/x/onecloud/pkg/util/stringutils2"
|
||||
)
|
||||
|
||||
type SScriptApplyRecord struct {
|
||||
db.SStatusStandaloneResourceBase
|
||||
ScriptId string `width:"36" charset:"ascii" nullable:"true" list:"user" index:"true"`
|
||||
ServerId string `width:"36" charset:"ascii" nullable:"true" list:"user"`
|
||||
StartTime time.Time `list:"user"`
|
||||
EndTime time.Time `list:"user"`
|
||||
Reason string `list:"user"`
|
||||
ScriptApplyId string `width:"36" charset:"ascii" nullable:"true" list:"user" index:"true"`
|
||||
StartTime time.Time `list:"user"`
|
||||
EndTime time.Time `list:"user"`
|
||||
Reason string `list:"user"`
|
||||
}
|
||||
|
||||
type SScriptApplyRecordManager struct {
|
||||
@@ -59,22 +60,56 @@ func (sarm *SScriptApplyRecordManager) ListItemFilter(ctx context.Context, q *sq
|
||||
if err != nil {
|
||||
return q, err
|
||||
}
|
||||
if len(input.ScriptId) > 0 {
|
||||
q = q.Equals("script_id", input.ScriptId)
|
||||
if len(input.ScriptApplyId) > 0 {
|
||||
q = q.Equals("script_apply_id", input.ScriptApplyId)
|
||||
}
|
||||
if len(input.ScriptId) > 0 || len(input.ServerId) > 0 {
|
||||
saq := ScriptApplyManager.Query("id")
|
||||
if len(input.ScriptId) > 0 {
|
||||
saq = saq.Equals("script_id", input.ScriptId)
|
||||
}
|
||||
if len(input.ServerId) > 0 {
|
||||
saq = saq.Equals("guest_id", input.ServerId)
|
||||
}
|
||||
saqSub := saq.SubQuery()
|
||||
q = q.Join(saqSub, sqlchemy.Equals(q.Field("script_apply_id"), saqSub.Field("id")))
|
||||
}
|
||||
return q, nil
|
||||
}
|
||||
|
||||
func (sarm *SScriptApplyRecordManager) CreateRecord(ctx context.Context, scriptId, serverId string) (*SScriptApplyRecord, error) {
|
||||
return sarm.createRecordWithResult(ctx, scriptId, serverId, nil, "")
|
||||
func (sarm *SScriptApplyRecordManager) FetchCustomizeColumns(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, objs []interface{}, fields stringutils2.SSortedStrings, isList bool) []api.ScriptApplyRecordDetails {
|
||||
sDetails := sarm.SStandaloneResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
|
||||
details := make([]api.ScriptApplyRecordDetails, len(objs))
|
||||
for i := range details {
|
||||
details[i].StandaloneResourceDetails = sDetails[i]
|
||||
scriptApplyRecord := objs[i].(*SScriptApplyRecord)
|
||||
sa, err := scriptApplyRecord.ScriptApply()
|
||||
if err != nil {
|
||||
log.Errorf("unable to get SScriptApply: %v", err)
|
||||
}
|
||||
details[i].ScriptId = sa.ScriptId
|
||||
details[i].ServerId = sa.GuestId
|
||||
}
|
||||
return details
|
||||
}
|
||||
|
||||
func (sarm *SScriptApplyRecordManager) createRecordWithResult(ctx context.Context, scriptId, serverId string, success *bool, reason string) (*SScriptApplyRecord, error) {
|
||||
func (sar *SScriptApplyRecord) ScriptApply() (*SScriptApply, error) {
|
||||
obj, err := ScriptApplyManager.FetchById(sar.ScriptApplyId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return obj.(*SScriptApply), nil
|
||||
}
|
||||
|
||||
func (sarm *SScriptApplyRecordManager) CreateRecord(ctx context.Context, scriptApplyId string) (*SScriptApplyRecord, error) {
|
||||
return sarm.createRecordWithResult(ctx, scriptApplyId, nil, "")
|
||||
}
|
||||
|
||||
func (sarm *SScriptApplyRecordManager) createRecordWithResult(ctx context.Context, scriptApplyId string, success *bool, reason string) (*SScriptApplyRecord, error) {
|
||||
now := time.Now()
|
||||
sar := &SScriptApplyRecord{
|
||||
StartTime: now,
|
||||
ScriptId: scriptId,
|
||||
ServerId: serverId,
|
||||
StartTime: now,
|
||||
ScriptApplyId: scriptApplyId,
|
||||
}
|
||||
if success == nil {
|
||||
sar.Status = api.SCRIPT_APPLY_RECORD_APPLYING
|
||||
@@ -117,7 +152,7 @@ func (sarm *SScriptApplyRecordManager) FetchOwnerId(ctx context.Context, data js
|
||||
}
|
||||
|
||||
func (sar *SScriptApplyRecord) GetOwnerId() mcclient.IIdentityProvider {
|
||||
obj, _ := ScriptManager.FetchById(sar.ScriptId)
|
||||
obj, _ := ScriptApplyManager.FetchById(sar.ScriptApplyId)
|
||||
if obj == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ func (self *ApplyScriptTask) taskSuccess(ctx context.Context, sa *models.SScript
|
||||
func (self *ApplyScriptTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
|
||||
sa := obj.(*models.SScriptApply)
|
||||
// create record
|
||||
sar, err := models.ScriptApplyRecordManager.CreateRecord(ctx, sa.ScriptId, sa.GuestId)
|
||||
sar, err := models.ScriptApplyRecordManager.CreateRecord(ctx, sa.GetId())
|
||||
if err != nil {
|
||||
self.taskFailed(ctx, sa, nil, err)
|
||||
return
|
||||
@@ -155,7 +155,7 @@ func (self *ApplyScriptTask) OnInit(ctx context.Context, obj db.IStandaloneModel
|
||||
// check proxy forward
|
||||
if ok := self.ensureLocalForwardWork(address, int(port)); !ok {
|
||||
self.clearLocalForward(session, forwardId)
|
||||
self.taskFailed(ctx, sa, sar, errors.Wrapf(err, "The created local forward is actually not usable"))
|
||||
self.taskFailed(ctx, sa, sar, errors.Error("The created local forward is actually not usable"))
|
||||
return
|
||||
}
|
||||
self.SetStage("OnAnsiblePlaybookComplete", updateData)
|
||||
@@ -180,6 +180,7 @@ func (self *ApplyScriptTask) checkSshable(session *mcclient.ClientSession, serve
|
||||
if err != nil {
|
||||
return sSSHable{}, errors.Wrapf(err, "unable to get sshable info of server %s", serverId)
|
||||
}
|
||||
log.Debugf("data to chech sshable:\n %s", data)
|
||||
methodTrieds, _ := data.GetArray("method_tried")
|
||||
sshable := sSSHable{}
|
||||
reasons := make([]string, 0, len(methodTrieds))
|
||||
@@ -216,6 +217,7 @@ func (self *ApplyScriptTask) ensureLocalForwardWork(host string, port int) bool
|
||||
if err == nil {
|
||||
return true
|
||||
}
|
||||
log.Debugf("no.%d times, try to connect to %s failed: %s", waitTimes, address, err)
|
||||
time.Sleep(wt)
|
||||
waitTimes += 1
|
||||
wt += 1 * time.Second
|
||||
|
||||
@@ -58,7 +58,9 @@ func (so *ScriptApplyOptions) Params() (jsonutils.JSONObject, error) {
|
||||
|
||||
type ScriptApplyRecordListOptions struct {
|
||||
options.BaseListOptions
|
||||
ScriptId string
|
||||
ScriptId string
|
||||
ScriptApplyId string
|
||||
ServerId string
|
||||
}
|
||||
|
||||
func (so *ScriptApplyRecordListOptions) Params() (jsonutils.JSONObject, error) {
|
||||
|
||||
Reference in New Issue
Block a user