2021-04-28 16:15:45 +02:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2021-07-17 00:23:12 +02:00
|
|
|
"strconv"
|
Fix nil dereference in nodes list command.
Fixes a nil pointer dereference observed when listing nodes that have
not yet connected.
```
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xb931a4]
goroutine 1 [running]:
github.com/juanfont/headscale/cmd/headscale/cli.glob..func8(0x13c93e0, 0xc0004c4220, 0x0, 0x2)
/go/src/headscale/cmd/headscale/cli/nodes.go:74 +0x364
github.com/spf13/cobra.(*Command).execute(0x13c93e0, 0xc0004c41e0, 0x2, 0x2, 0x13c93e0, 0xc0004c41e0)
/go/pkg/mod/github.com/spf13/cobra@v1.1.3/command.go:856 +0x2c2
github.com/spf13/cobra.(*Command).ExecuteC(0x13ca2e0, 0xc000497110, 0xe76416, 0x6)
/go/pkg/mod/github.com/spf13/cobra@v1.1.3/command.go:960 +0x375
github.com/spf13/cobra.(*Command).Execute(...)
/go/pkg/mod/github.com/spf13/cobra@v1.1.3/command.go:897
main.main()
/go/src/headscale/cmd/headscale/headscale.go:89 +0x805
command terminated with exit code 2
```
2021-06-20 01:18:13 +02:00
|
|
|
"time"
|
2021-04-28 16:15:45 +02:00
|
|
|
|
2021-07-17 11:09:42 +02:00
|
|
|
survey "github.com/AlecAivazis/survey/v2"
|
2021-11-14 18:31:51 +01:00
|
|
|
"github.com/juanfont/headscale"
|
2021-11-04 23:44:35 +01:00
|
|
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
2021-08-15 23:10:39 +02:00
|
|
|
"github.com/pterm/pterm"
|
2021-04-28 16:15:45 +02:00
|
|
|
"github.com/spf13/cobra"
|
2021-11-07 11:15:32 +01:00
|
|
|
"google.golang.org/grpc/status"
|
2021-11-27 00:30:42 +01:00
|
|
|
"tailscale.com/types/key"
|
2021-04-28 16:15:45 +02:00
|
|
|
)
|
|
|
|
|
2021-07-31 23:14:24 +02:00
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(nodeCmd)
|
2021-10-26 14:50:25 +02:00
|
|
|
listNodesCmd.Flags().StringP("namespace", "n", "", "Filter by namespace")
|
2021-10-24 23:02:57 +02:00
|
|
|
nodeCmd.AddCommand(listNodesCmd)
|
2021-10-27 23:51:42 +02:00
|
|
|
|
|
|
|
registerNodeCmd.Flags().StringP("namespace", "n", "", "Namespace")
|
2021-10-24 23:02:57 +02:00
|
|
|
err := registerNodeCmd.MarkFlagRequired("namespace")
|
2021-07-25 16:26:15 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
2021-10-28 15:30:41 +02:00
|
|
|
registerNodeCmd.Flags().StringP("key", "k", "", "Key")
|
|
|
|
err = registerNodeCmd.MarkFlagRequired("key")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
2021-07-25 15:04:06 +02:00
|
|
|
nodeCmd.AddCommand(registerNodeCmd)
|
2021-10-27 23:51:42 +02:00
|
|
|
|
2021-11-22 18:22:22 +01:00
|
|
|
expireNodeCmd.Flags().Uint64P("identifier", "i", 0, "Node identifier (ID)")
|
2021-11-21 14:40:44 +01:00
|
|
|
err = expireNodeCmd.MarkFlagRequired("identifier")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
nodeCmd.AddCommand(expireNodeCmd)
|
|
|
|
|
2021-11-22 18:22:22 +01:00
|
|
|
deleteNodeCmd.Flags().Uint64P("identifier", "i", 0, "Node identifier (ID)")
|
2021-10-28 15:30:41 +02:00
|
|
|
err = deleteNodeCmd.MarkFlagRequired("identifier")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
2021-07-25 15:04:06 +02:00
|
|
|
nodeCmd.AddCommand(deleteNodeCmd)
|
2021-10-27 23:51:42 +02:00
|
|
|
|
|
|
|
shareMachineCmd.Flags().StringP("namespace", "n", "", "Namespace")
|
|
|
|
err = shareMachineCmd.MarkFlagRequired("namespace")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
2021-11-22 18:22:22 +01:00
|
|
|
shareMachineCmd.Flags().Uint64P("identifier", "i", 0, "Node identifier (ID)")
|
2021-10-28 15:30:41 +02:00
|
|
|
err = shareMachineCmd.MarkFlagRequired("identifier")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
2021-09-10 00:26:46 +02:00
|
|
|
nodeCmd.AddCommand(shareMachineCmd)
|
2021-10-27 23:51:42 +02:00
|
|
|
|
|
|
|
unshareMachineCmd.Flags().StringP("namespace", "n", "", "Namespace")
|
|
|
|
err = unshareMachineCmd.MarkFlagRequired("namespace")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
2021-11-22 18:22:22 +01:00
|
|
|
unshareMachineCmd.Flags().Uint64P("identifier", "i", 0, "Node identifier (ID)")
|
2021-10-28 15:30:41 +02:00
|
|
|
err = unshareMachineCmd.MarkFlagRequired("identifier")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
2021-10-17 22:29:30 +02:00
|
|
|
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{
|
2021-10-28 15:30:41 +02:00
|
|
|
Use: "register",
|
2021-04-28 16:15:45 +02:00
|
|
|
Short: "Registers a machine to your network",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-11-04 23:44:35 +01:00
|
|
|
output, _ := cmd.Flags().GetString("output")
|
|
|
|
namespace, err := cmd.Flags().GetString("namespace")
|
2021-04-30 00:23:26 +02:00
|
|
|
if err != nil {
|
2021-11-04 23:44:35 +01:00
|
|
|
ErrorOutput(err, fmt.Sprintf("Error getting namespace: %s", err), output)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-11-04 23:44:35 +01:00
|
|
|
return
|
2021-04-30 00:23:26 +02:00
|
|
|
}
|
|
|
|
|
2021-11-07 10:41:14 +01:00
|
|
|
ctx, client, conn, cancel := getHeadscaleCLIClient()
|
2021-11-04 23:44:35 +01:00
|
|
|
defer cancel()
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
machineKey, err := cmd.Flags().GetString("key")
|
2021-10-28 15:30:41 +02:00
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf("Error getting machine key from flag: %s", err),
|
|
|
|
output,
|
|
|
|
)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-05-08 13:28:22 +02:00
|
|
|
return
|
|
|
|
}
|
2021-11-04 23:44:35 +01:00
|
|
|
|
|
|
|
request := &v1.RegisterMachineRequest{
|
|
|
|
Key: machineKey,
|
|
|
|
Namespace: namespace,
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := client.RegisterMachine(ctx, request)
|
2021-04-28 16:15:45 +02:00
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf(
|
|
|
|
"Cannot register machine: %s\n",
|
|
|
|
status.Convert(err).Message(),
|
|
|
|
),
|
|
|
|
output,
|
|
|
|
)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-04-28 16:15:45 +02:00
|
|
|
return
|
|
|
|
}
|
2021-11-04 23:44:35 +01:00
|
|
|
|
|
|
|
SuccessOutput(response.Machine, "Machine register", output)
|
2021-04-28 16:15:45 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-07-25 15:04:06 +02:00
|
|
|
var listNodesCmd = &cobra.Command{
|
2021-05-01 20:00:25 +02:00
|
|
|
Use: "list",
|
2021-10-24 23:02:57 +02:00
|
|
|
Short: "List nodes",
|
2021-05-01 20:00:25 +02:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-11-04 23:44:35 +01:00
|
|
|
output, _ := cmd.Flags().GetString("output")
|
|
|
|
namespace, err := cmd.Flags().GetString("namespace")
|
2021-05-01 20:00:25 +02:00
|
|
|
if err != nil {
|
2021-11-04 23:44:35 +01:00
|
|
|
ErrorOutput(err, fmt.Sprintf("Error getting namespace: %s", err), output)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-11-04 23:44:35 +01:00
|
|
|
return
|
2021-05-01 20:00:25 +02:00
|
|
|
}
|
|
|
|
|
2021-11-07 10:41:14 +01:00
|
|
|
ctx, client, conn, cancel := getHeadscaleCLIClient()
|
2021-11-04 23:44:35 +01:00
|
|
|
defer cancel()
|
|
|
|
defer conn.Close()
|
2021-09-02 17:06:47 +02:00
|
|
|
|
2021-11-04 23:44:35 +01:00
|
|
|
request := &v1.ListMachinesRequest{
|
|
|
|
Namespace: namespace,
|
2021-09-03 10:23:45 +02:00
|
|
|
}
|
|
|
|
|
2021-11-04 23:44:35 +01:00
|
|
|
response, err := client.ListMachines(ctx, request)
|
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf("Cannot get nodes: %s", status.Convert(err).Message()),
|
|
|
|
output,
|
|
|
|
)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-05-08 13:58:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-04 23:44:35 +01:00
|
|
|
if output != "" {
|
|
|
|
SuccessOutput(response.Machines, "", output)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-11-04 23:44:35 +01:00
|
|
|
return
|
2021-05-01 20:00:25 +02:00
|
|
|
}
|
|
|
|
|
2021-11-14 20:32:03 +01:00
|
|
|
tableData, err := nodesToPtables(namespace, response.Machines)
|
2021-08-15 23:10:39 +02:00
|
|
|
if err != nil {
|
2021-11-04 23:44:35 +01:00
|
|
|
ErrorOutput(err, fmt.Sprintf("Error converting to table: %s", err), output)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-11-04 23:44:35 +01:00
|
|
|
return
|
2021-05-01 20:00:25 +02:00
|
|
|
}
|
|
|
|
|
2021-11-14 20:32:03 +01:00
|
|
|
err = pterm.DefaultTable.WithHasHeader().WithData(tableData).Render()
|
2021-08-15 23:35:03 +02:00
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf("Failed to render pterm table: %s", err),
|
|
|
|
output,
|
|
|
|
)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-11-04 23:44:35 +01:00
|
|
|
return
|
2021-08-15 23:35:03 +02:00
|
|
|
}
|
2021-05-01 20:00:25 +02:00
|
|
|
},
|
|
|
|
}
|
2021-07-17 00:23:12 +02:00
|
|
|
|
2021-11-21 14:40:44 +01:00
|
|
|
var expireNodeCmd = &cobra.Command{
|
|
|
|
Use: "expire",
|
|
|
|
Short: "Expire (log out) a machine in your network",
|
2021-11-21 22:35:36 +01:00
|
|
|
Long: "Expiring a node will keep the node in the database and force it to reauthenticate.",
|
2021-11-21 14:40:44 +01:00
|
|
|
Aliases: []string{"logout"},
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
output, _ := cmd.Flags().GetString("output")
|
|
|
|
|
2021-11-21 22:34:03 +01:00
|
|
|
identifier, err := cmd.Flags().GetUint64("identifier")
|
2021-11-21 14:40:44 +01:00
|
|
|
if err != nil {
|
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf("Error converting ID to integer: %s", err),
|
|
|
|
output,
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, client, conn, cancel := getHeadscaleCLIClient()
|
|
|
|
defer cancel()
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
request := &v1.ExpireMachineRequest{
|
2021-11-21 22:34:03 +01:00
|
|
|
MachineId: identifier,
|
2021-11-21 14:40:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
response, err := client.ExpireMachine(ctx, request)
|
|
|
|
if err != nil {
|
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf(
|
|
|
|
"Cannot expire machine: %s\n",
|
|
|
|
status.Convert(err).Message(),
|
|
|
|
),
|
|
|
|
output,
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
SuccessOutput(response.Machine, "Machine expired", output)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-07-25 15:04:06 +02:00
|
|
|
var deleteNodeCmd = &cobra.Command{
|
2021-10-28 15:30:41 +02:00
|
|
|
Use: "delete",
|
2021-07-17 00:23:12 +02:00
|
|
|
Short: "Delete a node",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-10-12 23:48:08 +02:00
|
|
|
output, _ := cmd.Flags().GetString("output")
|
2021-11-04 23:44:35 +01:00
|
|
|
|
2021-11-21 22:34:03 +01:00
|
|
|
identifier, err := cmd.Flags().GetUint64("identifier")
|
2021-07-17 00:23:12 +02:00
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf("Error converting ID to integer: %s", err),
|
|
|
|
output,
|
|
|
|
)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-11-04 23:44:35 +01:00
|
|
|
return
|
2021-07-17 00:23:12 +02:00
|
|
|
}
|
2021-11-04 23:44:35 +01:00
|
|
|
|
2021-11-07 10:41:14 +01:00
|
|
|
ctx, client, conn, cancel := getHeadscaleCLIClient()
|
2021-11-04 23:44:35 +01:00
|
|
|
defer cancel()
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
getRequest := &v1.GetMachineRequest{
|
2021-11-21 22:34:03 +01:00
|
|
|
MachineId: identifier,
|
2021-11-04 23:44:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getResponse, err := client.GetMachine(ctx, getRequest)
|
2021-07-17 00:23:12 +02:00
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf(
|
|
|
|
"Error getting node node: %s",
|
|
|
|
status.Convert(err).Message(),
|
|
|
|
),
|
|
|
|
output,
|
|
|
|
)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-11-04 23:44:35 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteRequest := &v1.DeleteMachineRequest{
|
2021-11-22 18:22:22 +01:00
|
|
|
MachineId: identifier,
|
2021-07-17 00:23:12 +02:00
|
|
|
}
|
2021-07-17 11:09:42 +02:00
|
|
|
|
|
|
|
confirm := false
|
2021-10-16 12:30:52 +02:00
|
|
|
force, _ := cmd.Flags().GetBool("force")
|
|
|
|
if !force {
|
|
|
|
prompt := &survey.Confirm{
|
2021-11-13 09:36:45 +01:00
|
|
|
Message: fmt.Sprintf(
|
|
|
|
"Do you want to remove the node %s?",
|
|
|
|
getResponse.GetMachine().Name,
|
|
|
|
),
|
2021-10-16 12:30:52 +02:00
|
|
|
}
|
|
|
|
err = survey.AskOne(prompt, &confirm)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if confirm || force {
|
2021-11-04 23:44:35 +01:00
|
|
|
response, err := client.DeleteMachine(ctx, deleteRequest)
|
|
|
|
if output != "" {
|
|
|
|
SuccessOutput(response, "", output)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-10-12 23:48:08 +02:00
|
|
|
return
|
|
|
|
}
|
2021-07-17 11:09:42 +02:00
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf(
|
|
|
|
"Error deleting node: %s",
|
|
|
|
status.Convert(err).Message(),
|
|
|
|
),
|
|
|
|
output,
|
|
|
|
)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-10-12 23:48:08 +02:00
|
|
|
return
|
|
|
|
}
|
2021-11-13 09:36:45 +01:00
|
|
|
SuccessOutput(
|
|
|
|
map[string]string{"Result": "Node deleted"},
|
|
|
|
"Node deleted",
|
|
|
|
output,
|
|
|
|
)
|
2021-11-04 23:44:35 +01:00
|
|
|
} else {
|
|
|
|
SuccessOutput(map[string]string{"Result": "Node not deleted"}, "Node not deleted", output)
|
2021-07-17 00:23:12 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2021-08-15 23:10:39 +02:00
|
|
|
|
2021-11-04 23:44:35 +01:00
|
|
|
func sharingWorker(
|
|
|
|
cmd *cobra.Command,
|
|
|
|
) (string, *v1.Machine, *v1.Namespace, error) {
|
|
|
|
output, _ := cmd.Flags().GetString("output")
|
2021-10-28 14:39:27 +02:00
|
|
|
namespaceStr, err := cmd.Flags().GetString("namespace")
|
2021-10-27 23:51:42 +02:00
|
|
|
if err != nil {
|
2021-11-04 23:44:35 +01:00
|
|
|
ErrorOutput(err, fmt.Sprintf("Error getting namespace: %s", err), output)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-11-04 23:44:35 +01:00
|
|
|
return "", nil, nil, err
|
2021-10-27 23:51:42 +02:00
|
|
|
}
|
|
|
|
|
2021-11-07 10:41:14 +01:00
|
|
|
ctx, client, conn, cancel := getHeadscaleCLIClient()
|
2021-11-04 23:44:35 +01:00
|
|
|
defer cancel()
|
|
|
|
defer conn.Close()
|
|
|
|
|
2021-11-21 22:34:03 +01:00
|
|
|
identifier, err := cmd.Flags().GetUint64("identifier")
|
2021-10-24 23:02:57 +02:00
|
|
|
if err != nil {
|
2021-11-04 23:44:35 +01:00
|
|
|
ErrorOutput(err, fmt.Sprintf("Error converting ID to integer: %s", err), output)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-11-04 23:44:35 +01:00
|
|
|
return "", nil, nil, err
|
2021-10-24 23:02:57 +02:00
|
|
|
}
|
|
|
|
|
2021-11-04 23:44:35 +01:00
|
|
|
machineRequest := &v1.GetMachineRequest{
|
2021-11-21 22:34:03 +01:00
|
|
|
MachineId: identifier,
|
2021-10-24 23:02:57 +02:00
|
|
|
}
|
|
|
|
|
2021-11-04 23:44:35 +01:00
|
|
|
machineResponse, err := client.GetMachine(ctx, machineRequest)
|
2021-10-24 23:02:57 +02:00
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf("Error getting node node: %s", status.Convert(err).Message()),
|
|
|
|
output,
|
|
|
|
)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-11-04 23:44:35 +01:00
|
|
|
return "", nil, nil, err
|
2021-10-24 23:02:57 +02:00
|
|
|
}
|
2021-11-04 23:44:35 +01:00
|
|
|
|
|
|
|
namespaceRequest := &v1.GetNamespaceRequest{
|
|
|
|
Name: namespaceStr,
|
|
|
|
}
|
|
|
|
|
|
|
|
namespaceResponse, err := client.GetNamespace(ctx, namespaceRequest)
|
2021-10-24 23:02:57 +02:00
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf("Error getting node node: %s", status.Convert(err).Message()),
|
|
|
|
output,
|
|
|
|
)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-11-04 23:44:35 +01:00
|
|
|
return "", nil, nil, err
|
2021-10-24 23:02:57 +02:00
|
|
|
}
|
|
|
|
|
2021-11-04 23:44:35 +01:00
|
|
|
return output, machineResponse.GetMachine(), namespaceResponse.GetNamespace(), nil
|
2021-10-24 23:02:57 +02:00
|
|
|
}
|
|
|
|
|
2021-09-10 00:26:46 +02:00
|
|
|
var shareMachineCmd = &cobra.Command{
|
2021-10-28 15:30:41 +02:00
|
|
|
Use: "share",
|
2021-09-03 10:23:45 +02:00
|
|
|
Short: "Shares a node from the current namespace to the specified one",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-11-14 18:03:21 +01:00
|
|
|
output, machine, namespace, err := sharingWorker(cmd)
|
2021-11-04 23:44:35 +01:00
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf("Failed to fetch namespace or machine: %s", err),
|
|
|
|
output,
|
|
|
|
)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-09-03 10:23:45 +02:00
|
|
|
return
|
|
|
|
}
|
2021-11-04 23:44:35 +01:00
|
|
|
|
2021-11-07 10:41:14 +01:00
|
|
|
ctx, client, conn, cancel := getHeadscaleCLIClient()
|
2021-11-04 23:44:35 +01:00
|
|
|
defer cancel()
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
request := &v1.ShareMachineRequest{
|
|
|
|
MachineId: machine.Id,
|
|
|
|
Namespace: namespace.Name,
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := client.ShareMachine(ctx, request)
|
2021-09-03 10:23:45 +02:00
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf("Error sharing node: %s", status.Convert(err).Message()),
|
|
|
|
output,
|
|
|
|
)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-09-03 10:23:45 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-04 23:44:35 +01:00
|
|
|
SuccessOutput(response.Machine, "Node shared", output)
|
2021-09-03 10:23:45 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-10-17 22:29:30 +02:00
|
|
|
var unshareMachineCmd = &cobra.Command{
|
2021-10-28 15:30:41 +02:00
|
|
|
Use: "unshare",
|
2021-10-17 22:29:30 +02:00
|
|
|
Short: "Unshares a node from the specified namespace",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-11-14 18:03:21 +01:00
|
|
|
output, machine, namespace, err := sharingWorker(cmd)
|
2021-11-04 23:44:35 +01:00
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf("Failed to fetch namespace or machine: %s", err),
|
|
|
|
output,
|
|
|
|
)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-10-17 22:29:30 +02:00
|
|
|
return
|
|
|
|
}
|
2021-11-04 23:44:35 +01:00
|
|
|
|
2021-11-07 10:41:14 +01:00
|
|
|
ctx, client, conn, cancel := getHeadscaleCLIClient()
|
2021-11-04 23:44:35 +01:00
|
|
|
defer cancel()
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
request := &v1.UnshareMachineRequest{
|
|
|
|
MachineId: machine.Id,
|
|
|
|
Namespace: namespace.Name,
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := client.UnshareMachine(ctx, request)
|
2021-10-17 22:29:30 +02:00
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf("Error unsharing node: %s", status.Convert(err).Message()),
|
|
|
|
output,
|
|
|
|
)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-10-17 22:29:30 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-07 10:57:39 +01:00
|
|
|
SuccessOutput(response.Machine, "Node unshared", output)
|
2021-10-17 22:29:30 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-11-13 09:36:45 +01:00
|
|
|
func nodesToPtables(
|
|
|
|
currentNamespace string,
|
|
|
|
machines []*v1.Machine,
|
|
|
|
) (pterm.TableData, error) {
|
2021-11-14 20:32:03 +01:00
|
|
|
tableData := pterm.TableData{
|
2021-11-13 09:36:45 +01:00
|
|
|
{
|
|
|
|
"ID",
|
|
|
|
"Name",
|
|
|
|
"NodeKey",
|
|
|
|
"Namespace",
|
|
|
|
"IP address",
|
|
|
|
"Ephemeral",
|
|
|
|
"Last seen",
|
|
|
|
"Online",
|
2021-11-21 10:44:38 +01:00
|
|
|
"Expired",
|
2021-11-13 09:36:45 +01:00
|
|
|
},
|
|
|
|
}
|
2021-08-15 23:10:39 +02:00
|
|
|
|
2021-09-10 00:26:46 +02:00
|
|
|
for _, machine := range machines {
|
2021-08-15 23:10:39 +02:00
|
|
|
var ephemeral bool
|
2021-11-04 23:44:35 +01:00
|
|
|
if machine.PreAuthKey != nil && machine.PreAuthKey.Ephemeral {
|
2021-08-15 23:10:39 +02:00
|
|
|
ephemeral = true
|
|
|
|
}
|
2021-11-21 10:44:38 +01:00
|
|
|
|
2021-08-15 23:10:39 +02:00
|
|
|
var lastSeen time.Time
|
2021-09-01 23:44:42 +02:00
|
|
|
var lastSeenTime string
|
2021-09-10 00:37:01 +02:00
|
|
|
if machine.LastSeen != nil {
|
2021-11-04 23:44:35 +01:00
|
|
|
lastSeen = machine.LastSeen.AsTime()
|
2021-09-01 23:44:42 +02:00
|
|
|
lastSeenTime = lastSeen.Format("2006-01-02 15:04:05")
|
2021-08-15 23:10:39 +02:00
|
|
|
}
|
2021-11-21 10:44:38 +01:00
|
|
|
|
|
|
|
var expiry time.Time
|
|
|
|
if machine.Expiry != nil {
|
|
|
|
expiry = machine.Expiry.AsTime()
|
|
|
|
}
|
|
|
|
|
2021-11-27 00:50:42 +01:00
|
|
|
var nodeKey key.NodePublic
|
2021-11-27 21:25:12 +01:00
|
|
|
err := nodeKey.UnmarshalText(
|
|
|
|
[]byte(headscale.NodePublicKeyEnsurePrefix(machine.NodeKey)),
|
|
|
|
)
|
2021-08-15 23:10:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var online string
|
2021-11-13 09:36:45 +01:00
|
|
|
if lastSeen.After(
|
|
|
|
time.Now().Add(-5 * time.Minute),
|
|
|
|
) { // TODO: Find a better way to reliably show if online
|
2021-11-21 10:44:38 +01:00
|
|
|
online = pterm.LightGreen("online")
|
|
|
|
} else {
|
|
|
|
online = pterm.LightRed("offline")
|
|
|
|
}
|
|
|
|
|
|
|
|
var expired string
|
|
|
|
if expiry.IsZero() || expiry.After(time.Now()) {
|
|
|
|
expired = pterm.LightGreen("no")
|
2021-08-15 23:10:39 +02:00
|
|
|
} else {
|
2021-11-21 10:44:38 +01:00
|
|
|
expired = pterm.LightRed("yes")
|
2021-08-15 23:10:39 +02:00
|
|
|
}
|
2021-09-02 17:06:47 +02:00
|
|
|
|
|
|
|
var namespace string
|
2021-11-07 09:58:03 +01:00
|
|
|
if currentNamespace == "" || (currentNamespace == machine.Namespace.Name) {
|
2021-09-10 00:26:46 +02:00
|
|
|
namespace = pterm.LightMagenta(machine.Namespace.Name)
|
2021-09-02 17:06:47 +02:00
|
|
|
} else {
|
2021-10-24 23:02:57 +02:00
|
|
|
// Shared into this namespace
|
2021-09-10 00:26:46 +02:00
|
|
|
namespace = pterm.LightYellow(machine.Namespace.Name)
|
2021-09-02 17:06:47 +02:00
|
|
|
}
|
2021-11-14 20:32:03 +01:00
|
|
|
tableData = append(
|
|
|
|
tableData,
|
2021-11-04 23:44:35 +01:00
|
|
|
[]string{
|
2021-11-15 18:24:24 +01:00
|
|
|
strconv.FormatUint(machine.Id, headscale.Base10),
|
2021-11-04 23:44:35 +01:00
|
|
|
machine.Name,
|
|
|
|
nodeKey.ShortString(),
|
|
|
|
namespace,
|
|
|
|
machine.IpAddress,
|
|
|
|
strconv.FormatBool(ephemeral),
|
|
|
|
lastSeenTime,
|
|
|
|
online,
|
2021-11-21 10:44:38 +01:00
|
|
|
expired,
|
2021-11-04 23:44:35 +01:00
|
|
|
},
|
|
|
|
)
|
2021-08-15 23:10:39 +02:00
|
|
|
}
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-11-14 20:32:03 +01:00
|
|
|
return tableData, nil
|
2021-08-15 23:10:39 +02:00
|
|
|
}
|