mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
chore: delete the obsolete 'machine token' command
This commit is contained in:
@@ -17,7 +17,6 @@ func NewRootCommand() *cobra.Command {
|
|||||||
NewRenameCommand(),
|
NewRenameCommand(),
|
||||||
NewRmCommand(),
|
NewRmCommand(),
|
||||||
NewUpdateCommand(),
|
NewUpdateCommand(),
|
||||||
NewTokenCommand(),
|
|
||||||
)
|
)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,39 +0,0 @@
|
|||||||
package machine
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/psviderski/uncloud/internal/daemon"
|
|
||||||
"github.com/psviderski/uncloud/internal/machine"
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
)
|
|
||||||
|
|
||||||
type tokenOptions struct {
|
|
||||||
dataDir string
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewTokenCommand() *cobra.Command {
|
|
||||||
opts := tokenOptions{}
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "token",
|
|
||||||
Short: "Print the local machine's token for adding it to a cluster.",
|
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
|
||||||
token, err := daemon.MachineToken(opts.dataDir)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("get machine token: %w", err)
|
|
||||||
}
|
|
||||||
tokenStr, err := token.String()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("encode machine token: %w", err)
|
|
||||||
}
|
|
||||||
fmt.Println(tokenStr)
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd.Flags().StringVarP(&opts.dataDir, "data-dir", "d", machine.DefaultDataDir,
|
|
||||||
"Directory for storing persistent machine state.")
|
|
||||||
_ = cmd.MarkFlagDirname("data-dir")
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
package daemon
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"net/netip"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/psviderski/uncloud/internal/machine"
|
|
||||||
"github.com/psviderski/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) {
|
|
||||||
state, err := machine.ParseState(machine.StatePath(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(state.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(ips, publicIP)
|
|
||||||
}
|
|
||||||
|
|
||||||
endpoints := make([]netip.AddrPort, len(ips))
|
|
||||||
for i, ip := range ips {
|
|
||||||
endpoints[i] = netip.AddrPortFrom(ip, network.WireGuardPort)
|
|
||||||
}
|
|
||||||
return machine.NewToken(state.Network.PublicKey, publicIP, endpoints), nil
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user