github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/service/service.go (about)

     1  package service
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"regexp"
     7  	"strings"
     8  
     9  	"github.com/juju/utils/exec"
    10  
    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"
    15  )
    16  
    17  var _ Service = (*upstart.Service)(nil)
    18  var _ Service = (*windows.Service)(nil)
    19  
    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
    24  	Installed() bool
    25  	// Exists returns whether the service configuration exists in the
    26  	// init directory with the same content that this Service would have
    27  	// if installed.
    28  	Exists() bool
    29  	// Running returns a boolean value that denotes
    30  	// whether or not the service is running
    31  	Running() bool
    32  	// Start will try to start the service
    33  	Start() error
    34  	// Stop will try to stop the service
    35  	Stop() error
    36  	// StopAndRemove will stop the service and remove it
    37  	StopAndRemove() error
    38  	// Remove will remove the service
    39  	Remove() error
    40  	// Install installs a service
    41  	Install() error
    42  	// Config adds a config to the service, overwritting the current one
    43  	UpdateConfig(conf common.Conf)
    44  }
    45  
    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 {
    50  	case version.Windows:
    51  		svc := windows.NewService(name, conf)
    52  		return svc
    53  	default:
    54  		return upstart.NewService(name, conf)
    55  	}
    56  }
    57  
    58  func windowsListServices() ([]string, error) {
    59  	com := exec.RunParams{
    60  		Commands: `(Get-Service).Name`,
    61  	}
    62  	out, err := exec.RunCommands(com)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  	if out.Code != 0 {
    67  		return nil, fmt.Errorf("Error running %s: %s", com.Commands, string(out.Stderr))
    68  	}
    69  	return strings.Fields(string(out.Stdout)), nil
    70  }
    71  
    72  var servicesRe = regexp.MustCompile("^([a-zA-Z0-9-_:]+)\\.conf$")
    73  
    74  func upstartListServices(initDir string) ([]string, error) {
    75  	var services []string
    76  	fis, err := ioutil.ReadDir(initDir)
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  	for _, fi := range fis {
    81  		if groups := servicesRe.FindStringSubmatch(fi.Name()); len(groups) > 0 {
    82  			services = append(services, groups[1])
    83  		}
    84  	}
    85  	return services, nil
    86  }
    87  
    88  // ListServices lists all installed services on the running system
    89  func ListServices(initDir string) ([]string, error) {
    90  	switch version.Current.OS {
    91  	case version.Ubuntu:
    92  		return upstartListServices(initDir)
    93  	case version.Windows:
    94  		return windowsListServices()
    95  	default:
    96  		return upstartListServices(initDir)
    97  	}
    98  }