Skip to content

Commit

Permalink
Merge branch 'master' into slankdev-update-gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
AkiRa committed Jan 31, 2020
2 parents 6c8ec72 + 0c5d43b commit 1d4811b
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 61 deletions.
5 changes: 3 additions & 2 deletions cmd/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/tinynetwork/tinet/internal/pkg/utils"
)

// confCmd represents the conf command
Expand All @@ -20,7 +21,7 @@ var confCmd = &cobra.Command{
for _, nodeConfig := range tnconfig.NodeConfigs {
execConfCmds := nodeConfig.ExecConf(nodeinfo[nodeConfig.Name])
for _, execConfCmd := range execConfCmds {
fmt.Println(execConfCmd)
utils.PrintCmd(os.Stdout, execConfCmd, verbose)
}
}
},
Expand Down
7 changes: 4 additions & 3 deletions cmd/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
package cmd

import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/tinynetwork/tinet/internal/pkg/utils"
)

// downCmd represents the down command
Expand All @@ -15,11 +16,11 @@ var downCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
for _, node := range tnconfig.Nodes {
deleteNode := node.DeleteNode()
fmt.Println(strings.Join(deleteNode, "\n"))
utils.PrintCmd(os.Stdout, strings.Join(deleteNode, "\n"), verbose)
}
for _, br := range tnconfig.Switches {
delBrCmd := br.DeleteSwitch()
fmt.Println(delBrCmd)
utils.PrintCmd(os.Stdout, delBrCmd, verbose)
}
},
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/tinynetwork/tinet/internal/pkg/utils"
)

// execCmd represents the exec command
Expand All @@ -14,7 +15,7 @@ var execCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
execCmdArgs := cmd.Flags().Args()
execCommand := tnconfig.Exec(execCmdArgs[0], execCmdArgs[1:])
fmt.Println(execCommand)
utils.PrintCmd(os.Stdout, execCommand, verbose)
},
}

Expand Down
31 changes: 17 additions & 14 deletions cmd/reconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ package cmd
import (
"fmt"
"log"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/tinynetwork/tinet/internal/pkg/shell"
"github.com/tinynetwork/tinet/internal/pkg/utils"
)

