From 4e2640bbd25625f83819c48d08dfcc8cc73f81dd Mon Sep 17 00:00:00 2001 From: Pavel Sviderski Date: Tue, 10 Sep 2024 16:20:00 +1000 Subject: [PATCH] rename machines to connections in config --- internal/cli/cli.go | 6 +++--- internal/cli/client/cluster.go | 33 +++++++++++++++++++++++---------- internal/cli/config/cluster.go | 4 ++-- internal/cli/config/machine.go | 5 +++-- 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/internal/cli/cli.go b/internal/cli/cli.go index db87bbbd..91ee97c3 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -77,7 +77,7 @@ func (cli *CLI) GetCluster(name string) (*client.ClusterClient, error) { if err != nil { return nil, fmt.Errorf("create user: %w", err) } - wgConnector := connector.NewWireGuardConnector(user, clusterCfg.Machines) + wgConnector := connector.NewWireGuardConnector(user, clusterCfg.Connections) return client.NewClusterClient(clusterCfg, wgConnector) } @@ -178,7 +178,7 @@ func (cli *CLI) initRemoteMachine( connCfg := config.MachineConnection{ SSH: config.NewSSHDestination(remoteMachine.User, remoteMachine.Host, remoteMachine.Port), } - cli.config.Clusters[clusterName].Machines = append(cli.config.Clusters[clusterName].Machines, connCfg) + cli.config.Clusters[clusterName].Connections = append(cli.config.Clusters[clusterName].Connections, connCfg) if err = cli.config.Save(); err != nil { return fmt.Errorf("save config: %w", err) } @@ -234,7 +234,7 @@ func (cli *CLI) AddMachine( } fmt.Printf("Machine %q added to cluster %q\n", name, cluster.Name()) - cli.config.Clusters[cluster.Name()].Machines = append(cli.config.Clusters[cluster.Name()].Machines, connCfg) + cli.config.Clusters[cluster.Name()].Connections = append(cli.config.Clusters[cluster.Name()].Connections, connCfg) if err = cli.config.Save(); err != nil { return fmt.Errorf("save config: %w", err) } diff --git a/internal/cli/client/cluster.go b/internal/cli/client/cluster.go index a959567f..f91eeaa0 100644 --- a/internal/cli/client/cluster.go +++ b/internal/cli/client/cluster.go @@ -39,7 +39,7 @@ func (c *ClusterClient) Name() string { // HasMachines returns true if the cluster has at least one machine specified in the config. func (c *ClusterClient) HasMachines() bool { - return len(c.config.Machines) > 0 + return len(c.config.Connections) > 0 } func (c *ClusterClient) User() (*User, error) { @@ -108,10 +108,13 @@ func (c *ClusterClient) AddMachine( return "", config.MachineConnection{}, uErr } - _, rErr := exec.Run(ctx, sshexec.QuoteCommand( - sudoPrefix, "uncloud", "machine", "init", - "--name", name, - "--user-pubkey", clusterUser.PublicKey().String())) + _, rErr := exec.Run( + ctx, sshexec.QuoteCommand( + sudoPrefix, "uncloud", "machine", "init", + "--name", name, + "--user-pubkey", clusterUser.PublicKey().String(), + ), + ) if rErr != nil { return "", config.MachineConnection{}, fmt.Errorf("initialise a new cluster on machine: %w", rErr) } @@ -129,7 +132,9 @@ func (c *ClusterClient) AddMachine( _, err = exec.Run(ctx, sshexec.QuoteCommand(sudoPrefix, "mkdir", "-m", "700", "-p", machine.DefaultDataDir)) if err != nil { - return "", config.MachineConnection{}, fmt.Errorf("create data directory %q: %w", machine.DefaultDataDir, err) + return "", config.MachineConnection{}, fmt.Errorf( + "create data directory %q: %w", machine.DefaultDataDir, err, + ) } // Write the machine config to /var/lib/uncloud/machine.json by piping the JSON data to the file. @@ -139,8 +144,12 @@ func (c *ClusterClient) AddMachine( } mcfgPath := sshexec.Quote(machine.StatePath(machine.DefaultDataDir)) createFileCmd := fmt.Sprintf("%s touch %s && %s chmod 600 %s", sudoPrefix, mcfgPath, sudoPrefix, mcfgPath) - _, err = exec.Run(ctx, fmt.Sprintf("%s && echo %s | %s tee %s > /dev/null", - createFileCmd, sshexec.Quote(string(mcfgData)), sudoPrefix, mcfgPath)) + _, err = exec.Run( + ctx, fmt.Sprintf( + "%s && echo %s | %s tee %s > /dev/null", + createFileCmd, sshexec.Quote(string(mcfgData)), sudoPrefix, mcfgPath, + ), + ) if err != nil { return "", config.MachineConnection{}, fmt.Errorf("write machine config to %q: %w", mcfgPath, err) } @@ -162,8 +171,12 @@ func (c *ClusterClient) AddMachine( return "", config.MachineConnection{}, fmt.Errorf("parse machine token: %w", err) } // TODO: replace command runs with sending gRPC request to the machine API via unix socket. - name, err = exec.Run(ctx, fmt.Sprintf("%s cat %s | grep Name | cut -d'\"' -f4", - sudoPrefix, machine.StatePath(machine.DefaultDataDir))) + name, err = exec.Run( + ctx, fmt.Sprintf( + "%s cat %s | grep Name | cut -d'\"' -f4", + sudoPrefix, machine.StatePath(machine.DefaultDataDir), + ), + ) if err != nil { return "", config.MachineConnection{}, fmt.Errorf("get machine name: %w: %s", err, out) } diff --git a/internal/cli/config/cluster.go b/internal/cli/config/cluster.go index 45153366..cdcfe990 100644 --- a/internal/cli/config/cluster.go +++ b/internal/cli/config/cluster.go @@ -3,8 +3,8 @@ package config import "uncloud/internal/secret" type Cluster struct { - Name string `toml:"-"` - Machines []MachineConnection `toml:"machines"` + Name string `toml:"-"` + Connections []MachineConnection `toml:"connections"` // UserPrivateKey is the user's WireGuard private key used to connect to cluster machines. UserPrivateKey secret.Secret `toml:"user_private_key"` } diff --git a/internal/cli/config/machine.go b/internal/cli/config/machine.go index af555b26..d054c0ee 100644 --- a/internal/cli/config/machine.go +++ b/internal/cli/config/machine.go @@ -33,8 +33,9 @@ func NewSSHDestination(user, host string, port int) SSHDestination { } func (d SSHDestination) Parse() (user string, host string, port int, err error) { - if strings.Contains(string(d), "@") { - user, host, _ = strings.Cut(string(d), "@") + host = string(d) + if strings.Contains(host, "@") { + user, host, _ = strings.Cut(host, "@") } h, p, sErr := net.SplitHostPort(host) if sErr == nil {