Skip to content

Commit

Permalink
add: compatible with Precmd
Browse files Browse the repository at this point in the history
  • Loading branch information
ak1ra24 committed Dec 13, 2019
1 parent f32c1dd commit bd60dc4
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 32 deletions.
44 changes: 32 additions & 12 deletions cmd/reconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package cmd
import (
"fmt"
"log"
"strings"

"github.com/ak1ra24/tn/shell"
"github.com/spf13/cobra"
Expand All @@ -17,41 +18,57 @@ var reconfCmd = &cobra.Command{
// stop, remove
for _, node := range tnconfig.Nodes {
deleteNode := shell.DeleteNode(node)
fmt.Println(deleteNode)
fmt.Println(strings.Join(deleteNode, "\n"))
}
for _, br := range tnconfig.Switches {
delBrCmd := shell.DeleteSwitch(br)
fmt.Println(delBrCmd)
}

// Stop, remove, create, start and config
// create, start and config
if len(tnconfig.PreCmd.Cmds) != 0 {
preCmds := shell.ExecCmd(tnconfig.PreCmd.Cmds)
fmt.Println(strings.Join(preCmds, "\n"))
}
if len(tnconfig.PreInit.Cmds) != 0 {
shell.ExecCmd(tnconfig.PreInit.Cmds)
preInitCmds := shell.ExecCmd(tnconfig.PreInit.Cmds)
fmt.Println(strings.Join(preInitCmds, "\n"))
}
if len(tnconfig.PostInit.Cmds) != 0 {
shell.ExecCmd(tnconfig.PostInit.Cmds)
postInitCmds := shell.ExecCmd(tnconfig.PostInit.Cmds)
fmt.Println(strings.Join(postInitCmds, "\n"))
}
for _, node := range tnconfig.Nodes {
shell.CreateNode(node)
shell.Mount_docker_netns(node)
createNodeCmds := shell.CreateNode(node)
fmt.Println(strings.Join(createNodeCmds, "\n"))

if node.Type != "netns" {
mountDockerNetnsCmds := shell.Mount_docker_netns(node)
fmt.Println(strings.Join(mountDockerNetnsCmds, "\n"))
}
}

if len(tnconfig.Switches) != 0 {
for _, bridge := range tnconfig.Switches {
shell.CreateSwitch(bridge)
createSwitchCmds := shell.CreateSwitch(bridge)
fmt.Println(strings.Join(createSwitchCmds, "\n"))
}
}

for _, node := range tnconfig.Nodes {
for _, inf := range node.Interfaces {
if inf.Type == "direct" {
shell.N2nLink(node.Name, inf)
n2nLinkCmds := shell.N2nLink(node.Name, inf)
fmt.Println(strings.Join(n2nLinkCmds, "\n"))
} else if inf.Type == "bridge" {
shell.S2nLink(node.Name, inf)
s2nLinkCmd := shell.S2nLink(node.Name, inf)
fmt.Println(strings.Join(s2nLinkCmd, "\n"))
} else if inf.Type == "veth" {
shell.V2cLink(node.Name, inf)
v2cLinkCmds := shell.V2cLink(node.Name, inf)
fmt.Println(strings.Join(v2cLinkCmds, "\n"))
} else if inf.Type == "phys" {
shell.P2cLink(node.Name, inf)
p2cLinkCmds := shell.P2cLink(node.Name, inf)
fmt.Println(strings.Join(p2cLinkCmds, "\n"))
} else {
err := fmt.Errorf("not supported interface type: %s", inf.Type)
log.Fatal(err)
Expand All @@ -65,7 +82,10 @@ var reconfCmd = &cobra.Command{
}

for _, nodeConfig := range tnconfig.NodeConfigs {
shell.ExecConf(nodeinfo[nodeConfig.Name], nodeConfig)
execConfCmds := shell.ExecConf(nodeinfo[nodeConfig.Name], nodeConfig)
for _, execConfCmd := range execConfCmds {
fmt.Println(execConfCmd)
}
}
},
}
Expand Down
37 changes: 27 additions & 10 deletions cmd/reup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package cmd
import (
"fmt"
"log"
"strings"

"github.com/ak1ra24/tn/shell"
"github.com/spf13/cobra"
Expand All @@ -17,41 +18,57 @@ var reupCmd = &cobra.Command{
// stop, remove
for _, node := range tnconfig.Nodes {
deleteNode := shell.DeleteNode(node)
fmt.Println(deleteNode)
fmt.Println(strings.Join(deleteNode, "\n"))
}
for _, br := range tnconfig.Switches {
delBrCmd := shell.DeleteSwitch(br)
fmt.Println(delBrCmd)
}

// create, start
if len(tnconfig.PreCmd.Cmds) != 0 {
preCmds := shell.ExecCmd(tnconfig.PreCmd.Cmds)
fmt.Println(strings.Join(preCmds, "\n"))
}
if len(tnconfig.PreInit.Cmds) != 0 {
shell.ExecCmd(tnconfig.PreInit.Cmds)
preInitCmds := shell.ExecCmd(tnconfig.PreInit.Cmds)
fmt.Println(strings.Join(preInitCmds, "\n"))
}
if len(tnconfig.PostInit.Cmds) != 0 {
shell.ExecCmd(tnconfig.PostInit.Cmds)
postInitCmds := shell.ExecCmd(tnconfig.PostInit.Cmds)
fmt.Println(strings.Join(postInitCmds, "\n"))
}
for _, node := range tnconfig.Nodes {
shell.CreateNode(node)
shell.Mount_docker_netns(node)
createNodeCmds := shell.CreateNode(node)
fmt.Println(strings.Join(createNodeCmds, "\n"))

if node.Type != "netns" {
mountDockerNetnsCmds := shell.Mount_docker_netns(node)
fmt.Println(strings.Join(mountDockerNetnsCmds, "\n"))
}
}

if len(tnconfig.Switches) != 0 {
for _, bridge := range tnconfig.Switches {
shell.CreateSwitch(bridge)
createSwitchCmds := shell.CreateSwitch(bridge)
fmt.Println(strings.Join(createSwitchCmds, "\n"))
}
}

for _, node := range tnconfig.Nodes {
for _, inf := range node.Interfaces {
if inf.Type == "direct" {
shell.N2nLink(node.Name, inf)
n2nLinkCmds := shell.N2nLink(node.Name, inf)
fmt.Println(strings.Join(n2nLinkCmds, "\n"))
} else if inf.Type == "bridge" {
shell.S2nLink(node.Name, inf)
s2nLinkCmd := shell.S2nLink(node.Name, inf)
fmt.Println(strings.Join(s2nLinkCmd, "\n"))
} else if inf.Type == "veth" {
shell.V2cLink(node.Name, inf)
v2cLinkCmds := shell.V2cLink(node.Name, inf)
fmt.Println(strings.Join(v2cLinkCmds, "\n"))
} else if inf.Type == "phys" {
shell.P2cLink(node.Name, inf)
p2cLinkCmds := shell.P2cLink(node.Name, inf)
fmt.Println(strings.Join(p2cLinkCmds, "\n"))
} else {
err := fmt.Errorf("not supported interface type: %s", inf.Type)
log.Fatal(err)
Expand Down
4 changes: 4 additions & 0 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ var upCmd = &cobra.Command{
Use: "up",
Short: "Create and start containers",
Run: func(cmd *cobra.Command, args []string) {
if len(tnconfig.PreCmd.Cmds) != 0 {
preCmds := shell.ExecCmd(tnconfig.PreCmd.Cmds)
fmt.Println(strings.Join(preCmds, "\n"))
}
if len(tnconfig.PreInit.Cmds) != 0 {
preInitCmds := shell.ExecCmd(tnconfig.PreInit.Cmds)
fmt.Println(strings.Join(preInitCmds, "\n"))
Expand Down
42 changes: 32 additions & 10 deletions cmd/upconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package cmd
import (
"fmt"
"log"
"strings"

"github.com/ak1ra24/tn/shell"
"github.com/spf13/cobra"
Expand All @@ -14,47 +15,68 @@ var upconfCmd = &cobra.Command{
Use: "upconf",
Short: "Create, start and config",
Run: func(cmd *cobra.Command, args []string) {
// up
if len(tnconfig.PreCmd.Cmds) != 0 {
preCmds := shell.ExecCmd(tnconfig.PreCmd.Cmds)
fmt.Println(strings.Join(preCmds, "\n"))
}
if len(tnconfig.PreInit.Cmds) != 0 {
shell.ExecCmd(tnconfig.PreInit.Cmds)
preInitCmds := shell.ExecCmd(tnconfig.PreInit.Cmds)
fmt.Println(strings.Join(preInitCmds, "\n"))
}
if len(tnconfig.PostInit.Cmds) != 0 {
shell.ExecCmd(tnconfig.PostInit.Cmds)
postInitCmds := shell.ExecCmd(tnconfig.PostInit.Cmds)
fmt.Println(strings.Join(postInitCmds, "\n"))
}
for _, node := range tnconfig.Nodes {
shell.CreateNode(node)
shell.Mount_docker_netns(node)
createNodeCmds := shell.CreateNode(node)
fmt.Println(strings.Join(createNodeCmds, "\n"))

if node.Type != "netns" {
mountDockerNetnsCmds := shell.Mount_docker_netns(node)
fmt.Println(strings.Join(mountDockerNetnsCmds, "\n"))
}
}

if len(tnconfig.Switches) != 0 {
for _, bridge := range tnconfig.Switches {
shell.CreateSwitch(bridge)
createSwitchCmds := shell.CreateSwitch(bridge)
fmt.Println(strings.Join(createSwitchCmds, "\n"))
}
}

for _, node := range tnconfig.Nodes {
for _, inf := range node.Interfaces {
if inf.Type == "direct" {
shell.N2nLink(node.Name, inf)
n2nLinkCmds := shell.N2nLink(node.Name, inf)
fmt.Println(strings.Join(n2nLinkCmds, "\n"))
} else if inf.Type == "bridge" {
shell.S2nLink(node.Name, inf)
s2nLinkCmd := shell.S2nLink(node.Name, inf)
fmt.Println(strings.Join(s2nLinkCmd, "\n"))
} else if inf.Type == "veth" {
shell.V2cLink(node.Name, inf)
v2cLinkCmds := shell.V2cLink(node.Name, inf)
fmt.Println(strings.Join(v2cLinkCmds, "\n"))
} else if inf.Type == "phys" {
shell.P2cLink(node.Name, inf)
p2cLinkCmds := shell.P2cLink(node.Name, inf)
fmt.Println(strings.Join(p2cLinkCmds, "\n"))
} else {
err := fmt.Errorf("not supported interface type: %s", inf.Type)
log.Fatal(err)
}
}
}

// conf
nodeinfo := map[string]string{}
for _, node := range tnconfig.Nodes {
nodeinfo[node.Name] = node.Type
}

for _, nodeConfig := range tnconfig.NodeConfigs {
shell.ExecConf(nodeinfo[nodeConfig.Name], nodeConfig)
execConfCmds := shell.ExecConf(nodeinfo[nodeConfig.Name], nodeConfig)
for _, execConfCmd := range execConfCmds {
fmt.Println(execConfCmd)
}
}
},
}
Expand Down
15 changes: 15 additions & 0 deletions shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

// Tn tinet config
type Tn struct {
PreCmd PreCmd `yaml:"precmd"`
PreInit PreInit `yaml:"preinit"`
PostInit PostInit `yaml:"postinit"`
PostFini PostFini `yaml:"postfini"`
Expand All @@ -21,6 +22,11 @@ type Tn struct {
Test []Test `yaml:"test"`
}

// PreCmd
type PreCmd struct {
Cmds []Cmd `yaml:"cmds" mapstructure:"cmds"`
}

// PreInit
type PreInit struct {
Cmds []Cmd `yaml:"cmds" mapstructure:"cmds"`
Expand Down Expand Up @@ -170,6 +176,14 @@ func (tnconfig *Tn) Exec(nodeName string, Cmds []string) string {

// GenerateFile Generate tinet template config file
func (tnconfig *Tn) GenerateFile(cfgFile string) error {
precmd := PreCmd{
Cmds: []Cmd{
Cmd{
Cmd: "",
},
},
}

preinit := PreInit{
Cmds: []Cmd{
Cmd{
Expand Down Expand Up @@ -236,6 +250,7 @@ func (tnconfig *Tn) GenerateFile(cfgFile string) error {
}

tnconfig = &Tn{
PreCmd: precmd,
PreInit: preinit,
PostInit: postinit,
PostFini: postfini,
Expand Down

0 comments on commit bd60dc4

Please sign in to comment.