コード例 #1
0
func CreateService(c *cli.Context) service.Service {
	serviceName := c.String("service-name")
	displayName := c.String("service-name")
	if serviceName == "" {
		serviceName = defaultServiceName
		displayName = defaultDisplayName
	}

	svcConfig := &service.Config{
		Name:        serviceName,
		DisplayName: displayName,
		Description: defaultDescription,
		Arguments:   []string{"run"},
	}

	mr := &MultiRunner{
		configFile:       c.String("config"),
		workingDirectory: c.String("working-directory"),
	}

	s, err := service.New(mr, svcConfig)
	if err != nil {
		log.Fatal(err)
	}
	return s
}
コード例 #2
0
func (c *RunCommand) Execute(context *cli.Context) {
	svcConfig := &service.Config{
		Name:        c.ServiceName,
		DisplayName: c.ServiceName,
		Description: defaultDescription,
		Arguments:   []string{"run"},
	}

	service, err := service.New(c, svcConfig)
	if err != nil {
		log.Fatalln(err)
	}

	if c.Syslog {
		logger, err := service.SystemLogger(nil)
		if err == nil {
			log.AddHook(&ServiceLogHook{logger})
		} else {
			log.Errorln(err)
		}
	}

	err = service.Run()
	if err != nil {
		log.Fatalln(err)
	}
}
コード例 #3
0
func RunServiceControl(c *cli.Context) {
	serviceName := c.String("service-name")
	displayName := c.String("service-name")
	if serviceName == "" {
		serviceName = defaultServiceName
		displayName = defaultDisplayName
	}

	svcConfig := &service.Config{
		Name:        serviceName,
		DisplayName: displayName,
		Description: defaultDescription,
		Arguments:   []string{"run"},
		UserName:    c.String("user"),
	}

	switch runtime.GOOS {
	case "darwin":
		svcConfig.Option = service.KeyValue{
			"KeepAlive":     true,
			"RunAtLoad":     true,
			"SessionCreate": true,
			"UserService":   true,
		}

	case "windows":
		svcConfig.Option = service.KeyValue{
			"Password": c.String("password"),
		}
	}

	if wd := c.String("working-directory"); wd != "" {
		svcConfig.Arguments = append(svcConfig.Arguments, "--working-directory", wd)
	}

	if config := c.String("config"); config != "" {
		svcConfig.Arguments = append(svcConfig.Arguments, "--config", config)
	}

	if sn := c.String("service-name"); sn != "" {
		svcConfig.Arguments = append(svcConfig.Arguments, "--service-name", sn)
	}

	s, err := service.New(&NullService{}, svcConfig)
	if err != nil {
		log.Fatal(err)
	}

	err = service.Control(s, c.Command.Name)
	if err != nil {
		log.Fatal(err)
	}
}
コード例 #4
0
func RunServiceControl(c *cli.Context) {
	// detect whether we want to install as user service or system service
	isUserService := os.Getuid() != 0
	if runtime.GOOS == "windows" {
		isUserService = true
	}

	// when installing service as system wide service don't specify username for service
	serviceUserName := c.String("user")
	if !isUserService {
		serviceUserName = ""
	}

	if isUserService && runtime.GOOS == "linux" {
		log.Fatal("Please run the commands as root")
	}

	svcConfig := &service.Config{
		Name:        c.String("service"),
		DisplayName: c.String("service"),
		Description: defaultDescription,
		Arguments:   []string{"run"},
		UserName:    serviceUserName,
	}

	switch runtime.GOOS {
	case "darwin":
		svcConfig.Option = service.KeyValue{
			"KeepAlive":     true,
			"RunAtLoad":     true,
			"SessionCreate": true,
			"UserService":   isUserService,
		}

	case "windows":
		svcConfig.Option = service.KeyValue{
			"Password": c.String("password"),
		}
	}

	if wd := c.String("working-directory"); wd != "" {
		svcConfig.Arguments = append(svcConfig.Arguments, "--working-directory", wd)
	}

	if config := c.String("config"); config != "" {
		svcConfig.Arguments = append(svcConfig.Arguments, "--config", config)
	}

	if sn := c.String("service"); sn != "" {
		svcConfig.Arguments = append(svcConfig.Arguments, "--service", sn)
	}

	if user := c.String("user"); !isUserService && user != "" {
		svcConfig.Arguments = append(svcConfig.Arguments, "--user", user)
	}

	s, err := service.New(&NullService{}, svcConfig)
	if err != nil {
		log.Fatal(err)
	}

	switch c.Command.Name {
	case "install":
		err = runServiceInstall(s, c)
	default:
		err = service.Control(s, c.Command.Name)
	}

	if err != nil {
		log.Fatal(err)
	}
}