diff --git a/config.go b/config.go index b92c00c..d25651a 100644 --- a/config.go +++ b/config.go @@ -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"` } // RPCConfig should be in sync with rpc/config.go diff --git a/init.go b/init.go index 185dccf..21e9cec 100644 --- a/init.go +++ b/init.go @@ -8,22 +8,19 @@ 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 { return &command{ - log: log, - env: env, - command: cmd, - execTimeout: execTimeout, + log: log, + cfg: cfg, } } @@ -31,8 +28,16 @@ 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 { diff --git a/plugin.go b/plugin.go index c3ccc64..1ee70c9 100644 --- a/plugin.go +++ b/plugin.go @@ -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() if err != nil { p.log.Error("on_init was finished with errors", zap.Error(err)) }