ccl/internal/pkg/container/container.go

251 lines
6.8 KiB
Go

/*
Copyright © 2022 Joel D. Elkins <joel@elkins.co>
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{}
for _, n := range c.Networks {
if n.IPv6 != nil && !*n.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)))
cmds = append(cmds, command.NewShell(fmt.Sprintf("ip netns exec %s ip -6 address flush scope global", c.Name)))
cmds = append(cmds, command.NewShell(fmt.Sprintf("ip netns exec %s ip -6 route flush proto ra", c.Name)))
// TODO: test, and, if necessary, iterate through interfaces and set the accpet_ra parameter to zero for each
return cmds
}
}
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"
if wasRunning {
runMsg = "running"
}
return "Container " + c.Name + " is " + 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 {
if _, err := c.Pid(); err != nil {
return false
}
return true
}
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}}").CombinedOutput()
if err != nil {
return
}
c.pid, err = strconv.Atoi(string(pid_s))
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", c.Name, n.ToArgs())
c.createCommands = append(c.createCommands, command.NewShell(s))
}
}
c.upCommands = []command.Command{
command.NewShell("podman start " + c.Name),
command.NewFunc("assure_netns", func() string {
var err error
pid, err := c.Pid()
if err != nil {
return fmt.Sprintf("%s is not running\n", c.Name)
}
commands := command.NewSet([]command.Command{
command.NewShell(fmt.Sprintf("rm -f /var/run/netns/%s", c.Name)),
command.NewShell(fmt.Sprintf("ln -s /proc/%d/ns/net /var/run/netns/%s", pid, c.Name)),
})
err = commands.Execute(os.Stderr, false)
if err != nil {
return fmt.Sprintln("Error:", err)
}
return fmt.Sprintln("Pid =", pid)
}),
}
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)
}
}
}