Skip to content
This repository was archived by the owner on May 14, 2022. It is now read-only.

Commit 738e84c

Browse files
committed
feat: try gRPC approach
1 parent 6134f38 commit 738e84c

11 files changed

+123
-201
lines changed

README.md

+4-38
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,9 @@
11
# StackHead Plugin API
22

3-
## Expected structure
3+
## Requirements
44

5-
```go
6-
package main
5+
...
76

8-
import "github.com/getstackhead/pluginlib"
7+
## Example
98

10-
type MyPlugin struct {
11-
}
12-
13-
var PluginConfig = pluginlib.PluginConfig{
14-
Name: "Test",
15-
Description: "This is an example StackHead Proxy plugin",
16-
Version: "0.0.0-dev",
17-
Authors: []string{"Your Name"},
18-
PluginType: pluginlib.PluginType.PROXY,
19-
}
20-
21-
func (p MyPlugin) Setup() {
22-
// implement software setup action
23-
}
24-
25-
func (p MyPlugin) Deploy(project pluginlib.Project) {
26-
// implement project deploy action
27-
}
28-
29-
func (p MyPlugin) Destroy(project pluginlib.Project) {
30-
// implement project destroy action
31-
}
32-
33-
func (p MyPlugin) TriggerHook(hookName string, project pluginlib.Project) {
34-
// triggered by hooks
35-
}
36-
37-
// Export plugin to StackHead. Must be named "Plugin"!
38-
var Plugin MyPlugin
39-
```
40-
41-
```shell
42-
go build -buildmode=plugin -o plugin.so main.go
43-
```
9+
See [./example](./example)

events.go

-17
This file was deleted.

example/main.go

+35-23
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,47 @@
11
package main
22

33
import (
4-
"fmt"
4+
"log"
5+
"net"
56

6-
"github.com/getstackhead/pluginlib"
7-
)
8-
9-
var Plugin MyPlugin
10-
var PluginConfig = pluginlib.PluginConfig{
11-
Name: "Test",
12-
Description: "This is an example StackHead Proxy plugin",
13-
Version: "0.0.0-dev",
14-
Authors: []string{"Mario Lubenka"},
15-
PluginType: pluginlib.PluginType.PROXY,
16-
}
7+
"github.com/phayes/freeport"
8+
"google.golang.org/grpc"
179

18-
type MyPlugin struct {
19-
}
10+
pb "github.com/getstackhead/pluginlib/gen"
11+
)
2012

21-
func (p MyPlugin) Deploy(project pluginlib.Project) {
22-
fmt.Println("Hello " + project.Name)
23-
}
13+
var Plugin MyPluginWrapper
2414

