Skip to content

Commit 6a0adcd

Browse files
authored
Merge pull request #110 from thin-edge/feat-compose-sm-plugin-logging
feat(container-group): improve logging when removing a compose project
2 parents f23a357 + 6f0dca8 commit 6a0adcd

File tree

3 files changed

+54
-24
lines changed

3 files changed

+54
-24
lines changed

cli/container_group/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (c *InstallCommand) RunE(cmd *cobra.Command, args []string) error {
7070

7171
// Stop project
7272
if downFirst {
73-
if err := cli.ComposeDown(ctx, stderr, projectName); err != nil {
73+
if err := cli.ComposeDown(ctx, stderr, projectName, workingDir); err != nil {
7474
slog.Warn("Compose down failed, but continuing anyway.", "err", err)
7575
}
7676
}

cli/container_group/remove.go

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package container_group
66
import (
77
"context"
88
"log/slog"
9+
"path/filepath"
910

1011
"github.com/spf13/cobra"
1112
"github.com/thin-edge/tedge-container-plugin/pkg/cli"
@@ -15,29 +16,39 @@ import (
1516
type RemoveCommand struct {
1617
*cobra.Command
1718

18-
ModuleVersion string
19+
CommandContext cli.Cli
20+
ModuleVersion string
1921
}
2022

2123
// removeCmd represents the remove command
2224
func NewRemoveCommand(ctx cli.Cli) *cobra.Command {
23-
command := &RemoveCommand{}
25+
command := &RemoveCommand{
26+
CommandContext: ctx,
27+
}
2428
cmd := &cobra.Command{
2529
Use: "remove",
2630
Short: "Remove a container",
2731
Args: cobra.ExactArgs(1),
28-
RunE: func(cmd *cobra.Command, args []string) error {
29-
slog.Info("Executing", "cmd", cmd.CalledAs(), "args", args)
30-
ctx := context.Background()
31-
projectName := args[0]
32-
33-
cli, err := container.NewContainerClient()
34-
if err != nil {
35-
return err
36-
}
37-
38-
return cli.ComposeDown(ctx, cmd.ErrOrStderr(), projectName)
39-
},
32+
RunE: command.RunE,
4033
}
4134
cmd.Flags().StringVar(&command.ModuleVersion, "module-version", "", "Software version to remove")
4235
return cmd
4336
}
37+
38+
func (c *RemoveCommand) RunE(cmd *cobra.Command, args []string) error {
39+
slog.Info("Executing", "cmd", cmd.CalledAs(), "args", args)
40+
ctx := context.Background()
41+
projectName := args[0]
42+
43+
cli, err := container.NewContainerClient()
44+
if err != nil {
45+
return err
46+
}
47+
48+
persistentDir, err := c.CommandContext.PersistentDir(false)
49+
if err != nil {
50+
return err
51+
}
52+
workingDir := filepath.Join(persistentDir, "compose", projectName)
53+
return cli.ComposeDown(ctx, cmd.ErrOrStderr(), projectName, workingDir)
54+
}

pkg/container/container.go

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -723,31 +723,45 @@ func CheckPodmanComposeError(b string) error {
723723
return lastErr
724724
}
725725

726-
func (c *ContainerClient) ComposeDown(ctx context.Context, w io.Writer, projectName string) error {
726+
func (c *ContainerClient) ComposeDown(ctx context.Context, w io.Writer, projectName string, defaultWorkingDir string) error {
727727
// TODO: Read setting from configuration
728728
manualCleanup := false
729729
errs := make([]error, 0)
730730

731-
workingDir := ""
732-
733731
projectFilter := filters.NewArgs(
734732
filters.Arg("label", "com.docker.compose.project="+projectName),
735733
)
734+
slog.Info("Searching for project containers.", "project", projectName)
736735
projectContainers, err := c.Client.ContainerList(ctx, container.ListOptions{
737736
Filters: projectFilter,
738737
})
739738
if err != nil {
740739
errs = append(errs, err)
741740
}
741+
slog.Info("Found project containers.", "count", len(projectContainers))
742+
743+
workingDir := ""
744+
745+
// Prefer using the working_dir on the container rather than
746+
// the default project working dir as the project could
742747
for _, item := range projectContainers {
743748
if v, ok := item.Labels["com.docker.compose.project.working_dir"]; ok {
744-
workingDir = v
745-
break
749+
if utils.PathExists(v) {
750+
slog.Info("Using project working dir found on container label.", "project", projectName, "working_dir", workingDir, "container_id", item.ID, "container_name", item.Names)
751+
workingDir = v
752+
break
753+
}
746754
}
747755
}
748756

757+
// Fallback to the default project dir
758+
if workingDir == "" {
759+
slog.Info("Using default project working dir.", "path", defaultWorkingDir)
760+
workingDir = defaultWorkingDir
761+
}
762+
749763
// Find
750-
if workingDir != "" && utils.PathExists(workingDir) {
764+
if utils.PathExists(workingDir) {
751765
command, args, err := prepareComposeCommand("down", "--remove-orphans", "--volumes")
752766
if err != nil {
753767
return err
@@ -760,15 +774,16 @@ func (c *ContainerClient) ComposeDown(ctx context.Context, w io.Writer, projectN
760774

761775
if err == nil {
762776
slog.Info("Removing project directory.", "dir", workingDir)
763-
if err := os.RemoveAll(workingDir); err != nil {
777+
if removeErr := os.RemoveAll(workingDir); removeErr != nil {
764778
// non critical error
765-
slog.Warn("Failed to remove project directory.", "err", err)
779+
slog.Warn("Failed to remove project directory.", "err", removeErr)
766780
}
767-
return nil
768781
}
769782

770783
errs = append(errs, err)
771784
slog.Warn("compose failed.", "err", err)
785+
} else {
786+
errs = append(errs, fmt.Errorf("compose project working directory does not exist. dir=%s", workingDir))
772787
}
773788

774789
if !manualCleanup {
@@ -780,6 +795,7 @@ func (c *ContainerClient) ComposeDown(ctx context.Context, w io.Writer, projectN
780795

781796
// Stop containers
782797
for _, item := range projectContainers {
798+
slog.Info("Manually stopping container.", "id", item.ID, "names", item.Names)
783799
if err := c.Client.ContainerStop(ctx, item.ID, container.StopOptions{}); err != nil {
784800
slog.Warn("Failed to stop container.", "err", err)
785801
errs = append(errs, err)
@@ -788,6 +804,7 @@ func (c *ContainerClient) ComposeDown(ctx context.Context, w io.Writer, projectN
788804

789805
// Remove containers
790806
for _, item := range projectContainers {
807+
slog.Info("Manually removing container.", "id", item.ID, "names", item.Names)
791808
if err := c.Client.ContainerRemove(ctx, item.ID, container.RemoveOptions{
792809
RemoveVolumes: true,
793810
RemoveLinks: true,
@@ -808,6 +825,7 @@ func (c *ContainerClient) ComposeDown(ctx context.Context, w io.Writer, projectN
808825

809826
// Remove networks
810827
for _, item := range projectNetworks {
828+
slog.Info("Manually removing network.", "name", item.ID)
811829
if err := c.Client.NetworkRemove(ctx, item.ID); err != nil {
812830
slog.Warn("Failed to remove network.", "err", err)
813831
errs = append(errs, err)
@@ -823,6 +841,7 @@ func (c *ContainerClient) ComposeDown(ctx context.Context, w io.Writer, projectN
823841
}
824842

825843
for _, item := range projectVolumes.Volumes {
844+
slog.Info("Manually removing volume.", "name", item.Name)
826845
if err := c.Client.VolumeRemove(ctx, item.Name, true); err != nil {
827846
slog.Warn("Failed to remove volume.", "err", err)
828847
errs = append(errs, err)

0 commit comments

Comments
 (0)