Exemplo n.º 1
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)
	}
}
Exemplo n.º 2
0
func runServiceInstall(s service.Service, c *cli.Context) error {
	if user := c.String("user"); user == "" && os.Getuid() == 0 {
		log.Fatal("Please specify user that will run gitlab-runner service")
	}

	if configFile := c.String("config"); configFile != "" {
		// try to load existing config
		config := common.NewConfig()
		err := config.LoadConfig(configFile)
		if err != nil {
			return err
		}

		// save config for the first time
		if !config.Loaded {
			err = config.SaveConfig(configFile)
			if err != nil {
				return err
			}
		}
	}
	return service.Control(s, "install")
}
Exemplo n.º 3
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)
	}

	svcConfig.Arguments = append(svcConfig.Arguments, "--syslog")

	s, err := service_helpers.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)
	}
}