wesher/main.go

119 lines
3.2 KiB
Go
Raw Normal View History

2019-03-25 01:02:10 +01:00
package main // import "github.com/costela/wesher"
import (
2019-03-26 23:26:54 +01:00
"fmt"
"net"
2019-03-25 01:02:10 +01:00
"os"
"os/signal"
"syscall"
"time"
"github.com/cenkalti/backoff"
"github.com/costela/wesher/cluster"
"github.com/costela/wesher/common"
2019-03-26 23:26:29 +01:00
"github.com/costela/wesher/etchosts"
"github.com/costela/wesher/wg"
2020-01-31 19:31:50 +01:00
"github.com/sirupsen/logrus"
2019-03-25 01:02:10 +01:00
)
2019-03-26 23:26:54 +01:00
var version = "dev"
2019-03-25 01:02:10 +01:00
func main() {
config, err := loadConfig()
if err != nil {
logrus.Fatal(err)
}
2019-03-26 23:26:54 +01:00
if config.Version {
fmt.Println(version)
os.Exit(0)
}
2019-03-25 01:02:10 +01:00
logLevel, err := logrus.ParseLevel(config.LogLevel)
if err != nil {
2020-01-31 19:31:50 +01:00
logrus.WithError(err).Fatal("could not parse loglevel")
2019-03-25 01:02:10 +01:00
}
logrus.SetLevel(logLevel)
wg, err := wg.NewWGConfig(config.Interface, config.WireguardPort)
2019-03-25 01:02:10 +01:00
if err != nil {
2020-01-31 19:31:50 +01:00
logrus.WithError(err).Fatal("could not instantiate wireguard controller")
2019-03-25 01:02:10 +01:00
}
getMeta := func(limit int) []byte {
return common.EncodeNodeMeta(common.NodeMeta{
OverlayAddr: wg.OverlayAddr,
PubKey: wg.PubKey.String(),
}, limit)
}
cluster, err := cluster.New(config.Init, config.ClusterKey, config.BindAddr, config.ClusterPort, config.UseIPAsName, getMeta)
2019-03-25 01:02:10 +01:00
if err != nil {
2020-01-31 19:31:50 +01:00
logrus.WithError(err).Fatal("could not create cluster")
2019-03-25 01:02:10 +01:00
}
wg.AssignOverlayAddr((*net.IPNet)(config.OverlayNet), cluster.LocalName)
cluster.Update()
2019-03-25 01:02:10 +01:00
nodec := cluster.Members() // avoid deadlocks by starting before join
if err := backoff.RetryNotify(
func() error { return cluster.Join(config.Join) },
backoff.NewExponentialBackOff(),
func(err error, dur time.Duration) {
2020-01-31 19:31:50 +01:00
logrus.WithError(err).Errorf("could not join cluster, retrying in %s", dur)
},
); err != nil {
2020-01-31 19:31:50 +01:00
logrus.WithError(err).Fatal("could not join cluster")
2019-03-25 01:02:10 +01:00
}
incomingSigs := make(chan os.Signal, 1)
signal.Notify(incomingSigs, syscall.SIGTERM, os.Interrupt)
logrus.Debug("waiting for cluster events")
2019-03-25 01:02:10 +01:00
for {
select {
case rawNodes := <-nodec:
logrus.Info("cluster members:\n")
nodes := make([]common.Node, 0, len(rawNodes))
for _, node := range rawNodes {
meta, err := common.DecodeNodeMeta(node.Meta)
if err != nil {
logrus.Warnf("\t addr: %s, could not decode metadata", node.Addr)
continue
}
node.NodeMeta = meta
nodes = append(nodes, node)
logrus.Infof("\taddr: %s, overlay: %s, pubkey: %s", node.Addr, node.OverlayAddr, node.PubKey)
}
if err := wg.SetUpInterface(nodes); err != nil {
2020-01-31 19:31:50 +01:00
logrus.WithError(err).Error("could not up interface")
wg.DownInterface()
2019-03-25 01:02:10 +01:00
}
2019-03-26 23:26:29 +01:00
if !config.NoEtcHosts {
if err := writeToEtcHosts(nodes); err != nil {
2020-01-31 19:31:50 +01:00
logrus.WithError(err).Error("could not write hosts entries")
2019-03-26 23:26:29 +01:00
}
}
2019-03-25 01:02:10 +01:00
case <-incomingSigs:
logrus.Info("terminating...")
cluster.Leave()
if !config.NoEtcHosts {
if err := writeToEtcHosts(nil); err != nil {
2020-01-31 19:31:50 +01:00
logrus.WithError(err).Error("could not remove stale hosts entries")
}
2019-03-26 23:26:29 +01:00
}
if err := wg.DownInterface(); err != nil {
2020-01-31 19:31:50 +01:00
logrus.WithError(err).Error("could not down interface")
2019-03-27 22:52:34 +01:00
}
2019-03-25 01:02:10 +01:00
os.Exit(0)
}
}
}
2019-03-26 23:26:29 +01:00
func writeToEtcHosts(nodes []common.Node) error {
2019-03-26 23:26:29 +01:00
hosts := make(map[string][]string, len(nodes))
for _, n := range nodes {
hosts[n.OverlayAddr.IP.String()] = []string{n.Name}
2019-03-26 23:26:29 +01:00
}
hostsFile := &etchosts.EtcHosts{
Logger: logrus.StandardLogger(),
}
return hostsFile.WriteEntries(hosts)
}