mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-28 20:13:33 +00:00
rename machines to connections in config
This commit is contained in:
+3
-3
@@ -77,7 +77,7 @@ func (cli *CLI) GetCluster(name string) (*client.ClusterClient, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("create user: %w", err)
|
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)
|
return client.NewClusterClient(clusterCfg, wgConnector)
|
||||||
}
|
}
|
||||||
@@ -178,7 +178,7 @@ func (cli *CLI) initRemoteMachine(
|
|||||||
connCfg := config.MachineConnection{
|
connCfg := config.MachineConnection{
|
||||||
SSH: config.NewSSHDestination(remoteMachine.User, remoteMachine.Host, remoteMachine.Port),
|
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 {
|
if err = cli.config.Save(); err != nil {
|
||||||
return fmt.Errorf("save config: %w", err)
|
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())
|
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 {
|
if err = cli.config.Save(); err != nil {
|
||||||
return fmt.Errorf("save config: %w", err)
|
return fmt.Errorf("save config: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ func (c *ClusterClient) Name() string {
|
|||||||
|
|
||||||
// HasMachines returns true if the cluster has at least one machine specified in the config.
|
// HasMachines returns true if the cluster has at least one machine specified in the config.
|
||||||
func (c *ClusterClient) HasMachines() bool {
|
func (c *ClusterClient) HasMachines() bool {
|
||||||
return len(c.config.Machines) > 0
|
return len(c.config.Connections) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ClusterClient) User() (*User, error) {
|
func (c *ClusterClient) User() (*User, error) {
|
||||||
@@ -108,10 +108,13 @@ func (c *ClusterClient) AddMachine(
|
|||||||
return "", config.MachineConnection{}, uErr
|
return "", config.MachineConnection{}, uErr
|
||||||
}
|
}
|
||||||
|
|
||||||
_, rErr := exec.Run(ctx, sshexec.QuoteCommand(
|
_, rErr := exec.Run(
|
||||||
|
ctx, sshexec.QuoteCommand(
|
||||||
sudoPrefix, "uncloud", "machine", "init",
|
sudoPrefix, "uncloud", "machine", "init",
|
||||||
"--name", name,
|
"--name", name,
|
||||||
"--user-pubkey", clusterUser.PublicKey().String()))
|
"--user-pubkey", clusterUser.PublicKey().String(),
|
||||||
|
),
|
||||||
|
)
|
||||||
if rErr != nil {
|
if rErr != nil {
|
||||||
return "", config.MachineConnection{}, fmt.Errorf("initialise a new cluster on machine: %w", rErr)
|
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))
|
_, err = exec.Run(ctx, sshexec.QuoteCommand(sudoPrefix, "mkdir", "-m", "700", "-p", machine.DefaultDataDir))
|
||||||
if err != nil {
|
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.
|
// 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))
|
mcfgPath := sshexec.Quote(machine.StatePath(machine.DefaultDataDir))
|
||||||
createFileCmd := fmt.Sprintf("%s touch %s && %s chmod 600 %s", sudoPrefix, mcfgPath, sudoPrefix, mcfgPath)
|
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",
|
_, err = exec.Run(
|
||||||
createFileCmd, sshexec.Quote(string(mcfgData)), sudoPrefix, mcfgPath))
|
ctx, fmt.Sprintf(
|
||||||
|
"%s && echo %s | %s tee %s > /dev/null",
|
||||||
|
createFileCmd, sshexec.Quote(string(mcfgData)), sudoPrefix, mcfgPath,
|
||||||
|
),
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", config.MachineConnection{}, fmt.Errorf("write machine config to %q: %w", mcfgPath, err)
|
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)
|
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.
|
// 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",
|
name, err = exec.Run(
|
||||||
sudoPrefix, machine.StatePath(machine.DefaultDataDir)))
|
ctx, fmt.Sprintf(
|
||||||
|
"%s cat %s | grep Name | cut -d'\"' -f4",
|
||||||
|
sudoPrefix, machine.StatePath(machine.DefaultDataDir),
|
||||||
|
),
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", config.MachineConnection{}, fmt.Errorf("get machine name: %w: %s", err, out)
|
return "", config.MachineConnection{}, fmt.Errorf("get machine name: %w: %s", err, out)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import "uncloud/internal/secret"
|
|||||||
|
|
||||||
type Cluster struct {
|
type Cluster struct {
|
||||||
Name string `toml:"-"`
|
Name string `toml:"-"`
|
||||||
Machines []MachineConnection `toml:"machines"`
|
Connections []MachineConnection `toml:"connections"`
|
||||||
// UserPrivateKey is the user's WireGuard private key used to connect to cluster machines.
|
// UserPrivateKey is the user's WireGuard private key used to connect to cluster machines.
|
||||||
UserPrivateKey secret.Secret `toml:"user_private_key"`
|
UserPrivateKey secret.Secret `toml:"user_private_key"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,8 +33,9 @@ func NewSSHDestination(user, host string, port int) SSHDestination {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d SSHDestination) Parse() (user string, host string, port int, err error) {
|
func (d SSHDestination) Parse() (user string, host string, port int, err error) {
|
||||||
if strings.Contains(string(d), "@") {
|
host = string(d)
|
||||||
user, host, _ = strings.Cut(string(d), "@")
|
if strings.Contains(host, "@") {
|
||||||
|
user, host, _ = strings.Cut(host, "@")
|
||||||
}
|
}
|
||||||
h, p, sErr := net.SplitHostPort(host)
|
h, p, sErr := net.SplitHostPort(host)
|
||||||
if sErr == nil {
|
if sErr == nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user