mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-28 12:03:33 +00:00
fix run command, support 1 replica and global mode
This commit is contained in:
+52
-24
@@ -4,61 +4,89 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"uncloud/internal/api"
|
||||||
"uncloud/internal/cli"
|
"uncloud/internal/cli"
|
||||||
"uncloud/internal/cli/client"
|
|
||||||
"uncloud/internal/service"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewRunCommand() *cobra.Command {
|
type runOptions struct {
|
||||||
var (
|
command []string
|
||||||
|
image string
|
||||||
|
machine string
|
||||||
|
mode string
|
||||||
|
name string
|
||||||
|
publish []string
|
||||||
|
|
||||||
cluster string
|
cluster string
|
||||||
opts client.ServiceOptions
|
}
|
||||||
)
|
|
||||||
|
func NewRunCommand() *cobra.Command {
|
||||||
|
opts := runOptions{}
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "run IMAGE",
|
Use: "run IMAGE [COMMAND...]",
|
||||||
Short: "Run a service.",
|
Short: "Run a service.",
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.MinimumNArgs(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]
|
|
||||||
return runRun(cmd.Context(), uncli, cluster, &opts)
|
opts.image = args[0]
|
||||||
|
if len(args) > 1 {
|
||||||
|
opts.command = args[1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
return runRun(cmd.Context(), uncli, opts)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.Flags().StringVarP(&opts.Name, "name", "n", "",
|
// TODO: implement placement constraints and translate --machine to a constraint.
|
||||||
"Assign a name to the service. A random name is generated if not specified.")
|
//cmd.Flags().StringVarP(
|
||||||
cmd.Flags().StringVarP(
|
// &opts.machine, "machine", "m", "",
|
||||||
&opts.Machine, "machine", "m", "",
|
// "Name or ID of the machine to run the service on. (default is first available)",
|
||||||
"Name or ID of the machine to run the service on. (default is first available)",
|
//)
|
||||||
)
|
cmd.Flags().StringVar(&opts.mode, "mode", api.ServiceModeReplicated,
|
||||||
cmd.Flags().StringVar(&opts.Mode, "mode", service.ModeReplicated,
|
|
||||||
fmt.Sprintf("Replication mode of the service: either %q (a specified number of containers across "+
|
fmt.Sprintf("Replication mode of the service: either %q (a specified number of containers across "+
|
||||||
"the machines) or %q (one container on every machine).",
|
"the machines) or %q (one container on every machine).",
|
||||||
service.ModeReplicated, service.ModeGlobal))
|
api.ServiceModeReplicated, api.ServiceModeGlobal))
|
||||||
cmd.Flags().StringSliceVarP(&opts.Publish, "publish", "p", nil,
|
cmd.Flags().StringVarP(&opts.name, "name", "n", "",
|
||||||
|
"Assign a name to the service. A random name is generated if not specified.")
|
||||||
|
cmd.Flags().StringSliceVarP(&opts.publish, "publish", "p", nil,
|
||||||
"Publish a service port to make it accessible outside the cluster. Can be specified multiple times. "+
|
"Publish a service port to make it accessible outside the cluster. Can be specified multiple times. "+
|
||||||
"Format: [load_balancer_port:]container_port[/protocol]")
|
"Format: [load_balancer_port:]container_port[/protocol]")
|
||||||
|
|
||||||
cmd.Flags().StringVarP(
|
cmd.Flags().StringVarP(
|
||||||
&cluster, "cluster", "c", "",
|
&opts.cluster, "cluster", "c", "",
|
||||||
"Name of the cluster to run the service in. (default is the current cluster)",
|
"Name of the cluster to run the service in. (default is the current cluster)",
|
||||||
)
|
)
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func runRun(ctx context.Context, uncli *cli.CLI, clusterName string, opts *client.ServiceOptions) error {
|
func runRun(ctx context.Context, uncli *cli.CLI, opts runOptions) error {
|
||||||
c, err := uncli.ConnectCluster(ctx, clusterName)
|
switch opts.mode {
|
||||||
|
case "", api.ServiceModeReplicated, api.ServiceModeGlobal:
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("invalid replication mode: %q", opts.mode)
|
||||||
|
}
|
||||||
|
|
||||||
|
c, err := uncli.ConnectCluster(ctx, opts.cluster)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("connect to cluster: %w", err)
|
return fmt.Errorf("connect to cluster: %w", err)
|
||||||
}
|
}
|
||||||
defer c.Close()
|
defer c.Close()
|
||||||
|
|
||||||
resp, err := c.RunService(ctx, opts)
|
spec := api.ServiceSpec{
|
||||||
|
Container: api.ContainerSpec{
|
||||||
|
Command: opts.command,
|
||||||
|
Image: opts.image,
|
||||||
|
},
|
||||||
|
Mode: opts.mode,
|
||||||
|
Name: opts.name,
|
||||||
|
}
|
||||||
|
resp, err := c.RunService(ctx, spec)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("run service: %w", err)
|
return fmt.Errorf("run service: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Service %q started on machine %q.\n", resp.Name, resp.MachineName)
|
fmt.Printf("Service %q started.\n", resp.Name)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,17 +21,6 @@ import (
|
|||||||
"uncloud/internal/secret"
|
"uncloud/internal/secret"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ServiceOptions contains all the options for creating a service.
|
|
||||||
// TODO: replace with ServiceSpec.
|
|
||||||
type ServiceOptions struct {
|
|
||||||
Image string
|
|
||||||
Name string
|
|
||||||
Machine string
|
|
||||||
// Mode is the replication mode of the service.
|
|
||||||
Mode string
|
|
||||||
Publish []string
|
|
||||||
}
|
|
||||||
|
|
||||||
type RunServiceResponse struct {
|
type RunServiceResponse struct {
|
||||||
ID string
|
ID string
|
||||||
Name string
|
Name string
|
||||||
|
|||||||
Reference in New Issue
Block a user