diff --git a/.gitignore b/.gitignore index 7f3a4ad..6a0abfe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ vendor/ *.out -build/ \ No newline at end of file +build/ +*.lua \ No newline at end of file diff --git a/cmd.pb.go b/cmd.pb.go index e663c25..4084b0b 100644 --- a/cmd.pb.go +++ b/cmd.pb.go @@ -2871,8 +2871,9 @@ func (x *BindError) GetReason() string { type ScriptRunRes struct { state protoimpl.MessageState `protogen:"open.v1"` PrintOutput string `protobuf:"bytes,1,opt,name=print_output,json=printOutput,proto3" json:"print_output,omitempty"` // print输出 - Error string `protobuf:"bytes,2,opt,name=error,proto3" json:"error,omitempty"` // 错误信息 + ErrorOutput string `protobuf:"bytes,2,opt,name=error_output,json=errorOutput,proto3" json:"error_output,omitempty"` // 错误信息 TargetsOutput string `protobuf:"bytes,3,opt,name=targets_output,json=targetsOutput,proto3" json:"targets_output,omitempty"` // 策略输出 + DataspaceOut string `protobuf:"bytes,4,opt,name=dataspace_out,json=dataspaceOut,proto3" json:"dataspace_out,omitempty"` // 数据区输出 unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } @@ -2914,9 +2915,9 @@ func (x *ScriptRunRes) GetPrintOutput() string { return "" } -func (x *ScriptRunRes) GetError() string { +func (x *ScriptRunRes) GetErrorOutput() string { if x != nil { - return x.Error + return x.ErrorOutput } return "" } @@ -2928,6 +2929,13 @@ func (x *ScriptRunRes) GetTargetsOutput() string { return "" } +func (x *ScriptRunRes) GetDataspaceOut() string { + if x != nil { + return x.DataspaceOut + } + return "" +} + // ScriptUpdateRes 升级脚本返回 type ScriptUpdateRes struct { state protoimpl.MessageState `protogen:"open.v1"` @@ -3158,11 +3166,12 @@ const file_cmd_proto_rawDesc = "" + "\tBindError\x12\x17\n" + "\abind_id\x18\x01 \x01(\x03R\x06bindId\x12\x1b\n" + "\tbind_type\x18\x02 \x01(\x05R\bbindType\x12\x16\n" + - "\x06reason\x18\x03 \x01(\tR\x06reason\"n\n" + + "\x06reason\x18\x03 \x01(\tR\x06reason\"\xa0\x01\n" + "\fScriptRunRes\x12!\n" + - "\fprint_output\x18\x01 \x01(\tR\vprintOutput\x12\x14\n" + - "\x05error\x18\x02 \x01(\tR\x05error\x12%\n" + - "\x0etargets_output\x18\x03 \x01(\tR\rtargetsOutput\"\x11\n" + + "\fprint_output\x18\x01 \x01(\tR\vprintOutput\x12!\n" + + "\ferror_output\x18\x02 \x01(\tR\verrorOutput\x12%\n" + + "\x0etargets_output\x18\x03 \x01(\tR\rtargetsOutput\x12#\n" + + "\rdataspace_out\x18\x04 \x01(\tR\fdataspaceOut\"\x11\n" + "\x0fScriptUpdateRes*=\n" + "\bBindType\x12\x13\n" + "\x0fUnknownBindType\x10\x00\x12\r\n" + diff --git a/cmd.proto b/cmd.proto index 3802e84..abebfaa 100644 --- a/cmd.proto +++ b/cmd.proto @@ -301,8 +301,9 @@ message BindError { // ScriptRunRes 运行脚本返回 message ScriptRunRes { string print_output = 1; // print输出 - string error = 2; // 错误信息 + string error_output = 2; // 错误信息 string targets_output = 3; // 策略输出 + string dataspace_out = 4; // 数据区输出 } // ScriptUpdateRes 升级脚本返回 diff --git a/cmd/saastool/help.go b/cmd/saastool/help.go index d705499..7b92cf7 100644 --- a/cmd/saastool/help.go +++ b/cmd/saastool/help.go @@ -25,6 +25,7 @@ Commands: task Task commands target Target commands bind Bind commands + script Script commands "help" is the default command. diff --git a/cmd/saastool/main.go b/cmd/saastool/main.go index f976e6d..f9c47e9 100644 --- a/cmd/saastool/main.go +++ b/cmd/saastool/main.go @@ -36,6 +36,8 @@ func Run(args ...string) error { return RunTarget(args...) case "bind": return RunBind(args...) + case "script": + return RunScript(args...) default: err := fmt.Errorf(`unknown command "%v"`+"\n"+`Run 'saastool help' for usage`, name) fmt.Fprintln(os.Stderr, err.Error()) diff --git a/cmd/saastool/params.go b/cmd/saastool/params.go index 514d971..7b936d8 100644 --- a/cmd/saastool/params.go +++ b/cmd/saastool/params.go @@ -105,6 +105,18 @@ func paramIDs(fs *flag.FlagSet) *string { return fs.String("ids", "", "IDs for delete. Use commas to separate multiple IDs") } +func paramLua(fs *flag.FlagSet) *string { + return fs.String("lua", "", "LUA file name") +} + +func paramDid(fs *flag.FlagSet) *string { + return fs.String("did", "", "device md5 (lower case)") +} + +func paramOS(fs *flag.FlagSet) *uint { + return fs.Uint("os", 2, "1=iOS, 2=Android, default=2") +} + // ParseByteSize 解析字节大小字符串为字节数 func ParseByteSize(sizeStr string) (uint64, error) { sizeStr = strings.TrimSpace(sizeStr) diff --git a/cmd/saastool/script.go b/cmd/saastool/script.go new file mode 100644 index 0000000..7229708 --- /dev/null +++ b/cmd/saastool/script.go @@ -0,0 +1,40 @@ +package main + +import ( + "fmt" + "os" + "strings" +) + +func RunScript(args ...string) error { + name, args := ParseCommandName(args) + + // 从参数中解析出命令 + switch name { + case "", "help": + return RunScriptHelp(args...) + case "run": + return RunScriptRun(args...) + default: + err := fmt.Errorf(`unknown command "%s"`+"\n"+`Run 'saastool script help' for usage`, name) + fmt.Fprintln(os.Stderr, err) + + return err + } +} + +func RunScriptHelp(args ...string) error { + fmt.Println(strings.TrimSpace(scriptUsage)) + return nil +} + +const scriptUsage = ` +Usage: saastoola script COMMAND [OPTIONS] + +Commands: + run Run lua script test on server + +"help" is the default command. + +Use "saastool script COMMAND -help" for more information about a command. +` diff --git a/cmd/saastool/script_run.go b/cmd/saastool/script_run.go new file mode 100644 index 0000000..72219b9 --- /dev/null +++ b/cmd/saastool/script_run.go @@ -0,0 +1,112 @@ +package main + +import ( + "flag" + "fmt" + "io" + "net/http" + "os" + + "git.algo.com.cn/public/saasapi" + "git.algo.com.cn/public/saasapi/pkg/saashttp" +) + +type scriptRunParams struct { + luaScript string + did string + os saasapi.OS + saasHttp *saashttp.SaasClient +} + +func RunScriptRun(args ...string) error { + fs := flag.NewFlagSet("run", flag.ExitOnError) + cfgFile := paramConfig(fs) + luaFile := paramLua(fs) + did := paramDid(fs) + tos := paramOS(fs) + + if err := fs.Parse(args); err != nil { + fmt.Fprintln(os.Stderr, "command line parse error", "err", err) + return err + } + + if fs.NArg() > 0 || len(*luaFile) == 0 || len(*did) == 0 { + fs.PrintDefaults() + return nil + } + + if !(*tos == 1 || *tos == 2) { + fmt.Fprintln(os.Stderr, "OS error") + return nil + } + + file, err := os.Open(*luaFile) + if err != nil { + fmt.Fprintln(os.Stderr, "lua file error", "err", err) + return err + } + defer file.Close() + + body, err := io.ReadAll(file) + if err != nil { + fmt.Fprintln(os.Stderr, "lua file read error", "err", err) + return err + } + + cfg, err := LoadConfigFile(*cfgFile) + if err != nil { + fmt.Fprintln(os.Stderr, "LoadConfigFile error", "err", err) + return err + } + + scriptRunParams := scriptRunParams{ + luaScript: string(body), + did: *did, + os: saasapi.OS(*tos), + saasHttp: &saashttp.SaasClient{ + Client: &http.Client{}, + ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls), + Auth: &cfg.Auth, + }, + } + + return doScriptRun(scriptRunParams) +} + +func doScriptRun(scriptRunParams scriptRunParams) error { + saasReq := &saasapi.SaasReq{ + Cmd: &saasapi.SaasReq_ScriptRun{ + ScriptRun: &saasapi.ScriptRun{ + LuaScript: scriptRunParams.luaScript, + ServerDid: scriptRunParams.did, + Os: scriptRunParams.os, + }, + }, + } + + res, err := scriptRunParams.saasHttp.ScriptRun(saasReq) + if err != nil { + fmt.Fprintln(os.Stderr, "run script error", "err", err) + return err + } + + if res.Code != saasapi.ErrorCode_SUCC { + err = fmt.Errorf("run script failed. code:%v, status:%v", res.Code, res.Status) + fmt.Fprintln(os.Stderr, err) + return err + } + + scriptRunRes := res.GetScriptRunRes() + fmt.Println("ERROROUT_OUTPUT:") + fmt.Print(scriptRunRes.GetErrorOutput()) + fmt.Println() + fmt.Println("PRINT_OUTPUT:") + fmt.Print(scriptRunRes.GetPrintOutput()) + fmt.Println() + fmt.Println("DATASPACE_OUTPUT:") + fmt.Print(scriptRunRes.GetDataspaceOut()) + fmt.Println() + fmt.Println("TARGETS_OUTPUT:") + fmt.Print(scriptRunRes.GetTargetsOutput()) + return nil +} diff --git a/pkg/saashttp/cfg.go b/pkg/saashttp/cfg.go index 5dcf72f..ed40f54 100644 --- a/pkg/saashttp/cfg.go +++ b/pkg/saashttp/cfg.go @@ -8,7 +8,6 @@ const ( columnWritePath = "/saas/column_write" taskCreatePath = "/saas/task/create" taskListPath = "/saas/task/list" - taskCancelPath = "/saas/task/delete" taskInfoPath = "/saas/task/info" taskUploadPath = "/saas/task/upload" taskDownloadPath = "/saas/task/download" @@ -17,6 +16,7 @@ const ( targetListPath = "/saas/target/list" bindSetPath = "/saas/bind/set" bindDeletePath = "/saas/bind/delete" + scriptRunPath = "/saas/script/run" ) type ApiUrls struct { @@ -35,6 +35,7 @@ type ApiUrls struct { TargetListPath string BindSetPath string BindDeletePath string + ScriptRunPath string } func InitAPIUrl(c *ApiUrls) *ApiUrls { @@ -129,6 +130,12 @@ func InitAPIUrl(c *ApiUrls) *ApiUrls { r.BindDeletePath = bindDeletePath } + if c.ScriptRunPath != "" { + r.ScriptRunPath = c.ScriptRunPath + } else { + r.ScriptRunPath = scriptRunPath + } + return r } diff --git a/pkg/saashttp/httpcli.go b/pkg/saashttp/httpcli.go index dc2dfb9..0229362 100644 --- a/pkg/saashttp/httpcli.go +++ b/pkg/saashttp/httpcli.go @@ -103,6 +103,11 @@ func (c *SaasClient) BindDelete(saasReq *saasapi.SaasReq) (saasRes *saasapi.Saas return c.post(postUrl, saasReq) } +func (c *SaasClient) ScriptRun(saasReq *saasapi.SaasReq) (saasRes *saasapi.SaasRes, err error) { + postUrl := c.makeUrl(c.ApiUrls.BaseUrl, c.ApiUrls.ScriptRunPath) + return c.post(postUrl, saasReq) +} + func (c *SaasClient) makeUrl(baseUrl, path string, params ...string) string { url, err := url.Parse(baseUrl) if err != nil {