Various fixes after testing

This commit is contained in:
Joel Elkins 2022-07-19 01:38:25 -05:00
parent e9e4b94e6d
commit df54f05250
2 changed files with 25 additions and 27 deletions

View File

@ -119,7 +119,7 @@ func (c Command) GetShell() (string, error) {
if ok { if ok {
return s, nil return s, nil
} }
return s, fmt.Errorf("Problem extracting shell command: CommandType = %d", c.Type) return s, fmt.Errorf("Type error. Requested = %d, Type = %d", CT_SH, c.Type)
} }
func (c Command) CallRef() (string, error) { func (c Command) CallRef() (string, error) {
@ -140,21 +140,17 @@ func (c Command) Execute(output io.Writer, fake bool) error {
if err != nil { if err != nil {
return err return err
} }
if fake {
fmt.Fprintln(output, c.Type, "sh", "-c", cmd) fmt.Fprintln(output, c.Type, "sh", "-c", cmd)
return nil if !fake {
} else {
out, err := exec.Command("sh", "-c", cmd).CombinedOutput() out, err := exec.Command("sh", "-c", cmd).CombinedOutput()
fmt.Fprintln(output, out) fmt.Fprintln(output, string(out))
return err return err
} }
case CT_REF: case CT_REF:
if fake {
fmt.Fprintln(output, c.Type, c.Command) fmt.Fprintln(output, c.Type, c.Command)
return nil if !fake {
} else {
s, err := c.CallRef() s, err := c.CallRef()
fmt.Println(s) fmt.Fprintln(output, s)
return err return err
} }
case CT_INDIRECT: case CT_INDIRECT:

View File

@ -72,11 +72,11 @@ func (c *Container) RecreateCommands() []command.Command {
return []command.Command{ return []command.Command{
command.NewFunc("stash_run_state", func() string { command.NewFunc("stash_run_state", func() string {
wasRunning = c.isRunning() wasRunning = c.isRunning()
runMsg := "not running" runMsg := "not running. Will not start it after recreating."
if wasRunning { if wasRunning {
runMsg = "running" runMsg = "running. Will restart after recreating."
} }
return "Container " + c.Name + " is " + runMsg return fmt.Sprintf("Container %s is %s.", c.Name, runMsg)
}), }),
command.NewSet(c.DestroyCommands()), command.NewSet(c.DestroyCommands()),
command.NewSet(c.CreateCommands()), command.NewSet(c.CreateCommands()),
@ -116,10 +116,11 @@ func (c *Container) RestartCommands() []command.Command {
} }
func (c *Container) isRunning() bool { func (c *Container) isRunning() bool {
if _, err := c.Pid(); err != nil { pid, err := c.Pid()
if err != nil {
return false return false
} }
return true return pid > 1
} }
func (c *Container) UpdateCommands() []command.Command { func (c *Container) UpdateCommands() []command.Command {
@ -179,11 +180,11 @@ func (c *Container) Init(nets *[]network.Network) {
} }
func (c *Container) Pid() (pid int, err error) { func (c *Container) Pid() (pid int, err error) {
pid_s, err := exec.Command("podman", "inspect", "-f", "{{.State.Pid}}").CombinedOutput() pid_s, err := exec.Command("podman", "inspect", "-f", "{{.State.Pid}}", c.Name).CombinedOutput()
if err != nil { if err != nil {
return return
} }
c.pid, err = strconv.Atoi(string(pid_s)) c.pid, err = strconv.Atoi(strings.TrimSuffix(string(pid_s), "\n"))
return c.pid, err return c.pid, err
} }
@ -217,7 +218,7 @@ func (c *Container) initCommands() {
if len(c.Networks) > 1 { if len(c.Networks) > 1 {
for i := 1; i < len(c.Networks); i++ { for i := 1; i < len(c.Networks); i++ {
n := c.Networks[i] n := c.Networks[i]
s := fmt.Sprintf("podman network connect %s %s", c.Name, n.ToArgs()) s := fmt.Sprintf("podman network connect %s %s", n.ToArgs(), c.Name)
c.createCommands = append(c.createCommands, command.NewShell(s)) c.createCommands = append(c.createCommands, command.NewShell(s))
} }
} }
@ -225,20 +226,21 @@ func (c *Container) initCommands() {
c.upCommands = []command.Command{ c.upCommands = []command.Command{
command.NewShell("podman start " + c.Name), command.NewShell("podman start " + c.Name),
command.NewFunc("assure_netns", func() string { command.NewFunc("assure_netns", func() string {
var err error netns_b, err := exec.Command("podman", "inspect", "-f", "{{.NetworkSettings.SandboxKey}}", c.Name).CombinedOutput()
pid, err := c.Pid()
if err != nil { if err != nil {
return fmt.Sprintf("%s is not running\n", c.Name) return fmt.Sprintf("%s is not running\n", c.Name)
} }
commands := command.NewSet([]command.Command{ netns := strings.TrimRight(string(netns_b), "\n")
command.NewShell(fmt.Sprintf("rm -f /var/run/netns/%s", c.Name)), err = exec.Command("rm", "-f", "/var/run/netns/"+c.Name).Run()
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 { if err != nil {
return fmt.Sprintln("Error:", err) return fmt.Sprintln("Error:", err)
} }
return fmt.Sprintln("Pid =", pid) 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 { if len(c.Networks) > 0 && !*c.Networks[0].IPv6 {