update run command to run a stub service container on the connected node

This commit is contained in:
Pavel Sviderski
2024-11-01 15:05:07 +10:00
parent 5cb765d286
commit a54666a7ac
4 changed files with 77 additions and 23 deletions
+7 -2
View File
@@ -6,6 +6,7 @@ import (
"fmt"
"google.golang.org/grpc"
"uncloud/internal/machine/api/pb"
"uncloud/internal/machine/docker"
)
// Client is a client for the machine API.
@@ -15,8 +16,12 @@ type Client struct {
pb.MachineClient
pb.ClusterClient
*DockerClient
}
// DockerClient is a type alias for the Docker client to embed it in Client with a more specific name.
type DockerClient = docker.Client
// Connector is an interface for establishing a connection to the machine API.
type Connector interface {
Connect(ctx context.Context) (*grpc.ClientConn, error)
@@ -37,10 +42,10 @@ func New(ctx context.Context, connector Connector) (*Client, error) {
c.MachineClient = pb.NewMachineClient(c.conn)
c.ClusterClient = pb.NewClusterClient(c.conn)
c.DockerClient = docker.NewClient(c.conn)
return c, nil
}
func (c *Client) Close() error {
err := c.conn.Close()
return errors.Join(err, c.connector.Close())
return errors.Join(c.conn.Close(), c.connector.Close())
}
+41
View File
@@ -0,0 +1,41 @@
package cli
import (
"context"
"fmt"
"github.com/docker/docker/api/types/container"
)
// ServiceOptions contains all the options for creating a service.
type ServiceOptions struct {
Image string
Name string
Publish []string
}
func (cli *CLI) RunService(ctx context.Context, clusterName string, opts *ServiceOptions) error {
c, err := cli.ConnectCluster(ctx, clusterName)
if err != nil {
return fmt.Errorf("connect to cluster: %w", err)
}
defer func() {
_ = c.Close()
}()
config := &container.Config{
Image: opts.Image,
}
// TODO: generate a container name from the service name.
// TODO: set service labels on the container.
resp, err := c.CreateContainer(ctx, config, nil, nil, nil, opts.Name)
if err != nil {
return fmt.Errorf("create container: %w", err)
}
if err = c.StartContainer(ctx, resp.ID, container.StartOptions{}); err != nil {
return fmt.Errorf("start container: %w", err)
}
fmt.Printf("Service %q started with container ID %q\n", opts.Name, resp.ID)
return nil
}