Serge Bazanski | 8d7843c | 2018-10-04 10:37:36 +0100 | [diff] [blame] | 1 | package main |
| 2 | |
Serge Bazanski | c7be4a1 | 2018-10-06 13:18:05 +0100 | [diff] [blame] | 3 | //go:generate packr |
| 4 | |
Serge Bazanski | 8d7843c | 2018-10-04 10:37:36 +0100 | [diff] [blame] | 5 | import ( |
Serge Bazanski | 2c6c6d1 | 2018-10-06 11:55:04 +0100 | [diff] [blame] | 6 | "context" |
Serge Bazanski | 8d7843c | 2018-10-04 10:37:36 +0100 | [diff] [blame] | 7 | "flag" |
| 8 | "io/ioutil" |
| 9 | |
Serge Bazanski | 2c6c6d1 | 2018-10-06 11:55:04 +0100 | [diff] [blame] | 10 | "github.com/digitalocean/go-netbox/netbox" |
| 11 | "github.com/digitalocean/go-netbox/netbox/client" |
Serge Bazanski | 8d7843c | 2018-10-04 10:37:36 +0100 | [diff] [blame] | 12 | "github.com/golang/glog" |
| 13 | "github.com/golang/protobuf/proto" |
| 14 | |
| 15 | confpb "code.hackerspace.pl/q3k/topo/proto/config" |
Serge Bazanski | a758ef5 | 2018-10-06 17:54:25 +0100 | [diff] [blame] | 16 | |
| 17 | "code.hackerspace.pl/q3k/topo/graph" |
| 18 | "code.hackerspace.pl/q3k/topo/state" |
Serge Bazanski | 8d7843c | 2018-10-04 10:37:36 +0100 | [diff] [blame] | 19 | ) |
| 20 | |
| 21 | var ( |
Serge Bazanski | 88b572d | 2018-10-05 16:35:01 -0700 | [diff] [blame] | 22 | flagConfigPath string |
| 23 | flagNetboxHost string |
| 24 | flagNetboxAPIKey string |
Serge Bazanski | 4676508 | 2018-10-06 12:32:01 +0100 | [diff] [blame] | 25 | flagDebugListen string |
Serge Bazanski | 8d7843c | 2018-10-04 10:37:36 +0100 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | func init() { |
| 29 | flag.Set("logtostderr", "true") |
| 30 | } |
| 31 | |
| 32 | func main() { |
| 33 | flag.StringVar(&flagConfigPath, "config_path", "./topo.pb.text", "Text proto configuration of Topo (per config.proto)") |
Serge Bazanski | 2c6c6d1 | 2018-10-06 11:55:04 +0100 | [diff] [blame] | 34 | flag.StringVar(&flagNetboxHost, "netbox_host", "netbox.bgp.wtf", "Netbox host") |
Serge Bazanski | 88b572d | 2018-10-05 16:35:01 -0700 | [diff] [blame] | 35 | flag.StringVar(&flagNetboxAPIKey, "netbox_api_key", "", "Netbox API key") |
Serge Bazanski | 4676508 | 2018-10-06 12:32:01 +0100 | [diff] [blame] | 36 | flag.StringVar(&flagDebugListen, "debug_listen", "127.0.0.1:42001", "Debug HTTP listen address") |
Serge Bazanski | 8d7843c | 2018-10-04 10:37:36 +0100 | [diff] [blame] | 37 | flag.Parse() |
| 38 | |
Serge Bazanski | 2c6c6d1 | 2018-10-06 11:55:04 +0100 | [diff] [blame] | 39 | ctx := context.Background() |
| 40 | |
Serge Bazanski | 8d7843c | 2018-10-04 10:37:36 +0100 | [diff] [blame] | 41 | 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 Bazanski | 88b572d | 2018-10-05 16:35:01 -0700 | [diff] [blame] | 48 | |
Serge Bazanski | 16e4ba2 | 2018-10-07 00:22:52 +0100 | [diff] [blame^] | 49 | stm := state.NewManager() |
| 50 | err = stm.FetchState(ctx, &config) |
| 51 | if err != nil { |
| 52 | glog.Exitf("Initial state fetch failed: %v", err) |
| 53 | } |
| 54 | |
Serge Bazanski | 28f4907 | 2018-10-06 11:31:18 +0100 | [diff] [blame] | 55 | gr := graph.New() |
| 56 | err = gr.LoadConfig(&config) |
| 57 | if err != nil { |
| 58 | glog.Exitf("Initial config load failed: %v", err) |
Serge Bazanski | 88b572d | 2018-10-05 16:35:01 -0700 | [diff] [blame] | 59 | } |
Serge Bazanski | 28f4907 | 2018-10-06 11:31:18 +0100 | [diff] [blame] | 60 | |
Serge Bazanski | 2c6c6d1 | 2018-10-06 11:55:04 +0100 | [diff] [blame] | 61 | client.DefaultSchemes = []string{"https"} |
| 62 | nb := netbox.NewNetboxWithAPIKey(flagNetboxHost, flagNetboxAPIKey) |
| 63 | err = gr.FeedFromNetbox(ctx, nb) |
| 64 | if err != nil { |
| 65 | glog.Exitf("Initial netbox feed failed: %v", err) |
| 66 | } |
Serge Bazanski | 4676508 | 2018-10-06 12:32:01 +0100 | [diff] [blame] | 67 | |
| 68 | sconf := ServiceConfig{ |
| 69 | DebugListen: flagDebugListen, |
| 70 | } |
Serge Bazanski | a758ef5 | 2018-10-06 17:54:25 +0100 | [diff] [blame] | 71 | srv := NewService(gr, stm, sconf) |
Serge Bazanski | 4676508 | 2018-10-06 12:32:01 +0100 | [diff] [blame] | 72 | srv.Run() |
Serge Bazanski | 8d7843c | 2018-10-04 10:37:36 +0100 | [diff] [blame] | 73 | } |