Example #1
0
// NewBackends creates a new backend from a raw config structure
func NewBackends(raw []interface{}, disc discovery.ServiceBackend) ([]*Backend, error) {
	if raw == nil {
		return []*Backend{}, nil
	}
	var backends []*Backend
	if err := utils.DecodeRaw(raw, &backends); err != nil {
		return nil, fmt.Errorf("Backend configuration error: %v", err)
	}
	for _, b := range backends {
		if err := utils.ValidateServiceName(b.Name); err != nil {
			return nil, err
		}
		if b.OnChangeExec == nil {
			return nil, fmt.Errorf("`onChange` is required in backend %s",
				b.Name)
		}
		cmd, err := commands.NewCommand(b.OnChangeExec, b.Timeout)
		if err != nil {
			return nil, fmt.Errorf("Could not parse `onChange` in backend %s: %s",
				b.Name, err)
		}
		cmd.Name = fmt.Sprintf("%s.health", b.Name)
		b.onChangeCmd = cmd

		if b.Poll < 1 {
			return nil, fmt.Errorf("`poll` must be > 0 in backend %s",
				b.Name)
		}
		b.onChangeCmd = cmd
		b.discoveryService = disc
	}
	return backends, nil
}
Example #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
}
Example #3
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
}