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 support for services to configure working directory #245

Merged
merged 2 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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"
benhoyt marked this conversation as resolved.
Show resolved Hide resolved
`, 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
}
benhoyt marked this conversation as resolved.
Show resolved Hide resolved
s.After = append(s.After, other.After...)
s.Before = append(s.Before, other.Before...)
s.Requires = append(s.Requires, other.Requires...)
Expand Down