From 52211494c169f30a4524c965382708374a0a82d8 Mon Sep 17 00:00:00 2001 From: Jan Hajek Date: Fri, 5 Jun 2020 15:51:21 +0200 Subject: [PATCH] aaaa --- cmd/deploy.go | 2 +- cmd/root.go | 8 ++++---- cmd/startVpn.go | 6 +++--- src/command/login/handler.go | 2 +- src/command/startVpn/handler.go | 34 ++++++++++++++++++++++++++------ src/service/sudoers/handler.go | 2 +- src/service/zipClient/handler.go | 2 +- 7 files changed, 39 insertions(+), 17 deletions(-) diff --git a/cmd/deploy.go b/cmd/deploy.go index 0f509550..313c66ce 100644 --- a/cmd/deploy.go +++ b/cmd/deploy.go @@ -37,7 +37,7 @@ func deployCmd() *cobra.Command { defer closeFunc() httpClient := httpClient.New(httpClient.Config{ - HttpTimeout: time.Second * 10, + HttpTimeout: time.Minute * 15, }) zipClient := zipClient.New(zipClient.Config{}) diff --git a/cmd/root.go b/cmd/root.go index d6c21d7b..477bdadb 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -29,9 +29,9 @@ func ExecuteRootCmd(builtinToken string) { Use: "zcli", } - params.RegisterString(rootCmd, "restApiAddress", "https://api.zerops.io", "address of rest of zerops.io", paramsPackage.Persistent()) - params.RegisterString(rootCmd, "grpcApiAddress", "api.zerops.io:20902", "address of grpc api of zerops.io", paramsPackage.Persistent()) - params.RegisterString(rootCmd, "vpnAddress", "vpn.zerops.io", "address of vpn of zerops.io", paramsPackage.Persistent()) + params.RegisterString(rootCmd, "restApiAddress", "https://api.zerops.io", "address of rest api", paramsPackage.Persistent()) + params.RegisterString(rootCmd, "grpcApiAddress", "api.zerops.io:20902", "address of grpc api", paramsPackage.Persistent()) + params.RegisterString(rootCmd, "vpnApiAddress", "vpn.zerops.io", "address of vpn api", paramsPackage.Persistent()) params.RegisterString( rootCmd, "token", builtinToken, "authentication token", paramsPackage.Persistent(), @@ -53,7 +53,7 @@ func ExecuteRootCmd(builtinToken string) { err = rootCmd.Execute() if err != nil { - return + os.Exit(1) } } diff --git a/cmd/startVpn.go b/cmd/startVpn.go index 7a832a52..15ff952f 100644 --- a/cmd/startVpn.go +++ b/cmd/startVpn.go @@ -10,7 +10,7 @@ import ( func startVpnCmd() *cobra.Command { cmd := &cobra.Command{ - Use: "startVpn projectId", + Use: "startVpn projectName", SilenceUsage: true, Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { @@ -42,7 +42,7 @@ func startVpnCmd() *cobra.Command { return startVpn.New( startVpn.Config{ - VpnAddress: params.GetString("vpnAddress"), + VpnAddress: params.GetString("vpnApiAddress"), UserId: certReader.UserId, }, logger, @@ -50,7 +50,7 @@ func startVpnCmd() *cobra.Command { sudoers, storage, ).Run(ctx, startVpn.RunConfig{ - ProjectId: args[0], + ProjectName: args[0], }) }, } diff --git a/src/command/login/handler.go b/src/command/login/handler.go index eff23806..b1dcf611 100644 --- a/src/command/login/handler.go +++ b/src/command/login/handler.go @@ -48,7 +48,7 @@ func (h *Handler) Run(_ context.Context, runConfig RunConfig) error { return errors.New("param zeropsLogin must be set") } if runConfig.ZeropsPassword == "" { - return errors.New("param ZeropsPassword must be set") + return errors.New("param zeropsPassword must be set") } loginData, err := json.Marshal(struct { diff --git a/src/command/startVpn/handler.go b/src/command/startVpn/handler.go index 77a0a752..9a47098c 100644 --- a/src/command/startVpn/handler.go +++ b/src/command/startVpn/handler.go @@ -23,7 +23,7 @@ type Config struct { } type RunConfig struct { - ProjectId string + ProjectName string } type Handler struct { @@ -52,13 +52,35 @@ func New( func (h *Handler) Run(ctx context.Context, config RunConfig) error { - if h.storage.Data.ProjectId != "" && config.ProjectId != h.storage.Data.ProjectId { + if config.ProjectName == "" { + return errors.New("project name must be filled") + } + + projectsResponse, err := h.apiGrpcClient.GetProjectsByName(ctx, &zeropsApiProtocol.GetProjectsByNameRequest{ + Name: config.ProjectName, + }) + if err := helpers.HandleGrpcApiError(projectsResponse, err); err != nil { + return err + } + + projectsResponse.GetOutput().GetProjects() + + projects := projectsResponse.GetOutput().GetProjects() + if len(projects) == 0 { + return errors.New("project not found") + } + if len(projects) > 1 { + return errors.New("there are multiple project with same name") + } + project := projects[0] + + if h.storage.Data.ProjectId != "" && project.GetId() != h.storage.Data.ProjectId { if h.isVpnAlive() { return errors.New("vpn is started for another project, use stopVpn first") } } - err := h.cleanVpn() + err = h.cleanVpn() if err != nil { return err } @@ -69,7 +91,7 @@ func (h *Handler) Run(ctx context.Context, config RunConfig) error { } apiVpnRequestResponse, err := h.apiGrpcClient.PostVpnRequest(ctx, &zeropsApiProtocol.PostVpnRequestRequest{ - Id: config.ProjectId, + Id: project.GetId(), ClientPublicKey: publicKey, }) if err := helpers.HandleGrpcApiError(apiVpnRequestResponse, err); err != nil { @@ -97,7 +119,7 @@ func (h *Handler) Run(ctx context.Context, config RunConfig) error { defer closeFunc() startVpnResponse, err := vpnGrpcClient.StartVpn(ctx, &zeropsVpnProtocol.StartVpnRequest{ - InstanceId: config.ProjectId, + InstanceId: project.GetId(), UserId: h.config.UserId, ClientPublicKey: publicKey, Signature: signature, @@ -114,7 +136,7 @@ func (h *Handler) Run(ctx context.Context, config RunConfig) error { h.logger.Info("\nclient is connected \n") - h.storage.Data.ProjectId = config.ProjectId + h.storage.Data.ProjectId = project.GetId() err = h.storage.Save() if err != nil { return err diff --git a/src/service/sudoers/handler.go b/src/service/sudoers/handler.go index 645be031..683d5378 100644 --- a/src/service/sudoers/handler.go +++ b/src/service/sudoers/handler.go @@ -61,7 +61,7 @@ func runCommand(cmd *exec.Cmd) ([]byte, error) { } } } - return nil, errors.New(errOutput.String()) + return nil, err } return output.Bytes(), nil diff --git a/src/service/zipClient/handler.go b/src/service/zipClient/handler.go index d5fa7bfe..73673bf5 100644 --- a/src/service/zipClient/handler.go +++ b/src/service/zipClient/handler.go @@ -27,7 +27,7 @@ func (h *Handler) Zip(source string, w io.Writer) error { info, err := os.Stat(source) if err != nil { - return nil + return err } var baseDir string