1
0
Fork 0
hscloud/dc/topo/main.go

80 lines
1.7 KiB
Go
Raw Normal View History

2018-10-04 09:37:36 +00:00
package main
2018-10-06 12:18:05 +00:00
//go:generate packr
2018-10-04 09:37:36 +00:00
import (
2018-10-06 10:55:04 +00:00
"context"
2018-10-04 09:37:36 +00:00
"flag"
"io/ioutil"
2018-10-25 11:35:55 +00:00
"code.hackerspace.pl/hscloud/go/mirko"
2018-10-06 10:55:04 +00:00
"github.com/digitalocean/go-netbox/netbox"
"github.com/digitalocean/go-netbox/netbox/client"
2018-10-04 09:37:36 +00:00
"github.com/golang/glog"
"github.com/golang/protobuf/proto"
"code.hackerspace.pl/hscloud/dc/topo/graph"
pb "code.hackerspace.pl/hscloud/dc/topo/proto"
"code.hackerspace.pl/hscloud/dc/topo/state"
2018-10-04 09:37:36 +00:00
)
var (
2018-10-05 23:35:01 +00:00
flagConfigPath string
flagNetboxHost string
flagNetboxAPIKey string
2018-10-04 09:37:36 +00:00
)
func init() {
flag.Set("logtostderr", "true")
}
func main() {
flag.StringVar(&flagConfigPath, "config_path", "./topo.pb.text", "Text proto configuration of Topo (per config.proto)")
2018-10-06 10:55:04 +00:00
flag.StringVar(&flagNetboxHost, "netbox_host", "netbox.bgp.wtf", "Netbox host")
2018-10-05 23:35:01 +00:00
flag.StringVar(&flagNetboxAPIKey, "netbox_api_key", "", "Netbox API key")
2018-10-04 09:37:36 +00:00
flag.Parse()
2018-10-14 15:39:30 +00:00
m := mirko.New()
if err := m.Listen(); err != nil {
glog.Exitf("Listen(): %v", err)
}
2018-10-06 10:55:04 +00:00
ctx := context.Background()
2018-10-04 09:37:36 +00:00
data, err := ioutil.ReadFile(flagConfigPath)
if err != nil {
glog.Exitf("Could not read config: %v", err)
}
2018-10-25 12:36:18 +00:00
config := pb.Config{}
2018-10-04 09:37:36 +00:00
proto.UnmarshalText(string(data), &config)
2018-10-05 23:35:01 +00:00
2018-10-06 23:22:52 +00:00
stm := state.NewManager()
err = stm.FetchState(ctx, &config)
if err != nil {
glog.Exitf("Initial state fetch failed: %v", err)
}
2018-10-06 10:31:18 +00:00
gr := graph.New()
err = gr.LoadConfig(&config)
if err != nil {
glog.Exitf("Initial config load failed: %v", err)
2018-10-05 23:35:01 +00:00
}
2018-10-06 10:31:18 +00:00
2018-10-06 10:55:04 +00:00
client.DefaultSchemes = []string{"https"}
nb := netbox.NewNetboxWithAPIKey(flagNetboxHost, flagNetboxAPIKey)
err = gr.FeedFromNetbox(ctx, nb)
if err != nil {
glog.Exitf("Initial netbox feed failed: %v", err)
}
2018-10-06 11:32:01 +00:00
2018-10-14 15:39:30 +00:00
s := NewService(gr, stm)
s.Setup(m)
if err := m.Serve(); err != nil {
glog.Exitf("Serve(): %v", err)
2018-10-06 11:32:01 +00:00
}
2018-10-14 15:39:30 +00:00
select {}
2018-10-04 09:37:36 +00:00
}