From 0395b514639fb56dad0e1d4a1d5eaaaaf509403b Mon Sep 17 00:00:00 2001 From: rainzm Date: Thu, 5 Aug 2021 14:47:32 +0800 Subject: [PATCH] feat(devtool): support batch applying for virtual machines --- cmd/climc/shell/devtool/script.go | 1 + pkg/apis/devtool/script.go | 15 +++++++++++++ pkg/devtool/models/script.go | 30 ++++++++++++++++++++++++++ pkg/mcclient/options/devtool/script.go | 18 ++++++++++++---- 4 files changed, 60 insertions(+), 4 deletions(-) diff --git a/cmd/climc/shell/devtool/script.go b/cmd/climc/shell/devtool/script.go index 4a50a12a5a..8ed7b41827 100644 --- a/cmd/climc/shell/devtool/script.go +++ b/cmd/climc/shell/devtool/script.go @@ -24,6 +24,7 @@ func init() { cmd := shell.NewResourceCmd(&modules.DevToolScripts).WithKeyword("devtool-script") cmd.List(new(options.ScriptListOptions)) cmd.Perform("apply", new(options.ScriptApplyOptions)) + cmd.Perform("batch-apply", new(options.ScriptBatchApplyOptions)) cmd1 := shell.NewResourceCmd(&modules.DevToolScriptApplyRecords).WithKeyword("devtool-script-record") cmd1.List(new(options.ScriptApplyRecordListOptions)) } diff --git a/pkg/apis/devtool/script.go b/pkg/apis/devtool/script.go index dd771bff88..cea18a8586 100644 --- a/pkg/apis/devtool/script.go +++ b/pkg/apis/devtool/script.go @@ -29,6 +29,21 @@ type ScriptApplyOutput struct { ScriptApplyId string } +type ScriptBatchApplyInput struct { + ServerIds []string +} + +type ScriptBatchApplyOutput struct { + Results []ScriptBatchApplyResult +} + +type ScriptBatchApplyResult struct { + ServerId string + Succeed bool + Reason string + ScriptApplyId string +} + type ScriptApplyRecoredListInput struct { apis.StatusStandaloneResourceListInput // description: Id of Script diff --git a/pkg/devtool/models/script.go b/pkg/devtool/models/script.go index 9ecd4641c3..57f6bff347 100644 --- a/pkg/devtool/models/script.go +++ b/pkg/devtool/models/script.go @@ -152,6 +152,36 @@ func (s *SScript) PerformApply(ctx context.Context, userCred mcclient.TokenCrede return output, nil } +func (s *SScript) AllowPerformBatchApply(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) bool { + return s.IsOwner(userCred) || db.IsAdminAllowPerform(userCred, s, "batch-apply") +} + +func (s *SScript) PerformBatchApply(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.ScriptBatchApplyInput) (api.ScriptBatchApplyOutput, error) { + output := api.ScriptBatchApplyOutput{ + Results: make([]api.ScriptBatchApplyResult, len(input.ServerIds)), + } + var argsGenerator string + if s.Name == MonitorAgent { + argsGenerator = MonitorAgent + } + for i, serverId := range input.ServerIds { + output.Results[i].ServerId = serverId + sa, err := ScriptApplyManager.createScriptApply(ctx, s.Id, serverId, nil, argsGenerator) + if err != nil { + output.Results[i].Reason = err.Error() + continue + } + err = sa.StartApply(ctx, userCred) + if err != nil { + output.Results[i].Reason = err.Error() + continue + } + output.Results[i].Succeed = true + output.Results[i].ScriptApplyId = sa.Id + } + return output, nil +} + type sServerInfo struct { ServerId string VpcId string diff --git a/pkg/mcclient/options/devtool/script.go b/pkg/mcclient/options/devtool/script.go index 50a98cb5f1..0dc1a215af 100644 --- a/pkg/mcclient/options/devtool/script.go +++ b/pkg/mcclient/options/devtool/script.go @@ -41,10 +41,7 @@ func (so *ScriptOptions) Params() (jsonutils.JSONObject, error) { } type SscriptApplyOptions struct { - SERVERID string `help:"server id" json:"server_id"` - EipFirst bool `help:"whether to use eip first"` - ProxyEndpointId string `help:"proxy endpoint id"` - AutoChooseProxyEndpoint bool `help:"automatically choose proxy endpoint"` + SERVERID string `help:"server id" json:"server_id"` } type ScriptApplyOptions struct { @@ -56,6 +53,19 @@ func (so *ScriptApplyOptions) Params() (jsonutils.JSONObject, error) { return jsonutils.Marshal(so.SscriptApplyOptions), nil } +type SscriptBatchApplyOptions struct { + ServerIds []string `help:"server id list"` +} + +type ScriptBatchApplyOptions struct { + ScriptOptions + SscriptBatchApplyOptions +} + +func (so *ScriptBatchApplyOptions) Params() (jsonutils.JSONObject, error) { + return jsonutils.Marshal(so.SscriptBatchApplyOptions), nil +} + type ScriptApplyRecordListOptions struct { options.BaseListOptions ScriptId string