]> ruderich.org/simon Gitweb - linux-network-namespace-labs/linux-network-namespace-labs.git/commitdiff
Add "cmd" option to start commands on each node
authorSimon Ruderich <simon@ruderich.org>
Fri, 1 Nov 2024 08:37:16 +0000 (09:37 +0100)
committerSimon Ruderich <simon@ruderich.org>
Fri, 1 Nov 2024 08:37:16 +0000 (09:37 +0100)
config.go
main.go

index 3e69cadb997180923e2c7919005795ed0cea71ce..ab29d16a48a5256d20143c7a31123d51f37f3852 100644 (file)
--- a/config.go
+++ b/config.go
@@ -17,6 +17,7 @@ type Config struct {
        Nets  map[string]*Net
        Nodes map[string]*Node
        Links []*Link
+       Cmds  []string
 }
 
 type Net struct {
@@ -91,8 +92,11 @@ func LoadConfig(path string) (*Config, error) {
                case "link":
                        usage = `"link" <node> <node> <net-name>`
                        err = parseConfigLink(&cfg, xs[1:])
+               case "cmd":
+                       usage = `"cmd" <string-passed-to-sh-c>`
+                       err = parseConfigCmd(&cfg, xs[1:])
                default:
-                       usage = `"net" | "node" | "link"`
+                       usage = `"net" | "node" | "link" | "cmd"`
                        err = fmt.Errorf("unknown option %q", xs[0])
                }
                if err != nil {
@@ -218,4 +222,13 @@ func parseConfigLink(cfg *Config, args []string) error {
        return nil
 }
 
+func parseConfigCmd(cfg *Config, args []string) error {
+       if len(args) < 1 {
+               return fmt.Errorf("not enough arguments")
+       }
+
+       cfg.Cmds = append(cfg.Cmds, strings.Join(args, " "))
+       return nil
+}
+
 // vi: set noet ts=4 sw=4 sts=4:
diff --git a/main.go b/main.go
index 85ddd3b16eec63b5f585ffb1c0cd7b9ce3e2ba00..0ceeadf587aed2ed80f791c5b0ac89159a74e72e 100644 (file)
--- a/main.go
+++ b/main.go
@@ -122,6 +122,15 @@ func main() {
                ip("-n", nsa, "link", "set", la, "up")
                ip("-n", nsb, "link", "set", lb, "up")
        }
+
+       log.Printf("Starting cmds ...")
+       for _, node := range cfg.Nodes {
+               log.Printf(" For node %q", node.Name)
+               ns := node.Name
+               for _, cmd := range cfg.Cmds {
+                       ip("netns", "exec", ns, "sh", "-c", cmd, "argv0", ns)
+               }
+       }
 }
 
 func ip(args ...string) {