25-
func (p MyPlugin) Destroy(project pluginlib.Project) {
26-
fmt.Println("Bye " + project.Name)
15+
type MyPluginWrapper struct {
2716
}
2817

29-
func (p MyPlugin) Setup() {
30-
fmt.Println("Setup...")
18+
type plugin struct {
19+
pb.UnimplementedStackHeadPlugin
3120
}
3221

33-
func (p MyPlugin) TriggerHook(hookName string, project pluginlib.Project) {
34-
fmt.Println("Hook " + hookName + " triggered for project " + project.Name + ".")
22+
func (p MyPluginWrapper) LaunchServer(callback func(int)) {
23+
// Set up connection with local StackHead server
24+
conn, err := grpc.Dial("localhost:1412", grpc.WithInsecure(), grpc.WithBlock())
25+
if err != nil {
26+
log.Fatalf("did not connect: %v", err)
27+
}
28+
defer conn.Close()
29+
StackHeadClient := pb.NewStackHeadServiceClient(conn)
30+
31+
port, err := freeport.GetFreePort()
32+
if err != nil {
33+
log.Fatal(err)
34+
return
35+
}
36+
lis, err := net.Listen("tcp", ":"+port)
37+
if err != nil {
38+
log.Fatalf("failed to listen: %v", err)
39+
}
40+
s := grpc.NewServer()
41+
pb.RegisterStackHeadPluginServer(s, &plugin{})
42+
log.Printf("server listening at %v", lis.Addr())
43+
if err := s.Serve(lis); err != nil {
44+
log.Fatalf("failed to serve: %v", err)
45+
}
46+
callback(port)
3547
}

go.mod

-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
module github.com/getstackhead/pluginlib
22

33
go 1.17
4-
5-
require (
6-
github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef
7-
github.com/valyala/gorpc v0.0.0-20160519171614-908281bef774
8-
)

go.sum

-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +0,0 @@
1-
github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef h1:2JGTg6JapxP9/R33ZaagQtAM4EkkSYnIAlOG5EI8gkM=
2-
github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef/go.mod h1:JS7hed4L1fj0hXcyEejnW57/7LCetXggd+vwrRnYeII=
3-
github.com/valyala/gorpc v0.0.0-20160519171614-908281bef774 h1:SUHFQHAaySqF0YHCmmm0EIFooFZpDPpi5KTom7YJ07c=
4-
github.com/valyala/gorpc v0.0.0-20160519171614-908281bef774/go.mod h1:8uNqM1i7pr0jO7gdvbNCgsSa8Ki2vMh7JCQxO9BlF90=

install.go

-13
This file was deleted.

plugin.go

+1-42
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,5 @@
11
package pluginlib
22

3-
import (
4-
"plugin"
5-
)
6-
73
type Plugin interface {
8-
Setup() <-chan error
9-
Deploy(project Project) <-chan error
10-
Destroy(project Project) <-chan error
11-
TriggerHook(hookName string, project Project)
12-
}
13-
14-
func LoadPlugin(path string) (Plugin, *PluginConfig, error) {
15-
// load module
16-
plug, err := plugin.Open(path)
17-
if err != nil {
18-
return nil, nil, err
19-
}
20-
21-
// 2. look up a symbol (an exported function or variable)
22-
symPlugin, err := plug.Lookup("Plugin")
23-
if err != nil {
24-
return nil, nil, err
25-
}
26-
var ok bool
27-
var pluginObj Plugin
28-
pluginObj, ok = symPlugin.(Plugin)
29-
if !ok {
30-
return nil, nil, err
31-
}
32-
33-
// 3. look up a symbol (an exported function or variable)
34-
symPluginConfig, err := plug.Lookup("PluginConfig")
35-
if err != nil {
36-
return nil, nil, err
37-
}
38-
var pluginConfig *PluginConfig
39-
pluginConfig, ok = symPluginConfig.(*PluginConfig)
40-
if !ok {
41-
return nil, nil, err
42-
}
43-
44-
// 4. Assert that loaded symbol is of a desired type
45-
return pluginObj, pluginConfig, nil
4+
LaunchServer(callback func(int))
465
}

plugin_config.go

-31
This file was deleted.

project.go

-5
This file was deleted.

stackhead.go

-23
This file was deleted.

stackhead.proto

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
syntax = "proto3";
2+
3+
option go_package = "github.com/getstackhead/pluginlib/gen";
4+
option java_multiple_files = true;
5+
option java_package = "io.stackhead.pluginlib.gen";
6+
option java_outer_classname = "pluginlib";
7+
8+
package pluginlib;
9+
10+
// StackHeadService is implemented by StackHead and allows
11+
// plugins to interact with Terraform or the package managers
12+
service StackHeadService {
13+
rpc Execute(StackHeadServiceRequest) returns (StackHeadServiceReply) {}
14+
rpc InstallPackage(StackHeadPackageInformation) returns (StackHeadServiceReply) {}
15+
rpc UninstallPackage(StackHeadPackageInformation) returns (StackHeadServiceReply) {}
16+
}
17+
18+
service StackHeadPlugin {
19+
rpc GetConfig(NullRequest) returns (StackHeadPluginConfig) {}
20+
rpc Setup(SetupRequest) returns (StackHeadPluginReply) {}
21+
rpc Deploy(DeployRequest) returns (StackHeadPluginReply) {}
22+
rpc Destroy(DestroyRequest) returns (StackHeadPluginReply) {}
23+
rpc TriggerHook(HookRequest) returns (StackHeadPluginReply) {}
24+
}
25+
26+
message NullRequest{}
27+
28+
message StackHeadPluginConfig {
29+
string AdditionalJsonData = 1;
30+
string Name = 2;
31+
string Description = 3;
32+
optional string Version = 4;
33+
repeated string Authors = 5;
34+
string PluginType = 6;
35+
optional StackHeadPluginTerraformConfig Terraform = 7;
36+
}
37+
38+
message StackHeadPluginTerraformConfig {
39+
string Name = 1;
40+
string vendor = 2;
41+
string version = 3;
42+
string resourceName = 4;
43+
string init = 5;
44+
bool providerPerProject = 6;
45+
}
46+
47+
message SetupRequest {
48+
}
49+
50+
message HookRequest {
51+
string hookName = 1;
52+
StackHeadProject project = 2;
53+
}
54+
message DeployRequest {
55+
StackHeadProject project = 1;
56+
}
57+
message DestroyRequest {
58+
StackHeadProject project = 1;
59+
}
60+
61+
message StackHeadProject {
62+
string AdditionalJsonData = 1;
63+
string Name = 2;
64+
}
65+
66+
message StackHeadPackageInformation {
67+
string ApkPackageName = 1;
68+
}
69+
70+
message StackHeadServiceRequest {
71+
string Command = 1;
72+
string Data = 2;
73+
}
74+
75+
message StackHeadServiceReply {
76+
bool success = 1;
77+
string errorMessage = 2;
78+
}
79+
80+
message StackHeadPluginReply {
81+
bool success = 1;
82+
string errorMessage = 2;
83+
}

0 commit comments

Comments
 (0)