-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
173 lines (142 loc) · 4.3 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package service
import (
"fmt"
"github.com/juju/errors"
"github.com/juju/juju/service/common"
"github.com/juju/juju/service/upstart"
"github.com/juju/juju/service/windows"
"github.com/juju/juju/version"
)
var _ Service = (*upstart.Service)(nil)
var _ Service = (*windows.Service)(nil)
// Service represents a service running on the current system
type Service interface {
// Name returns the service's name.
Name() string
// Conf returns the service's conf data.
Conf() common.Conf
// Config adds a config to the service, overwritting the current one
UpdateConfig(conf common.Conf)
// Running returns a boolean value that denotes
// whether or not the service is running
Running() bool
// Start will try to start the service
Start() error
// Stop will try to stop the service
Stop() error
// TODO(ericsnow) Eliminate StopAndRemove.
// StopAndRemove will stop the service and remove it
StopAndRemove() error
// Exists returns whether the service configuration exists in the
// init directory with the same content that this Service would have
// if installed.
Exists() bool
// Installed will return a boolean value that denotes
// whether or not the service is installed
Installed() bool
// Install installs a service
Install() error
// Remove will remove the service
Remove() error
// InstallCommands returns the list of commands to run on a
// (remote) host to install the service.
InstallCommands() ([]string, error)
}
// TODO(ericsnow) Eliminate the need to pass an empty conf here for
// most service methods.
// NewService returns a new Service based on the provided info.
func NewService(name string, conf common.Conf, initSystem string) (Service, error) {
switch initSystem {
case "windows":
return windows.NewService(name, conf), nil
case "upstart":
return upstart.NewService(name, conf), nil
default:
return nil, errors.NotFoundf("init system %q", initSystem)
}
}
// DiscoverService returns an interface to a service apropriate
// for the current system
func DiscoverService(name string, conf common.Conf) (Service, error) {
initName := VersionInitSystem(version.Current)
if initName == "" {
return nil, errors.NotFoundf("init system on local host")
}
service, err := NewService(name, conf, initName)
return service, errors.Trace(err)
}
// VersionInitSystem returns an init system name based on the provided
// version info.
func VersionInitSystem(vers version.Binary) string {
switch vers.OS {
case version.Windows:
return "windows"
case version.Ubuntu:
switch vers.Series {
case "precise", "quantal", "raring", "saucy", "trusty", "utopic":
return "upstart"
default:
// vivid and later
return "systemd"
}
// TODO(ericsnow) Support other OSes, like version.CentOS.
default:
return ""
}
}
// ListServices lists all installed services on the running system
func ListServices(initDir string) ([]string, error) {
initName := VersionInitSystem(version.Current)
if initName == "" {
return nil, errors.NotFoundf("init system on local host")
}
switch initName {
case "windows":
services, err := windows.ListServices()
return services, errors.Trace(err)
case "upstart":
services, err := upstart.ListServices(initDir)
return services, errors.Trace(err)
default:
return nil, errors.NotFoundf("init system %q", initName)
}
}
var linuxExecutables = map[string]string{
"/sbin/init": "upstart",
}
// TODO(ericsnow) Is it too much to cat once for each executable?
const initSystemTest = `[[ "$(cat /proc/1/cmdline)" == "%s" ]]`
// ListServicesCommand returns the command that should be run to get
// a list of service names on a host.
func ListServicesCommand() string {
// TODO(ericsnow) Allow passing in "initSystems ...string".
executables := linuxExecutables
// TODO(ericsnow) build the command in a better way?
cmdAll := ""
for executable, initSystem := range executables {
cmd := listServicesCommand(initSystem)
if cmd == "" {
continue
}
test := fmt.Sprintf(initSystemTest, executable)
cmd = fmt.Sprintf("if %s; then %s\n", test, cmd)
if cmdAll != "" {
cmd = "el" + cmd
}
cmdAll += cmd
}
if cmdAll != "" {
cmdAll += "fi"
}
return cmdAll
}
func listServicesCommand(initSystem string) string {
switch initSystem {
case "windows":
return windows.ListCommand()
case "upstart":
return upstart.ListCommand()
default:
return ""
}
}