1
0
mirror of https://github.com/juanfont/headscale.git synced 2024-12-20 19:09:07 +01:00
juanfont.headscale/cmd/headscale/cli/nodes.go

319 lines
7.9 KiB
Go
Raw Normal View History

package cli
import (
"fmt"
"log"
2021-07-17 00:23:12 +02:00
"strconv"
"strings"
"time"
2021-07-17 11:09:42 +02:00
survey "github.com/AlecAivazis/survey/v2"
2021-08-15 23:10:39 +02:00
"github.com/juanfont/headscale"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
2021-08-15 23:10:39 +02:00
"tailscale.com/tailcfg"
"tailscale.com/types/wgkey"
)
2021-07-31 23:14:24 +02:00
func init() {
rootCmd.AddCommand(nodeCmd)
2021-07-25 15:04:06 +02:00
nodeCmd.PersistentFlags().StringP("namespace", "n", "", "Namespace")
err := nodeCmd.MarkPersistentFlagRequired("namespace")
if err != nil {
log.Fatalf(err.Error())
}
2021-07-25 15:04:06 +02:00
nodeCmd.AddCommand(listNodesCmd)
nodeCmd.AddCommand(registerNodeCmd)
nodeCmd.AddCommand(deleteNodeCmd)
nodeCmd.AddCommand(shareMachineCmd)
nodeCmd.AddCommand(unshareMachineCmd)
2021-07-25 15:04:06 +02:00
}
var nodeCmd = &cobra.Command{
2021-06-28 20:04:05 +02:00
Use: "nodes",
Short: "Manage the nodes of Headscale",
}
2021-07-25 15:04:06 +02:00
var registerNodeCmd = &cobra.Command{
Use: "register machineID",
Short: "Registers a machine to your network",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
2021-08-15 23:10:39 +02:00
return fmt.Errorf("missing parameters")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
n, err := cmd.Flags().GetString("namespace")
if err != nil {
log.Fatalf("Error getting namespace: %s", err)
}
o, _ := cmd.Flags().GetString("output")
h, err := getHeadscaleApp()
if err != nil {
log.Fatalf("Error initializing: %s", err)
}
m, err := h.RegisterMachine(args[0], n)
if strings.HasPrefix(o, "json") {
2021-05-08 17:06:36 +02:00
JsonOutput(m, err, o)
return
}
if err != nil {
fmt.Printf("Cannot register machine: %s\n", err)
return
}
fmt.Printf("Machine registered\n")
},
}
2021-07-25 15:04:06 +02:00
var listNodesCmd = &cobra.Command{
2021-05-01 20:00:25 +02:00
Use: "list",
Short: "List the nodes in a given namespace",
Run: func(cmd *cobra.Command, args []string) {
n, err := cmd.Flags().GetString("namespace")
if err != nil {
log.Fatalf("Error getting namespace: %s", err)
}
2021-05-08 13:58:51 +02:00
o, _ := cmd.Flags().GetString("output")
2021-05-01 20:00:25 +02:00
h, err := getHeadscaleApp()
if err != nil {
log.Fatalf("Error initializing: %s", err)
}
2021-09-02 17:06:47 +02:00
namespace, err := h.GetNamespace(n)
2021-09-02 17:06:47 +02:00
if err != nil {
log.Fatalf("Error fetching namespace: %s", err)
}
2021-05-01 20:00:25 +02:00
machines, err := h.ListMachinesInNamespace(n)
2021-09-03 10:23:45 +02:00
if err != nil {
log.Fatalf("Error fetching machines: %s", err)
}
sharedMachines, err := h.ListSharedMachinesInNamespace(n)
if err != nil {
log.Fatalf("Error fetching shared machines: %s", err)
}
allMachines := append(*machines, *sharedMachines...)
2021-05-08 13:58:51 +02:00
if strings.HasPrefix(o, "json") {
2021-09-03 10:23:45 +02:00
JsonOutput(allMachines, err, o)
2021-05-08 13:58:51 +02:00
return
}
2021-05-01 20:00:25 +02:00
if err != nil {
log.Fatalf("Error getting nodes: %s", err)
}
d, err := nodesToPtables(*namespace, allMachines)
2021-08-15 23:10:39 +02:00
if err != nil {
log.Fatalf("Error converting to table: %s", err)
2021-05-01 20:00:25 +02:00
}
2021-08-15 23:35:03 +02:00
err = pterm.DefaultTable.WithHasHeader().WithData(d).Render()
if err != nil {
log.Fatal(err)
}
2021-05-01 20:00:25 +02:00
},
}
2021-07-17 00:23:12 +02:00
2021-07-25 15:04:06 +02:00
var deleteNodeCmd = &cobra.Command{
2021-07-17 00:23:12 +02:00
Use: "delete ID",
Short: "Delete a node",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
2021-08-15 23:10:39 +02:00
return fmt.Errorf("missing parameters")
2021-07-17 00:23:12 +02:00
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
output, _ := cmd.Flags().GetString("output")
2021-07-17 00:23:12 +02:00
h, err := getHeadscaleApp()
if err != nil {
log.Fatalf("Error initializing: %s", err)
}
id, err := strconv.Atoi(args[0])
if err != nil {
log.Fatalf("Error converting ID to integer: %s", err)
}
m, err := h.GetMachineByID(uint64(id))
if err != nil {
log.Fatalf("Error getting node: %s", err)
}
2021-07-17 11:09:42 +02:00
confirm := false
force, _ := cmd.Flags().GetBool("force")
if !force {
prompt := &survey.Confirm{
Message: fmt.Sprintf("Do you want to remove the node %s?", m.Name),
}
err = survey.AskOne(prompt, &confirm)
if err != nil {
return
}
}
if confirm || force {
2021-07-17 11:09:42 +02:00
err = h.DeleteMachine(m)
if strings.HasPrefix(output, "json") {
JsonOutput(map[string]string{"Result": "Node deleted"}, err, output)
return
}
2021-07-17 11:09:42 +02:00
if err != nil {
log.Fatalf("Error deleting node: %s", err)
}
fmt.Printf("Node deleted\n")
} else {
if strings.HasPrefix(output, "json") {
JsonOutput(map[string]string{"Result": "Node not deleted"}, err, output)
return
}
2021-07-17 11:09:42 +02:00
fmt.Printf("Node not deleted\n")
2021-07-17 00:23:12 +02:00
}
},
}
2021-08-15 23:10:39 +02:00
var shareMachineCmd = &cobra.Command{
2021-09-03 10:23:45 +02:00
Use: "share ID namespace",
Short: "Shares a node from the current namespace to the specified one",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return fmt.Errorf("missing parameters")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
namespace, err := cmd.Flags().GetString("namespace")
2021-09-03 10:23:45 +02:00
if err != nil {
log.Fatalf("Error getting namespace: %s", err)
}
output, _ := cmd.Flags().GetString("output")
2021-09-03 10:23:45 +02:00
h, err := getHeadscaleApp()
if err != nil {
log.Fatalf("Error initializing: %s", err)
}
_, err = h.GetNamespace(namespace)
2021-09-03 10:23:45 +02:00
if err != nil {
log.Fatalf("Error fetching origin namespace: %s", err)
}
destinationNamespace, err := h.GetNamespace(args[1])
2021-09-03 10:23:45 +02:00
if err != nil {
log.Fatalf("Error fetching destination namespace: %s", err)
}
id, err := strconv.Atoi(args[0])
if err != nil {
log.Fatalf("Error converting ID to integer: %s", err)
}
machine, err := h.GetMachineByID(uint64(id))
2021-09-03 10:23:45 +02:00
if err != nil {
log.Fatalf("Error getting node: %s", err)
}
err = h.AddSharedMachineToNamespace(machine, destinationNamespace)
2021-09-10 00:37:01 +02:00
if strings.HasPrefix(output, "json") {
JsonOutput(map[string]string{"Result": "Node shared"}, err, output)
2021-09-03 10:23:45 +02:00
return
}
if err != nil {
fmt.Printf("Error sharing node: %s\n", err)
return
}
fmt.Println("Node shared!")
},
}
var unshareMachineCmd = &cobra.Command{
Use: "unshare ID",
Short: "Unshares a node from the specified namespace",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("missing parameters")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
namespace, err := cmd.Flags().GetString("namespace")
if err != nil {
log.Fatalf("Error getting namespace: %s", err)
}
output, _ := cmd.Flags().GetString("output")
h, err := getHeadscaleApp()
if err != nil {
log.Fatalf("Error initializing: %s", err)
}
n, err := h.GetNamespace(namespace)
if err != nil {
log.Fatalf("Error fetching namespace: %s", err)
}
id, err := strconv.Atoi(args[0])
if err != nil {
log.Fatalf("Error converting ID to integer: %s", err)
}
machine, err := h.GetMachineByID(uint64(id))
if err != nil {
log.Fatalf("Error getting node: %s", err)
}
err = h.RemoveSharedMachineFromNamespace(machine, n)
if strings.HasPrefix(output, "json") {
JsonOutput(map[string]string{"Result": "Node unshared"}, err, output)
return
}
if err != nil {
fmt.Printf("Error unsharing node: %s\n", err)
return
}
fmt.Println("Node unshared!")
},
}
func nodesToPtables(currentNamespace headscale.Namespace, machines []headscale.Machine) (pterm.TableData, error) {
2021-09-02 17:06:47 +02:00
d := pterm.TableData{{"ID", "Name", "NodeKey", "Namespace", "IP address", "Ephemeral", "Last seen", "Online"}}
2021-08-15 23:10:39 +02:00
for _, machine := range machines {
2021-08-15 23:10:39 +02:00
var ephemeral bool
2021-09-10 00:37:01 +02:00
if machine.AuthKey != nil && machine.AuthKey.Ephemeral {
2021-08-15 23:10:39 +02:00
ephemeral = true
}
var lastSeen time.Time
var lastSeenTime string
2021-09-10 00:37:01 +02:00
if machine.LastSeen != nil {
2021-09-10 00:52:08 +02:00
lastSeen = *machine.LastSeen
lastSeenTime = lastSeen.Format("2006-01-02 15:04:05")
2021-08-15 23:10:39 +02:00
}
2021-09-10 00:37:01 +02:00
nKey, err := wgkey.ParseHex(machine.NodeKey)
2021-08-15 23:10:39 +02:00
if err != nil {
return nil, err
}
nodeKey := tailcfg.NodeKey(nKey)
var online string
if lastSeen.After(time.Now().Add(-5 * time.Minute)) { // TODO: Find a better way to reliably show if online
2021-09-10 00:52:08 +02:00
online = pterm.LightGreen("true")
2021-08-15 23:10:39 +02:00
} else {
online = pterm.LightRed("false")
}
2021-09-02 17:06:47 +02:00
var namespace string
if currentNamespace.ID == machine.NamespaceID {
namespace = pterm.LightMagenta(machine.Namespace.Name)
2021-09-02 17:06:47 +02:00
} else {
namespace = pterm.LightYellow(machine.Namespace.Name)
2021-09-02 17:06:47 +02:00
}
2021-09-10 00:49:50 +02:00
d = append(d, []string{strconv.FormatUint(machine.ID, 10), machine.Name, nodeKey.ShortString(), namespace, machine.IPAddress, strconv.FormatBool(ephemeral), lastSeenTime, online})
2021-08-15 23:10:39 +02:00
}
return d, nil
}