2017-06-02 09:55:29 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-07-15 21:15:59 +00:00
|
|
|
"strings"
|
2017-06-02 09:55:29 +00:00
|
|
|
"time"
|
2017-07-15 21:15:59 +00:00
|
|
|
|
|
|
|
discover "github.com/hashicorp/go-discover"
|
2017-06-02 09:55:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// RetryJoin is used to handle retrying a join until it succeeds or all
|
|
|
|
// retries are exhausted.
|
|
|
|
func (a *Agent) retryJoin() {
|
|
|
|
cfg := a.config
|
2017-07-15 21:15:59 +00:00
|
|
|
if len(cfg.RetryJoin) == 0 {
|
2017-06-23 08:38:55 +00:00
|
|
|
return
|
|
|
|
}
|
2017-06-02 09:55:29 +00:00
|
|
|
|
2017-08-02 15:59:47 +00:00
|
|
|
disco := discover.Discover{}
|
|
|
|
a.logger.Printf("[INFO] agent: Retry join is supported for: %s", strings.Join(disco.Names(), " "))
|
2017-06-02 09:55:29 +00:00
|
|
|
a.logger.Printf("[INFO] agent: Joining cluster...")
|
2017-06-23 08:38:55 +00:00
|
|
|
attempt := 0
|
2017-06-02 09:55:29 +00:00
|
|
|
for {
|
2017-07-15 21:15:59 +00:00
|
|
|
var addrs []string
|
2017-06-23 08:38:55 +00:00
|
|
|
var err error
|
2017-07-15 21:15:59 +00:00
|
|
|
|
|
|
|
for _, addr := range cfg.RetryJoin {
|
|
|
|
switch {
|
|
|
|
case strings.Contains(addr, "provider="):
|
2017-08-02 15:59:47 +00:00
|
|
|
servers, err := disco.Addrs(addr, a.logger)
|
2017-07-15 21:15:59 +00:00
|
|
|
if err != nil {
|
|
|
|
a.logger.Printf("[ERR] agent: %s", err)
|
|
|
|
} else {
|
|
|
|
addrs = append(addrs, servers...)
|
|
|
|
a.logger.Printf("[INFO] agent: Discovered servers: %s", strings.Join(servers, " "))
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
addrs = append(addrs, addr)
|
2017-06-23 08:38:55 +00:00
|
|
|
}
|
2017-06-21 08:40:38 +00:00
|
|
|
}
|
|
|
|
|
2017-07-15 21:15:59 +00:00
|
|
|
if len(addrs) > 0 {
|
|
|
|
n, err := a.JoinLAN(addrs)
|
2017-06-23 08:38:55 +00:00
|
|
|
if err == nil {
|
|
|
|
a.logger.Printf("[INFO] agent: Join completed. Synced with %d initial agents", n)
|
|
|
|
return
|
|
|
|
}
|
2017-06-02 09:55:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-15 21:15:59 +00:00
|
|
|
if len(addrs) == 0 {
|
|
|
|
err = fmt.Errorf("No servers to join")
|
|
|
|
}
|
|
|
|
|
2017-06-23 08:38:55 +00:00
|
|
|
attempt++
|
|
|
|
if cfg.RetryMaxAttempts > 0 && attempt > cfg.RetryMaxAttempts {
|
2017-06-21 08:40:38 +00:00
|
|
|
a.retryJoinCh <- fmt.Errorf("agent: max join retry exhausted, exiting")
|
|
|
|
return
|
|
|
|
}
|
2017-06-23 08:38:55 +00:00
|
|
|
|
2017-06-02 09:55:29 +00:00
|
|
|
a.logger.Printf("[WARN] agent: Join failed: %v, retrying in %v", err, cfg.RetryInterval)
|
|
|
|
time.Sleep(cfg.RetryInterval)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RetryJoinWan is used to handle retrying a join -wan until it succeeds or all
|
|
|
|
// retries are exhausted.
|
|
|
|
func (a *Agent) retryJoinWan() {
|
|
|
|
cfg := a.config
|
|
|
|
|
|
|
|
if len(cfg.RetryJoinWan) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.logger.Printf("[INFO] agent: Joining WAN cluster...")
|
|
|
|
|
|
|
|
attempt := 0
|
|
|
|
for {
|
|
|
|
n, err := a.JoinWAN(cfg.RetryJoinWan)
|
|
|
|
if err == nil {
|
|
|
|
a.logger.Printf("[INFO] agent: Join -wan completed. Synced with %d initial agents", n)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
attempt++
|
|
|
|
if cfg.RetryMaxAttemptsWan > 0 && attempt > cfg.RetryMaxAttemptsWan {
|
|
|
|
a.retryJoinCh <- fmt.Errorf("agent: max join -wan retry exhausted, exiting")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.logger.Printf("[WARN] agent: Join -wan failed: %v, retrying in %v", err, cfg.RetryIntervalWan)
|
|
|
|
time.Sleep(cfg.RetryIntervalWan)
|
|
|
|
}
|
|
|
|
}
|