Skip to content

Commit

Permalink
Update backup config and quagga status get method.
Browse files Browse the repository at this point in the history
  • Loading branch information
kishiguro committed May 15, 2018
1 parent 1718d1f commit 15c9b72
Show file tree
Hide file tree
Showing 13 changed files with 515 additions and 56 deletions.
10 changes: 10 additions & 0 deletions config/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/coreswitch/cmd"
"github.com/coreswitch/component"
"github.com/coreswitch/openconfigd/quagga"
"github.com/coreswitch/process"
)

Expand Down Expand Up @@ -90,6 +91,12 @@ func showIpBgpNeighbor(Args []string) (inst int, instStr string) {
return
}

func showQuaggaPassword(Args []string) (inst int, instStr string) {
inst = CliSuccessShow
instStr = fmt.Sprintf("quagga password is: %s", quagga.GetPasswd())
return
}

func enableFunc(Args []string) (inst int, instStr string) {
inst = CliSuccessExec
instStr = "CLI_PRIVILEGE=15;_cli_refresh"
Expand Down Expand Up @@ -355,6 +362,8 @@ func (this *CliComponent) Start() component.Component {
&cmd.Param{Helps: []string{"Start", "Process"}})
mode.InstallLine("stop process WORD", stopProcess,
&cmd.Param{Helps: []string{"Stop", "Process"}})
mode.InstallLine("show quagga password", showQuaggaPassword,
&cmd.Param{Helps: []string{"Show running system information", "quagga infromation", "Show password"}})

// Link "run" command to operational node.
run := mode.Parser.Lookup("run")
Expand All @@ -363,6 +372,7 @@ func (this *CliComponent) Start() component.Component {
Parser.InstallLine("system host-name WORD", HostnameApi)
Parser.InstallLine("system etcd endpoints WORD", EtcdEndpointsApi)
Parser.InstallLine("system etcd path WORD", EtcdPathApi)
Parser.InstallLine("system gobgp grpcendpoint WORD", ConfigureGobgpGrpcEndpointApi)
Parser.InstallLine("interfaces interface WORD dhcp-relay-group WORD", RelayApi)

TopCmd = Cmd
Expand Down
40 changes: 33 additions & 7 deletions config/gobgp.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ import (
log "github.com/sirupsen/logrus"
)

const defaultGrpcEndpoint = ":50051"

var (
gobgpConfig GobgpConfig
gobgpRouterId string
gobgpGrpcEndpoint string = defaultGrpcEndpoint
)

type GobgpConfig struct {
Expand Down Expand Up @@ -126,6 +129,10 @@ func (lhs *VrfRib) Equal(rhs *VrfRib) bool {
return true
}

func NewGobgpClient() (*client.Client, error) {
return client.New(gobgpGrpcEndpoint)
}

func GobgpRouterIdRegister(routerId string) {
if gobgpRouterId == routerId {
return
Expand Down Expand Up @@ -184,7 +191,7 @@ func GobgpStaticPath(s *Route) (*table.Path, error) {
}

func GobgpClearVrfRib(c *VrfConfig) error {
client, err := client.New("")
client, err := NewGobgpClient()
if err != nil {
log.WithFields(log.Fields{
"error": err,
Expand Down Expand Up @@ -359,7 +366,7 @@ func GobgpSetGlobal(client *client.Client, cfg *GobgpConfig) {
}

func GobgpSetZebraRoutine() error {
client, err := client.New("")
client, err := NewGobgpClient()
if err != nil {
return err
}
Expand Down Expand Up @@ -538,7 +545,7 @@ func GobgpClearGlobalPolicy(client *client.Client) {
}

func GobgpClearAll() {
client, err := client.New("")
client, err := NewGobgpClient()
if err != nil {
fmt.Println("GobgpStopServer:", err)
return
Expand Down Expand Up @@ -636,7 +643,7 @@ func GobgpUpdateVrf(client *client.Client, cfg *GobgpConfig) {

func GobgpUpdate(cfg *GobgpConfig) error {
fmt.Println("Updating configuration")
client, err := client.New("")
client, err := NewGobgpClient()
if err != nil {
return err
}
Expand All @@ -662,8 +669,7 @@ func GobgpUpdate(cfg *GobgpConfig) error {
}

func GobgpReset(cfg *GobgpConfig) error {
fmt.Println("New configuration")
client, err := client.New("")
client, err := NewGobgpClient()
if err != nil {
log.WithFields(log.Fields{
"error": err,
Expand Down Expand Up @@ -855,7 +861,7 @@ func GobgpVrfRibSync(name string, old, new []VrfRib) {
}
fmt.Println("------")

client, err := client.New("")
client, err := NewGobgpClient()
if err != nil {
log.WithFields(log.Fields{
"error": err,
Expand Down Expand Up @@ -1150,3 +1156,23 @@ func stringToCommunityValue(comStr string) uint32 {
val, _ := strconv.ParseUint(elem[1], 10, 16)
return uint32(asn<<16 | val)
}

func ConfigureGobgpGrpcEndpointApi(set bool, args []interface{}) {
if len(args) != 1 {
return
}
grpcEndPoint := args[0].(string)
if set {
SetGobgpGrpcEndpoint(grpcEndPoint)
} else {
ClearGobgpGrpcEndpoint()
}
}

func SetGobgpGrpcEndpoint(grpcEndpoint string) {
gobgpGrpcEndpoint = grpcEndpoint
}

func ClearGobgpGrpcEndpoint() {
gobgpGrpcEndpoint = defaultGrpcEndpoint
}
6 changes: 4 additions & 2 deletions config/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,13 @@ func DynamicCompletion(commands []string, module string, args []string) []string
}

// RPC component.
type RpcComponent struct{}
type RpcComponent struct {
GrpcEndpoint string
}

// RPC component start method.
func (this *RpcComponent) Start() component.Component {
lis, err := net.Listen("tcp", ":2650")
lis, err := net.Listen("tcp", this.GrpcEndpoint)
if err != nil {
grpclog.Fatalf("failed %v", err)
}
Expand Down
17 changes: 7 additions & 10 deletions config/ospf-status.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import (
"time"

"github.com/coreos/etcd/clientv3"
"github.com/coreswitch/openconfigd/quagga"
"github.com/mitchellh/mapstructure"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
)

Expand Down Expand Up @@ -128,21 +130,16 @@ func OspfStatusRoute(vrfName string, stat *OspfVrfStat) error {
}
return nil
}

func OspfStatusNeighbor(vrfName string, stat *OspfVrfStat) error {
cmd := exec.Command("vtysh", "-c", "show ip ospf neighbor")
env := os.Environ()
env = append(env, fmt.Sprintf("VRF=%s", vrfName))
env = append(env, "LD_PRELOAD=/usr/bin/vrf_socket.so")
cmd.Env = env

out, err := cmd.Output()
var in []string
in = append(in, "show ip ospf neighbor\n")
out, err := VrfQuaggaGet(vrfName, "ospfd", quagga.GetPasswd(), time.Second, in)
if err != nil {
fmt.Println("OspfStatusNeighbor err:", err)
log.Error("QuaggaStatusBgpSummary: VrfQuaggaGet()", err)
return err
}

strs := strings.Split(string(out), "\n")
strs := strings.Split(out[0], "\n")
for _, str := range strs {
r := regexp.MustCompile(`^[0-9]`)
if r.MatchString(str) {
Expand Down
4 changes: 3 additions & 1 deletion config/ospf.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"os"
"text/template"

"github.com/coreswitch/openconfigd/quagga"
"github.com/coreswitch/process"
)

Expand Down Expand Up @@ -75,7 +76,7 @@ func (lhs *OspfArray) Equal(rhs *OspfArray) bool {
}

var ospfTemplate = `!
password 8 AU67iiPSXYm96
password 8 {{passwdHash}}
service password-encryption
!
{{range $i, $v := .OspfArray}}{{interfaceIp2Config $v}}{{end}}
Expand Down Expand Up @@ -164,6 +165,7 @@ func OspfExec(vrfId int, ospfArray *OspfArray) {
tmpl := template.Must(template.New("ospfTmpl").Funcs(template.FuncMap{
"areaAuthentication": areaAuthentication,
"interfaceIp2Config": interfaceIp2Config,
"passwdHash": quagga.GetHash,
}).Parse(ospfTemplate))
tmpl.Execute(f, &TemplValue{OspfArray: ospfArray})

Expand Down
35 changes: 15 additions & 20 deletions config/quagga-status.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ package config
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"time"

"github.com/coreos/etcd/clientv3"
"github.com/coreswitch/openconfigd/quagga"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
)

Expand All @@ -33,20 +33,16 @@ type QuaggaStat struct {
}

func QuaggaStatusBgpSummary(vrf string, stat *GobgpStat) error {
cmd := exec.Command("vtysh", "-c", "show ip bgp sum")
env := os.Environ()
env = append(env, fmt.Sprintf("VRF=%s", vrf))
env = append(env, "LD_PRELOAD=/usr/bin/vrf_socket.so")
cmd.Env = env

out, err := cmd.Output()
var in []string
in = append(in, "show ip bgp sum\n")
out, err := VrfQuaggaGet(vrf, "bgpd", quagga.GetPasswd(), time.Second, in)
if err != nil {
fmt.Println("QuaggaStatusBgpSummary: err", err)
log.Error("QuaggaStatusBgpSummary: VrfQuaggaGet()", err)
return err
}
neighbor := false

strs := strings.Split(string(out), "\n")
strs := strings.Split(out[0], "\n")
for _, str := range strs {
if !neighbor {
r := regexp.MustCompile(`^BGP[a-z\s]+([\d\.]+).*\s([\d\.]+)$`)
Expand Down Expand Up @@ -84,20 +80,18 @@ func QuaggaStatusBgpSummary(vrf string, stat *GobgpStat) error {
}

func QuaggaStatusRib(vrf string, stat *GobgpStat) error {
cmd := exec.Command("vtysh", "-c", "show ip bgp")
env := os.Environ()
env = append(env, fmt.Sprintf("VRF=%s", vrf))
env = append(env, "LD_PRELOAD=/usr/bin/vrf_socket.so")
cmd.Env = env
var in []string
fmt.Println("QuaggaStatusRib(): neighbor", stat.Neighbor[0].Peer)

out, err := cmd.Output()
in = append(in, fmt.Sprintf("show ip bgp neighbor %s received-routes\n", stat.Neighbor[0].Peer))
out, err := VrfQuaggaGet(vrf, "bgpd", quagga.GetPasswd(), time.Second, in)
if err != nil {
fmt.Println("QuaggaStatusBgpSummary: err", err)
log.Error("QuaggaStatusBgpRib: VrfQuaggaGet()", err)
return err
}
network := false

strs := strings.Split(string(out), "\n")
strs := strings.Split(out[0], "\n")
for _, str := range strs {
if !network {
r := regexp.MustCompile(`^\s+Network`)
Expand Down Expand Up @@ -154,7 +148,8 @@ func QuaggaStatusUpdate() {
stats := QuaggaStat{}

for vrfId, _ := range QuaggaProc {
QuaggaStatusVrf(fmt.Sprintf("vrf%d", vrfId), &stats)
vrf := fmt.Sprintf("vrf%d", vrfId)
QuaggaStatusVrf(vrf, &stats)
}

str := ""
Expand Down
7 changes: 6 additions & 1 deletion config/quagga.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import (
"fmt"
"io/ioutil"
"os"
"regexp"

"github.com/coreswitch/netutil"
"github.com/coreswitch/openconfigd/quagga"
"github.com/coreswitch/process"
)

Expand All @@ -38,7 +40,7 @@ func QuaggaExit() {
}

func QuaggaDelete(vrfId int) {
fmt.Printf("[quagga]delete: vrfId %+v, Processes: %+v\n", vrfId, QuaggaProc[vrfId])
fmt.Println("[quagga]delete: vrfId, Processes: %+v", vrfId, QuaggaProc[vrfId])
_, ok := QuaggaProc[vrfId]
if ok {
for _, proc := range QuaggaProc[vrfId] {
Expand All @@ -56,6 +58,9 @@ func QuaggaDelete(vrfId int) {
}

func QuaggaExec(vrfId int, interfaceName string, configStr string) {
re := regexp.MustCompile(`password 8 ([0-9A-Za-z]{13})`)
configStr = re.ReplaceAllString(configStr, "password 8 "+quagga.GetHash())

fmt.Println("[quagga]config: vrfId", vrfId, "Interface Name: ", interfaceName, configStr)
configFileName := fmt.Sprintf("/etc/quagga/bgpd-vrf%d-%s.conf", vrfId, interfaceName)
zapiSocketName := fmt.Sprintf("/var/run/zserv-vrf%d.api", vrfId)
Expand Down
Loading

0 comments on commit 15c9b72

Please sign in to comment.