Skip to content

Commit

Permalink
add config check cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
ak1ra24 committed Dec 15, 2019
1 parent 99fa3de commit 88baeea
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 4 deletions.
76 changes: 76 additions & 0 deletions cmd/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Package cmd
package cmd

import (
"log"
"strings"

"github.com/spf13/cobra"
)

// checkCmd represents the check command
var checkCmd = &cobra.Command{
Use: "check",
Short: "check config",
Run: func(cmd *cobra.Command, args []string) {
nodes := tnconfig.Nodes
bridges := tnconfig.Switches
confmap := map[string]string{}

for _, node := range nodes {
for _, inf := range node.Interfaces {
// fmt.Println(node.Name, " : ", inf.InfName, "->", inf.PeerNode, " : ", inf.PeerInf)
if inf.Type == "direct" {
host := node.Name + ":" + inf.Name
peer := strings.Split(inf.Args, "#")
target := peer[0] + ":" + peer[1]
confmap[host] = target
} else if inf.Type == "bridge" {
host := node.Name + ":" + inf.Name
target := inf.Args + ":" + node.Name
confmap[host] = target
}
}
}

for _, bridge := range bridges {
for _, inf := range bridge.Interfaces {
host := bridge.Name + ":" + inf.Args
target := inf.Args + ":" + inf.Name
confmap[host] = target
}
}

var matchNum int
falseConfigMap := map[string]string{}

for key, value := range confmap {
if confmap[key] == value && confmap[value] == key {
matchNum++
} else {
falseConfigMap[key] = value
}
}

if len(confmap) == matchNum {
log.Println("Success Check!")
} else {
log.Fatalf("Failed Check: %s\n", falseConfigMap)
}

},
}

func init() {
rootCmd.AddCommand(checkCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// checkCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// checkCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func init() {
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

checkCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./spec.yaml)")
confCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./spec.yaml)")
downCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./spec.yaml)")
execCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./spec.yaml)")
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/k0kubun/pp v3.0.1+incompatible
github.com/logrusorgru/aurora v0.0.0-20191116043053-66b7ad493a23
github.com/mitchellh/go-homedir v1.1.0
github.com/sirupsen/logrus v1.2.0
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.6.1
gopkg.in/yaml.v2 v2.2.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb
github.com/ryanuber/go-glob v0.0.0-20160226084822-572520ed46db/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/sethgrid/pester v0.0.0-20180227223404-ed9870dad317/go.mod h1:Ad7IjTpvzZO8Fl0vh9AzQ+j/jYZfyp2diGwI8m5q+ns=
github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
Expand Down Expand Up @@ -273,6 +274,7 @@ golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnf
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190506204251-e1dfcc566284 h1:rlLehGeYg6jfoyz/eDqDU1iRXLKfR42nnNh57ytKEWo=
golang.org/x/crypto v0.0.0-20190506204251-e1dfcc566284/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down
16 changes: 12 additions & 4 deletions shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package shell
import (
"fmt"
"io/ioutil"
"log"
"strings"

l "github.com/sirupsen/logrus"

"github.com/ak1ra24/tn/utils"
"gopkg.in/yaml.v2"
)

var log = l.New()

// Tn tinet config
type Tn struct {
PreCmd []PreCmd `yaml:"precmd"`
Expand Down Expand Up @@ -58,6 +61,7 @@ type Interface struct {
Name string `yaml:"name"`
Type string `yaml:"type"`
Args string `yaml:"args"`
Addr string `yaml:"addr"`
}

// Sysctl
Expand Down Expand Up @@ -90,7 +94,6 @@ type Test struct {

// BuildCmd
func BuildCmd(nodes []Node) string {
// log.Println("sorry not implement...")
return "sorry not implement..."
}

Expand Down Expand Up @@ -158,7 +161,7 @@ func (tnconfig *Tn) Exec(nodeName string, Cmds []string) string {

if selectedNode == nil {
err := fmt.Errorf("no such node...\n")
log.Fatal(err)
log.Error(err)
}

if selectedNode.Type == "docker" {
Expand All @@ -169,7 +172,7 @@ func (tnconfig *Tn) Exec(nodeName string, Cmds []string) string {
execCommand = fmt.Sprintf("docker exec %s", nodeName)
} else {
err := fmt.Errorf("no such node type...\n")
log.Fatal(err)
log.Error(err)
}

var cmdStr string
Expand Down Expand Up @@ -435,6 +438,11 @@ func N2nLink(nodeName string, inf Interface) []string {
n2nLinkCmds = append(n2nLinkCmds, NetnsLinkUp(nodeName, nodeinf))
n2nLinkCmds = append(n2nLinkCmds, NetnsLinkUp(peerNode, peerinf))

if len(inf.Addr) != 0 {
addrSetCmd := fmt.Sprintf("ip netns exec %s ip link set %s address %s", nodeName, inf.Name, inf.Addr)
n2nLinkCmds = append(n2nLinkCmds, addrSetCmd)
}

return n2nLinkCmds
}

Expand Down
13 changes: 13 additions & 0 deletions shell/shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,19 @@ func TestN2nLink(t *testing.T) {
},
want: []string{"ip link add net0 netns R1 type veth peer name net0 netns R2", "ip netns exec R1 ip link set net0 up", "ip netns exec R2 ip link set net0 up"},
},
{
name: "peer between containers and set macaddress",
args: args{
nodeName: "R1",
inf: Interface{
Name: "net0",
Type: "direct",
Args: "R2#net0",
Addr: "52:54:00:11:11:11",
},
},
want: []string{"ip link add net0 netns R1 type veth peer name net0 netns R2", "ip netns exec R1 ip link set net0 up", "ip netns exec R2 ip link set net0 up", "ip netns exec R1 ip link set net0 address 52:54:00:11:11:11"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 88baeea

Please sign in to comment.