rename machines to connections in config

This commit is contained in:
Pavel Sviderski
2024-09-10 16:20:00 +10:00
parent ddf7d23351
commit 4e2640bbd2
4 changed files with 31 additions and 17 deletions
+3 -3
View File
@@ -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)
}
+23 -10
View File
@@ -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)
}
+2 -2
View File
@@ -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"`
}
+3 -2
View File
@@ -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 {