2023-03-22 17:45:56 +00:00
|
|
|
// Copyright 2023 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
|
2023-03-22 17:45:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
2024-02-21 12:11:57 +00:00
|
|
|
"path/filepath"
|
2023-04-06 19:05:21 +00:00
|
|
|
"strings"
|
2023-03-22 17:45:56 +00:00
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
2023-04-06 19:05:21 +00:00
|
|
|
|
2023-05-13 10:14:45 +00:00
|
|
|
"code.dumpstack.io/tools/out-of-tree/container"
|
2023-03-22 17:45:56 +00:00
|
|
|
)
|
|
|
|
|
2023-04-06 19:05:21 +00:00
|
|
|
type ContainerCmd struct {
|
|
|
|
Filter string `help:"filter by name"`
|
|
|
|
|
|
|
|
List ContainerListCmd `cmd:"" help:"list 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() (names []string) {
|
2023-05-13 10:14:45 +00:00
|
|
|
images, err := container.Images()
|
2023-04-06 19:05:21 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, img := range images {
|
|
|
|
if cmd.Filter != "" && !strings.Contains(img.Name, cmd.Filter) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
names = append(names, img.Name)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type ContainerListCmd struct{}
|
|
|
|
|
|
|
|
func (cmd ContainerListCmd) Run(containerCmd *ContainerCmd) (err error) {
|
|
|
|
for _, name := range containerCmd.Containers() {
|
|
|
|
fmt.Println(name)
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
for _, name := range containerCmd.Containers() {
|
|
|
|
nlog := log.With().Str("name", name).Logger()
|
|
|
|
|
|
|
|
output := filepath.Join(cmd.OutDir, 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(name, output)
|
|
|
|
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) {
|
|
|
|
var output []byte
|
|
|
|
for _, name := range containerCmd.Containers() {
|
2023-05-13 10:14:45 +00:00
|
|
|
output, err = exec.Command(container.Runtime, "image", "rm", name).
|
2023-04-07 18:57:18 +00:00
|
|
|
CombinedOutput()
|
2023-04-06 19:05:21 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Str("output", string(output)).Msg("")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|