fix: reset already initialised machine on 'uc machine init' when using ssh+cli connection

This commit is contained in:
Pasha Sviderski
2026-01-28 18:08:57 +10:00
parent ce46a67d6d
commit 476a2d57ca
+10 -20
View File
@@ -19,7 +19,6 @@ import (
// and running `uncloudd dial-stdio` on the remote machine. // and running `uncloudd dial-stdio` on the remote machine.
type SSHCLIConnector struct { type SSHCLIConnector struct {
config SSHConnectorConfig config SSHConnectorConfig
conn net.Conn
// Path to SSH control socket for connection reuse. // Path to SSH control socket for connection reuse.
controlSockPath string controlSockPath string
} }
@@ -123,28 +122,22 @@ func (d *sshCLIDialer) DialContext(ctx context.Context, network, address string)
} }
func (c *SSHCLIConnector) Connect(ctx context.Context) (*grpc.ClientConn, error) { func (c *SSHCLIConnector) Connect(ctx context.Context) (*grpc.ClientConn, error) {
if c.conn == nil { // Create gRPC client with a dialer that spawns a new SSH connection on demand.
args := c.buildSSHArgs() // Each dial attempt runs `ssh ... uncloudd dial-stdio`, reusing the control socket if available.
// Create connection using docker's commandconn.
conn, err := commandconn.New(ctx, "ssh", args...)
if err != nil {
return nil, fmt.Errorf("SSH connection to %s: %w", c.config.Destination(), err)
}
c.conn = conn
}
// Create gRPC client over the connection. Use a custom dialer that returns our existing connection.
grpcConn, err := grpc.NewClient( grpcConn, err := grpc.NewClient(
"passthrough:///", // Dummy target since we're using a custom dialer. "passthrough:///", // Dummy target since we're using a custom dialer.
grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithDefaultServiceConfig(defaultServiceConfig), grpc.WithDefaultServiceConfig(defaultServiceConfig),
grpc.WithContextDialer(func(ctx context.Context, _ string) (net.Conn, error) { grpc.WithContextDialer(func(ctx context.Context, _ string) (net.Conn, error) {
return c.conn, nil args := c.buildSSHArgs()
conn, err := commandconn.New(ctx, "ssh", args...)
if err != nil {
return nil, fmt.Errorf("SSH connection to %s: %w", c.config.Destination(), err)
}
return conn, nil
}), }),
) )
if err != nil { if err != nil {
c.conn.Close()
return nil, fmt.Errorf("create machine API client: %w", err) return nil, fmt.Errorf("create machine API client: %w", err)
} }
@@ -212,10 +205,7 @@ func (c *SSHCLIConnector) Dialer() (proxy.ContextDialer, error) {
} }
func (c *SSHCLIConnector) Close() error { func (c *SSHCLIConnector) Close() error {
if c.conn != nil { // Individual connections are managed by gRPC and closed when the gRPC connection closes.
err := c.conn.Close() // The SSH control socket may persist for connection reuse across CLI invocations.
c.conn = nil
return err
}
return nil return nil
} }