Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user param to on_init command section #68

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type InitConfig struct {
ExecTimeout time.Duration `mapstructure:"exec_timeout"`
// Env represents application environment.
Env map[string]string `mapstructure:"env"`
// Env represents UID
User string `mapstructure:"user"`
rustatian marked this conversation as resolved.
Show resolved Hide resolved
}

// RPCConfig should be in sync with rpc/config.go
Expand Down
27 changes: 16 additions & 11 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,36 @@ import (
"time"

"github.com/roadrunner-server/errors"
"github.com/roadrunner-server/sdk/v4/utils"
"go.uber.org/zap"
)

type command struct {
log *zap.Logger
env map[string]string
command []string
execTimeout time.Duration
log *zap.Logger
cfg *InitConfig
}

func newCommand(log *zap.Logger, env map[string]string, cmd []string, execTimeout time.Duration) *command {
func newCommand(log *zap.Logger, cfg *InitConfig) *command {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch with *InitConfig 👍

return &command{
log: log,
env: env,
command: cmd,
execTimeout: execTimeout,
log: log,
cfg: cfg,
}
}

func (b *command) start() error {
const op = errors.Op("server_on_init")
stopCh := make(chan struct{}, 1)

cmd := b.createProcess(b.env, b.command)
timer := time.NewTimer(b.execTimeout)
cmd := b.createProcess(b.cfg.Env, b.cfg.Command)

if b.cfg.User != "" {
err := utils.ExecuteFromUser(cmd, b.cfg.User)
if err != nil {
return errors.E(op, err)
}
}

timer := time.NewTimer(b.cfg.ExecTimeout)

err := cmd.Start()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (p *Plugin) Serve() chan error {
errCh := make(chan error, 1)

if p.cfg.OnInit != nil {
err := newCommand(p.log, p.cfg.OnInit.Env, p.cfg.OnInit.Command, p.cfg.OnInit.ExecTimeout).start()
err := newCommand(p.log, p.cfg.OnInit).start()
rustatian marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
p.log.Error("on_init was finished with errors", zap.Error(err))
}
Expand Down
Loading