mirror of
https://github.com/containers/podman.git
synced 2025-09-17 23:18:39 +08:00
pkg/systemd: add dbus support
Move the dbus-connection code from libpod's healthcheck to pkg/systemd to allow for sharing the logic. Needed for the auto-updates work. Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
This commit is contained in:
@ -4,50 +4,14 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/containers/libpod/pkg/rootless"
|
"github.com/containers/libpod/pkg/rootless"
|
||||||
"github.com/coreos/go-systemd/v22/dbus"
|
"github.com/containers/libpod/pkg/systemd"
|
||||||
godbus "github.com/godbus/dbus/v5"
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func dbusAuthRootlessConnection(createBus func(opts ...godbus.ConnOption) (*godbus.Conn, error)) (*godbus.Conn, error) {
|
|
||||||
conn, err := createBus()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
methods := []godbus.Auth{godbus.AuthExternal(strconv.Itoa(rootless.GetRootlessUID()))}
|
|
||||||
|
|
||||||
err = conn.Auth(methods)
|
|
||||||
if err != nil {
|
|
||||||
conn.Close()
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return conn, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func newRootlessConnection() (*dbus.Conn, error) {
|
|
||||||
return dbus.NewConnection(func() (*godbus.Conn, error) {
|
|
||||||
return dbusAuthRootlessConnection(func(opts ...godbus.ConnOption) (*godbus.Conn, error) {
|
|
||||||
path := filepath.Join(os.Getenv("XDG_RUNTIME_DIR"), "systemd/private")
|
|
||||||
return godbus.Dial(fmt.Sprintf("unix:path=%s", path))
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func getConnection() (*dbus.Conn, error) {
|
|
||||||
if rootless.IsRootless() {
|
|
||||||
return newRootlessConnection()
|
|
||||||
}
|
|
||||||
return dbus.NewSystemdConnection()
|
|
||||||
}
|
|
||||||
|
|
||||||
// createTimer systemd timers for healthchecks of a container
|
// createTimer systemd timers for healthchecks of a container
|
||||||
func (c *Container) createTimer() error {
|
func (c *Container) createTimer() error {
|
||||||
if c.disableHealthCheckSystemd() {
|
if c.disableHealthCheckSystemd() {
|
||||||
@ -64,7 +28,7 @@ func (c *Container) createTimer() error {
|
|||||||
}
|
}
|
||||||
cmd = append(cmd, "--unit", c.ID(), fmt.Sprintf("--on-unit-inactive=%s", c.HealthCheckConfig().Interval.String()), "--timer-property=AccuracySec=1s", podman, "healthcheck", "run", c.ID())
|
cmd = append(cmd, "--unit", c.ID(), fmt.Sprintf("--on-unit-inactive=%s", c.HealthCheckConfig().Interval.String()), "--timer-property=AccuracySec=1s", podman, "healthcheck", "run", c.ID())
|
||||||
|
|
||||||
conn, err := getConnection()
|
conn, err := systemd.ConnectToDBUS()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "unable to get systemd connection to add healthchecks")
|
return errors.Wrapf(err, "unable to get systemd connection to add healthchecks")
|
||||||
}
|
}
|
||||||
@ -83,7 +47,7 @@ func (c *Container) startTimer() error {
|
|||||||
if c.disableHealthCheckSystemd() {
|
if c.disableHealthCheckSystemd() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
conn, err := getConnection()
|
conn, err := systemd.ConnectToDBUS()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "unable to get systemd connection to start healthchecks")
|
return errors.Wrapf(err, "unable to get systemd connection to start healthchecks")
|
||||||
}
|
}
|
||||||
@ -98,7 +62,7 @@ func (c *Container) removeTimer() error {
|
|||||||
if c.disableHealthCheckSystemd() {
|
if c.disableHealthCheckSystemd() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
conn, err := getConnection()
|
conn, err := systemd.ConnectToDBUS()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "unable to get systemd connection to remove healthchecks")
|
return errors.Wrapf(err, "unable to get systemd connection to remove healthchecks")
|
||||||
}
|
}
|
||||||
|
47
pkg/systemd/dbus.go
Normal file
47
pkg/systemd/dbus.go
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
package systemd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/containers/libpod/pkg/rootless"
|
||||||
|
"github.com/coreos/go-systemd/v22/dbus"
|
||||||
|
godbus "github.com/godbus/dbus/v5"
|
||||||
|
)
|
||||||
|
|
||||||
|
func dbusAuthRootlessConnection(createBus func(opts ...godbus.ConnOption) (*godbus.Conn, error)) (*godbus.Conn, error) {
|
||||||
|
conn, err := createBus()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
methods := []godbus.Auth{godbus.AuthExternal(strconv.Itoa(rootless.GetRootlessUID()))}
|
||||||
|
|
||||||
|
err = conn.Auth(methods)
|
||||||
|
if err != nil {
|
||||||
|
conn.Close()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return conn, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func newRootlessConnection() (*dbus.Conn, error) {
|
||||||
|
return dbus.NewConnection(func() (*godbus.Conn, error) {
|
||||||
|
return dbusAuthRootlessConnection(func(opts ...godbus.ConnOption) (*godbus.Conn, error) {
|
||||||
|
path := filepath.Join(os.Getenv("XDG_RUNTIME_DIR"), "systemd/private")
|
||||||
|
return godbus.Dial(fmt.Sprintf("unix:path=%s", path))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConnectToDBUS returns a DBUS connection. It works both as root and non-root
|
||||||
|
// users.
|
||||||
|
func ConnectToDBUS() (*dbus.Conn, error) {
|
||||||
|
if rootless.IsRootless() {
|
||||||
|
return newRootlessConnection()
|
||||||
|
}
|
||||||
|
return dbus.NewSystemdConnection()
|
||||||
|
}
|
Reference in New Issue
Block a user