forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
258 lines (225 loc) · 6.43 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Copyright 2015 Cloudbase Solutions
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package windows
import (
"fmt"
"runtime"
"strings"
"syscall"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/utils/shell"
"github.com/juju/juju/service/common"
)
var (
logger = loggo.GetLogger("juju.worker.deployer.service")
renderer = &shell.PowershellRenderer{}
)
const (
// c_ERROR_SERVICE_DOES_NOT_EXIST is returned by the OS when trying to open
// an inexistent service
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms684330%28v=vs.85%29.aspx
c_ERROR_SERVICE_DOES_NOT_EXIST syscall.Errno = 0x424
// c_ERROR_SERVICE_EXISTS is returned by the operating system if the service
// we are trying to create, already exists
c_ERROR_SERVICE_EXISTS syscall.Errno = 0x431
// This is the user under which juju services start. We chose to use a
// normal user for this purpose because some installers require a normal
// user with a proper user profile to actually run. This user is created
// via userdata, and should exist on all juju bootstrapped systems.
// Required privileges for this user are:
// SeAssignPrimaryTokenPrivilege
// SeServiceLogonRight
jujudUser = ".\\jujud"
)
// IsRunning returns whether or not windows is the local init system.
func IsRunning() (bool, error) {
return runtime.GOOS == "windows", nil
}
// ListServices returns the name of all installed services on the
// local host.
func ListServices() ([]string, error) {
return listServices()
}
// ListCommand returns a command that will list the services on a host.
func ListCommand() string {
return `(Get-Service).Name`
}
// ServiceManager exposes methods needed to manage a windows service
type ServiceManager interface {
// Start starts a service.
Start(name string) error
// Stop stops a service.
Stop(name string) error
// Delete deletes a service.
Delete(name string) error
// Create creates a service with the given config.
Create(name string, conf common.Conf) error
// Running returns the status of a service.
Running(name string) (bool, error)
// Exists checks whether the config of the installed service matches the
// config supplied to this function
Exists(name string, conf common.Conf) (bool, error)
// ChangeServicePassword can change the password of a service
// as long as it belongs to the user defined in this package
ChangeServicePassword(name, newPassword string) error
}
// Service represents a service running on the current system
type Service struct {
common.Service
manager ServiceManager
}
func newService(name string, conf common.Conf, manager ServiceManager) *Service {
return &Service{
Service: common.Service{
Name: name,
Conf: conf,
},
manager: manager,
}
}
// NewService returns a new Service type
func NewService(name string, conf common.Conf) (*Service, error) {
m, err := NewServiceManager()
if err != nil {
return nil, errors.Trace(err)
}
return newService(name, conf, m), nil
}
// Name implements service.Service.
func (s *Service) Name() string {
return s.Service.Name
}
// Conf implements service.Service.
func (s *Service) Conf() common.Conf {
return s.Service.Conf
}
// Validate checks the service for invalid values.
func (s *Service) Validate() error {
if err := s.Service.Validate(renderer); err != nil {
return errors.Trace(err)
}
if s.Service.Conf.Transient {
return errors.NotSupportedf("transient services")
}
if s.Service.Conf.AfterStopped != "" {
return errors.NotSupportedf("Conf.AfterStopped")
}
return nil
}
func (s *Service) Running() (bool, error) {
if ok, err := s.Installed(); err != nil {
return false, errors.Trace(err)
} else if !ok {
return false, nil
}
return s.manager.Running(s.Name())
}
// Installed returns whether the service is installed
func (s *Service) Installed() (bool, error) {
services, err := ListServices()
if err != nil {
return false, errors.Trace(err)
}
for _, val := range services {
if s.Name() == val {
return true, nil
}
}
return false, nil
}
// Exists returns whether the service configuration reflects the
// desired state
func (s *Service) Exists() (bool, error) {
return s.manager.Exists(s.Name(), s.Conf())
}
// Start starts the service.
func (s *Service) Start() error {
logger.Infof("Starting service %q", s.Service.Name)
running, err := s.Running()
if err != nil {
return errors.Trace(err)
}
if running {
logger.Infof("Service %q already running", s.Service.Name)
return nil
}
err = s.manager.Start(s.Name())
return err
}
// Stop stops the service.
func (s *Service) Stop() error {
running, err := s.Running()
if err != nil {
return errors.Trace(err)
}
if !running {
return nil
}
err = s.manager.Stop(s.Name())
return err
}
// Remove deletes the service.
func (s *Service) Remove() error {
installed, err := s.Installed()
if err != nil {
return err
}
if !installed {
return nil
}
err = s.Stop()
if err != nil {
return errors.Trace(err)
}
err = s.manager.Delete(s.Name())
return err
}
// Install installs and starts the service.
func (s *Service) Install() error {
err := s.Validate()
if err != nil {
return errors.Trace(err)
}
installed, err := s.Installed()
if err != nil {
return errors.Trace(err)
}
if installed {
return errors.Errorf("Service %s already installed", s.Name())
}
logger.Infof("Installing Service %v", s.Name())
err = s.manager.Create(s.Name(), s.Conf())
if err != nil {
return errors.Trace(err)
}
return nil
}
// InstallCommands returns shell commands to install the service.
func (s *Service) InstallCommands() ([]string, error) {
cmd := fmt.Sprintf(serviceCreateCommandTemplate[1:],
renderer.Quote(s.Service.Name),
renderer.Quote(s.Service.Conf.Desc),
renderer.Quote(s.Service.Conf.ExecStart),
renderer.Quote(s.Service.Name),
renderer.Quote(s.Service.Conf.Desc),
renderer.Quote(s.Service.Conf.ExecStart),
renderer.Quote(s.Service.Name),
renderer.Quote(s.Service.Name),
)
return strings.Split(cmd, "\n"), nil
}
// StartCommands returns shell commands to start the service.
func (s *Service) StartCommands() ([]string, error) {
cmd := fmt.Sprintf(`Start-Service %s`, renderer.Quote(s.Service.Name))
return []string{cmd}, nil
}
const serviceCreateCommandTemplate = `
if ($jujuCreds) {
New-Service -Credential $jujuCreds -Name %s -DependsOn Winmgmt -DisplayName %s %s
} else {
New-Service -Name %s -DependsOn Winmgmt -DisplayName %s %s
}
sc.exe failure %s reset=5 actions=restart/1000
sc.exe failureflag %s 1`