systemd.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package rkt
  14. import (
  15. "fmt"
  16. "strconv"
  17. "strings"
  18. "github.com/coreos/go-systemd/dbus"
  19. )
  20. // systemdVersion is a type wraps the int to implement kubecontainer.Version interface.
  21. type systemdVersion int
  22. func (s systemdVersion) String() string {
  23. return fmt.Sprintf("%d", s)
  24. }
  25. func (s systemdVersion) Compare(other string) (int, error) {
  26. v, err := strconv.Atoi(other)
  27. if err != nil {
  28. return -1, err
  29. }
  30. if int(s) < v {
  31. return -1, nil
  32. } else if int(s) > v {
  33. return 1, nil
  34. }
  35. return 0, nil
  36. }
  37. // systemdInterface is an abstraction of the go-systemd/dbus to make
  38. // it mockable for testing.
  39. // TODO(yifan): Eventually we should move these functionalities to:
  40. // 1. a package for launching/stopping rkt pods.
  41. // 2. rkt api-service interface for listing pods.
  42. // See https://github.com/coreos/rkt/issues/1769.
  43. type systemdInterface interface {
  44. // Version returns the version of the systemd.
  45. Version() (systemdVersion, error)
  46. // ListUnits lists all the loaded units.
  47. ListUnits() ([]dbus.UnitStatus, error)
  48. // StopUnits stops the unit with the given name.
  49. StopUnit(name string, mode string, ch chan<- string) (int, error)
  50. // StopUnits restarts the unit with the given name.
  51. RestartUnit(name string, mode string, ch chan<- string) (int, error)
  52. // ResetFailedUnit resets the "failed" state of a specific unit.
  53. ResetFailedUnit(name string) error
  54. }
  55. // systemd implements the systemdInterface using dbus and systemctl.
  56. // All the functions other then Version() are already implemented by go-systemd/dbus.
  57. type systemd struct {
  58. *dbus.Conn
  59. }
  60. // newSystemd creates a systemd object that implements systemdInterface.
  61. func newSystemd() (*systemd, error) {
  62. dbusConn, err := dbus.New()
  63. if err != nil {
  64. return nil, err
  65. }
  66. return &systemd{dbusConn}, nil
  67. }
  68. // Version returns the version of the systemd.
  69. func (s *systemd) Version() (systemdVersion, error) {
  70. versionStr, err := s.Conn.GetManagerProperty("Version")
  71. if err != nil {
  72. return -1, err
  73. }
  74. result, err := strconv.Atoi(strings.Trim(versionStr, "\""))
  75. if err != nil {
  76. return -1, err
  77. }
  78. return systemdVersion(result), nil
  79. }