/* Copyright © 2022 Joel D. Elkins Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package container import ( "fmt" "os" "os/exec" "strconv" "strings" "gitea.elkins.co/Networking/ccl/internal/pkg/command" "gitea.elkins.co/Networking/ccl/internal/pkg/network" ) type Container struct { Category string Name string Image string Hostname string Command string Arguments string Networks []network.Network createCommands []command.Command upCommands []command.Command pid int } func (c *Container) flushIPv6Commands() []command.Command { cmds := []command.Command{} flush6Routes := false if len(c.Networks) > 0 && !*c.Networks[0].IPv6 { cmds = append(cmds, command.NewShell(fmt.Sprintf("ip netns exec %s sysctl -w net.ipv6.conf.default.accept_ra=0", c.Name))) cmds = append(cmds, command.NewShell(fmt.Sprintf("ip netns exec %s sysctl -w net.ipv6.conf.all.accept_ra=0", c.Name))) flush6Routes = true } for i, n := range c.Networks { // Note, n.IPv6 should never be nil, since we forcibly create the // value in Init() if not specified in the config file if !*n.IPv6 { cmds = append(cmds, command.NewShell(fmt.Sprintf("ip netns exec %s sysctl -w net.ipv6.conf.eth%d.accept_ra=0", c.Name, i))) cmds = append(cmds, command.NewShell(fmt.Sprintf("ip netns exec %s ip -6 address flush dev eth%d scope global", c.Name, i))) flush6Routes = true } } if flush6Routes { cmds = append(cmds, command.NewShell(fmt.Sprintf("ip netns exec %s ip -6 route flush proto ra", c.Name))) } return cmds } func (c *Container) CreateCommands() []command.Command { if len(c.createCommands) == 0 { c.initCommands() } return c.createCommands } func (c *Container) RecreateCommands() []command.Command { wasRunning := false return []command.Command{ command.NewFunc("stash_run_state", func() string { wasRunning = c.isRunning() runMsg := "not running. Will not start it after recreating." if wasRunning { runMsg = "running. Will restart after recreating." } return fmt.Sprintf("Container %s is %s.", c.Name, runMsg) }), command.NewSet(c.DestroyCommands()), command.NewSet(c.CreateCommands()), command.NewConditional("start_if_was_running", func() bool { return wasRunning }, command.NewSet(c.StartCommands()), command.NewNop(), ), } } func (c *Container) DestroyCommands() []command.Command { cmds := c.StopCommands() cmds = append(cmds, command.NewShell("podman rm "+c.Name)) return cmds } func (c *Container) StartCommands() []command.Command { if len(c.upCommands) == 0 { c.initCommands() } return []command.Command{ command.NewConditional("start_unless_running", c.isRunning, command.NewNop(), command.NewSet(c.upCommands), ), } } func (c *Container) RestartCommands() []command.Command { return []command.Command{ command.NewSet(c.StopCommands()), command.NewShell("sleep 1"), command.NewSet(c.StartCommands()), } } func (c *Container) isRunning() bool { pid, err := c.Pid() if err != nil { return false } return pid > 1 } func (c *Container) UpdateCommands() []command.Command { wasRunning := false return []command.Command{ command.NewFunc("stash_run_state", func() string { wasRunning = c.isRunning() runMsg := "not running" if wasRunning { runMsg = "running" } return "Container " + c.Name + " is " + runMsg }), command.NewShell("podman pull " + c.Image), command.NewSet(c.RecreateCommands()), command.NewConditional("restart_if_was_running", func() bool { return wasRunning }, command.NewSet(c.StartCommands()), command.NewNop(), ), } } func (c *Container) StopCommands() []command.Command { return []command.Command{ command.NewConditional("stop_if_running", c.isRunning, command.NewShell("podman stop "+c.Name), command.NewNop(), ), } } func (c *Container) Init(nets *[]network.Network) { for i := range c.Networks { var n *network.Network for j := range *nets { if (*nets)[j].Name == c.Networks[i].Name { n = &(*nets)[j] } } if n == nil { continue } if len(c.Networks[i].DNS) == 0 { c.Networks[i].DNS = n.DNS } if c.Networks[i].IPv6 == nil { if n.IPv6 != nil { c.Networks[i].IPv6 = n.IPv6 } else { yes := true c.Networks[i].IPv6 = &yes } } } } func (c *Container) Pid() (pid int, err error) { pid_s, err := exec.Command("podman", "inspect", "-f", "{{.State.Pid}}", c.Name).CombinedOutput() if err != nil { return } c.pid, err = strconv.Atoi(strings.TrimSuffix(string(pid_s), "\n")) return c.pid, err } func (c *Container) initCommands() { c.createCommands = []command.Command{ command.NewShell("podman create --name %s%s%s%s%s %s%s"), } hostname := "" if c.Hostname != "" { hostname = fmt.Sprintf(" --hostname %s", c.Hostname) } net := "" dns := "" if len(c.Networks) > 0 { net = " --net " + c.Networks[0].ToArgs() if len(c.Networks[0].DNS) > 0 { dns = " --dns " + strings.Join(c.Networks[0].DNS, ",") } } args := "" if c.Arguments != "" { args = " " + c.Arguments } entry := "" if c.Command != "" { entry = " " + c.Command } t, _ := c.createCommands[0].GetShell() c.createCommands[0] = command.NewShell(fmt.Sprintf(t, c.Name, hostname, net, dns, args, c.Image, entry)) if len(c.Networks) > 1 { for i := 1; i < len(c.Networks); i++ { n := c.Networks[i] s := fmt.Sprintf("podman network connect %s %s", n.ToArgs(), c.Name) c.createCommands = append(c.createCommands, command.NewShell(s)) } } c.upCommands = []command.Command{ command.NewShell("podman start " + c.Name), command.NewFunc("assure_netns", func() string { netns_b, err := exec.Command("podman", "inspect", "-f", "{{.NetworkSettings.SandboxKey}}", c.Name).CombinedOutput() if err != nil { return fmt.Sprintf("%s is not running\n", c.Name) } netns := strings.TrimRight(string(netns_b), "\n") err = exec.Command("rm", "-f", "/var/run/netns/"+c.Name).Run() if err != nil { return fmt.Sprintln("Error:", err) } err = exec.Command("ln", "-sf", netns, "/var/run/netns/"+c.Name).Run() if err != nil { fmt.Fprintln(os.Stderr, "Warning: could not establish network namespace", err) return fmt.Sprintln("Error:", err) } return "" }), } if len(c.Networks) > 0 && !*c.Networks[0].IPv6 { c.upCommands = append(c.upCommands, command.NewShell("sleep 1")) for _, k := range c.flushIPv6Commands() { c.upCommands = append(c.upCommands, k) } } }