Skip to content

Commit

Permalink
feat: set module plugin working dir (#1247)
Browse files Browse the repository at this point in the history
  • Loading branch information
liu-hm19 authored Aug 1, 2024
1 parent 881d542 commit a687f8f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ require (
github.com/hashicorp/hc-install v0.6.2
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/imdario/mergo v0.3.16
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
Expand Down
2 changes: 1 addition & 1 deletion pkg/modules/generators/app_configurations_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ func (g *appConfigurationGenerator) invokeModule(
) (*proto.GeneratorResponse, error) {
// init the plugin
if pluginMap[key] == nil {
plugin, err := modules.NewPlugin(key)
plugin, err := modules.NewPlugin(key, g.stack.Path)
if err != nil {
return nil, err
}
Expand Down
15 changes: 10 additions & 5 deletions pkg/modules/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ type Plugin struct {
client *plugin.Client
// Module represents the real module impl
Module Module
// dir represents the working directory of the plugin binary, which will be typically set as the stack path.
dir string
}

func NewPlugin(key string) (*Plugin, error) {
func NewPlugin(key, dir string) (*Plugin, error) {
if key == "" {
return nil, fmt.Errorf("module key can not be empty")
}
p := &Plugin{key: key}
p := &Plugin{key: key, dir: dir}
err := p.initModule()
if err != nil {
return nil, err
Expand All @@ -75,7 +77,7 @@ func (p *Plugin) initModule() error {
return err
}
pluginName := prefix[0] + "-" + prefix[1]
client, err := NewPluginClient(pluginPath, pluginName)
client, err := NewPluginClient(pluginPath, pluginName, p.dir)
if err != nil {
return err
}
Expand Down Expand Up @@ -122,7 +124,7 @@ func buildPluginPath(namespace, resourceType, version string) (string, error) {
return p, nil
}

func NewPluginClient(modulePluginPath, moduleName string) (*plugin.Client, error) {
func NewPluginClient(modulePluginPath, moduleName, workingDir string) (*plugin.Client, error) {
// create the plugin log file
var logFilePath string
dir, err := kfile.KusionDataFolder()
Expand All @@ -148,11 +150,14 @@ func NewPluginClient(modulePluginPath, moduleName string) (*plugin.Client, error
Level: hclog.Debug,
})

cmd := exec.Command(modulePluginPath)
cmd.Dir = workingDir

// We're a host! Start by launching the plugin process.Need to defer kill
client := plugin.NewClient(&plugin.ClientConfig{
HandshakeConfig: HandshakeConfig,
Plugins: PluginMap,
Cmd: exec.Command(modulePluginPath),
Cmd: cmd,
AllowedProtocols: []plugin.Protocol{
plugin.ProtocolGRPC,
},
Expand Down

0 comments on commit a687f8f

Please sign in to comment.