Fix interaction with systemd (#274)
The library is using inconsistent names when calling `systemctl` commands. Let's say that the service is named `name`. The unit file will be then named `name.service` - this is the popular pattern used also by this library. While for most common commands like `systemctl start` there is no difference whether `name` or `name.service` will be used as the service identifier, the `list-unit-files` command expects explicitly the unit file name. Currently just the name - without the `.service` suffix - is being used, which causes the command to return no output, not match the name of the service and finally respond the status of installed but inactive service as `StatusUnknown` with `ErrNotInstalled` error. While `StatusStopped` without any error is expected. Considering `systemctl` behavior it's just easier to: - have one method that constructs the `name.service` name, - use it for all `systemctl` commands calls. And this is the change that this commit is adding.
This commit is contained in:
@@ -69,7 +69,7 @@ func (s *systemd) Platform() string {
|
||||
|
||||
func (s *systemd) configPath() (cp string, err error) {
|
||||
if !s.isUserService() {
|
||||
cp = "/etc/systemd/system/" + s.Config.Name + ".service"
|
||||
cp = "/etc/systemd/system/" + s.unitName()
|
||||
return
|
||||
}
|
||||
homeDir, err := os.UserHomeDir()
|
||||
@@ -81,10 +81,14 @@ func (s *systemd) configPath() (cp string, err error) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
cp = filepath.Join(systemdUserDir, s.Config.Name+".service")
|
||||
cp = filepath.Join(systemdUserDir, s.unitName())
|
||||
return
|
||||
}
|
||||
|
||||
func (s *systemd) unitName() string {
|
||||
return s.Config.Name + ".service"
|
||||
}
|
||||
|
||||
func (s *systemd) getSystemdVersion() int64 {
|
||||
_, out, err := runWithOutput("systemctl", "--version")
|
||||
if err != nil {
|
||||
@@ -230,7 +234,7 @@ func (s *systemd) Run() (err error) {
|
||||
}
|
||||
|
||||
func (s *systemd) Status() (Status, error) {
|
||||
exitCode, out, err := runWithOutput("systemctl", "is-active", s.Name)
|
||||
exitCode, out, err := runWithOutput("systemctl", "is-active", s.unitName())
|
||||
if exitCode == 0 && err != nil {
|
||||
return StatusUnknown, err
|
||||
}
|
||||
@@ -240,7 +244,7 @@ func (s *systemd) Status() (Status, error) {
|
||||
return StatusRunning, nil
|
||||
case strings.HasPrefix(out, "inactive"):
|
||||
// inactive can also mean its not installed, check unit files
|
||||
exitCode, out, err := runWithOutput("systemctl", "list-unit-files", "-t", "service", s.Name)
|
||||
exitCode, out, err := runWithOutput("systemctl", "list-unit-files", "-t", "service", s.unitName())
|
||||
if exitCode == 0 && err != nil {
|
||||
return StatusUnknown, err
|
||||
}
|
||||
@@ -279,7 +283,7 @@ func (s *systemd) run(action string, args ...string) error {
|
||||
}
|
||||
|
||||
func (s *systemd) runAction(action string) error {
|
||||
return s.run(action, s.Name+".service")
|
||||
return s.run(action, s.unitName())
|
||||
}
|
||||
|
||||
const systemdScript = `[Unit]
|
||||
|
||||
Reference in New Issue
Block a user