feat: check automatically if uc can connect via Unix socket when running on cluster machine (#296)

* Check autom if we can connect via Unix socket

When there is no config, but a unix socket does exist, connect via the
unix socket. Prohibit saving the config if this is the case.

Fixes: #148

Use the new CutPrefix to shorten some code.

Signed-off-by: Miek Gieben <miek@miek.nl>

* remove error checking; it can not be hit

Signed-off-by: Miek Gieben <miek@miek.nl>

* Extra error text when logged in locally

Signed-off-by: Miek Gieben <miek@miek.nl>

---------

Signed-off-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
Miek Gieben
2026-04-13 16:37:48 +10:00
committed by GitHub
parent f60a9ff293
commit dd989a281e
3 changed files with 27 additions and 12 deletions
+4 -2
View File
@@ -150,11 +150,13 @@ Connection methods:
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.
// Config is nil when connecting directly to a remote machine (--connect) without using Uncloud config
// or when being logged in on a machine and using the uncloud socket directly.
return fmt.Errorf(
"do not use --connect when initialising a new cluster: --connect is for overriding the connection " +
"to an existing cluster, but 'machine init' creates a new one and writes the new cluster context " +
"to the Uncloud config file (--uncloud-config)")
"to the Uncloud config file (--uncloud-config), when logged in on a cluster machine, 'machine init' " +
"is not supported")
}
netPrefix, err := netip.ParsePrefix(opts.network)
+18 -10
View File
@@ -11,7 +11,7 @@ import (
cmdcontext "github.com/psviderski/uncloud/cmd/uncloud/context"
"github.com/psviderski/uncloud/cmd/uncloud/dns"
"github.com/psviderski/uncloud/cmd/uncloud/image"
"github.com/psviderski/uncloud/cmd/uncloud/machine"
cmdmachine "github.com/psviderski/uncloud/cmd/uncloud/machine"
"github.com/psviderski/uncloud/cmd/uncloud/service"
"github.com/psviderski/uncloud/cmd/uncloud/volume"
"github.com/psviderski/uncloud/cmd/uncloud/wg"
@@ -19,6 +19,7 @@ import (
"github.com/psviderski/uncloud/internal/cli/config"
"github.com/psviderski/uncloud/internal/fs"
"github.com/psviderski/uncloud/internal/log"
"github.com/psviderski/uncloud/internal/machine"
"github.com/psviderski/uncloud/internal/version"
"github.com/spf13/cobra"
)
@@ -46,24 +47,22 @@ func main() {
var conn *config.MachineConnection
if opts.connect != "" {
if strings.HasPrefix(opts.connect, "tcp://") {
addrPort, err := netip.ParseAddrPort(strings.TrimPrefix(opts.connect, "tcp://"))
if after, ok := strings.CutPrefix(opts.connect, "tcp://"); ok {
addrPort, err := netip.ParseAddrPort(after)
if err != nil {
return fmt.Errorf("parse TCP address: %w", err)
}
conn = &config.MachineConnection{
TCP: &addrPort,
}
} else if strings.HasPrefix(opts.connect, "ssh+go://") {
dest := strings.TrimPrefix(opts.connect, "ssh+go://")
} else if after, ok := strings.CutPrefix(opts.connect, "ssh+go://"); ok {
conn = &config.MachineConnection{
SSHGo: config.SSHDestination(dest),
SSHGo: config.SSHDestination(after),
}
} else if strings.HasPrefix(opts.connect, "ssh+cli://") {
} else if after, ok := strings.CutPrefix(opts.connect, "ssh+cli://"); ok {
// Backward-compatible alias for ssh://.
dest := strings.TrimPrefix(opts.connect, "ssh+cli://")
conn = &config.MachineConnection{
SSH: config.SSHDestination(dest),
SSH: config.SSHDestination(after),
}
} else if strings.HasPrefix(opts.connect, "unix://") {
conn = &config.MachineConnection{
@@ -79,6 +78,15 @@ func main() {
}
configPath := fs.ExpandHomeDir(opts.configPath)
if opts.connect == "" {
if !fs.Exists(configPath) && fs.Exists(machine.DefaultUncloudSockPath) {
conn = &config.MachineConnection{
Unix: machine.DefaultUncloudSockPath,
}
}
}
uncli, err := cli.New(configPath, conn, opts.context)
if err != nil {
return fmt.Errorf("initialise CLI: %w", err)
@@ -130,7 +138,7 @@ func main() {
cmdcontext.NewRootCommand(),
dns.NewRootCommand(),
image.NewRootCommand(),
machine.NewRootCommand(),
cmdmachine.NewRootCommand(),
service.NewRootCommand(),
service.NewExecCommand("service"),
service.NewInspectCommand("service"),
+5
View File
@@ -71,3 +71,8 @@ func Chown(path, username, group string) error {
}
return nil
}
func Exists(path string) bool {
_, err := os.Stat(path)
return err == nil
}