Ejemplo n.º 1
0
func parseTask(task *Task) error {
	args, err := utils.ToStringArray(task.Command)
	if err != nil {
		return err
	}
	task.Args = args
	if task.Args == nil || len(task.Args) == 0 {
		return fmt.Errorf("Task did not provide a command")
	}
	if task.Name == "" {
		task.Name = strings.Join(task.Args, " ")
	}
	freq, err := utils.ParseDuration(task.Frequency)
	if err != nil {
		return fmt.Errorf("Unable to parse frequency %s: %v", task.Frequency, err)
	}
	if freq < time.Millisecond {
		return fmt.Errorf("Frequency %v cannot be less that %v", freq, taskMinDuration)
	}
	task.freqDuration = freq
	task.timeoutDuration = freq
	if task.Timeout != "" {
		timeout, err := utils.ParseDuration(task.Timeout)
		if err != nil {
			return fmt.Errorf("Unable to parse timeout %s: %v", task.Timeout, err)
		}
		if timeout < taskMinDuration {
			return fmt.Errorf("Timeout %v cannot be less that %v", timeout, taskMinDuration)
		}
		task.timeoutDuration = timeout
	} else {
		task.Timeout = task.Frequency
	}
	return nil
}
Ejemplo n.º 2
0
func parseService(s *Service, disc discovery.ServiceBackend) error {
	if err := utils.ValidateServiceName(s.Name); err != nil {
		return err
	}
	hostname, _ := os.Hostname()
	s.ID = fmt.Sprintf("%s-%s", s.Name, hostname)
	s.discoveryService = disc
	if s.Poll < 1 {
		return fmt.Errorf("`poll` must be > 0 in service %s", s.Name)
	}
	if s.TTL < 1 {
		return fmt.Errorf("`ttl` must be > 0 in service %s", s.Name)
	}
	if s.Port < 1 {
		return fmt.Errorf("`port` must be > 0 in service %s", s.Name)
	}

	// if the HealthCheckExec is nil then we'll have no health check
	// command; this is useful for the telemetry service
	cmd, err := utils.ParseCommandArgs(s.HealthCheckExec)
	if err != nil {
		return fmt.Errorf("Could not parse `health` in service %s: %s", s.Name, err)
	}
	s.healthCheckCmd = cmd

	interfaces, ifaceErr := utils.ToStringArray(s.Interfaces)
	if ifaceErr != nil {
		return ifaceErr
	}

	ipAddress, err := utils.GetIP(interfaces)
	if err != nil {
		return err
	}
	s.IPAddress = ipAddress

	s.definition = &discovery.ServiceDefinition{
		ID:        s.ID,
		Name:      s.Name,
		Port:      s.Port,
		TTL:       s.TTL,
		Tags:      s.Tags,
		IPAddress: s.IPAddress,
	}
	return nil
}
Ejemplo n.º 3
0
func parseCoprocess(coprocess *Coprocess) error {
	args, err := utils.ToStringArray(coprocess.Command)
	if err != nil {
		return err
	}
	coprocess.Args = args
	if coprocess.Args == nil || len(coprocess.Args) == 0 {
		return fmt.Errorf("Coprocess did not provide a command")
	}
	cmd := utils.ArgsToCmd(coprocess.Args)
	coprocess.cmd = cmd

	if coprocess.Name == "" {
		coprocess.Name = strings.Join(coprocess.Args, " ")
	}

	return parseCoprocessRestarts(coprocess)
}
Ejemplo n.º 4
0
func parseService(s *Service, disc discovery.ServiceBackend) error {
	if err := utils.ValidateServiceName(s.Name); err != nil {
		return err
	}
	hostname, _ := os.Hostname()
	s.ID = fmt.Sprintf("%s-%s", s.Name, hostname)
	s.discoveryService = disc
	if s.Poll < 1 {
		return fmt.Errorf("`poll` must be > 0 in service %s", s.Name)
	}
	if s.TTL < 1 {
		return fmt.Errorf("`ttl` must be > 0 in service %s", s.Name)
	}
	if s.Port < 1 {
		return fmt.Errorf("`port` must be > 0 in service %s", s.Name)
	}

	// if the HealthCheckExec is nil then we'll have no health check
	// command; this is useful for the telemetry service
	if s.HealthCheckExec != nil {
		cmd, err := commands.NewCommand(s.HealthCheckExec, s.Timeout)
		if err != nil {
			return fmt.Errorf("Could not parse `health` in service %s: %s", s.Name, err)
		}
		cmd.Name = fmt.Sprintf("%s.health", s.Name)
		s.healthCheckCmd = cmd
	}

	interfaces, ifaceErr := utils.ToStringArray(s.Interfaces)
	if ifaceErr != nil {
		return ifaceErr
	}

	ipAddress, err := utils.GetIP(interfaces)
	if err != nil {
		return err
	}
	s.IPAddress = ipAddress

	var consulExtras *discovery.ConsulExtras
	if s.ConsulConfig != nil {

		if s.ConsulConfig.DeregisterCriticalServiceAfter != "" {
			if _, err := time.ParseDuration(s.ConsulConfig.DeregisterCriticalServiceAfter); err != nil {
				return fmt.Errorf("Could not parse consul `deregisterCriticalServiceAfter` in service %s: %s", s.Name, err)
			}
		}

		consulExtras = &discovery.ConsulExtras{
			DeregisterCriticalServiceAfter: s.ConsulConfig.DeregisterCriticalServiceAfter,
			EnableTagOverride:              s.ConsulConfig.EnableTagOverride,
		}
	}

	s.definition = &discovery.ServiceDefinition{
		ID:           s.ID,
		Name:         s.Name,
		Port:         s.Port,
		TTL:          s.TTL,
		Tags:         s.Tags,
		IPAddress:    s.IPAddress,
		ConsulExtras: consulExtras,
	}
	return nil
}