mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
fix: panic when initialising new cluster with --connect
This commit is contained in:
@@ -33,7 +33,7 @@ type initOptions struct {
|
|||||||
func NewInitCommand() *cobra.Command {
|
func NewInitCommand() *cobra.Command {
|
||||||
opts := initOptions{}
|
opts := initOptions{}
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "init [USER@HOST:PORT]",
|
Use: "init [schema://]USER@HOST[:PORT]",
|
||||||
Short: "Initialise a new cluster with a remote machine as the first member.",
|
Short: "Initialise a new cluster with a remote machine as the first member.",
|
||||||
Long: `Initialise a new cluster by setting up a remote machine as the first member.
|
Long: `Initialise a new cluster by setting up a remote machine as the first member.
|
||||||
This command creates a new context in your Uncloud config to manage the cluster.
|
This command creates a new context in your Uncloud config to manage the cluster.
|
||||||
@@ -128,6 +128,11 @@ Connection methods:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func initCluster(ctx context.Context, uncli *cli.CLI, remoteMachine *cli.RemoteMachine, opts initOptions) error {
|
func initCluster(ctx context.Context, uncli *cli.CLI, remoteMachine *cli.RemoteMachine, opts initOptions) error {
|
||||||
|
if uncli.Config == nil {
|
||||||
|
// Config is nil when connecting directly to a remote machine (--connect) without using Uncloud config.
|
||||||
|
return fmt.Errorf("do not specify --connect when initialising a new cluster")
|
||||||
|
}
|
||||||
|
|
||||||
netPrefix, err := netip.ParsePrefix(opts.network)
|
netPrefix, err := netip.ParsePrefix(opts.network)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("parse network CIDR: %w", err)
|
return fmt.Errorf("parse network CIDR: %w", err)
|
||||||
|
|||||||
+1
-1
@@ -40,8 +40,8 @@ func main() {
|
|||||||
SilenceErrors: true,
|
SilenceErrors: true,
|
||||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
cli.BindEnvToFlag(cmd, "connect", "UNCLOUD_CONNECT")
|
cli.BindEnvToFlag(cmd, "connect", "UNCLOUD_CONNECT")
|
||||||
cli.BindEnvToFlag(cmd, "uncloud-config", "UNCLOUD_CONFIG")
|
|
||||||
cli.BindEnvToFlag(cmd, "context", "UNCLOUD_CONTEXT")
|
cli.BindEnvToFlag(cmd, "context", "UNCLOUD_CONTEXT")
|
||||||
|
cli.BindEnvToFlag(cmd, "uncloud-config", "UNCLOUD_CONFIG")
|
||||||
|
|
||||||
var conn *config.MachineConnection
|
var conn *config.MachineConnection
|
||||||
if opts.connect != "" {
|
if opts.connect != "" {
|
||||||
|
|||||||
+21
-5
@@ -171,7 +171,7 @@ func (cli *CLI) InitCluster(ctx context.Context, opts InitClusterOptions) (*clie
|
|||||||
return cli.initRemoteMachine(ctx, opts)
|
return cli.initRemoteMachine(ctx, opts)
|
||||||
}
|
}
|
||||||
// TODO: implement local machine initialisation
|
// TODO: implement local machine initialisation
|
||||||
return nil, fmt.Errorf("local machine initialisation is not implemented yet")
|
return nil, fmt.Errorf("local machine initialisation is not implemented yet. Please specify a remote machine")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *CLI) initRemoteMachine(ctx context.Context, opts InitClusterOptions) (*client.Client, error) {
|
func (cli *CLI) initRemoteMachine(ctx context.Context, opts InitClusterOptions) (*client.Client, error) {
|
||||||
@@ -246,9 +246,17 @@ func (cli *CLI) initRemoteMachine(ctx context.Context, opts InitClusterOptions)
|
|||||||
MachineID: resp.Machine.Id,
|
MachineID: resp.Machine.Id,
|
||||||
}
|
}
|
||||||
if opts.RemoteMachine.UseSSHCLI {
|
if opts.RemoteMachine.UseSSHCLI {
|
||||||
connCfg.SSHCLI = config.NewSSHDestination(opts.RemoteMachine.User, opts.RemoteMachine.Host, opts.RemoteMachine.Port)
|
connCfg.SSHCLI = config.NewSSHDestination(
|
||||||
|
opts.RemoteMachine.User,
|
||||||
|
opts.RemoteMachine.Host,
|
||||||
|
opts.RemoteMachine.Port,
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
connCfg.SSH = config.NewSSHDestination(opts.RemoteMachine.User, opts.RemoteMachine.Host, opts.RemoteMachine.Port)
|
connCfg.SSH = config.NewSSHDestination(
|
||||||
|
opts.RemoteMachine.User,
|
||||||
|
opts.RemoteMachine.Host,
|
||||||
|
opts.RemoteMachine.Port,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
cli.Config.Contexts[contextName].Connections = append(cli.Config.Contexts[contextName].Connections, connCfg)
|
cli.Config.Contexts[contextName].Connections = append(cli.Config.Contexts[contextName].Connections, connCfg)
|
||||||
if err = cli.Config.Save(); err != nil {
|
if err = cli.Config.Save(); err != nil {
|
||||||
@@ -415,9 +423,17 @@ func (cli *CLI) AddMachine(ctx context.Context, opts AddMachineOptions) (*client
|
|||||||
MachineID: addResp.Machine.Id,
|
MachineID: addResp.Machine.Id,
|
||||||
}
|
}
|
||||||
if opts.RemoteMachine.UseSSHCLI {
|
if opts.RemoteMachine.UseSSHCLI {
|
||||||
connCfg.SSHCLI = config.NewSSHDestination(opts.RemoteMachine.User, opts.RemoteMachine.Host, opts.RemoteMachine.Port)
|
connCfg.SSHCLI = config.NewSSHDestination(
|
||||||
|
opts.RemoteMachine.User,
|
||||||
|
opts.RemoteMachine.Host,
|
||||||
|
opts.RemoteMachine.Port,
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
connCfg.SSH = config.NewSSHDestination(opts.RemoteMachine.User, opts.RemoteMachine.Host, opts.RemoteMachine.Port)
|
connCfg.SSH = config.NewSSHDestination(
|
||||||
|
opts.RemoteMachine.User,
|
||||||
|
opts.RemoteMachine.Host,
|
||||||
|
opts.RemoteMachine.Port,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
cli.Config.Contexts[contextName].Connections = append(cli.Config.Contexts[contextName].Connections, connCfg)
|
cli.Config.Contexts[contextName].Connections = append(cli.Config.Contexts[contextName].Connections, connCfg)
|
||||||
if err = cli.Config.Save(); err != nil {
|
if err = cli.Config.Save(); err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user