Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Original file: https://github.com/kubernetes/kubernetes/blob/release-1.15/pkg/util/initsystem/initsystem_unix.go
*/
package initsystem
import (
"fmt"
"os/exec"
"strings"
)
type OpenRCInitSystem struct{}
func (openrc OpenRCInitSystem) ServiceStart(service string) error {
args := []string{service, "start"}
return exec.Command("rc-service", args...).Run()
}
func (openrc OpenRCInitSystem) ServiceStop(service string) error {
args := []string{service, "stop"}
return exec.Command("rc-service", args...).Run()
}
func (openrc OpenRCInitSystem) ServiceRestart(service string) error {
args := []string{service, "restart"}
return exec.Command("rc-service", args...).Run()
}
func (openrc OpenRCInitSystem) ServiceExists(service string) bool {
args := []string{service, "status"}
outBytes, _ := exec.Command("rc-service", args...).CombinedOutput()
if strings.Contains(string(outBytes), "does not exist") {
return false
}
return true
}
func (openrc OpenRCInitSystem) ServiceIsEnabled(service string) bool {
args := []string{"show", "default"}
outBytes, _ := exec.Command("rc-update", args...).Output()
if strings.Contains(string(outBytes), service) {
return true
}
return false
}
func (openrc OpenRCInitSystem) ServiceIsActive(service string) bool {
args := []string{service, "status"}
outBytes, _ := exec.Command("rc-service", args...).CombinedOutput()
outStr := string(outBytes)
return !strings.Contains(outStr, "stopped") && !strings.Contains(outStr, "does not exist")
}
func (openrc OpenRCInitSystem) EnableCommand(service string) string {
return fmt.Sprintf("rc-update add %s default", service)
}
func (openrc OpenRCInitSystem) ServiceDisable(service string) error {
args := []string{service, "stop"}
return exec.Command("rc-update", args...).Run()
}
func (openrc OpenRCInitSystem) ServiceEnable(service string) error {
args := []string{service, "start"}
return exec.Command("rc-update", args...).Run()
}
type SystemdInitSystem struct{}
func (sysd SystemdInitSystem) EnableCommand(service string) string {
return fmt.Sprintf("systemctl enable %s.service", service)
}
func (sysd SystemdInitSystem) reloadSystemd() error {
if err := exec.Command("systemctl", "daemon-reload").Run(); err != nil {
return fmt.Errorf("failed to reload systemd: %v", err)
}
return nil
}
func (sysd SystemdInitSystem) ServiceStart(service string) error {
if err := sysd.reloadSystemd(); err != nil {
return err
}
args := []string{"start", service}
return exec.Command("systemctl", args...).Run()
}
func (sysd SystemdInitSystem) ServiceRestart(service string) error {
if err := sysd.reloadSystemd(); err != nil {
return err
}
args := []string{"restart", service}
return exec.Command("systemctl", args...).Run()
}
func (sysd SystemdInitSystem) ServiceStop(service string) error {
args := []string{"stop", service}
return exec.Command("systemctl", args...).Run()
}
func (sysd SystemdInitSystem) ServiceExists(service string) bool {
args := []string{"status", service}
outBytes, _ := exec.Command("systemctl", args...).Output()
output := string(outBytes)
return !strings.Contains(output, "could not be found") && output != ""
}
func (sysd SystemdInitSystem) ServiceIsEnabled(service string) bool {
args := []string{"is-enabled", service}
err := exec.Command("systemctl", args...).Run()
return err == nil
}
func (sysd SystemdInitSystem) ServiceIsActive(service string) bool {
args := []string{"is-active", service}
outBytes, _ := exec.Command("systemctl", args...).Output()
output := strings.TrimSpace(string(outBytes))
if output == "active" || output == "activating" {
return true
}
return false
}
func (sysd SystemdInitSystem) ServiceDisable(service string) error {
args := []string{"disable", service}
return exec.Command("systemctl", args...).Run()
}
func (sysd SystemdInitSystem) ServiceEnable(service string) error {
args := []string{"enable", service}
return exec.Command("systemctl", args...).Run()
}
func GetInitSystem() (InitSystem, error) {
_, err := exec.LookPath("systemctl")
if err == nil {
return &SystemdInitSystem{}, nil
}
_, err = exec.LookPath("openrc")
if err == nil {
return &OpenRCInitSystem{}, nil
}
return nil, fmt.Errorf("no supported init system detected, skipping checking for services")
}