Exemplo n.º 1
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
}
Exemplo 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
	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
}