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"
|
2019-03-26 23:26:29 +01:00
|
|
|
"github.com/costela/wesher/etchosts"
|
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)
|
|
|
|
|
2019-03-25 23:31:32 +01:00
|
|
|
wg, err := 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
|
|
|
}
|
|
|
|
|
2020-05-06 19:14:33 +02:00
|
|
|
getMeta := func(limit int) []byte {
|
|
|
|
return encodeNodeMeta(nodeMeta{
|
|
|
|
OverlayAddr: wg.OverlayAddr,
|
|
|
|
PubKey: wg.PubKey.String(),
|
|
|
|
}, limit)
|
|
|
|
}
|
|
|
|
|
2020-05-06 19:19:55 +02:00
|
|
|
cluster, err := newCluster(config.Init, config.ClusterKey, config.BindAddr, config.BindIface, 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
|
|
|
}
|
2020-05-06 19:14:33 +02:00
|
|
|
wg.assignOverlayAddr((*net.IPNet)(config.OverlayNet), cluster.localName)
|
|
|
|
cluster.update()
|
2019-03-25 01:02:10 +01:00
|
|
|
|
2020-05-06 19:27:21 +02:00
|
|
|
nodec := cluster.members() // avoid deadlocks by starting before join
|
2019-08-06 21:53:38 +02:00
|
|
|
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)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-06 19:38:49 +02:00
|
|
|
nodes := parseNodesMeta(rawNodes)
|
2019-07-12 11:08:14 +02:00
|
|
|
if err := wg.setUpInterface(nodes); err != nil {
|
2020-01-31 19:31:50 +01:00
|
|
|
logrus.WithError(err).Error("could not up interface")
|
2019-07-29 19:20:53 +02:00
|
|
|
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()
|
2019-07-06 22:41:58 +02:00
|
|
|
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-07-06 22:41:58 +02:00
|
|
|
}
|
2019-03-26 23:26:29 +01:00
|
|
|
}
|
2019-03-27 22:52:34 +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 []node) error {
|
|
|
|
hosts := make(map[string][]string, len(nodes))
|
|
|
|
for _, n := range nodes {
|
2019-07-12 11:08:14 +02:00
|
|
|
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)
|
|
|
|
}
|