From fd314bcf657f4871b076f4b02c04d79cbcde0f32 Mon Sep 17 00:00:00 2001 From: Pavel Sviderski Date: Tue, 10 Sep 2024 13:46:17 +1000 Subject: [PATCH] add ssh destination to connection config --- internal/cli/config/cluster.go | 4 ++-- internal/cli/config/machine.go | 39 ++++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/internal/cli/config/cluster.go b/internal/cli/config/cluster.go index 5fc5ab96..28ff5238 100644 --- a/internal/cli/config/cluster.go +++ b/internal/cli/config/cluster.go @@ -6,6 +6,6 @@ type Cluster struct { Name string `toml:"-"` Machines []MachineConnection `toml:"machines"` Secret secret.Secret `toml:"secret"` - // UserKey is the user's WireGuard private key used to connect to cluster machines. - UserKey secret.Secret `toml:"user_key"` + // 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 88c42bc9..af555b26 100644 --- a/internal/cli/config/machine.go +++ b/internal/cli/config/machine.go @@ -1,10 +1,45 @@ package config import ( + "net" + "strconv" + "strings" "uncloud/internal/secret" ) +const ( + DefaultSSHUser = "root" + DefaultSSHPort = 22 +) + type MachineConnection struct { - Host string `toml:"host"` - PublicKey secret.Secret `toml:"public_key"` + SSH SSHDestination `toml:"ssh,omitempty"` + Host string `toml:"host,omitempty"` + PublicKey secret.Secret `toml:"public_key,omitempty"` +} + +type SSHDestination string + +func NewSSHDestination(user, host string, port int) SSHDestination { + dst := host + if port != 0 && port != DefaultSSHPort { + dst = net.JoinHostPort(host, strconv.Itoa(port)) + } + if user == "" { + user = DefaultSSHUser + } + dst = user + "@" + dst + return SSHDestination(dst) +} + +func (d SSHDestination) Parse() (user string, host string, port int, err error) { + if strings.Contains(string(d), "@") { + user, host, _ = strings.Cut(string(d), "@") + } + h, p, sErr := net.SplitHostPort(host) + if sErr == nil { + host = h + port, err = strconv.Atoi(p) + } + return }