1
0
out-of-tree/cmd/container.go

162 lines
3.5 KiB
Go
Raw Normal View History

// Copyright 2024 Mikhail Klementev. All rights reserved.
// Use of this source code is governed by a AGPLv3 license
// (or later) that can be found in the LICENSE file.
2024-02-17 22:38:43 +00:00
package cmd
import (
"errors"
"fmt"
"os/exec"
2024-02-21 12:11:57 +00:00
"path/filepath"
"github.com/rs/zerolog/log"
2023-04-06 19:05:21 +00:00
"code.dumpstack.io/tools/out-of-tree/container"
"code.dumpstack.io/tools/out-of-tree/distro"
)
2023-04-06 19:05:21 +00:00
type ContainerCmd struct {
DistroID string `help:"filter by distribution"`
DistroRelease string `help:"filter by distribution release"`
2023-04-06 19:05:21 +00:00
List ContainerListCmd `cmd:"" help:"list containers"`
Update ContainerUpdateCmd `cmd:"" help:"update containers"`
2024-02-21 12:11:57 +00:00
Save ContainerSaveCmd `cmd:"" help:"save containers"`
2023-04-06 19:05:21 +00:00
Cleanup ContainerCleanupCmd `cmd:"" help:"cleanup containers"`
}
func (cmd ContainerCmd) Containers() (diis []container.Image, err error) {
images, err := container.Images()
2023-04-06 19:05:21 +00:00
if err != nil {
return
}
var dt distro.Distro
if cmd.DistroID != "" {
dt.ID, err = distro.NewID(cmd.DistroID)
if err != nil {
return
}
if cmd.DistroRelease != "" {
dt.Release = cmd.DistroRelease
}
} else if cmd.DistroRelease != "" {
err = errors.New("--distro-release has no use on its own")
return
2023-04-06 19:05:21 +00:00
}
for _, img := range images {
if dt.ID != distro.None && dt.ID != img.Distro.ID {
log.Debug().Msgf("skip %s", img.Name)
continue
}
if dt.Release != "" && dt.Release != img.Distro.Release {
log.Debug().Msgf("skip %s", img.Name)
2023-04-06 19:05:21 +00:00
continue
}
log.Debug().Msgf("append %s", img.Name)
diis = append(diis, img)
2023-04-06 19:05:21 +00:00
}
return
}
type ContainerListCmd struct{}
func (cmd ContainerListCmd) Run(containerCmd *ContainerCmd) (err error) {
images, err := containerCmd.Containers()
if err != nil {
return
}
for _, img := range images {
fmt.Printf("%s\n", img.Distro.String())
2023-04-06 19:05:21 +00:00
}
return
}
type ContainerUpdateCmd struct{}
func (cmd ContainerUpdateCmd) Run(g *Globals, containerCmd *ContainerCmd) (err error) {
images, err := containerCmd.Containers()
if err != nil {
return
}
container.UseCache = false
container.UsePrebuilt = false
// TODO move from all commands to main command line handler
container.Commands = g.Config.Docker.Commands
container.Registry = g.Config.Docker.Registry
container.Timeout = g.Config.Docker.Timeout.Duration
for _, img := range images {
_, err = img.Distro.Packages()
if err != nil {
return
}
}
return
}
2024-02-21 12:11:57 +00:00
type ContainerSaveCmd struct {
OutDir string `help:"directory to save containers" default:"./" type:"existingdir"`
}
func (cmd ContainerSaveCmd) Run(containerCmd *ContainerCmd) (err error) {
images, err := containerCmd.Containers()
if err != nil {
return
}
2024-02-21 12:11:57 +00:00
for _, img := range images {
nlog := log.With().Str("name", img.Name).Logger()
output := filepath.Join(cmd.OutDir, img.Name+".tar")
2024-02-21 12:38:29 +00:00
nlog.Info().Msgf("saving to %v", output)
2024-02-21 12:11:57 +00:00
err = container.Save(img.Name, output)
2024-02-21 12:11:57 +00:00
if err != nil {
return
}
compressed := output + ".gz"
nlog.Info().Msgf("compressing to %v", compressed)
var raw []byte
raw, err = exec.Command("gzip", output).CombinedOutput()
if err != nil {
nlog.Error().Err(err).Msg(string(raw))
return
}
nlog.Info().Msg("done")
}
return
}
2023-04-06 19:05:21 +00:00
type ContainerCleanupCmd struct{}
func (cmd ContainerCleanupCmd) Run(containerCmd *ContainerCmd) (err error) {
images, err := containerCmd.Containers()
if err != nil {
return
}
2023-04-06 19:05:21 +00:00
var output []byte
for _, img := range images {
output, err = exec.Command(container.Runtime, "image", "rm", img.Name).
CombinedOutput()
2023-04-06 19:05:21 +00:00
if err != nil {
log.Error().Err(err).Str("output", string(output)).Msg("")
return
}
}
return
}