Skip to content
This repository has been archived by the owner on Jun 13, 2023. It is now read-only.

Use a more portable version of listUnits #44

Merged
merged 2 commits into from
Jun 11, 2018
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
3 changes: 2 additions & 1 deletion pkg/job/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/DataDog/pupernetes/pkg/config"
"github.com/DataDog/pupernetes/pkg/logging"
"github.com/DataDog/pupernetes/pkg/util"
)

const (
Expand Down Expand Up @@ -65,7 +66,7 @@ func RunSystemdJob(givenRootPath string) error {
if !strings.HasSuffix(unitName, ".service") {
unitName = unitName + ".service"
}
units, err := dbus.ListUnitsByNames([]string{unitName})
units, err := util.GetUnitStates(dbus, []string{unitName})
if err != nil {
glog.Errorf("Cannot get the status of %s: %v", unitName, err)
return err
Expand Down
5 changes: 3 additions & 2 deletions pkg/run/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package run

import (
"fmt"
"github.com/DataDog/pupernetes/pkg/util"
"github.com/golang/glog"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -31,15 +32,15 @@ func (r *Runtime) httpProbe(url string) error {
}

func (r *Runtime) probeUnitStatuses() ([]string, error) {
units, err := r.env.GetDBUSClient().ListUnitsByNames(r.env.GetSystemdUnits())
units, err := util.GetUnitStates(r.env.GetDBUSClient(), r.env.GetSystemdUnits())
if err != nil {
glog.Errorf("Unexpected error: %v", err)
return nil, err
}
var failed []string
for _, u := range units {
s := fmt.Sprintf("unit %q with load state %q is %q", u.Name, u.LoadState, u.SubState)
glog.V(3).Infof("%s", s)
glog.V(3).Infof("Current state: %s", s)
switch u.SubState {
case "running":
continue
Expand Down
7 changes: 6 additions & 1 deletion pkg/setup/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ func (e *Environment) renderTemplates(category string) error {
return err
}
glog.V(4).Infof("Rendering templates with the following metadata: %v", string(b))
prefix := ""
if category == defaultTemplates.ManifestSystemdUnit {
prefix = e.systemdUnitPrefix
glog.V(4).Infof("Currently rendering %s with file prefix %q", category, prefix)
}
for _, f := range files {
p := path.Join(sourceDir, f.Name())

Expand All @@ -73,7 +78,7 @@ func (e *Environment) renderTemplates(category string) error {
return err
}

destPath := path.Join(e.rootABSPath, category, f.Name())
destPath := path.Join(e.rootABSPath, category, prefix+f.Name())
glog.V(4).Infof("Rendering manifest %s to %s", f.Name(), destPath)
dest, err := os.OpenFile(destPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0444)
if err != nil {
Expand Down
7 changes: 3 additions & 4 deletions pkg/setup/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,16 @@ func (e *Environment) createEnd2EndSection() []*unit2.UnitOption {
}

func (e *Environment) createUnitFromTemplate(unitName string) error {
unitNameNoPrefix := strings.TrimPrefix(unitName, e.systemdUnitPrefix)
manifestUnitName := path.Join(e.manifestSystemdUnit, unitNameNoPrefix)
manifestUnitName := path.Join(e.manifestSystemdUnit, unitName)
fd, err := os.OpenFile(manifestUnitName, os.O_RDONLY, 0)
if err != nil {
glog.Errorf("Cannot read %s: %v", unitNameNoPrefix, err)
glog.Errorf("Cannot read %s: %v", manifestUnitName, err)
return err
}
defer fd.Close()
unitOptions, err := unit2.Deserialize(fd)
if err != nil {
glog.Errorf("Unexpected error during parsing s: %v", unitNameNoPrefix, err)
glog.Errorf("Unexpected error during parsing s: %v", manifestUnitName, err)
return err
}
// TODO see how to insert e.systemdEnd2EndSection
Expand Down
40 changes: 40 additions & 0 deletions pkg/util/systemd_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/coreos/go-systemd/dbus"
"github.com/golang/glog"
"strings"
)

func executeSystemdAction(unitName string, systemdAction func(string, string, chan<- string) (int, error)) error {
Expand All @@ -37,12 +38,51 @@ func executeSystemdAction(unitName string, systemdAction func(string, string, ch
}
}

// StartUnit call dbus to start the given unit name
func StartUnit(d *dbus.Conn, unitName string) error {
glog.Infof("Starting systemd unit: %s ...", unitName)
return executeSystemdAction(unitName, d.StartUnit)
}

// StopUnit call dbus to stop the given unit name
func StopUnit(d *dbus.Conn, unitName string) error {
glog.Infof("Stopping systemd unit: %s ...", unitName)
return executeSystemdAction(unitName, d.StopUnit)
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Exported method needs a comment

// GetUnitStates returns the dbus UnitStates of unit names passed in parameter
func GetUnitStates(d *dbus.Conn, unitNames []string) ([]dbus.UnitStatus, error) {
var units []dbus.UnitStatus

allUnits, err := d.ListUnits()
if err != nil {
glog.Errorf("Cannot ListUnits: %v", err)
return nil, err
}
intersectName := make(map[string][]dbus.UnitStatus)
for _, elt := range allUnits {
if !strings.HasSuffix(elt.Name, ".service") {
continue
}
// Note that units may be known by multiple names at the same time
intersectName[elt.Name] = append(intersectName[elt.Name], elt)
}
for _, wantedUnit := range unitNames {
unitStatuses, ok := intersectName[wantedUnit]
if !ok {
glog.V(2).Infof("cannot find %s in actual running units", wantedUnit)
continue
}
if len(unitStatuses) != 1 {
err := fmt.Errorf("invalid number of unitStatuses %s: %d", wantedUnit, len(unitStatuses))
glog.Errorf("Cannot select unit: %v", err)
for _, elt := range unitStatuses {
glog.Errorf("Dbus yield for %s: %s %d %s %s %s %s", wantedUnit, elt.Name, elt.JobId, elt.LoadState, elt.SubState)
}
return nil, err
}
glog.V(4).Infof("Found wanted unitStatus %s", wantedUnit)
units = append(units, unitStatuses[0])
}
return units, nil
}
4 changes: 3 additions & 1 deletion pkg/wait/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package wait
import (
"fmt"
"github.com/DataDog/pupernetes/pkg/logging"
"github.com/DataDog/pupernetes/pkg/util"
"github.com/coreos/go-systemd/dbus"
"github.com/golang/glog"
"strings"
Expand All @@ -26,7 +27,7 @@ func NewWaiter(systemdUnitName string, timeout, loggingSince time.Duration) *Wai
}

func getSystemdUnitState(conn *dbus.Conn, unitName string) (string, error) {
units, err := conn.ListUnitsByNames([]string{unitName})
units, err := util.GetUnitStates(conn, []string{unitName})
if err != nil {
glog.Errorf("Cannot list units: %v", err)
return "", err
Expand Down Expand Up @@ -82,6 +83,7 @@ func (w *Wait) Wait() error {
if err != nil {
return err
}
glog.V(4).Infof("Systemd unit %s is %s", w.systemdUnitName, state)
if state == "running" {
glog.Infof("Systemd unit %s is %s", w.systemdUnitName, state)
return nil
Expand Down