feat: progress for removing service(s)

This commit is contained in:
Pavel Sviderski
2025-02-11 21:21:50 +10:00
parent 4da51636fd
commit f209851986
4 changed files with 20 additions and 22 deletions
+9 -4
View File
@@ -3,7 +3,10 @@ package service
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/docker/cli/cli/streams"
"github.com/docker/compose/v2/pkg/progress"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"os"
"uncloud/internal/cli" "uncloud/internal/cli"
) )
@@ -40,10 +43,12 @@ func rm(ctx context.Context, uncli *cli.CLI, opts rmOptions) error {
defer client.Close() defer client.Close()
for _, s := range opts.services { for _, s := range opts.services {
if err = client.RemoveService(ctx, s); err != nil { err = progress.RunWithTitle(ctx, func(ctx context.Context) error {
return fmt.Errorf("remove service %q: %w", s, err) if err = client.RemoveService(ctx, s); err != nil {
} return fmt.Errorf("remove service '%s': %w", s, err)
fmt.Printf("Service %q removed.\n", s) }
return nil
}, streams.NewOut(os.Stdout), "Removing service "+s)
} }
return nil return nil
+4
View File
@@ -18,6 +18,10 @@ type Container struct {
types.Container types.Container
} }
func (c *Container) Name() string {
return c.Names[0][1:] // Remove leading slash.
}
// ServiceID returns the ID of the service this container belongs to. // ServiceID returns the ID of the service this container belongs to.
func (c *Container) ServiceID() string { func (c *Container) ServiceID() string {
return c.Labels[LabelServiceID] return c.Labels[LabelServiceID]
+4 -4
View File
@@ -229,7 +229,7 @@ func (cli *Client) InspectContainer(ctx context.Context, serviceID, containerID
} }
for _, c := range svc.Containers { for _, c := range svc.Containers {
if c.Container.ID == containerID || c.Container.Names[0] == containerID { if c.Container.ID == containerID || c.Container.Name() == containerID {
ctr = c ctr = c
} }
} }
@@ -254,7 +254,7 @@ func (cli *Client) StartContainer(ctx context.Context, serviceID, containerID st
ctx = proxyToMachine(ctx, machine.Machine) ctx = proxyToMachine(ctx, machine.Machine)
pw := progress.ContextWriter(ctx) pw := progress.ContextWriter(ctx)
eventID := fmt.Sprintf("Container %s on %s", ctr.Container.Names[0], machine.Machine.Name) eventID := fmt.Sprintf("Container %s on %s", ctr.Container.Name(), machine.Machine.Name)
pw.Event(progress.StartingEvent(eventID)) pw.Event(progress.StartingEvent(eventID))
if err = cli.Docker.StartContainer(ctx, ctr.Container.ID, container.StartOptions{}); err != nil { if err = cli.Docker.StartContainer(ctx, ctr.Container.ID, container.StartOptions{}); err != nil {
@@ -281,7 +281,7 @@ func (cli *Client) StopContainer(
ctx = proxyToMachine(ctx, machine.Machine) ctx = proxyToMachine(ctx, machine.Machine)
pw := progress.ContextWriter(ctx) pw := progress.ContextWriter(ctx)
eventID := fmt.Sprintf("Container %s on %s", ctr.Container.Names[0], machine.Machine.Name) eventID := fmt.Sprintf("Container %s on %s", ctr.Container.Name(), machine.Machine.Name)
pw.Event(progress.StoppingEvent(eventID)) pw.Event(progress.StoppingEvent(eventID))
if err = cli.Docker.StopContainer(ctx, ctr.Container.ID, opts); err != nil { if err = cli.Docker.StopContainer(ctx, ctr.Container.ID, opts); err != nil {
@@ -308,7 +308,7 @@ func (cli *Client) RemoveContainer(
ctx = proxyToMachine(ctx, machine.Machine) ctx = proxyToMachine(ctx, machine.Machine)
pw := progress.ContextWriter(ctx) pw := progress.ContextWriter(ctx)
eventID := fmt.Sprintf("Container %s on %s", ctr.Container.Names[0], machine.Machine.Name) eventID := fmt.Sprintf("Container %s on %s", ctr.Container.Name(), machine.Machine.Name)
pw.Event(progress.RemovingEvent(eventID)) pw.Event(progress.RemovingEvent(eventID))
if err = cli.Docker.RemoveContainer(ctx, ctr.Container.ID, opts); err != nil { if err = cli.Docker.RemoveContainer(ctx, ctr.Container.ID, opts); err != nil {
+3 -14
View File
@@ -13,12 +13,12 @@ import (
"uncloud/internal/ucind" "uncloud/internal/ucind"
) )
func TestService(t *testing.T) { func TestRunService(t *testing.T) {
t.Parallel() t.Parallel()
clusterName := "ucind-test.service" clusterName := "ucind-test.run-service"
ctx := context.Background() ctx := context.Background()
c, _ := createTestCluster(t, clusterName, ucind.CreateClusterOptions{Machines: 1}, true) c, _ := createTestCluster(t, clusterName, ucind.CreateClusterOptions{Machines: 3}, true)
cli, err := c.Machines[0].Connect(ctx) cli, err := c.Machines[0].Connect(ctx)
require.NoError(t, err) require.NoError(t, err)
@@ -59,17 +59,6 @@ func TestService(t *testing.T) {
err = cli.RemoveContainer(ctx, svcName, ctr.ID, container.RemoveOptions{}) err = cli.RemoveContainer(ctx, svcName, ctr.ID, container.RemoveOptions{})
require.ErrorIs(t, err, client.ErrNotFound) require.ErrorIs(t, err, client.ErrNotFound)
}) })
}
func TestRunService(t *testing.T) {
t.Parallel()
clusterName := "ucind-test.run-service"
ctx := context.Background()
c, _ := createTestCluster(t, clusterName, ucind.CreateClusterOptions{Machines: 3}, true)
cli, err := c.Machines[0].Connect(ctx)
require.NoError(t, err)
t.Run("1 replica", func(t *testing.T) { t.Run("1 replica", func(t *testing.T) {
t.Parallel() t.Parallel()