-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.go
149 lines (118 loc) · 3.07 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
package main
import (
"os"
"path/filepath"
"strings"
"github.com/spf13/afero"
)
const (
ServicesPath = "/var/run/s6/services"
LogPath = "/var/log/hera"
)
var fs = afero.NewOsFs()
// Service holds config for an s6 service
type Service struct {
Hostname string
Commander
}
// NewService returns a new Service. Services are used to start and stop tunnel processes,
// as well as supervise processes to ensure they are kept alive.
func NewService(hostname string) *Service {
service := &Service{
Hostname: hostname,
Commander: Command{},
}
return service
}
// servicePath returns the full path for the service
func (s *Service) servicePath() string {
return filepath.Join(ServicesPath, s.Hostname)
}
// ConfigFilePath returns the full path for the service config file
func (s *Service) ConfigFilePath() string {
return filepath.Join(s.servicePath(), "config.yml")
}
// RunFilePath returns the full path for the service run command
func (s *Service) RunFilePath() string {
return filepath.Join(s.servicePath(), "run")
}
// supervisePath returns the full path for the service supervise command
func (s *Service) supervisePath() string {
return filepath.Join(s.servicePath(), "supervise")
}
// LogFilePath returns the full path for the service log file
func (s *Service) LogFilePath() string {
logPath := []string{filepath.Join(LogPath, s.Hostname), "log"}
return strings.Join(logPath, ".")
}
// Create creates a new service directory if one does not already exist
func (s *Service) Create() error {
exists, err := afero.DirExists(fs, s.servicePath())
if err != nil {
return err
}
if !exists {
fs.Mkdir(s.servicePath(), os.ModePerm)
}
return nil
}
// Supervise supervises a service
func (s *Service) Supervise() error {
_, err := s.Commander.Run("s6-svscanctl", "-a", ServicesPath)
if err != nil {
return err
}
return nil
}
// Start starts a service
func (s *Service) Start() error {
_, err := s.Commander.Run("s6-svc", "-u", s.servicePath())
if err != nil {
return err
}
return nil
}
// Stop stops a service
func (s *Service) Stop() error {
_, err := s.Commander.Run("s6-svc", "-d", s.servicePath())
if err != nil {
return err
}
return nil
}
// Restart restarts a service
func (s *Service) Restart() error {
err := s.waitUntilDown()
if err != nil {
return err
}
err = s.Start()
if err != nil {
return err
}
return nil
}
// waitUntilDown returns when the service is down
func (s *Service) waitUntilDown() error {
_, err := s.Commander.Run("s6-svwait", "-d", s.servicePath())
if err != nil {
return err
}
return nil
}
// IsSupervised returns a bool to indicate if a service is supervised or not
func (s *Service) IsSupervised() (bool, error) {
registered, err := afero.DirExists(fs, s.supervisePath())
if err != nil {
return false, err
}
return registered, nil
}
// IsRunning returns a bool to indicate if a service is running or not
func (s *Service) IsRunning() (bool, error) {
out, err := s.Commander.Run("s6-svstat", "-u", s.servicePath())
if err != nil {
return false, err
}
return strings.Contains(string(out), "true"), nil
}