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

176 lines
4.1 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)
}
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)
}
machines, err := h.ListMachinesInNamespace(n)
2021-05-08 13:58:51 +02:00
if strings.HasPrefix(o, "json") {
2021-05-08 17:06:36 +02:00
JsonOutput(machines, 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)
}
2021-08-15 23:10:39 +02:00
d, err := nodesToPtables(*machines)
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) {
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
prompt := &survey.Confirm{
Message: fmt.Sprintf("Do you want to remove the node %s?", m.Name),
}
2021-07-17 11:17:42 +02:00
err = survey.AskOne(prompt, &confirm)
if err != nil {
return
}
2021-07-17 11:09:42 +02:00
if confirm {
err = h.DeleteMachine(m)
if err != nil {
log.Fatalf("Error deleting node: %s", err)
}
fmt.Printf("Node deleted\n")
} else {
fmt.Printf("Node not deleted\n")
2021-07-17 00:23:12 +02:00
}
},
}
2021-08-15 23:10:39 +02:00
func nodesToPtables(m []headscale.Machine) (pterm.TableData, error) {
d := pterm.TableData{{"ID", "Name", "NodeKey", "IP address", "Ephemeral", "Last seen", "Online"}}
for _, m := range m {
var ephemeral bool
if m.AuthKey != nil && m.AuthKey.Ephemeral {
ephemeral = true
}
var lastSeen time.Time
if m.LastSeen != nil {
lastSeen = *m.LastSeen
}
nKey, err := wgkey.ParseHex(m.NodeKey)
if err != nil {
return nil, err
}
nodeKey := tailcfg.NodeKey(nKey)
var online string
if m.LastSeen.After(time.Now().Add(-5 * time.Minute)) { // TODO: Find a better way to reliably show if online
online = pterm.LightGreen("true")
} else {
online = pterm.LightRed("false")
}
d = append(d, []string{strconv.FormatUint(m.ID, 10), m.Name, nodeKey.ShortString(), m.IPAddress, strconv.FormatBool(ephemeral), lastSeen.Format("2006-01-02 15:04:05"), online})
}
return d, nil
}