Skip to content

Commit

Permalink
Add support for service's to configure working directory
Browse files Browse the repository at this point in the history
Fixes #158 (in preparation for #159)
  • Loading branch information
benhoyt committed Jun 26, 2023
1 parent 8902cbe commit 987e5d2
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,10 @@ services:
# group and group-id are specified, the group's GID must match group-id.
group-id: <gid>
# (Optional) Working directory to run command in. By default, the
# command is run in the service manager's current directory.
working-dir: <directory>
# (Optional) Defines what happens when the service exits with a zero
# exit code. Possible values are: "restart" (default) which restarts
# the service after the backoff delay, "shutdown" which shuts down and
Expand Down Expand Up @@ -653,7 +657,8 @@ checks:
# match group-id.
group-id: <gid>
# (Optional) Working directory to run command in.
# (Optional) Working directory to run command in. By default, the
# command is run in the service manager's current directory.
working-dir: <directory>
```

Expand Down
2 changes: 2 additions & 0 deletions internals/overlord/servstate/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,8 @@ func (s *serviceData) startInternal() error {
environment[k] = v
}

s.cmd.Dir = s.config.WorkingDir

// Start as another user if specified in plan.
uid, gid, err := osutil.NormalizeUidGid(s.config.UserID, s.config.GroupID, s.config.User, s.config.Group)
if err != nil {
Expand Down
59 changes: 59 additions & 0 deletions internals/overlord/servstate/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1763,3 +1763,62 @@ type fakeLogManager struct{}
func (f fakeLogManager) ServiceStarted(serviceName string, logs *servicelog.RingBuffer) {
// no-op
}

func (s *S) TestNoWorkingDir(c *C) {
// Service command should run in current directory (package directory)
// if "working-dir" config option not set.
dir := c.MkDir()
err := os.Mkdir(filepath.Join(dir, "layers"), 0755)
c.Assert(err, IsNil)
manager, err := servstate.NewManager(s.st, s.runner, dir, nil, nil, fakeLogManager{})
c.Assert(err, IsNil)
defer manager.Stop()

outputPath := filepath.Join(dir, "output")
layer := parseLayer(c, 0, "layer", fmt.Sprintf(`
services:
nowrkdir:
override: replace
command: /bin/sh -c "pwd >%s; sleep %g"
`, outputPath, shortOkayDelay.Seconds()+0.01))
err = manager.AppendLayer(layer)
c.Assert(err, IsNil)

chg := s.startServices(c, []string{"nowrkdir"}, 1)
s.st.Lock()
c.Assert(chg.Err(), IsNil)
s.st.Unlock()

output, err := ioutil.ReadFile(outputPath)
c.Assert(err, IsNil)
c.Check(string(output), Matches, ".*/overlord/servstate\n")
}

func (s *S) TestWorkingDir(c *C) {
dir := c.MkDir()
err := os.Mkdir(filepath.Join(dir, "layers"), 0755)
c.Assert(err, IsNil)
manager, err := servstate.NewManager(s.st, s.runner, dir, nil, nil, fakeLogManager{})
c.Assert(err, IsNil)
defer manager.Stop()

outputPath := filepath.Join(dir, "output")
layer := parseLayer(c, 0, "layer", fmt.Sprintf(`
services:
wrkdir:
override: replace
command: /bin/sh -c "pwd >%s; sleep %g"
working-dir: %s
`, outputPath, shortOkayDelay.Seconds()+0.01, dir))
err = manager.AppendLayer(layer)
c.Assert(err, IsNil)

chg := s.startServices(c, []string{"wrkdir"}, 1)
s.st.Lock()
c.Assert(chg.Err(), IsNil)
s.st.Unlock()

output, err := ioutil.ReadFile(outputPath)
c.Assert(err, IsNil)
c.Check(string(output), Equals, dir+"\n")
}
4 changes: 4 additions & 0 deletions internals/plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type Service struct {
User string `yaml:"user,omitempty"`
GroupID *int `yaml:"group-id,omitempty"`
Group string `yaml:"group,omitempty"`
WorkingDir string `yaml:"working-dir,omitempty"`

// Auto-restart and backoff functionality
OnSuccess ServiceAction `yaml:"on-success,omitempty"`
Expand Down Expand Up @@ -154,6 +155,9 @@ func (s *Service) Merge(other *Service) {
if other.Group != "" {
s.Group = other.Group
}
if other.WorkingDir != "" {
s.WorkingDir = other.WorkingDir
}
s.After = append(s.After, other.After...)
s.Before = append(s.Before, other.Before...)
s.Requires = append(s.Requires, other.Requires...)
Expand Down

0 comments on commit 987e5d2

Please sign in to comment.