mirror of
https://gitea.elkins.co/Networking/ccl.git
synced 2025-03-09 20:51:39 -05:00
198 lines
5.3 KiB
Go
198 lines
5.3 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) ClearRACommands() []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)))
|
||
|
// TODO: iterate through invoices 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 {
|
||
|
cmds := c.DestroyCommands()
|
||
|
create := c.CreateCommands()
|
||
|
for _, c := range create {
|
||
|
cmds = append(cmds, c)
|
||
|
}
|
||
|
return cmds
|
||
|
}
|
||
|
|
||
|
func (c *Container) StopCommands() []command.Command {
|
||
|
cmds := []command.Command{}
|
||
|
if _, err := c.Pid(); err != nil {
|
||
|
cmds = append(cmds, command.NewShell("podman stop "+c.Name))
|
||
|
}
|
||
|
cmds = append(cmds, command.NewShell("podman rm "+c.Name))
|
||
|
return cmds
|
||
|
}
|
||
|
|
||
|
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 {
|
||
|
_, err := c.Pid()
|
||
|
if err != nil {
|
||
|
if len(c.upCommands) == 0 {
|
||
|
c.initCommands()
|
||
|
}
|
||
|
return c.upCommands
|
||
|
}
|
||
|
return []command.Command{}
|
||
|
}
|
||
|
|
||
|
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"),
|
||
|
}
|
||
|
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, 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(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)
|
||
|
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.ClearRACommands() {
|
||
|
c.upCommands = append(c.upCommands, k)
|
||
|
}
|
||
|
}
|
||
|
}
|