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"
|
2019-03-25 01:02:10 +01:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2019-03-26 23:26:29 +01:00
|
|
|
|
|
|
|
"github.com/costela/wesher/etchosts"
|
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 {
|
|
|
|
logrus.Fatalf("could not parse loglevel: %s", err)
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cluster, err := newCluster(config, wg)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatalf("could not create cluster: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nodec, errc := cluster.members() // avoid deadlocks by starting before join
|
2019-03-25 23:31:32 +01:00
|
|
|
if err := cluster.join(config.Join); err != nil {
|
2019-03-25 01:02:10 +01:00
|
|
|
logrus.Fatalf("could not join cluster: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
case nodes := <-nodec:
|
|
|
|
logrus.Info("cluster members:\n")
|
|
|
|
for _, node := range nodes {
|
|
|
|
logrus.Infof("\taddr: %s, overlay: %s, pubkey: %s", node.Addr, node.OverlayAddr, node.PubKey)
|
|
|
|
}
|
2019-07-12 11:08:14 +02:00
|
|
|
if err := wg.setUpInterface(nodes); err != nil {
|
2019-03-25 01:02:10 +01:00
|
|
|
logrus.Errorf("could not up interface: %s", err)
|
|
|
|
}
|
2019-03-26 23:26:29 +01:00
|
|
|
if !config.NoEtcHosts {
|
|
|
|
if err := writeToEtcHosts(nodes); err != nil {
|
|
|
|
logrus.Errorf("could not write hosts entries: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2019-03-25 01:02:10 +01:00
|
|
|
case errs := <-errc:
|
|
|
|
logrus.Errorf("could not receive node info: %s", errs)
|
|
|
|
case <-incomingSigs:
|
|
|
|
logrus.Info("terminating...")
|
|
|
|
cluster.leave()
|
2019-07-06 22:41:58 +02:00
|
|
|
if !config.NoEtcHosts {
|
|
|
|
if err := writeToEtcHosts(nil); err != nil {
|
|
|
|
logrus.Errorf("could not remove stale hosts entries: %s", err)
|
|
|
|
}
|
2019-03-26 23:26:29 +01:00
|
|
|
}
|
2019-03-27 22:52:34 +01:00
|
|
|
if err := wg.downInterface(); err != nil {
|
|
|
|
logrus.Errorf("could not down interface: %s", err)
|
|
|
|
}
|
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)
|
|
|
|
}
|