mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
feat: progress for removing service(s)
This commit is contained in:
@@ -3,7 +3,10 @@ package service
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/docker/cli/cli/streams"
|
||||
"github.com/docker/compose/v2/pkg/progress"
|
||||
"github.com/spf13/cobra"
|
||||
"os"
|
||||
"uncloud/internal/cli"
|
||||
)
|
||||
|
||||
@@ -40,10 +43,12 @@ func rm(ctx context.Context, uncli *cli.CLI, opts rmOptions) error {
|
||||
defer client.Close()
|
||||
|
||||
for _, s := range opts.services {
|
||||
if err = client.RemoveService(ctx, s); err != nil {
|
||||
return fmt.Errorf("remove service %q: %w", s, err)
|
||||
}
|
||||
fmt.Printf("Service %q removed.\n", s)
|
||||
err = progress.RunWithTitle(ctx, func(ctx context.Context) error {
|
||||
if err = client.RemoveService(ctx, s); err != nil {
|
||||
return fmt.Errorf("remove service '%s': %w", s, err)
|
||||
}
|
||||
return nil
|
||||
}, streams.NewOut(os.Stdout), "Removing service "+s)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -18,6 +18,10 @@ type Container struct {
|
||||
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.
|
||||
func (c *Container) ServiceID() string {
|
||||
return c.Labels[LabelServiceID]
|
||||
|
||||
@@ -229,7 +229,7 @@ func (cli *Client) InspectContainer(ctx context.Context, serviceID, containerID
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -254,7 +254,7 @@ func (cli *Client) StartContainer(ctx context.Context, serviceID, containerID st
|
||||
ctx = proxyToMachine(ctx, machine.Machine)
|
||||
|
||||
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))
|
||||
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)
|
||||
|
||||
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))
|
||||
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)
|
||||
|
||||
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))
|
||||
if err = cli.Docker.RemoveContainer(ctx, ctr.Container.ID, opts); err != nil {
|
||||
|
||||
@@ -13,12 +13,12 @@ import (
|
||||
"uncloud/internal/ucind"
|
||||
)
|
||||
|
||||
func TestService(t *testing.T) {
|
||||
func TestRunService(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
clusterName := "ucind-test.service"
|
||||
clusterName := "ucind-test.run-service"
|
||||
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)
|
||||
require.NoError(t, err)
|
||||
@@ -59,17 +59,6 @@ func TestService(t *testing.T) {
|
||||
err = cli.RemoveContainer(ctx, svcName, ctr.ID, container.RemoveOptions{})
|
||||
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.Parallel()
|
||||
|
||||
Reference in New Issue
Block a user