update init command to send RPC calls over SSH tunnel

This commit is contained in:
Pavel Sviderski
2024-09-10 13:45:49 +10:00
parent 86d949ae48
commit 2514c804dc
5 changed files with 50 additions and 90 deletions
+2 -43
View File
@@ -2,8 +2,6 @@ package machine
import (
"context"
"errors"
"fmt"
"github.com/spf13/cobra"
"uncloud/internal/cli"
)
@@ -19,6 +17,7 @@ type addOptions struct {
func NewAddCommand() *cobra.Command {
opts := addOptions{}
cmd := &cobra.Command{
// TODO: add support for [USER@]HOST[:PORT] syntax
Use: "add HOST",
Short: "Add a new machine to a cluster.",
Args: cobra.ExactArgs(1),
@@ -38,45 +37,5 @@ func NewAddCommand() *cobra.Command {
}
func add(ctx context.Context, uncli *cli.CLI, host string, opts addOptions) error {
var (
cluster *cli.Cluster
err error
)
if opts.cluster == "" {
// If the cluster is not specified, use the current cluster. If there are no clusters, create a default one.
cluster, err = uncli.GetCurrentCluster()
if err != nil {
if errors.Is(err, cli.ErrNotFound) {
// Do not create a default cluster if there are already clusters but the current cluster is not set.
clusters, cErr := uncli.ListClusters()
if cErr != nil {
return fmt.Errorf("list clusters: %w", cErr)
}
if len(clusters) > 0 {
return errors.New("the current cluster is not set in the Uncloud config. " +
"Please specify a cluster with the --cluster flag or set current_cluster in the config")
}
cluster, err = uncli.CreateDefaultCluster()
if err != nil {
return fmt.Errorf("create default cluster: %w", err)
}
fmt.Printf("Created %q cluster\n", cluster.Name)
} else {
return fmt.Errorf("get current cluster: %w", err)
}
}
} else {
cluster, err = uncli.GetCluster(opts.cluster)
if err != nil {
return fmt.Errorf("get cluster %q: %w", opts.cluster, err)
}
}
name, err := cluster.AddMachine(ctx, opts.name, opts.user, host, opts.port, opts.sshKey)
if err != nil {
return fmt.Errorf("add machine to cluster %q: %w", cluster.Name, err)
}
fmt.Printf("Machine %q added to cluster %q\n", name, cluster.Name)
return nil
return uncli.AddMachine(ctx, opts.cluster, opts.name, opts.user, host, opts.port, opts.sshKey)
}