add Token RPC endpoint to grab a machine token for adding it to a cluster

This commit is contained in:
Pavel Sviderski
2024-09-12 09:55:02 +10:00
parent fb236fb922
commit a64caf34a7
7 changed files with 214 additions and 58 deletions
+15 -5
View File
@@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"google.golang.org/protobuf/types/known/emptypb"
"net/netip"
"uncloud/internal/cli/client"
"uncloud/internal/cli/client/connector"
@@ -201,6 +202,7 @@ func (cli *CLI) AddMachine(ctx context.Context, remoteMachine RemoteMachine, clu
_ = c.Close()
}()
// Create a command executor and a machine API client over the SSH connection to the remote machine.
sshClient, err := sshexec.Connect(remoteMachine.User, remoteMachine.Host, remoteMachine.Port, remoteMachine.KeyPath)
if err != nil {
return fmt.Errorf(
@@ -210,12 +212,12 @@ func (cli *CLI) AddMachine(ctx context.Context, remoteMachine RemoteMachine, clu
}
machineExec := sshexec.NewRemote(sshClient)
conn := connector.NewSSHConnectorFromClient(sshClient)
machineClient, err := client.New(ctx, conn)
if err != nil {
return fmt.Errorf("connect to remote machine API: %w", err)
}
// TODO: Check if the machine is already provisioned using machineClient and ask the user to reset it first.
//conn := connector.NewSSHConnectorFromClient(sshClient)
//machineClient, err := client.New(ctx, conn)
//if err != nil {
// return fmt.Errorf("connect to remote machine API: %w", err)
//}
// TODO: Download and install the latest uncloudd binary by running the install shell script from GitHub.
// For now upload the binary using scp manually.
@@ -223,6 +225,14 @@ func (cli *CLI) AddMachine(ctx context.Context, remoteMachine RemoteMachine, clu
return fmt.Errorf("uncloudd binary not found on the remote machine: %w", err)
}
resp, err := machineClient.Token(ctx, &emptypb.Empty{})
if err != nil {
return fmt.Errorf("get remote machine token: %w", err)
}
token := resp.Token
fmt.Println("Token:", token)
//req := &pb.AddMachineRequest{
// Name: machineName,
// Network: &pb.NetworkConfig{
+10 -15
View File
@@ -28,17 +28,7 @@ type SSHConnector struct {
}
func NewSSHConnector(cfg *SSHConnectorConfig) *SSHConnector {
c := &SSHConnector{config: *cfg}
if c.config.User == "" {
c.config.User = "root"
}
if c.config.Port == 0 {
c.config.Port = 22
}
if c.config.APISockPath == "" {
c.config.APISockPath = machine.DefaultAPISockPath
}
return c
return &SSHConnector{config: *cfg}
}
func NewSSHConnectorFromClient(client *ssh.Client) *SSHConnector {
@@ -59,8 +49,12 @@ func (c *SSHConnector) Connect(ctx context.Context) (*grpc.ClientConn, error) {
}
}
apiSockPath := c.config.APISockPath
if apiSockPath == "" {
apiSockPath = machine.DefaultAPISockPath
}
conn, err := grpc.NewClient(
"unix://"+c.config.APISockPath,
"unix://"+apiSockPath,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithContextDialer(
func(ctx context.Context, addr string) (net.Conn, error) {
@@ -68,9 +62,10 @@ func (c *SSHConnector) Connect(ctx context.Context) (*grpc.ClientConn, error) {
conn, dErr := c.client.DialContext(ctx, "unix", addr)
if dErr != nil {
return nil, fmt.Errorf(
"connect to machine API socket %s through SSH tunnel (is the Uncloud daemon running "+
"on the remote machine and does the SSH user have permissions to access the socket?): %w",
addr, dErr,
"connect to machine API socket '%s' through SSH tunnel (is the Uncloud daemon running "+
"on the remote machine and does the SSH user '%s' have permissions to access the socket?):"+
" %w",
addr, c.client.User(), dErr,
)
}
return conn, nil