Esempio n. 1
0
// setupConsulClient creates a ConsulService
func (c *Client) setupConsulClient() error {
	cfg := consul.ConsulConfig{
		Addr:      c.config.ReadDefault("consul.address", "127.0.0.1:8500"),
		Token:     c.config.Read("consul.token"),
		Auth:      c.config.Read("consul.auth"),
		EnableSSL: c.config.ReadBoolDefault("consul.ssl", false),
		VerifySSL: c.config.ReadBoolDefault("consul.verifyssl", true),
	}

	cs, err := consul.NewConsulService(&cfg, c.logger, "")
	c.consulService = cs
	return err
}
Esempio n. 2
0
// syncAgentServicesWithConsul syncs the client and server services with Consul
func (a *Agent) syncAgentServicesWithConsul(clientHttpAddr string, serverHttpAddr string) error {
	cs, err := consul.NewConsulService(a.consulConfig, a.logger)
	if err != nil {
		return err
	}
	a.consulService = cs
	var services []*structs.Service
	if a.client != nil && a.config.ConsulConfig.ClientServiceName != "" {
		if err != nil {
			return err
		}
		clientService := &structs.Service{
			Name:      a.config.ConsulConfig.ClientServiceName,
			PortLabel: clientHttpAddr,
		}
		services = append(services, clientService)
		cs.SetServiceIdentifier("agent-client")
	}
	if a.server != nil && a.config.ConsulConfig.ServerServiceName != "" {
		serverService := &structs.Service{
			Name:      a.config.ConsulConfig.ServerServiceName,
			PortLabel: serverHttpAddr,
		}
		services = append(services, serverService)
		cs.SetServiceIdentifier("agent-server")
	}

	cs.SetAddrFinder(func(portLabel string) (string, int) {
		host, port, err := net.SplitHostPort(portLabel)
		if err != nil {
			return "", 0
		}

		// if the addr for the service is ":port", then we default to
		// registering the service with ip as the loopback addr
		if host == "" {
			host = "127.0.0.1"
		}
		p, err := strconv.Atoi(port)
		if err != nil {
			return "", 0
		}
		return host, p
	})

	return cs.SyncServices(services)
}
Esempio n. 3
0
func (e *UniversalExecutor) SyncServices(ctx *ConsulContext) error {
	e.logger.Printf("[INFO] executor: registering services")
	e.consulCtx = ctx
	if e.consulService == nil {
		cs, err := consul.NewConsulService(ctx.ConsulConfig, e.logger, e.ctx.AllocID)
		if err != nil {
			return err
		}
		cs.SetDelegatedChecks(e.createCheckMap(), e.createCheck)
		e.consulService = cs
	}
	if e.ctx != nil {
		e.interpolateServices(e.ctx.Task)
	}
	err := e.consulService.SyncTask(e.ctx.Task)
	go e.consulService.PeriodicSync()
	return err
}
Esempio n. 4
0
// SyncServices syncs the services of the task that the executor is running with
// Consul
func (e *UniversalExecutor) SyncServices(ctx *ConsulContext) error {
	e.logger.Printf("[INFO] executor: registering services")
	e.consulCtx = ctx
	if e.consulService == nil {
		cs, err := consul.NewConsulService(ctx.ConsulConfig, e.logger)
		if err != nil {
			return err
		}
		cs.SetDelegatedChecks(e.createCheckMap(), e.createCheck)
		cs.SetServiceIdentifier(consul.GenerateServiceIdentifier(e.ctx.AllocID, e.ctx.Task.Name))
		cs.SetAddrFinder(e.ctx.Task.FindHostAndPortFor)
		e.consulService = cs
	}
	if e.ctx != nil {
		e.interpolateServices(e.ctx.Task)
	}
	err := e.consulService.SyncServices(e.ctx.Task.Services)
	go e.consulService.PeriodicSync()
	return err
}
Esempio n. 5
0
// setupConsulClient creates a ConsulService
func (c *Client) setupConsulClient() error {
	cs, err := consul.NewConsulService(c.config.ConsulConfig, c.logger)
	c.consulService = cs
	return err
}