From 8a18d561fc0df212894c3be1e5eda765f695b45b Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Fri, 1 Nov 2024 09:37:16 +0100 Subject: [PATCH] Add "cmd" option to start commands on each node --- config.go | 15 ++++++++++++++- main.go | 9 +++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/config.go b/config.go index 3e69cad..ab29d16 100644 --- 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" ` err = parseConfigLink(&cfg, xs[1:]) + case "cmd": + usage = `"cmd" ` + 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 85ddd3b..0ceeadf 100644 --- 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) { -- 2.45.2