Multithread all libpod actions

This commit is contained in:
Joel Elkins 2022-07-29 23:07:19 -05:00
parent 515929fd8f
commit 4f78efe42c
9 changed files with 42 additions and 46 deletions

View File

@ -22,7 +22,9 @@ THE SOFTWARE.
package cmd
import (
"gitea.elkins.co/Networking/ccl/internal/pkg/command"
"gitea.elkins.co/Networking/ccl/internal/pkg/config"
"gitea.elkins.co/Networking/ccl/internal/pkg/container"
"github.com/spf13/cobra"
)
@ -36,11 +38,7 @@ var createCmd = &cobra.Command{
names or categories. Multiple arguments are supported.`,
Run: func(_ *cobra.Command, args []string) {
conts := config.Union(args, contMask)
for _, cont := range conts {
if err := getAndExecute(cont.CreateCommands, "CREATE", cont.Name, output, fake); err != nil {
cont.LogEntry().WithField("error", err).Errorln("Create failed")
}
}
getAndExecute("CREATE", conts, func(c *container.Container) command.Commands { return c.CreateCommands() })
},
}

View File

@ -23,15 +23,27 @@ package cmd
import (
"fmt"
"io"
"sync"
"gitea.elkins.co/Networking/ccl/internal/pkg/command"
"gitea.elkins.co/Networking/ccl/internal/pkg/container"
)
func getAndExecute(getCmds func() command.Commands, action, label string, output io.Writer, fake bool) error {
fmt.Fprintln(output, action, label)
if err := getCmds().Execute(output, fake); err != nil {
return err
func getAndExecute(action string, tgts []container.Container, getCmds func(*container.Container) command.Commands) {
var wg sync.WaitGroup
for i := range tgts {
fmt.Fprintln(output, action, tgts[i].Name)
wg.Add(1)
go func(cont *container.Container) {
defer wg.Done()
for _, cmd := range getCmds(cont) {
if err := cmd.Execute(output, fake); err != nil {
cont.LogEntry().WithField("error", err).Errorln("Could not", action)
return
}
return nil
}
}(&tgts[i])
}
wg.Wait()
}

View File

@ -22,7 +22,9 @@ THE SOFTWARE.
package cmd
import (
"gitea.elkins.co/Networking/ccl/internal/pkg/command"
"gitea.elkins.co/Networking/ccl/internal/pkg/config"
"gitea.elkins.co/Networking/ccl/internal/pkg/container"
"github.com/spf13/cobra"
)
@ -37,11 +39,7 @@ affected: the old image will still remain, though untagged, and any defined cont
will still use it.`,
Run: func(cmd *cobra.Command, args []string) {
conts := config.Union(args, contMask)
for _, cont := range conts {
if err := getAndExecute(cont.PullCommands, "PULL", cont.Name, output, fake); err != nil {
cont.LogEntry().WithField("error", err).Errorln("Pull failed")
}
}
getAndExecute("PULL", conts, func(c *container.Container) command.Commands { return c.PullCommands() })
},
}

View File

@ -22,7 +22,9 @@ THE SOFTWARE.
package cmd
import (
"gitea.elkins.co/Networking/ccl/internal/pkg/command"
"gitea.elkins.co/Networking/ccl/internal/pkg/config"
"gitea.elkins.co/Networking/ccl/internal/pkg/container"
"github.com/spf13/cobra"
)
@ -36,11 +38,7 @@ var recreateCmd = &cobra.Command{
one or more container names or categories. If empty, "all" is assumed.`,
Run: func(_ *cobra.Command, args []string) {
conts := config.Union(args, contMask)
for _, cont := range conts {
if err := getAndExecute(cont.RecreateCommands, "RECREATE", cont.Name, output, fake); err != nil {
cont.LogEntry().WithField("error", err).Errorln("Recreation failed")
}
}
getAndExecute("RECREATE", conts, func(c *container.Container) command.Commands { return c.RecreateCommands() })
},
}

View File

@ -22,7 +22,9 @@ THE SOFTWARE.
package cmd
import (
"gitea.elkins.co/Networking/ccl/internal/pkg/command"
"gitea.elkins.co/Networking/ccl/internal/pkg/config"
"gitea.elkins.co/Networking/ccl/internal/pkg/container"
"github.com/spf13/cobra"
)
@ -36,11 +38,7 @@ var restartCmd = &cobra.Command{
one or more container names or categories. If empty, "all" is assumed.`,
Run: func(_ *cobra.Command, args []string) {
conts := config.Union(args, contMask)
for _, cont := range conts {
if err := getAndExecute(cont.RestartCommands, "RESTART", cont.Name, output, fake); err != nil {
cont.LogEntry().WithField("error", err).Errorln("Restart failed")
}
}
getAndExecute("RESTART", conts, func(c *container.Container) command.Commands { return c.RestartCommands() })
},
}

