Skip to content

Commit

Permalink
feat: support to start plugin in the server sub-cmd (#195)
Browse files Browse the repository at this point in the history
* feat: support to start plugin in the server sub-cmd

* add pull flag into the service sub-cmd
  • Loading branch information
LinuxSuRen authored Aug 31, 2023
1 parent c74eb67 commit 9efccb1
Show file tree
Hide file tree
Showing 8 changed files with 278 additions and 192 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ copy-restart: build-embed-ui
make copy
atest service restart

# plugins
plugin-git:
GOOS=${OS} go build -ldflags "-w -s" -o bin/atest-store-git extensions/store-git/main.go

test:
go test ./... -cover -v -coverprofile=coverage.out
go tool cover -func=coverage.out
Expand Down
37 changes: 33 additions & 4 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func createServerCmd(execer fakeruntime.Execer, gRPCServer gRPCServer, httpServe
flags.BoolVarP(&opt.printProto, "print-proto", "", false, "Print the proto content and exit")
flags.StringArrayVarP(&opt.localStorage, "local-storage", "", []string{"*.yaml"}, "The local storage path")
flags.StringVarP(&opt.consolePath, "console-path", "", "", "The path of the console")
flags.StringVarP(&opt.configDir, "config-dir", "", "$HOME/.config/atest", "The config directory")
flags.StringVarP(&opt.configDir, "config-dir", "", os.ExpandEnv("$HOME/.config/atest"), "The config directory")
flags.StringVarP(&opt.secretServer, "secret-server", "", "", "The secret server URL")
return
}
Expand Down Expand Up @@ -108,19 +108,28 @@ func (o *serverOption) runE(cmd *cobra.Command, args []string) (err error) {
template.SetSecretGetter(remote.NewGRPCSecretGetter(secretServer))
}

removeServer := server.NewRemoteServer(loader, remote.NewGRPCloaderFromStore(), secretServer, o.configDir)
remoteServer := server.NewRemoteServer(loader, remote.NewGRPCloaderFromStore(), secretServer, o.configDir)
kinds, storeKindsErr := remoteServer.GetStoreKinds(nil, nil)
if storeKindsErr != nil {
cmd.PrintErrf("failed to get store kinds, error: %p\n", storeKindsErr)
} else {
if err = startPlugins(o.execer, kinds); err != nil {
return
}
}

s := o.gRPCServer
go func() {
if gRPCServer, ok := s.(reflection.GRPCServer); ok {
reflection.Register(gRPCServer)
}
server.RegisterRunnerServer(s, removeServer)
server.RegisterRunnerServer(s, remoteServer)
log.Printf("gRPC server listening at %v", lis.Addr())
s.Serve(lis)
}()

mux := runtime.NewServeMux(runtime.WithMetadata(server.MetadataStoreFunc)) // runtime.WithIncomingHeaderMatcher(func(key string) (s string, b bool) {
err = server.RegisterRunnerHandlerServer(cmd.Context(), mux, removeServer)
err = server.RegisterRunnerHandlerServer(cmd.Context(), mux, remoteServer)
if err == nil {
mux.HandlePath(http.MethodGet, "/", frontEndHandlerWithLocation(o.consolePath))
mux.HandlePath(http.MethodGet, "/assets/{asset}", frontEndHandlerWithLocation(o.consolePath))
Expand All @@ -134,6 +143,26 @@ func (o *serverOption) runE(cmd *cobra.Command, args []string) (err error) {
return
}

func startPlugins(execer fakeruntime.Execer, kinds *server.StoreKinds) (err error) {
const socketPrefix = "unix://"

for _, kind := range kinds.Data {
if kind.Enabled && strings.HasPrefix(kind.Url, socketPrefix) {
binaryPath, lookErr := execer.LookPath(kind.Name)
if lookErr != nil {
log.Printf("failed to find %s, error: %v", kind.Name, lookErr)
} else {
go func(socketURL, plugin string) {
if err = execer.RunCommand(plugin, "--socket", strings.TrimPrefix(socketURL, socketPrefix)); err != nil {
log.Printf("failed to start %s, error: %v", socketURL, err)
}
}(kind.Url, binaryPath)
}
}
}
return
}

func frontEndHandlerWithLocation(consolePath string) func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
return func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
target := r.URL.Path
Expand Down
10 changes: 7 additions & 3 deletions cmd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Mirror Images: docker.m.daocloud.io/linuxsuren/api-testing`,
flags.StringVarP(&opt.mode, "mode", "m", string(ServiceModeOS),
fmt.Sprintf("Availeble values: %v", ServiceModeOS.All()))
flags.StringVarP(&opt.image, "image", "", defaultImage, "The image of the service which as a container")
flags.StringVarP(&opt.pull, "pull", "", "always", `Pull image before creating ("always"|"missing"|"never")`)
flags.StringVarP(&opt.version, "version", "", version.GetVersion(), "The version of the service image")
flags.StringVarP(&opt.localStorage, "local-storage", "", "/var/data/atest",
"The local storage path which will be mounted into the container")
Expand All @@ -56,6 +57,7 @@ type serviceOption struct {
fakeruntime.Execer
mode string
localStorage string
pull string

stdOut io.Writer
}
Expand Down Expand Up @@ -211,7 +213,7 @@ func (o *serviceOption) getContainerService() (service Service, err error) {
clientPath = client
}
service = newContainerService(o.Execer, clientPath,
o.image, o.version, o.localStorage, o.stdOut)
o.image, o.version, o.pull, o.localStorage, o.stdOut)
}
return
}
Expand Down Expand Up @@ -315,14 +317,15 @@ type containerService struct {
client string
image string
tag string
pull string
localStorage string
stdOut io.Writer
errOut io.Writer
}

const defaultImage = "ghcr.io/linuxsuren/api-testing"

func newContainerService(execer fakeruntime.Execer, client, image, tag, localStorage string, writer io.Writer) (svc Service) {
func newContainerService(execer fakeruntime.Execer, client, image, tag, pull, localStorage string, writer io.Writer) (svc Service) {
if tag == "" {
tag = "latest"
}
Expand All @@ -336,6 +339,7 @@ func newContainerService(execer fakeruntime.Execer, client, image, tag, localSto
name: service.ServiceName,
image: image,
tag: tag,
pull: pull,
localStorage: localStorage,
stdOut: writer,
errOut: writer,
Expand Down Expand Up @@ -397,7 +401,7 @@ func (s *containerService) getStartArgs() []string {
return []string{"run", "--name=" + s.name,
"--restart=always",
"-d",
"--pull=always",
fmt.Sprintf("--pull=%s", s.pull),
"--network=host",
"-v", s.localStorage + ":/var/www/data",
"-v", "/root/.config/atest:/root/.config/atest",
Expand Down
16 changes: 16 additions & 0 deletions pkg/server/remote_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,22 @@ func (s *server) FunctionsQueryStream(srv Runner_FunctionsQueryStreamServer) err
}
}

func (s *server) GetStoreKinds(context.Context, *Empty) (kinds *StoreKinds, err error) {
storeFactory := testing.NewStoreFactory(s.configDir)
var stores []testing.StoreKind
if stores, err = storeFactory.GetStoreKinds(); err == nil {
kinds = &StoreKinds{}
for _, store := range stores {
kinds.Data = append(kinds.Data, &StoreKind{
Name: store.Name,
Enabled: store.Enabled,
Url: store.URL,
})
}
}
return
}

func (s *server) GetStores(ctx context.Context, in *Empty) (reply *Stores, err error) {
storeFactory := testing.NewStoreFactory(s.configDir)
var stores []testing.Store
Expand Down
Loading

0 comments on commit 9efccb1

Please sign in to comment.