mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 19:13:34 +00:00
move RunService from CLI to the client, print output in the command
This commit is contained in:
@@ -37,9 +37,7 @@ func runList(ctx context.Context, uncli *cli.CLI, clusterName string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("connect to cluster: %w", err)
|
return fmt.Errorf("connect to cluster: %w", err)
|
||||||
}
|
}
|
||||||
defer func() {
|
defer c.Close()
|
||||||
_ = c.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
listResp, err := c.ListMachines(ctx, &emptypb.Empty{})
|
listResp, err := c.ListMachines(ctx, &emptypb.Empty{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -1,14 +1,17 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"uncloud/internal/cli"
|
"uncloud/internal/cli"
|
||||||
|
"uncloud/internal/cli/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewRunCommand() *cobra.Command {
|
func NewRunCommand() *cobra.Command {
|
||||||
var (
|
var (
|
||||||
cluster string
|
cluster string
|
||||||
opts cli.ServiceOptions
|
opts client.ServiceOptions
|
||||||
)
|
)
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
@@ -17,9 +20,8 @@ func NewRunCommand() *cobra.Command {
|
|||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
uncli := cmd.Context().Value("cli").(*cli.CLI)
|
uncli := cmd.Context().Value("cli").(*cli.CLI)
|
||||||
|
|
||||||
opts.Image = args[0]
|
opts.Image = args[0]
|
||||||
return uncli.RunService(cmd.Context(), cluster, &opts)
|
return runRun(cmd.Context(), uncli, cluster, &opts)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,3 +41,19 @@ func NewRunCommand() *cobra.Command {
|
|||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func runRun(ctx context.Context, uncli *cli.CLI, clusterName string, opts *client.ServiceOptions) error {
|
||||||
|
c, err := uncli.ConnectCluster(ctx, clusterName)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("connect to cluster: %w", err)
|
||||||
|
}
|
||||||
|
defer c.Close()
|
||||||
|
|
||||||
|
resp, err := c.RunService(ctx, opts)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("run service: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Service %q started on machine %q.\n", resp.Name, resp.MachineName)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package cli
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -16,26 +16,28 @@ type ServiceOptions struct {
|
|||||||
Publish []string
|
Publish []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *CLI) RunService(ctx context.Context, clusterName string, opts *ServiceOptions) error {
|
type RunServiceResponse struct {
|
||||||
c, err := cli.ConnectCluster(ctx, clusterName)
|
ID string
|
||||||
if err != nil {
|
Name string
|
||||||
return fmt.Errorf("connect to cluster: %w", err)
|
MachineName string
|
||||||
}
|
}
|
||||||
defer func() {
|
|
||||||
_ = c.Close()
|
func (c *Client) RunService(ctx context.Context, opts *ServiceOptions) (RunServiceResponse, error) {
|
||||||
}()
|
var resp RunServiceResponse
|
||||||
|
|
||||||
listResp, err := c.ListMachines(ctx, &emptypb.Empty{})
|
listResp, err := c.ListMachines(ctx, &emptypb.Empty{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("list machines: %w", err)
|
return resp, fmt.Errorf("list machines: %w", err)
|
||||||
}
|
}
|
||||||
// TODO: update ListMachine endpoint to return machine status based on the Corrosion member list.
|
|
||||||
|
|
||||||
|
// TODO: find the first available machine (state UP).
|
||||||
machineIP, _ := listResp.Machines[0].Machine.Network.ManagementIp.ToAddr()
|
machineIP, _ := listResp.Machines[0].Machine.Network.ManagementIp.ToAddr()
|
||||||
|
resp.MachineName = listResp.Machines[0].Machine.Name
|
||||||
if opts.Machine != "" {
|
if opts.Machine != "" {
|
||||||
for _, m := range listResp.Machines {
|
for _, m := range listResp.Machines {
|
||||||
if m.Machine.Name == opts.Machine || m.Machine.Id == opts.Machine {
|
if m.Machine.Name == opts.Machine || m.Machine.Id == opts.Machine {
|
||||||
machineIP, _ = m.Machine.Network.ManagementIp.ToAddr()
|
machineIP, _ = m.Machine.Network.ManagementIp.ToAddr()
|
||||||
|
resp.MachineName = m.Machine.Name
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -44,20 +46,23 @@ func (cli *CLI) RunService(ctx context.Context, clusterName string, opts *Servic
|
|||||||
md := metadata.Pairs("machines", machineIP.String())
|
md := metadata.Pairs("machines", machineIP.String())
|
||||||
ctx = metadata.NewOutgoingContext(ctx, md)
|
ctx = metadata.NewOutgoingContext(ctx, md)
|
||||||
|
|
||||||
|
// TODO: generate a random service ID.
|
||||||
|
resp.ID = "todo-service-id"
|
||||||
|
// TODO: generate a random service name if not specified.
|
||||||
|
resp.Name = opts.Name
|
||||||
|
// TODO: generate a container name from the service name.
|
||||||
|
// TODO: set service labels on the container.
|
||||||
|
|
||||||
config := &container.Config{
|
config := &container.Config{
|
||||||
Image: opts.Image,
|
Image: opts.Image,
|
||||||
}
|
}
|
||||||
// TODO: generate a container name from the service name.
|
createResp, err := c.CreateContainer(ctx, config, nil, nil, nil, opts.Name)
|
||||||
// TODO: set service labels on the container.
|
|
||||||
resp, err := c.CreateContainer(ctx, config, nil, nil, nil, opts.Name)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("create container: %w", err)
|
return resp, fmt.Errorf("create container: %w", err)
|
||||||
|
}
|
||||||
|
if err = c.StartContainer(ctx, createResp.ID, container.StartOptions{}); err != nil {
|
||||||
|
return resp, fmt.Errorf("start container: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = c.StartContainer(ctx, resp.ID, container.StartOptions{}); err != nil {
|
return resp, nil
|
||||||
return fmt.Errorf("start container: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("Service %q started with container ID %q\n", opts.Name, resp.ID)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user