Exemplo n.º 1
0
func NewClient(address, dc, aclToken string) (*ConsulAlertClient, error) {
	config := consulapi.DefaultConfig()
	config.Address = address
	config.Datacenter = dc
	config.Token = aclToken
	api, _ := consulapi.NewClient(config)
	alertConfig := DefaultAlertConfig()

	client := &ConsulAlertClient{
		api:    api,
		config: alertConfig,
	}

	try := 1
	for {
		try += try
		log.Println("Checking consul agent connection...")
		_, err := client.api.Status().Leader()
		if err != nil {
			log.Println("Waiting for consul:", err)
			if try > 10 {
				return nil, err
			}
			time.Sleep(10000 * time.Millisecond)
		} else {
			break
		}
	}

	client.LoadConfig()
	client.UpdateCheckData()
	return client, nil
}
Exemplo n.º 2
0
// ConsulClient is a helper to create the consulapi client for access to the Consul cluster.
func (c *Candidate) consulClient() *consulapi.Client {
	config := consulapi.DefaultConfig()
	if c.ConsulAddress != "" {
		config.Address = c.ConsulAddress
	}
	if c.ConsulDatacenter != "" {
		config.Datacenter = c.ConsulDatacenter
	}
	if c.ConsulAclToken != "" {
		config.Token = c.ConsulAclToken
	}
	client, _ := consulapi.NewClient(config)
	return client
}
Exemplo n.º 3
0
func startLeaderElection(addr, dc, acl string) *LeaderElection {
	config := consulapi.DefaultConfig()
	config.Address = addr
	config.Datacenter = dc
	config.Token = acl
	client, _ := consulapi.NewClient(config)
	lock, _ := client.LockKey(LockKey)

	leader := &LeaderElection{
		lock:           lock,
		cleanupChannel: make(chan struct{}, 1),
		stopChannel:    make(chan struct{}, 1),
	}

	go leader.start()

	return leader
}
Exemplo n.º 4
0
func NewClient(address, dc, aclToken string) (*ConsulAlertClient, error) {
	config := consulapi.DefaultConfig()
	config.Address = address
	config.Datacenter = dc
	config.Token = aclToken
	api, _ := consulapi.NewClient(config)
	alertConfig := DefaultAlertConfig()

	client := &ConsulAlertClient{
		api:    api,
		config: alertConfig,
	}

	log.Println("Checking consul agent connection...")
	if _, err := client.api.Status().Leader(); err != nil {
		return nil, err
	}

	client.LoadConfig()
	client.UpdateCheckData()
	return client, nil
}