-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web_server_bin_cmd): add
Web Service Binary Command Execution
…
…gRPC Server BREAKING CHANGE: the `bifrost` gRPC server APIs have added `WebServerBinCMD`.`Exec` server The `protobuf` of the `bifrost` gRPC server APIs has been added as follows: Protocols of addition: ```protobuf service WebServerBinCMD { rpc Exec(ExecuteRequest) returns (ExecuteResponse) {} } message ExecuteRequest { string ServerName = 1; repeated string Args = 2; } message ExecuteResponse { bool Successful = 1; bytes Stdout = 2; bytes Stderr = 3; } ``` The `bifrost` gRPC server APIs client SDK has been added as follows: Methods of addition to Client Service Factory: ```go type Factory interface { ... WebServerBinCMD() WebServerBinCMDService } ``` Interface of addition to Client Service: ```go type WebServerBinCMDService interface { Exec(servername string, arg ...string) (isSuccessful bool, stdout, stderr string, err error) } ```
- Loading branch information
Showing
47 changed files
with
948 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package v1 | ||
|
||
type ExecuteRequest struct { | ||
ServerName string `json:"server_name"` | ||
Args []string `json:"args"` | ||
} | ||
type ExecuteResponse struct { | ||
Successful bool `json:"successful"` | ||
StandardOutput []byte `json:"stdout"` | ||
StandardError []byte `json:"stderr"` | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package v1 | ||
|
||
import "github.com/go-kit/kit/endpoint" | ||
|
||
type WebServerBinCMDEndpoints interface { | ||
EndpointExec() endpoint.Endpoint | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package web_server_bin_cmd | ||
|
||
import ( | ||
"context" | ||
v1 "github.com/ClessLi/bifrost/api/bifrost/v1" | ||
|
||
"github.com/go-kit/kit/endpoint" | ||
"github.com/marmotedu/errors" | ||
) | ||
|
||
func (w *webServerBinCMDEndpoints) EndpointExec() endpoint.Endpoint { | ||
return func(ctx context.Context, request interface{}) (response interface{}, err error) { | ||
if req, ok := request.(*v1.ExecuteRequest); ok { | ||
return w.svc.WebServerBinCMD().Exec(ctx, req) | ||
} | ||
|
||
return nil, errors.Errorf("invalid get request, need *pbv1.ExecuteRequest, not %T", request) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
internal/bifrost/endpoint/v1/web_server_bin_cmd/web_server_status.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package web_server_bin_cmd | ||
|
||
import svcv1 "github.com/ClessLi/bifrost/internal/bifrost/service/v1" | ||
|
||
type webServerBinCMDEndpoints struct { | ||
svc svcv1.ServiceFactory | ||
} | ||
|
||
func NewWebServerBinCMDEndpoints(svc svcv1.ServiceFactory) *webServerBinCMDEndpoints { | ||
return &webServerBinCMDEndpoints{svc: svc} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package logging | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"time" | ||
|
||
v1 "github.com/ClessLi/bifrost/api/bifrost/v1" | ||
svcv1 "github.com/ClessLi/bifrost/internal/bifrost/service/v1" | ||
) | ||
|
||
type loggingWebServerBinCMDService struct { | ||
svc svcv1.WebServerBinCMDService | ||
} | ||
|
||
func (l *loggingWebServerBinCMDService) Exec(ctx context.Context, request *v1.ExecuteRequest) (response *v1.ExecuteResponse, err error) { | ||
defer func(begin time.Time) { | ||
logF := newLogFormatter(ctx, l.svc.Exec) | ||
logF.SetBeginTime(begin) | ||
defer logF.Result() | ||
if response != nil { | ||
result, _ := json.Marshal(response) | ||
logF.SetResult(getLimitResult(result)) | ||
} | ||
logF.SetErr(err) | ||
}(time.Now().Local()) | ||
|
||
return l.svc.Exec(ctx, request) | ||
} | ||
|
||
func newWebServerBinCMDMiddleware(svc svcv1.ServiceFactory) svcv1.WebServerBinCMDService { | ||
return &loggingWebServerBinCMDService{svc: svc.WebServerBinCMD()} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package v1 | ||
|
||
import ( | ||
"context" | ||
|
||
v1 "github.com/ClessLi/bifrost/api/bifrost/v1" | ||
) | ||
|
||
type WebServerBinCMDService interface { | ||
Exec(ctx context.Context, request *v1.ExecuteRequest) (*v1.ExecuteResponse, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package web_server_bin_cmd | ||
|
||
import ( | ||
"context" | ||
|
||
v1 "github.com/ClessLi/bifrost/api/bifrost/v1" | ||
) | ||
|
||
func (w *webServerBinCMDService) Exec(ctx context.Context, req *v1.ExecuteRequest) (*v1.ExecuteResponse, error) { | ||
return w.store.WebServerBinCMD().Exec(ctx, req) | ||
} |
11 changes: 11 additions & 0 deletions
11
internal/bifrost/service/v1/web_server_bin_cmd/web_server_status.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package web_server_bin_cmd | ||
|
||
import storev1 "github.com/ClessLi/bifrost/internal/bifrost/store/v1" | ||
|
||
type webServerBinCMDService struct { | ||
store storev1.StoreFactory | ||
} | ||
|
||
func NewWebServerBinCMDService(store storev1.StoreFactory) *webServerBinCMDService { | ||
return &webServerBinCMDService{store: store} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package nginx | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
v1 "github.com/ClessLi/bifrost/api/bifrost/v1" | ||
storev1 "github.com/ClessLi/bifrost/internal/bifrost/store/v1" | ||
"github.com/ClessLi/bifrost/internal/pkg/code" | ||
"github.com/marmotedu/errors" | ||
"os/exec" | ||
) | ||
|
||
type webServerBinCMDStore struct { | ||
serversBinCMD map[string]func(arg ...string) *exec.Cmd | ||
} | ||
|
||
func (w webServerBinCMDStore) Exec(ctx context.Context, request *v1.ExecuteRequest) (*v1.ExecuteResponse, error) { | ||
if f, has := w.serversBinCMD[request.ServerName]; has { | ||
cmd := f(request.Args...) | ||
|
||
var stdout, stderr bytes.Buffer | ||
cmd.Stdout = &stdout | ||
cmd.Stderr = &stderr | ||
|
||
var isSuccessful = cmd.Run() == nil | ||
|
||
return &v1.ExecuteResponse{ | ||
Successful: isSuccessful, | ||
StandardOutput: stdout.Bytes(), | ||
StandardError: stderr.Bytes(), | ||
}, nil | ||
} | ||
|
||
return nil, errors.WithCode(code.ErrWebServerNotFound, "nginx server '%s' not found", request.ServerName) | ||
} | ||
|
||
var _ storev1.WebServerBinCMDStore = &webServerBinCMDStore{} | ||
|
||
func newNginxBinCMDStore(store *webServerStore) storev1.WebServerBinCMDStore { | ||
return &webServerBinCMDStore{serversBinCMD: store.configsManger.GetServersBinCMD()} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package v1 | ||
|
||
import ( | ||
"context" | ||
|
||
v1 "github.com/ClessLi/bifrost/api/bifrost/v1" | ||
) | ||
|
||
type WebServerBinCMDStore interface { | ||
Exec(ctx context.Context, request *v1.ExecuteRequest) (*v1.ExecuteResponse, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
internal/bifrost/transport/v1/decoder/web_server_bin_cmd.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package decoder | ||
|
||
import ( | ||
"context" | ||
v1 "github.com/ClessLi/bifrost/api/bifrost/v1" | ||
|
||
"github.com/marmotedu/errors" | ||
|
||
pbv1 "github.com/ClessLi/bifrost/api/protobuf-spec/bifrostpb/v1" | ||
"github.com/ClessLi/bifrost/internal/pkg/code" | ||
) | ||
|
||
type webServerBinCMD struct{} | ||
|
||
var _ Decoder = webServerBinCMD{} | ||
|
||
func (w webServerBinCMD) DecodeRequest(ctx context.Context, r interface{}) (interface{}, error) { | ||
switch r := r.(type) { | ||
case *pbv1.ExecuteRequest: | ||
return &v1.ExecuteRequest{ | ||
ServerName: r.ServerName, | ||
Args: r.Args, | ||
}, nil | ||
default: | ||
return nil, errors.WithCode(code.ErrDecodingFailed, "invalid execute request: %v", r) | ||
} | ||
} | ||
|
||
func NewWebServerBinCMDDecoder() Decoder { | ||
return new(webServerBinCMD) | ||
} |
Oops, something went wrong.