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"
|
2020-05-06 19:14:33 +02:00
|
|
|
"net"
|
2019-03-25 01:02:10 +01:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2019-08-06 21:53:38 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/cenkalti/backoff"
|
2020-05-07 10:25:58 +02:00
|
|
|
"github.com/costela/wesher/cluster"
|
|
|
|
"github.com/costela/wesher/common"
|
2019-03-26 23:26:29 +01:00
|
|
|
"github.com/costela/wesher/etchosts"
|
2020-05-07 10:25:58 +02:00
|
|
|
"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() {
|
2020-05-07 14:45:45 +02:00
|
|
|
// General initialization
|
2019-03-25 01:02:10 +01:00
|
|
|
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)
|
|
|
|
|
2020-05-07 14:45:45 +02:00
|
|
|
// Create the wireguard and cluster configuration
|
2020-05-07 14:40:22 +02:00
|
|
|
wgstate, err := wg.New(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
|
|
|
}
|
2020-05-07 11:36:46 +02:00
|
|
|
cluster, err := cluster.New(config.Init, config.ClusterKey, config.BindAddr, config.ClusterPort, config.UseIPAsName)
|
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
|
|
|
}
|
2020-05-07 11:36:46 +02:00
|
|
|
|
2020-05-07 14:51:03 +02:00
|
|
|
// Prepare the /etc/hosts writer
|
|
|
|
hostsFile := &etchosts.EtcHosts{
|
|
|
|
Logger: logrus.StandardLogger(),
|
|
|
|
}
|
|
|
|
|
2020-05-07 14:45:45 +02:00
|
|
|
// Assign a local node address and propagate it to the cluster
|
2020-05-07 14:40:22 +02:00
|
|
|
wgstate.AssignOverlayAddr((*net.IPNet)(config.OverlayNet), cluster.LocalName)
|
2020-05-07 14:33:10 +02:00
|
|
|
localNode := common.Node{}
|
2020-05-07 14:40:22 +02:00
|
|
|
localNode.OverlayAddr = wgstate.OverlayAddr
|
|
|
|
localNode.PubKey = wgstate.PubKey.String()
|
2020-05-07 11:36:46 +02:00
|
|
|
cluster.Update(localNode)
|
2019-03-25 01:02:10 +01:00
|
|
|
|
2020-05-07 14:45:45 +02:00
|
|
|
// Join the cluster
|
2020-05-07 10:25:58 +02:00
|
|
|
nodec := cluster.Members() // avoid deadlocks by starting before join
|
2019-08-06 21:53:38 +02:00
|
|
|
if err := backoff.RetryNotify(
|
2020-05-07 10:25:58 +02:00
|
|
|
func() error { return cluster.Join(config.Join) },
|
2019-08-06 21:53:38 +02:00
|
|
|
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)
|
2019-08-06 21:53:38 +02:00
|
|
|
},
|
|
|
|
); 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
|
|
|
}
|
|
|
|
|
2020-05-07 14:45:45 +02:00
|
|
|
// Main loop
|
2019-03-25 01:02:10 +01:00
|
|
|
incomingSigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(incomingSigs, syscall.SIGTERM, os.Interrupt)
|
2019-07-06 22:41:58 +02:00
|
|
|
logrus.Debug("waiting for cluster events")
|
2019-03-25 01:02:10 +01:00
|
|
|
for {
|
|
|
|
select {
|
2020-05-06 19:27:21 +02:00
|
|
|
case rawNodes := <-nodec:
|
2020-05-07 10:25:58 +02:00
|
|
|
nodes := make([]common.Node, 0, len(rawNodes))
|
2020-05-07 14:51:03 +02:00
|
|
|
hosts := make(map[string][]string, len(rawNodes))
|
|
|
|
logrus.Info("cluster members:\n")
|
2020-05-07 09:58:31 +02:00
|
|
|
for _, node := range rawNodes {
|
2020-05-07 11:36:46 +02:00
|
|
|
if err := node.Decode(); err != nil {
|
2020-05-07 09:58:31 +02:00
|
|
|
logrus.Warnf("\t addr: %s, could not decode metadata", node.Addr)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
logrus.Infof("\taddr: %s, overlay: %s, pubkey: %s", node.Addr, node.OverlayAddr, node.PubKey)
|
2020-05-07 14:51:03 +02:00
|
|
|
nodes = append(nodes, node)
|
|
|
|
hosts[node.OverlayAddr.IP.String()] = []string{node.Name}
|
2020-05-07 09:58:31 +02:00
|
|
|
}
|
2020-05-07 14:40:22 +02:00
|
|
|
if err := wgstate.SetUpInterface(nodes); err != nil {
|
2020-01-31 19:31:50 +01:00
|
|
|
logrus.WithError(err).Error("could not up interface")
|
2020-05-07 14:40:22 +02:00
|
|
|
wgstate.DownInterface()
|
2019-03-25 01:02:10 +01:00
|
|
|
}
|
2019-03-26 23:26:29 +01:00
|
|
|
if !config.NoEtcHosts {
|
2020-05-07 14:51:03 +02:00
|
|
|
if err := hostsFile.WriteEntries(hosts); 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...")
|
2020-05-07 10:25:58 +02:00
|
|
|
cluster.Leave()
|
2019-07-06 22:41:58 +02:00
|
|
|
if !config.NoEtcHosts {
|
2020-05-07 14:51:03 +02:00
|
|
|
if err := hostsFile.WriteEntries(map[string][]string{}); err != nil {
|
2020-01-31 19:31:50 +01:00
|
|
|
logrus.WithError(err).Error("could not remove stale hosts entries")
|
2019-07-06 22:41:58 +02:00
|
|
|
}
|
2019-03-26 23:26:29 +01:00
|
|
|
}
|
2020-05-07 14:40:22 +02:00
|
|
|
if err := wgstate.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|