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