mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
refactor: client StartContainer pass serviceID
This commit is contained in:
@@ -17,6 +17,7 @@ import (
|
|||||||
"uncloud/internal/secret"
|
"uncloud/internal/secret"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// CreateContainer creates a new container for the given service on the specified machine.
|
||||||
func (cli *Client) CreateContainer(
|
func (cli *Client) CreateContainer(
|
||||||
ctx context.Context, serviceID string, spec api.ServiceSpec, machineID string,
|
ctx context.Context, serviceID string, spec api.ServiceSpec, machineID string,
|
||||||
) (container.CreateResponse, error) {
|
) (container.CreateResponse, error) {
|
||||||
@@ -218,20 +219,33 @@ func toPullProgressEvent(jm jsonmessage.JSONMessage) *progress.Event {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *Client) StartContainer(ctx context.Context, id string, machineID string) error {
|
// StartContainer starts the specified container within the service.
|
||||||
|
func (cli *Client) StartContainer(ctx context.Context, serviceID, containerID string) error {
|
||||||
|
svc, err := cli.InspectService(ctx, serviceID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("inspect service: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var ctr api.Container
|
||||||
|
var machineID string
|
||||||
|
for _, c := range svc.Containers {
|
||||||
|
if c.Container.ID == containerID || c.Container.Names[0] == containerID {
|
||||||
|
ctr = c.Container
|
||||||
|
machineID = c.MachineID
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ctr.ID == "" {
|
||||||
|
return ErrNotFound
|
||||||
|
}
|
||||||
|
|
||||||
machine, err := cli.InspectMachine(ctx, machineID)
|
machine, err := cli.InspectMachine(ctx, machineID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("inspect machine '%s': %w", machineID, err)
|
return fmt.Errorf("inspect machine '%s': %w", machineID, err)
|
||||||
}
|
}
|
||||||
ctx = proxyToMachine(ctx, machine.Machine)
|
ctx = proxyToMachine(ctx, machine.Machine)
|
||||||
|
|
||||||
ctr, err := cli.Docker.InspectContainer(ctx, id)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
pw := progress.ContextWriter(ctx)
|
pw := progress.ContextWriter(ctx)
|
||||||
eventID := fmt.Sprintf("Container %s on %s", ctr.Name, machine.Machine.Name)
|
eventID := fmt.Sprintf("Container %s on %s", ctr.Names[0], machine.Machine.Name)
|
||||||
|
|
||||||
pw.Event(progress.StartingEvent(eventID))
|
pw.Event(progress.StartingEvent(eventID))
|
||||||
if err = cli.Docker.StartContainer(ctx, ctr.ID, container.StartOptions{}); err != nil {
|
if err = cli.Docker.StartContainer(ctx, ctr.ID, container.StartOptions{}); err != nil {
|
||||||
|
|||||||
@@ -214,7 +214,7 @@ func (cli *Client) runContainer(
|
|||||||
return resp, fmt.Errorf("create container: %w", err)
|
return resp, fmt.Errorf("create container: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = cli.StartContainer(ctx, resp.ID, machine.Name); err != nil {
|
if err = cli.StartContainer(ctx, serviceID, resp.ID); err != nil {
|
||||||
return resp, fmt.Errorf("start container: %w", err)
|
return resp, fmt.Errorf("start container: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -319,7 +319,7 @@ func (cli *Client) InspectService(ctx context.Context, id string) (api.Service,
|
|||||||
serviceID := containers[0].Container.ServiceID()
|
serviceID := containers[0].Container.ServiceID()
|
||||||
for _, mc := range containers[1:] {
|
for _, mc := range containers[1:] {
|
||||||
if mc.Container.ServiceID() != serviceID {
|
if mc.Container.ServiceID() != serviceID {
|
||||||
return svc, fmt.Errorf("multiple services found with name: %s", id)
|
return svc, fmt.Errorf("multiple services found with name '%s', use the service ID instead", id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,9 +24,9 @@ func TestService(t *testing.T) {
|
|||||||
t.Run("container lifecycle", func(t *testing.T) {
|
t.Run("container lifecycle", func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
name := "busybox-container-lifecycle"
|
svcName := "busybox-container-lifecycle"
|
||||||
spec := api.ServiceSpec{
|
spec := api.ServiceSpec{
|
||||||
Name: name,
|
Name: svcName,
|
||||||
Container: api.ContainerSpec{
|
Container: api.ContainerSpec{
|
||||||
Command: []string{"sleep", "infinity"},
|
Command: []string{"sleep", "infinity"},
|
||||||
Image: "busybox:latest",
|
Image: "busybox:latest",
|
||||||
@@ -34,11 +34,11 @@ func TestService(t *testing.T) {
|
|||||||
}
|
}
|
||||||
machineID := c.Machines[0].Name
|
machineID := c.Machines[0].Name
|
||||||
|
|
||||||
ctr, err := cli.CreateContainer(ctx, name, spec, machineID)
|
ctr, err := cli.CreateContainer(ctx, svcName, spec, machineID)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.NotEmpty(t, ctr.ID)
|
assert.NotEmpty(t, ctr.ID)
|
||||||
|
|
||||||
err = cli.StartContainer(ctx, ctr.ID, machineID)
|
err = cli.StartContainer(ctx, svcName, ctr.ID)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user