initsystem_unix.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // +build !windows
  2. /*
  3. Copyright 2017 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package initsystem
  15. import (
  16. "fmt"
  17. "os/exec"
  18. "strings"
  19. )
  20. type OpenRCInitSystem struct{}
  21. func (openrc OpenRCInitSystem) ServiceStart(service string) error {
  22. args := []string{service, "start"}
  23. return exec.Command("rc-service", args...).Run()
  24. }
  25. func (openrc OpenRCInitSystem) ServiceStop(service string) error {
  26. args := []string{service, "stop"}
  27. return exec.Command("rc-service", args...).Run()
  28. }
  29. func (openrc OpenRCInitSystem) ServiceRestart(service string) error {
  30. args := []string{service, "restart"}
  31. return exec.Command("rc-service", args...).Run()
  32. }
  33. // openrc writes to stderr if a service is not found or not enabled
  34. // this is in contrast to systemd which only writes to stdout.
  35. // Hence, we use the Combinedoutput, and ignore the error.
  36. func (openrc OpenRCInitSystem) ServiceExists(service string) bool {
  37. args := []string{service, "status"}
  38. outBytes, _ := exec.Command("rc-service", args...).CombinedOutput()
  39. if strings.Contains(string(outBytes), "does not exist") {
  40. return false
  41. }
  42. return true
  43. }
  44. func (openrc OpenRCInitSystem) ServiceIsEnabled(service string) bool {
  45. args := []string{"show", "default"}
  46. outBytes, _ := exec.Command("rc-update", args...).Output()
  47. if strings.Contains(string(outBytes), service) {
  48. return true
  49. }
  50. return false
  51. }
  52. func (openrc OpenRCInitSystem) ServiceIsActive(service string) bool {
  53. args := []string{service, "status"}
  54. outBytes, _ := exec.Command("rc-service", args...).Output()
  55. if strings.Contains(string(outBytes), "stopped") {
  56. return false
  57. }
  58. return true
  59. }
  60. func (openrc OpenRCInitSystem) EnableCommand(service string) string {
  61. return fmt.Sprintf("rc-update add %s default", service)
  62. }
  63. type SystemdInitSystem struct{}
  64. func (sysd SystemdInitSystem) EnableCommand(service string) string {
  65. return fmt.Sprintf("systemctl enable %s.service", service)
  66. }
  67. func (sysd SystemdInitSystem) reloadSystemd() error {
  68. if err := exec.Command("systemctl", "daemon-reload").Run(); err != nil {
  69. return fmt.Errorf("failed to reload systemd: %v", err)
  70. }
  71. return nil
  72. }
  73. func (sysd SystemdInitSystem) ServiceStart(service string) error {
  74. // Before we try to start any service, make sure that systemd is ready
  75. if err := sysd.reloadSystemd(); err != nil {
  76. return err
  77. }
  78. args := []string{"start", service}
  79. return exec.Command("systemctl", args...).Run()
  80. }
  81. func (sysd SystemdInitSystem) ServiceRestart(service string) error {
  82. // Before we try to restart any service, make sure that systemd is ready
  83. if err := sysd.reloadSystemd(); err != nil {
  84. return err
  85. }
  86. args := []string{"restart", service}
  87. return exec.Command("systemctl", args...).Run()
  88. }
  89. func (sysd SystemdInitSystem) ServiceStop(service string) error {
  90. args := []string{"stop", service}
  91. return exec.Command("systemctl", args...).Run()
  92. }
  93. func (sysd SystemdInitSystem) ServiceExists(service string) bool {
  94. args := []string{"status", service}
  95. outBytes, _ := exec.Command("systemctl", args...).Output()
  96. output := string(outBytes)
  97. if strings.Contains(output, "Loaded: not-found") {
  98. return false
  99. }
  100. return true
  101. }
  102. func (sysd SystemdInitSystem) ServiceIsEnabled(service string) bool {
  103. args := []string{"is-enabled", service}
  104. err := exec.Command("systemctl", args...).Run()
  105. if err != nil {
  106. return false
  107. }
  108. return true
  109. }
  110. // ServiceIsActive will check is the service is "active". In the case of
  111. // crash looping services (kubelet in our case) status will return as
  112. // "activating", so we will consider this active as well.
  113. func (sysd SystemdInitSystem) ServiceIsActive(service string) bool {
  114. args := []string{"is-active", service}
  115. // Ignoring error here, command returns non-0 if in "activating" status:
  116. outBytes, _ := exec.Command("systemctl", args...).Output()
  117. output := strings.TrimSpace(string(outBytes))
  118. if output == "active" || output == "activating" {
  119. return true
  120. }
  121. return false
  122. }
  123. // GetInitSystem returns an InitSystem for the current system, or nil
  124. // if we cannot detect a supported init system.
  125. // This indicates we will skip init system checks, not an error.
  126. func GetInitSystem() (InitSystem, error) {
  127. // Assume existence of systemctl in path implies this is a systemd system:
  128. _, err := exec.LookPath("systemctl")
  129. if err == nil {
  130. return &SystemdInitSystem{}, nil
  131. }
  132. _, err = exec.LookPath("openrc")
  133. if err == nil {
  134. return &OpenRCInitSystem{}, nil
  135. }
  136. return nil, fmt.Errorf("no supported init system detected, skipping checking for services")
  137. }