forked from k0sproject/rig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
150 lines (131 loc) · 4.37 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package rig
import (
"context"
"errors"
"fmt"
"github.com/k0sproject/rig/v2/cmd"
"github.com/k0sproject/rig/v2/initsystem"
)
type serviceState int
const (
// serviceStateStopped is the stopped state.
serviceStateStopped serviceState = 0
// serviceStateStarted is the started state.
serviceStateStarted serviceState = 1
)
// Service running on a host.
type Service struct {
runner cmd.ContextRunner
name string
initsys initsystem.ServiceManager
}
// Name returns the name of the service.
func (m *Service) Name() string {
return m.name
}
// String returns a string representation of the service.
func (m *Service) String() string {
return fmt.Sprintf("%s@%s", m.Name(), m.runner.String())
}
// Start the service.
func (m *Service) Start(ctx context.Context) error {
if err := m.initsys.StartService(ctx, m.runner, m.name); err != nil {
return fmt.Errorf("failed to start service '%s': %w", m.name, err)
}
return m.waitState(ctx, serviceStateStarted)
}
// Stop the service.
func (m *Service) Stop(ctx context.Context) error {
if err := m.initsys.StopService(ctx, m.runner, m.name); err != nil {
return fmt.Errorf("failed to stop service '%s': %w", m.name, err)
}
return m.waitState(ctx, serviceStateStopped)
}
// Restart the service.
func (m *Service) Restart(ctx context.Context) error {
if restarter, ok := m.initsys.(initsystem.ServiceManagerRestarter); ok {
if err := restarter.RestartService(ctx, m.runner, m.name); err != nil {
return fmt.Errorf("failed to restart service '%s': %w", m.name, err)
}
}
if err := m.Stop(ctx); err != nil {
return fmt.Errorf("failed to stop service '%s' for restart: %w", m.name, err)
}
if err := m.Start(ctx); err != nil {
return fmt.Errorf("failed to restart service '%s: %w", m.name, err)
}
return nil
}
func (m *Service) waitState(ctx context.Context, state serviceState) error {
for {
select {
case <-ctx.Done():
return fmt.Errorf("service '%s' did not reach the desired state: %w", m.name, ctx.Err())
default:
if m.initsys.ServiceIsRunning(ctx, m.runner, m.name) == (state == serviceStateStarted) {
return nil
}
}
}
}
// Enable the service.
func (m *Service) Enable(ctx context.Context) error {
if err := m.initsys.EnableService(ctx, m.runner, m.name); err != nil {
return fmt.Errorf("failed to enable service: %w", err)
}
if reloader, ok := m.initsys.(initsystem.ServiceManagerReloader); ok {
if err := reloader.DaemonReload(ctx, m.runner); err != nil {
return fmt.Errorf("failed to reload init system after enabling service '%s': %w", m.name, err)
}
}
return nil
}
// Disable the service.
func (m *Service) Disable(ctx context.Context) error {
if err := m.initsys.DisableService(ctx, m.runner, m.name); err != nil {
return fmt.Errorf("failed to disable service '%s': %w", m.name, err)
}
if reloader, ok := m.initsys.(initsystem.ServiceManagerReloader); ok {
if err := reloader.DaemonReload(ctx, m.runner); err != nil {
return fmt.Errorf("failed to reload init system after disabling service '%s': %w", m.name, err)
}
}
return nil
}
// ScriptPath returns the path to the service script.
func (m *Service) ScriptPath(ctx context.Context) (string, error) {
path, err := m.initsys.ServiceScriptPath(ctx, m.runner, m.name)
if err != nil {
return "", fmt.Errorf("failed to get service script path: %w", err)
}
return path, nil
}
// IsRunning returns true if the service is running.
func (m *Service) IsRunning(ctx context.Context) bool {
return m.initsys.ServiceIsRunning(ctx, m.runner, m.name)
}
var errLogReaderNotSupported = errors.New("init system provider does not implement log reader")
// Logs returns latest log lines for the service.
func (m *Service) Logs(ctx context.Context, lines int) ([]string, error) {
logreader, ok := m.initsys.(initsystem.ServiceManagerLogReader)
if !ok {
return nil, errLogReaderNotSupported
}
rows, err := logreader.ServiceLogs(ctx, m.runner, m.name, lines)
if err != nil {
return nil, fmt.Errorf("get logs: %w", err)
}
return rows, nil
}
// GetService returns a manager for a single service using an auto-detected service manager implementation from the default providers.
func GetService(runner cmd.ContextRunner, name string) (*Service, error) {
initsys, err := GetServiceManager(runner)
if err != nil {
return nil, fmt.Errorf("get init system service manager: %w", err)
}
return &Service{
runner: runner,
name: name,
initsys: initsys,
}, nil
}