From f246e667907601d14a42b1545570491d417d6e0f Mon Sep 17 00:00:00 2001 From: rainzm Date: Mon, 19 Apr 2021 11:11:31 +0800 Subject: [PATCH 1/2] feat(devtool): add ScriptApplyId fro ScriptApplyRecord --- pkg/apis/devtool/script.go | 17 ++++++ pkg/apis/devtool/zz_generated.model.go | 9 ++-- pkg/devtool/models/script_apply.go | 2 +- pkg/devtool/models/script_apply_record.go | 63 ++++++++++++++++++----- pkg/devtool/tasks/apply_script_task.go | 2 +- pkg/mcclient/options/devtool/script.go | 4 +- 6 files changed, 75 insertions(+), 22 deletions(-) diff --git a/pkg/apis/devtool/script.go b/pkg/apis/devtool/script.go index 0a60baf0c8..01af4f2507 100644 --- a/pkg/apis/devtool/script.go +++ b/pkg/apis/devtool/script.go @@ -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 { diff --git a/pkg/apis/devtool/zz_generated.model.go b/pkg/apis/devtool/zz_generated.model.go index 342a508cd5..119bbef188 100644 --- a/pkg/apis/devtool/zz_generated.model.go +++ b/pkg/apis/devtool/zz_generated.model.go @@ -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. diff --git a/pkg/devtool/models/script_apply.go b/pkg/devtool/models/script_apply.go index 7a6045d4e5..8da8caa3fe 100644 --- a/pkg/devtool/models/script_apply.go +++ b/pkg/devtool/models/script_apply.go @@ -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") } diff --git a/pkg/devtool/models/script_apply_record.go b/pkg/devtool/models/script_apply_record.go index 2d820a1972..0482c25605 100644 --- a/pkg/devtool/models/script_apply_record.go +++ b/pkg/devtool/models/script_apply_record.go @@ -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 } diff --git a/pkg/devtool/tasks/apply_script_task.go b/pkg/devtool/tasks/apply_script_task.go index f56dc85dcf..5e4bd6a7d8 100644 --- a/pkg/devtool/tasks/apply_script_task.go +++ b/pkg/devtool/tasks/apply_script_task.go @@ -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 diff --git a/pkg/mcclient/options/devtool/script.go b/pkg/mcclient/options/devtool/script.go index 0911f523d4..50a98cb5f1 100644 --- a/pkg/mcclient/options/devtool/script.go +++ b/pkg/mcclient/options/devtool/script.go @@ -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) { From 9b994792ec3634f06eebf52180793fcbb1f19f05 Mon Sep 17 00:00:00 2001 From: rainzm Date: Mon, 19 Apr 2021 11:12:25 +0800 Subject: [PATCH 2/2] fix(devtool): fix a bug that may trigger panic --- pkg/devtool/tasks/apply_script_task.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/devtool/tasks/apply_script_task.go b/pkg/devtool/tasks/apply_script_task.go index 5e4bd6a7d8..c5e7c0ddeb 100644 --- a/pkg/devtool/tasks/apply_script_task.go +++ b/pkg/devtool/tasks/apply_script_task.go @@ -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