add uncloud machine token command to print machine token to add machine to cluster

This commit is contained in:
Pavel Sviderski
2024-09-05 16:13:27 +10:00
parent 4656bdd132
commit 945eff471b
9 changed files with 327 additions and 42 deletions
+41
View File
@@ -0,0 +1,41 @@
package daemon
import (
"errors"
"fmt"
"net/netip"
"os"
"uncloud/internal/machine"
"uncloud/internal/machine/network"
)
// MachineToken returns the local machine's token that can be used for adding the machine to a cluster.
// TODO: ideally, this should be an RPC call to the daemon API to ensure the config is created and up-to-date.
func MachineToken(dataDir string) (machine.Token, error) {
cfg, err := machine.ParseConfig(machine.ConfigPath(dataDir))
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return machine.Token{}, fmt.Errorf("load machine config (is uncloudd daemon running?): %w", err)
}
return machine.Token{}, fmt.Errorf("load machine config: %w", err)
}
if len(cfg.Network.PublicKey) == 0 {
return machine.Token{}, errors.New("public key is not set in machine config")
}
ips, err := network.ListRoutableIPs()
if err != nil {
return machine.Token{}, fmt.Errorf("list routable addresses: %w", err)
}
publicIP, err := network.GetPublicIP()
// Ignore the error if failed to get the public IP using API services.
if err == nil {
ips = append([]netip.Addr{publicIP}, ips...)
}
endpoints := make([]netip.AddrPort, len(ips))
for i, ip := range ips {
endpoints[i] = netip.AddrPortFrom(ip, network.WireGuardPort)
}
return machine.NewToken(cfg.Network.PublicKey, endpoints), nil
}