9
"github.com/juju/utils/exec"
11
"github.com/juju/juju/service/common"
12
"github.com/juju/juju/service/upstart"
13
"github.com/juju/juju/service/windows"
14
"github.com/juju/juju/version"
17
var _ Service = (*upstart.Service)(nil)
18
var _ Service = (*windows.Service)(nil)
20
// Service represents a service running on the current system
21
type Service interface {
22
// Installed will return a boolean value that denotes
23
// whether or not the service is installed
25
// Exists returns whether the service configuration exists in the
26
// init directory with the same content that this Service would have
29
// Running returns a boolean value that denotes
30
// whether or not the service is running
32
// Start will try to start the service
34
// Stop will try to stop the service
36
// StopAndRemove will stop the service and remove it
38
// Remove will remove the service
40
// Install installs a service
42
// Config adds a config to the service, overwritting the current one
43
UpdateConfig(conf common.Conf)
46
// NewService returns an interface to a service apropriate
47
// for the current system
48
func NewService(name string, conf common.Conf) Service {
49
switch version.Current.OS {
51
svc := windows.NewService(name, conf)
54
return upstart.NewService(name, conf)
58
func windowsListServices() ([]string, error) {
59
com := exec.RunParams{
60
Commands: `(Get-Service).Name`,
62
out, err := exec.RunCommands(com)
67
return nil, fmt.Errorf("Error running %s: %s", com.Commands, string(out.Stderr))
69
return strings.Fields(string(out.Stdout)), nil
72
var servicesRe = regexp.MustCompile("^([a-zA-Z0-9-_:]+)\\.conf$")
74
func upstartListServices(initDir string) ([]string, error) {
76
fis, err := ioutil.ReadDir(initDir)
80
for _, fi := range fis {
81
if groups := servicesRe.FindStringSubmatch(fi.Name()); len(groups) > 0 {
82
services = append(services, groups[1])
88
// ListServices lists all installed services on the running system
89
func ListServices(initDir string) ([]string, error) {
90
switch version.Current.OS {
92
return upstartListServices(initDir)
94
return windowsListServices()
96
return upstartListServices(initDir)