// reconfCmd represents the reconf command
Expand All @@ -18,57 +20,57 @@ var reconfCmd = &cobra.Command{
// stop, remove
for _, node := range tnconfig.Nodes {
deleteNode := node.DeleteNode()
fmt.Println(strings.Join(deleteNode, "\n"))
utils.PrintCmd(os.Stdout, strings.Join(deleteNode, "\n"), verbose)
}
for _, br := range tnconfig.Switches {
delBrCmd := br.DeleteSwitch()
fmt.Println(delBrCmd)
utils.PrintCmd(os.Stdout, delBrCmd, verbose)
}

// create, start and config
if len(tnconfig.PreCmd) != 0 {
for _, preCmds := range tnconfig.PreCmd {
preExecCmds := shell.ExecCmd(preCmds.Cmds)
fmt.Println(strings.Join(preExecCmds, "\n"))
utils.PrintCmd(os.Stdout, strings.Join(preExecCmds, "\n"), verbose)
}
}
if len(tnconfig.PreInit) != 0 {
for _, preInitCmds := range tnconfig.PreInit {
preExecInitCmds := shell.ExecCmd(preInitCmds.Cmds)
fmt.Println(strings.Join(preExecInitCmds, "\n"))
utils.PrintCmd(os.Stdout, strings.Join(preExecInitCmds, "\n"), verbose)
}
}
for _, node := range tnconfig.Nodes {
createNodeCmds := node.CreateNode()
fmt.Println(strings.Join(createNodeCmds, "\n"))
utils.PrintCmd(os.Stdout, strings.Join(createNodeCmds, "\n"), verbose)

if node.Type != "netns" {
mountDockerNetnsCmds := node.Mount_docker_netns()
fmt.Println(strings.Join(mountDockerNetnsCmds, "\n"))
utils.PrintCmd(os.Stdout, strings.Join(mountDockerNetnsCmds, "\n"), verbose)
}
}

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

for _, node := range tnconfig.Nodes {
for _, inf := range node.Interfaces {
if inf.Type == "direct" {
n2nLinkCmds := inf.N2nLink(node.Name)
fmt.Println(strings.Join(n2nLinkCmds, "\n"))
utils.PrintCmd(os.Stdout, strings.Join(n2nLinkCmds, "\n"), verbose)
} else if inf.Type == "bridge" {
s2nLinkCmd := inf.S2nLink(node.Name)
fmt.Println(strings.Join(s2nLinkCmd, "\n"))
s2nLinkCmds := inf.S2nLink(node.Name)
utils.PrintCmd(os.Stdout, strings.Join(s2nLinkCmds, "\n"), verbose)
} else if inf.Type == "veth" {
v2cLinkCmds := inf.V2cLink(node.Name)
fmt.Println(strings.Join(v2cLinkCmds, "\n"))
utils.PrintCmd(os.Stdout, strings.Join(v2cLinkCmds, "\n"), verbose)
} else if inf.Type == "phys" {
p2cLinkCmds := inf.P2cLink(node.Name)
fmt.Println(strings.Join(p2cLinkCmds, "\n"))
utils.PrintCmd(os.Stdout, strings.Join(p2cLinkCmds, "\n"), verbose)
} else {
err := fmt.Errorf("not supported interface type: %s", inf.Type)
log.Fatal(err)
Expand All @@ -79,10 +81,11 @@ var reconfCmd = &cobra.Command{
if len(tnconfig.PostInit) != 0 {
for _, postInitCmds := range tnconfig.PostInit {
postExecInitCmds := shell.ExecCmd(postInitCmds.Cmds)
fmt.Println(strings.Join(postExecInitCmds, "\n"))
utils.PrintCmd(os.Stdout, strings.Join(postExecInitCmds, "\n"), verbose)
}
}

// conf
nodeinfo := map[string]string{}
for _, node := range tnconfig.Nodes {
nodeinfo[node.Name] = node.Type
Expand All @@ -91,7 +94,7 @@ var reconfCmd = &cobra.Command{
for _, nodeConfig := range tnconfig.NodeConfigs {
execConfCmds := nodeConfig.ExecConf(nodeinfo[nodeConfig.Name])
for _, execConfCmd := range execConfCmds {
fmt.Println(execConfCmd)
utils.PrintCmd(os.Stdout, execConfCmd, verbose)
}
}
},
Expand Down
30 changes: 16 additions & 14 deletions cmd/reup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ package cmd
import (
"fmt"
"log"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/tinynetwork/tinet/internal/pkg/shell"
"github.com/tinynetwork/tinet/internal/pkg/utils"
)

// reupCmd represents the reup command
Expand All @@ -18,57 +20,57 @@ var reupCmd = &cobra.Command{
// stop, remove
for _, node := range tnconfig.Nodes {
deleteNode := node.DeleteNode()
fmt.Println(strings.Join(deleteNode, "\n"))
utils.PrintCmd(os.Stdout, strings.Join(deleteNode, "\n"), verbose)
}
for _, br := range tnconfig.Switches {
delBrCmd := br.DeleteSwitch()
fmt.Println(delBrCmd)
utils.PrintCmd(os.Stdout, delBrCmd, verbose)
}

// create, start
// create, start and config
if len(tnconfig.PreCmd) != 0 {
for _, preCmds := range tnconfig.PreCmd {
preExecCmds := shell.ExecCmd(preCmds.Cmds)
fmt.Println(strings.Join(preExecCmds, "\n"))
utils.PrintCmd(os.Stdout, strings.Join(preExecCmds, "\n"), verbose)
}
}
if len(tnconfig.PreInit) != 0 {
for _, preInitCmds := range tnconfig.PreInit {
preExecInitCmds := shell.ExecCmd(preInitCmds.Cmds)
fmt.Println(strings.Join(preExecInitCmds, "\n"))
utils.PrintCmd(os.Stdout, strings.Join(preExecInitCmds, "\n"), verbose)
}
}
for _, node := range tnconfig.Nodes {
createNodeCmds := node.CreateNode()
fmt.Println(strings.Join(createNodeCmds, "\n"))
utils.PrintCmd(os.Stdout, strings.Join(createNodeCmds, "\n"), verbose)

if node.Type != "netns" {
mountDockerNetnsCmds := node.Mount_docker_netns()
fmt.Println(strings.Join(mountDockerNetnsCmds, "\n"))
utils.PrintCmd(os.Stdout, strings.Join(mountDockerNetnsCmds, "\n"), verbose)
}
}

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

for _, node := range tnconfig.Nodes {
for _, inf := range node.Interfaces {
if inf.Type == "direct" {
n2nLinkCmds := inf.N2nLink(node.Name)
fmt.Println(strings.Join(n2nLinkCmds, "\n"))
utils.PrintCmd(os.Stdout, strings.Join(n2nLinkCmds, "\n"), verbose)
} else if inf.Type == "bridge" {
s2nLinkCmd := inf.S2nLink(node.Name)
fmt.Println(strings.Join(s2nLinkCmd, "\n"))
s2nLinkCmds := inf.S2nLink(node.Name)
utils.PrintCmd(os.Stdout, strings.Join(s2nLinkCmds, "\n"), verbose)
} else if inf.Type == "veth" {
v2cLinkCmds := inf.V2cLink(node.Name)
fmt.Println(strings.Join(v2cLinkCmds, "\n"))
utils.PrintCmd(os.Stdout, strings.Join(v2cLinkCmds, "\n"), verbose)
} else if inf.Type == "phys" {
p2cLinkCmds := inf.P2cLink(node.Name)
fmt.Println(strings.Join(p2cLinkCmds, "\n"))
utils.PrintCmd(os.Stdout, strings.Join(p2cLinkCmds, "\n"), verbose)
} else {
err := fmt.Errorf("not supported interface type: %s", inf.Type)
log.Fatal(err)
Expand All @@ -79,7 +81,7 @@ var reupCmd = &cobra.Command{
if len(tnconfig.PostInit) != 0 {
for _, postInitCmds := range tnconfig.PostInit {
postExecInitCmds := shell.ExecCmd(postInitCmds.Cmds)
fmt.Println(strings.Join(postExecInitCmds, "\n"))
utils.PrintCmd(os.Stdout, strings.Join(postExecInitCmds, "\n"), verbose)
}
}
},
Expand Down
11 changes: 11 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

var cfgFile string
var tnconfig shell.Tn
var verbose bool

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -57,6 +58,16 @@ func init() {
upCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./spec.yaml)")
upconfCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./spec.yaml)")

// verbose option
confCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose (default: false)")
upconfCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose (default: false)")
upCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose (default: false)")
reupCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose (default: false)")
reconfCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose (default: false)")
execCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose (default: false)")
downCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose (default: false)")
testCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose (default: false)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
// rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
Expand Down
6 changes: 3 additions & 3 deletions cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
package cmd

import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/tinynetwork/tinet/internal/pkg/shell"
"github.com/tinynetwork/tinet/internal/pkg/utils"
)

// testCmd represents the test command
Expand All @@ -15,8 +16,7 @@ var testCmd = &cobra.Command{
Short: "Execute tests",
Run: func(cmd *cobra.Command, args []string) {
tnTestCmds := shell.TnTestCmdExec(tnconfig.Test)
fmt.Println(strings.Join(tnTestCmds, "\n"))

utils.PrintCmd(os.Stdout, strings.Join(tnTestCmds, "\n"), verbose)
},
}

Expand Down
24 changes: 13 additions & 11 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ package cmd
import (
"fmt"
"log"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/tinynetwork/tinet/internal/pkg/shell"
"github.com/tinynetwork/tinet/internal/pkg/utils"
)

// upCmd represents the up command
Expand All @@ -18,46 +20,46 @@ var upCmd = &cobra.Command{
if len(tnconfig.PreCmd) != 0 {
for _, preCmds := range tnconfig.PreCmd {
preExecCmds := shell.ExecCmd(preCmds.Cmds)
fmt.Println(strings.Join(preExecCmds, "\n"))
utils.PrintCmd(os.Stdout, strings.Join(preExecCmds, "\n"), verbose)
}
}
if len(tnconfig.PreInit) != 0 {
for _, preInitCmds := range tnconfig.PreInit {
preExecInitCmds := shell.ExecCmd(preInitCmds.Cmds)
fmt.Println(strings.Join(preExecInitCmds, "\n"))
utils.PrintCmd(os.Stdout, strings.Join(preExecInitCmds, "\n"), verbose)
}
}
for _, node := range tnconfig.Nodes {
createNodeCmds := node.CreateNode()
fmt.Println(strings.Join(createNodeCmds, "\n"))
utils.PrintCmd(os.Stdout, strings.Join(createNodeCmds, "\n"), verbose)

if node.Type != "netns" {
mountDockerNetnsCmds := node.Mount_docker_netns()
fmt.Println(strings.Join(mountDockerNetnsCmds, "\n"))
utils.PrintCmd(os.Stdout, strings.Join(mountDockerNetnsCmds, "\n"), verbose)
}
}

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

for _, node := range tnconfig.Nodes {
for _, inf := range node.Interfaces {
if inf.Type == "direct" {
n2nLinkCmds := inf.N2nLink(node.Name)
fmt.Println(strings.Join(n2nLinkCmds, "\n"))
utils.PrintCmd(os.Stdout, strings.Join(n2nLinkCmds, "\n"), verbose)
} else if inf.Type == "bridge" {
s2nLinkCmd := inf.S2nLink(node.Name)
fmt.Println(strings.Join(s2nLinkCmd, "\n"))
s2nLinkCmds := inf.S2nLink(node.Name)
utils.PrintCmd(os.Stdout, strings.Join(s2nLinkCmds, "\n"), verbose)
} else if inf.Type == "veth" {
v2cLinkCmds := inf.V2cLink(node.Name)
fmt.Println(strings.Join(v2cLinkCmds, "\n"))
utils.PrintCmd(os.Stdout, strings.Join(v2cLinkCmds, "\n"), verbose)
} else if inf.Type == "phys" {
p2cLinkCmds := inf.P2cLink(node.Name)
fmt.Println(strings.Join(p2cLinkCmds, "\n"))
utils.PrintCmd(os.Stdout, strings.Join(p2cLinkCmds, "\n"), verbose)
} else {
err := fmt.Errorf("not supported interface type: %s", inf.Type)
log.Fatal(err)
Expand All @@ -68,7 +70,7 @@ var upCmd = &cobra.Command{
if len(tnconfig.PostInit) != 0 {
for _, postInitCmds := range tnconfig.PostInit {
postExecInitCmds := shell.ExecCmd(postInitCmds.Cmds)
fmt.Println(strings.Join(postExecInitCmds, "\n"))
utils.PrintCmd(os.Stdout, strings.Join(postExecInitCmds, "\n"), verbose)
}
}
},
Expand Down
Loading

0 comments on commit 1d4811b

Please sign in to comment.