blob: c538eaab10f4cb529b3e4519c3f279d135ac4e51 [file] [log] [blame]
Serge Bazanski8d7843c2018-10-04 10:37:36 +01001package main
2
Serge Bazanskic7be4a12018-10-06 13:18:05 +01003//go:generate packr
4
Serge Bazanski8d7843c2018-10-04 10:37:36 +01005import (
Serge Bazanski2c6c6d12018-10-06 11:55:04 +01006 "context"
Serge Bazanski8d7843c2018-10-04 10:37:36 +01007 "flag"
8 "io/ioutil"
9
Serge Bazanski2c6c6d12018-10-06 11:55:04 +010010 "github.com/digitalocean/go-netbox/netbox"
11 "github.com/digitalocean/go-netbox/netbox/client"
Serge Bazanski8d7843c2018-10-04 10:37:36 +010012 "github.com/golang/glog"
13 "github.com/golang/protobuf/proto"
14
15 confpb "code.hackerspace.pl/q3k/topo/proto/config"
Serge Bazanskia758ef52018-10-06 17:54:25 +010016
17 "code.hackerspace.pl/q3k/topo/graph"
18 "code.hackerspace.pl/q3k/topo/state"
Serge Bazanski8d7843c2018-10-04 10:37:36 +010019)
20
21var (
Serge Bazanski88b572d2018-10-05 16:35:01 -070022 flagConfigPath string
23 flagNetboxHost string
24 flagNetboxAPIKey string
Serge Bazanski46765082018-10-06 12:32:01 +010025 flagDebugListen string
Serge Bazanski8d7843c2018-10-04 10:37:36 +010026)
27
28func init() {
29 flag.Set("logtostderr", "true")
30}
31
32func main() {
33 flag.StringVar(&flagConfigPath, "config_path", "./topo.pb.text", "Text proto configuration of Topo (per config.proto)")
Serge Bazanski2c6c6d12018-10-06 11:55:04 +010034 flag.StringVar(&flagNetboxHost, "netbox_host", "netbox.bgp.wtf", "Netbox host")
Serge Bazanski88b572d2018-10-05 16:35:01 -070035 flag.StringVar(&flagNetboxAPIKey, "netbox_api_key", "", "Netbox API key")
Serge Bazanski46765082018-10-06 12:32:01 +010036 flag.StringVar(&flagDebugListen, "debug_listen", "127.0.0.1:42001", "Debug HTTP listen address")
Serge Bazanski8d7843c2018-10-04 10:37:36 +010037 flag.Parse()
38
Serge Bazanski2c6c6d12018-10-06 11:55:04 +010039 ctx := context.Background()
40
Serge Bazanski8d7843c2018-10-04 10:37:36 +010041 data, err := ioutil.ReadFile(flagConfigPath)
42 if err != nil {
43 glog.Exitf("Could not read config: %v", err)
44 }
45
46 config := confpb.Config{}
47 proto.UnmarshalText(string(data), &config)
Serge Bazanski88b572d2018-10-05 16:35:01 -070048
Serge Bazanski28f49072018-10-06 11:31:18 +010049 gr := graph.New()
50 err = gr.LoadConfig(&config)
51 if err != nil {
52 glog.Exitf("Initial config load failed: %v", err)
Serge Bazanski88b572d2018-10-05 16:35:01 -070053 }
Serge Bazanski28f49072018-10-06 11:31:18 +010054
Serge Bazanski2c6c6d12018-10-06 11:55:04 +010055 client.DefaultSchemes = []string{"https"}
56 nb := netbox.NewNetboxWithAPIKey(flagNetboxHost, flagNetboxAPIKey)
57 err = gr.FeedFromNetbox(ctx, nb)
58 if err != nil {
59 glog.Exitf("Initial netbox feed failed: %v", err)
60 }
Serge Bazanski46765082018-10-06 12:32:01 +010061
Serge Bazanskia758ef52018-10-06 17:54:25 +010062 stm := state.NewManager()
63
Serge Bazanski46765082018-10-06 12:32:01 +010064 sconf := ServiceConfig{
65 DebugListen: flagDebugListen,
66 }
Serge Bazanskia758ef52018-10-06 17:54:25 +010067 srv := NewService(gr, stm, sconf)
Serge Bazanski46765082018-10-06 12:32:01 +010068 srv.Run()
Serge Bazanski8d7843c2018-10-04 10:37:36 +010069}