initsystem_unix.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. // OpenRCInitSystem defines openrc
  21. type OpenRCInitSystem struct{}
  22. // ServiceStart tries to start a specific service
  23. func (openrc OpenRCInitSystem) ServiceStart(service string) error {
  24. args := []string{service, "start"}
  25. return exec.Command("rc-service", args...).Run()
  26. }
  27. // ServiceStop tries to stop a specific service
  28. func (openrc OpenRCInitSystem) ServiceStop(service string) error {
  29. args := []string{service, "stop"}
  30. return exec.Command("rc-service", args...).Run()
  31. }
  32. // ServiceRestart tries to reload the environment and restart the specific service
  33. func (openrc OpenRCInitSystem) ServiceRestart(service string) error {
  34. args := []string{service, "restart"}
  35. return exec.Command("rc-service", args...).Run()
  36. }
  37. // ServiceExists ensures the service is defined for this init system.
  38. // openrc writes to stderr if a service is not found or not enabled
  39. // this is in contrast to systemd which only writes to stdout.
  40. // Hence, we use the Combinedoutput, and ignore the error.
  41. func (openrc OpenRCInitSystem) ServiceExists(service string) bool {
  42. args := []string{service, "status"}
  43. outBytes, _ := exec.Command("rc-service", args...).CombinedOutput()
  44. return !strings.Contains(string(outBytes), "does not exist")
  45. }
  46. // ServiceIsEnabled ensures the service is enabled to start on each boot.
  47. func (openrc OpenRCInitSystem) ServiceIsEnabled(service string) bool {
  48. args := []string{"show", "default"}
  49. outBytes, _ := exec.Command("rc-update", args...).Output()
  50. return strings.Contains(string(outBytes), service)
  51. }
  52. // ServiceIsActive ensures the service is running, or attempting to run. (crash looping in the case of kubelet)
  53. func (openrc OpenRCInitSystem) ServiceIsActive(service string) bool {
  54. args := []string{service, "status"}
  55. outBytes, _ := exec.Command("rc-service", args...).CombinedOutput()
  56. outStr := string(outBytes)
  57. return !strings.Contains(outStr, "stopped") && !strings.Contains(outStr, "does not exist")
  58. }
  59. // EnableCommand return a string describing how to enable a service
  60. func (openrc OpenRCInitSystem) EnableCommand(service string) string {
  61. return fmt.Sprintf("rc-update add %s default", service)
  62. }
  63. // SystemdInitSystem defines systemd
  64. type SystemdInitSystem struct{}
  65. // EnableCommand return a string describing how to enable a service
  66. func (sysd SystemdInitSystem) EnableCommand(service string) string {
  67. return fmt.Sprintf("systemctl enable %s.service", service)
  68. }
  69. // reloadSystemd reloeads the systemd daemon
  70. func (sysd SystemdInitSystem) reloadSystemd() error {
  71. if err := exec.Command("systemctl", "daemon-reload").Run(); err != nil {
  72. return fmt.Errorf("failed to reload systemd: %v", err)
  73. }
  74. return nil
  75. }
  76. // ServiceStart tries to start a specific service
  77. func (sysd SystemdInitSystem) ServiceStart(service string) error {
  78. // Before we try to start any service, make sure that systemd is ready
  79. if err := sysd.reloadSystemd(); err != nil {
  80. return err
  81. }
  82. args := []string{"start", service}
  83. return exec.Command("systemctl", args...).Run()
  84. }
  85. // ServiceRestart tries to reload the environment and restart the specific service
  86. func (sysd SystemdInitSystem) ServiceRestart(service string) error {
  87. // Before we try to restart any service, make sure that systemd is ready
  88. if err := sysd.reloadSystemd(); err != nil {
  89. return err
  90. }
  91. args := []string{"restart", service}
  92. return exec.Command("systemctl", args...).Run()
  93. }
  94. // ServiceStop tries to stop a specific service
  95. func (sysd SystemdInitSystem) ServiceStop(service string) error {
  96. args := []string{"stop", service}
  97. return exec.Command("systemctl", args...).Run()
  98. }
  99. // ServiceExists ensures the service is defined for this init system.
  100. func (sysd SystemdInitSystem) ServiceExists(service string) bool {
  101. args := []string{"status", service}
  102. outBytes, _ := exec.Command("systemctl", args...).Output()
  103. output := string(outBytes)
  104. return !strings.Contains(output, "Loaded: not-found")
  105. }
  106. // ServiceIsEnabled ensures the service is enabled to start on each boot.
  107. func (sysd SystemdInitSystem) ServiceIsEnabled(service string) bool {
  108. args := []string{"is-enabled", service}
  109. err := exec.Command("systemctl", args...).Run()
  110. return err == nil
  111. }
  112. // ServiceIsActive will check is the service is "active". In the case of
  113. // crash looping services (kubelet in our case) status will return as
  114. // "activating", so we will consider this active as well.
  115. func (sysd SystemdInitSystem) ServiceIsActive(service string) bool {
  116. args := []string{"is-active", service}
  117. // Ignoring error here, command returns non-0 if in "activating" status:
  118. outBytes, _ := exec.Command("systemctl", args...).Output()
  119. output := strings.TrimSpace(string(outBytes))
  120. if output == "active" || output == "activating" {
  121. return true
  122. }
  123. return false
  124. }
  125. // GetInitSystem returns an InitSystem for the current system, or nil
  126. // if we cannot detect a supported init system.
  127. // This indicates we will skip init system checks, not an error.
  128. func GetInitSystem() (InitSystem, error) {
  129. // Assume existence of systemctl in path implies this is a systemd system:
  130. _, err := exec.LookPath("systemctl")
  131. if err == nil {
  132. return &SystemdInitSystem{}, nil
  133. }
  134. _, err = exec.LookPath("openrc")
  135. if err == nil {
  136. return &OpenRCInitSystem{}, nil
  137. }
  138. return nil, fmt.Errorf("no supported init system detected, skipping checking for services")
  139. }