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