View File

@ -22,7 +22,9 @@ THE SOFTWARE.
package cmd
import (
"gitea.elkins.co/Networking/ccl/internal/pkg/command"
"gitea.elkins.co/Networking/ccl/internal/pkg/config"
"gitea.elkins.co/Networking/ccl/internal/pkg/container"
"github.com/spf13/cobra"
)
@ -37,11 +39,7 @@ var rmCmd = &cobra.Command{
If running, they will first be stopped.`,
Run: func(cmd *cobra.Command, args []string) {
conts := config.Union(args, contMask)
for _, cont := range conts {
if err := getAndExecute(cont.DestroyCommands, "REMOVE", cont.Name, output, fake); err != nil {
cont.LogEntry().WithField("error", err).Errorln("Remove failed")
}
}
getAndExecute("REMOVE", conts, func(c *container.Container) command.Commands { return c.DestroyCommands() })
},
}

View File

@ -22,7 +22,9 @@ THE SOFTWARE.
package cmd
import (
"gitea.elkins.co/Networking/ccl/internal/pkg/command"
"gitea.elkins.co/Networking/ccl/internal/pkg/config"
"gitea.elkins.co/Networking/ccl/internal/pkg/container"
"github.com/spf13/cobra"
)
@ -36,11 +38,7 @@ var startCmd = &cobra.Command{
one or more container names or categories. If empty, "all" is assumed.`,
Run: func(_ *cobra.Command, args []string) {
conts := config.Union(args, contMask)
for _, cont := range conts {
if err := getAndExecute(cont.StartCommands, "START", cont.Name, output, fake); err != nil {
cont.LogEntry().WithField("error", err).Errorln("Start failed")
}
}
getAndExecute("START", conts, func(c *container.Container) command.Commands { return c.StartCommands() })
},
}

View File

@ -22,7 +22,9 @@ THE SOFTWARE.
package cmd
import (
"gitea.elkins.co/Networking/ccl/internal/pkg/command"
"gitea.elkins.co/Networking/ccl/internal/pkg/config"
"gitea.elkins.co/Networking/ccl/internal/pkg/container"
"github.com/spf13/cobra"
)
@ -36,11 +38,7 @@ var stopCmd = &cobra.Command{
one or more container names or categories. If empty, "all" is assumed.`,
Run: func(_ *cobra.Command, args []string) {
conts := config.Union(args, contMask)
for _, cont := range conts {
if err := getAndExecute(cont.StopCommands, "STOP", cont.Name, output, fake); err != nil {
cont.LogEntry().WithField("error", err).Errorln("Stop failed")
}
}
getAndExecute("STOP", conts, func(c *container.Container) command.Commands { return c.StopCommands() })
},
}

View File

@ -22,7 +22,9 @@ THE SOFTWARE.
package cmd
import (
"gitea.elkins.co/Networking/ccl/internal/pkg/command"
"gitea.elkins.co/Networking/ccl/internal/pkg/config"
"gitea.elkins.co/Networking/ccl/internal/pkg/container"
"github.com/spf13/cobra"
)
@ -36,11 +38,7 @@ var updateCmd = &cobra.Command{
one or more container names or categories. If empty, "all" is assumed.`,
Run: func(_ *cobra.Command, args []string) {
conts := config.Union(args, contMask)
for _, cont := range conts {
if err := getAndExecute(cont.UpdateCommands, "UPDATE", cont.Name, output, fake); err != nil {
cont.LogEntry().WithField("error", err).Errorln("Update failed")
}
}
getAndExecute("UPDATE", conts, func(c *container.Container) command.Commands { return c.UpdateCommands() })
},
}