Sergiusz Bazanski | e08e6da | 2018-08-27 20:40:10 +0100 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "flag" |
| 5 | "fmt" |
| 6 | |
| 7 | "github.com/golang/glog" |
| 8 | "github.com/ybbus/jsonrpc" |
| 9 | ) |
| 10 | |
| 11 | var ( |
| 12 | flagAristaAPI string |
| 13 | flagListenAddress string |
| 14 | flagDebugAddress string |
| 15 | flagCAPath string |
| 16 | flagCertificatePath string |
| 17 | flagKeyPath string |
Sergiusz Bazanski | e08e6da | 2018-08-27 20:40:10 +0100 | [diff] [blame] | 18 | ) |
| 19 | |
| 20 | type aristaClient struct { |
| 21 | rpc jsonrpc.RPCClient |
| 22 | } |
| 23 | |
| 24 | func (c *aristaClient) structuredCall(res interface{}, command ...string) error { |
| 25 | cmd := struct { |
| 26 | Version int `json:"version"` |
| 27 | Cmds []string `json:"cmds"` |
| 28 | Format string `json:"format"` |
| 29 | }{ |
| 30 | Version: 1, |
| 31 | Cmds: command, |
| 32 | Format: "json", |
| 33 | } |
| 34 | |
| 35 | err := c.rpc.CallFor(res, "runCmds", cmd) |
| 36 | if err != nil { |
| 37 | return fmt.Errorf("could not execute structured call: %v", err) |
| 38 | } |
| 39 | return nil |
| 40 | } |
| 41 | |
| 42 | func main() { |
| 43 | flag.StringVar(&flagAristaAPI, "arista_api", "http://admin:password@1.2.3.4:80/command-api", "Arista remote endpoint") |
Sergiusz Bazanski | c3caeff | 2018-08-27 21:03:03 +0100 | [diff] [blame] | 44 | flag.StringVar(&flagListenAddress, "listen_address", "127.0.0.1:42000", "gRPC listen address") |
| 45 | flag.StringVar(&flagDebugAddress, "debug_address", "127.0.0.1:42001", "Debug HTTP listen address, or empty to disable") |
Sergiusz Bazanski | e08e6da | 2018-08-27 20:40:10 +0100 | [diff] [blame] | 46 | flag.StringVar(&flagCAPath, "tls_ca_path", "pki/ca.pem", "Path to PKI CA certificate") |
| 47 | flag.StringVar(&flagCertificatePath, "tls_certificate_path", "pki/service.pem", "Path to PKI service certificate") |
| 48 | flag.StringVar(&flagKeyPath, "tls_key_path", "pki/service-key.pem", "Path to PKI service private key") |
Sergiusz Bazanski | e08e6da | 2018-08-27 20:40:10 +0100 | [diff] [blame] | 49 | flag.Set("logtostderr", "true") |
| 50 | flag.Parse() |
| 51 | |
| 52 | arista := &aristaClient{ |
| 53 | rpc: jsonrpc.NewClient(flagAristaAPI), |
| 54 | } |
| 55 | |
| 56 | opts := &serverOpts{ |
| 57 | listenAddress: flagListenAddress, |
| 58 | debugAddress: flagDebugAddress, |
| 59 | tlsCAPath: flagCAPath, |
| 60 | tlsCertificatePath: flagCertificatePath, |
| 61 | tlsKeyPath: flagKeyPath, |
Sergiusz Bazanski | e08e6da | 2018-08-27 20:40:10 +0100 | [diff] [blame] | 62 | } |
| 63 | server, err := newServer(opts, arista) |
| 64 | if err != nil { |
| 65 | glog.Errorf("Could not create server: %v", err) |
| 66 | } |
| 67 | |
| 68 | glog.Info("Starting up...") |
| 69 | server.serveForever() |
| 70 | } |