2021-04-28 16:15:45 +02:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2021-11-13 20:01:05 +01:00
|
|
|
survey "github.com/AlecAivazis/survey/v2"
|
2021-11-15 20:18:14 +01:00
|
|
|
"github.com/juanfont/headscale"
|
2021-11-04 23:42:21 +01:00
|
|
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
2021-08-15 23:20:38 +02:00
|
|
|
"github.com/pterm/pterm"
|
2021-10-29 19:16:54 +02:00
|
|
|
"github.com/rs/zerolog/log"
|
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-04-28 16:15:45 +02:00
|
|
|
)
|
|
|
|
|
2021-07-25 15:03:45 +02:00
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(namespaceCmd)
|
|
|
|
namespaceCmd.AddCommand(createNamespaceCmd)
|
|
|
|
namespaceCmd.AddCommand(listNamespacesCmd)
|
|
|
|
namespaceCmd.AddCommand(destroyNamespaceCmd)
|
2021-10-16 17:20:06 +02:00
|
|
|
namespaceCmd.AddCommand(renameNamespaceCmd)
|
2021-07-25 15:03:45 +02:00
|
|
|
}
|
|
|
|
|
2021-11-15 20:18:14 +01:00
|
|
|
const (
|
|
|
|
errMissingParameter = headscale.Error("missing parameters")
|
|
|
|
)
|
|
|
|
|
2021-07-25 15:03:45 +02:00
|
|
|
var namespaceCmd = &cobra.Command{
|
2022-03-02 05:35:20 +01:00
|
|
|
Use: "namespaces",
|
|
|
|
Short: "Manage the namespaces of Headscale",
|
|
|
|
Aliases: []string{"namespace", "ns", "user", "users"},
|
2021-04-28 16:15:45 +02:00
|
|
|
}
|
|
|
|
|
2021-07-25 15:03:45 +02:00
|
|
|
var createNamespaceCmd = &cobra.Command{
|
2022-03-02 05:35:20 +01:00
|
|
|
Use: "create NAME",
|
|
|
|
Short: "Creates a new namespace",
|
|
|
|
Aliases: []string{"c", "new"},
|
2021-04-28 16:15:45 +02:00
|
|
|
Args: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if len(args) < 1 {
|
2021-11-15 20:18:14 +01:00
|
|
|
return errMissingParameter
|
2021-04-28 16:15:45 +02:00
|
|
|
}
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-04-28 16:15:45 +02:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-11-04 23:42:21 +01:00
|
|
|
output, _ := cmd.Flags().GetString("output")
|
|
|
|
|
|
|
|
namespaceName := args[0]
|
2021-10-29 19:16:54 +02:00
|
|
|
|
2021-11-07 10:41:14 +01:00
|
|
|
ctx, client, conn, cancel := getHeadscaleCLIClient()
|
2021-10-31 17:26:51 +01:00
|
|
|
defer cancel()
|
2021-10-29 19:16:54 +02:00
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
log.Trace().Interface("client", client).Msg("Obtained gRPC client")
|
|
|
|
|
2021-11-04 23:42:21 +01:00
|
|
|
request := &v1.CreateNamespaceRequest{Name: namespaceName}
|
2021-10-29 19:16:54 +02:00
|
|
|
|
|
|
|
log.Trace().Interface("request", request).Msg("Sending CreateNamespace request")
|
|
|
|
response, err := client.CreateNamespace(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 create namespace: %s",
|
|
|
|
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:42:21 +01:00
|
|
|
|
|
|
|
SuccessOutput(response.Namespace, "Namespace created", output)
|
2021-04-28 16:15:45 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-07-25 15:03:45 +02:00
|
|
|
var destroyNamespaceCmd = &cobra.Command{
|
2022-03-02 05:35:20 +01:00
|
|
|
Use: "destroy NAME",
|
|
|
|
Short: "Destroys a namespace",
|
|
|
|
Aliases: []string{"delete"},
|
2021-05-09 17:12:05 +02:00
|
|
|
Args: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if len(args) < 1 {
|
2021-11-15 20:18:14 +01:00
|
|
|
return errMissingParameter
|
2021-05-09 17:12:05 +02:00
|
|
|
}
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-05-09 17:12:05 +02:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-11-04 23:42:21 +01:00
|
|
|
output, _ := cmd.Flags().GetString("output")
|
|
|
|
|
|
|
|
namespaceName := args[0]
|
|
|
|
|
2021-11-13 20:01:05 +01:00
|
|
|
request := &v1.GetNamespaceRequest{
|
|
|
|
Name: namespaceName,
|
|
|
|
}
|
|
|
|
|
2021-11-07 10:41:14 +01:00
|
|
|
ctx, client, conn, cancel := getHeadscaleCLIClient()
|
2021-11-04 23:42:21 +01:00
|
|
|
defer cancel()
|
|
|
|
defer conn.Close()
|
|
|
|
|
2021-11-13 20:01:05 +01:00
|
|
|
_, err := client.GetNamespace(ctx, request)
|
2021-05-09 17:12:05 +02:00
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
2021-11-14 09:30:48 +01:00
|
|
|
fmt.Sprintf("Error: %s", status.Convert(err).Message()),
|
2021-11-13 09:36:45 +01:00
|
|
|
output,
|
|
|
|
)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-05-09 17:12:05 +02:00
|
|
|
return
|
|
|
|
}
|
2021-11-04 23:42:21 +01:00
|
|
|
|
2021-11-13 20:01:05 +01:00
|
|
|
confirm := false
|
|
|
|
force, _ := cmd.Flags().GetBool("force")
|
|
|
|
if !force {
|
|
|
|
prompt := &survey.Confirm{
|
2021-11-14 09:30:48 +01:00
|
|
|
Message: fmt.Sprintf(
|
|
|
|
"Do you want to remove the namespace '%s' and any associated preauthkeys?",
|
|
|
|
namespaceName,
|
|
|
|
),
|
2021-11-13 20:01:05 +01:00
|
|
|
}
|
|
|
|
err := survey.AskOne(prompt, &confirm)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if confirm || force {
|
|
|
|
request := &v1.DeleteNamespaceRequest{Name: namespaceName}
|
|
|
|
|
|
|
|
response, err := client.DeleteNamespace(ctx, request)
|
|
|
|
if err != nil {
|
2021-11-14 09:30:48 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf(
|
|
|
|
"Cannot destroy namespace: %s",
|
|
|
|
status.Convert(err).Message(),
|
|
|
|
),
|
|
|
|
output,
|
|
|
|
)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-11-13 20:01:05 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
SuccessOutput(response, "Namespace destroyed", output)
|
|
|
|
} else {
|
|
|
|
SuccessOutput(map[string]string{"Result": "Namespace not destroyed"}, "Namespace not destroyed", output)
|
|
|
|
}
|
2021-05-09 17:12:05 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-07-25 15:03:45 +02:00
|
|
|
var listNamespacesCmd = &cobra.Command{
|
2022-03-02 05:35:20 +01:00
|
|
|
Use: "list",
|
|
|
|
Short: "List all the namespaces",
|
|
|
|
Aliases: []string{"ls", "show"},
|
2021-04-28 16:15:45 +02:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-11-04 23:42:21 +01:00
|
|
|
output, _ := cmd.Flags().GetString("output")
|
|
|
|
|
2021-11-07 10:41:14 +01:00
|
|
|
ctx, client, conn, cancel := getHeadscaleCLIClient()
|
2021-11-04 23:42:21 +01:00
|
|
|
defer cancel()
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
request := &v1.ListNamespacesRequest{}
|
|
|
|
|
|
|
|
response, err := client.ListNamespaces(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 get namespaces: %s", status.Convert(err).Message()),
|
|
|
|
output,
|
|
|
|
)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-05-08 13:28:22 +02:00
|
|
|
return
|
|
|
|
}
|
2021-11-04 23:42:21 +01:00
|
|
|
|
|
|
|
if output != "" {
|
|
|
|
SuccessOutput(response.Namespaces, "", output)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-04-28 16:15:45 +02:00
|
|
|
return
|
|
|
|
}
|
2021-08-15 23:20:38 +02:00
|
|
|
|
2021-11-14 20:32:03 +01:00
|
|
|
tableData := pterm.TableData{{"ID", "Name", "Created"}}
|
2021-11-04 23:42:21 +01:00
|
|
|
for _, namespace := range response.GetNamespaces() {
|
2021-11-14 20:32:03 +01:00
|
|
|
tableData = append(
|
|
|
|
tableData,
|
2021-11-04 23:42:21 +01:00
|
|
|
[]string{
|
|
|
|
namespace.GetId(),
|
|
|
|
namespace.GetName(),
|
|
|
|
namespace.GetCreatedAt().AsTime().Format("2006-01-02 15:04:05"),
|
|
|
|
},
|
2021-10-29 19:16:54 +02:00
|
|
|
)
|
2021-04-28 16:15:45 +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:42:21 +01:00
|
|
|
return
|
2021-08-15 23:35:03 +02:00
|
|
|
}
|
2021-04-28 16:15:45 +02:00
|
|
|
},
|
|
|
|
}
|
2021-10-16 17:20:06 +02:00
|
|
|
|
|
|
|
var renameNamespaceCmd = &cobra.Command{
|
2022-03-02 05:35:20 +01:00
|
|
|
Use: "rename OLD_NAME NEW_NAME",
|
|
|
|
Short: "Renames a namespace",
|
|
|
|
Aliases: []string{"mv"},
|
2021-10-16 17:20:06 +02:00
|
|
|
Args: func(cmd *cobra.Command, args []string) error {
|
2021-11-14 18:31:51 +01:00
|
|
|
expectedArguments := 2
|
|
|
|
if len(args) < expectedArguments {
|
2021-11-15 20:18:14 +01:00
|
|
|
return errMissingParameter
|
2021-10-16 17:20:06 +02:00
|
|
|
}
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-10-16 17:20:06 +02:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-11-04 23:42:21 +01:00
|
|
|
output, _ := cmd.Flags().GetString("output")
|
|
|
|
|
2021-11-07 10:41:14 +01:00
|
|
|
ctx, client, conn, cancel := getHeadscaleCLIClient()
|
2021-11-04 23:42:21 +01:00
|
|
|
defer cancel()
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
request := &v1.RenameNamespaceRequest{
|
|
|
|
OldName: args[0],
|
|
|
|
NewName: args[1],
|
2021-10-16 17:20:06 +02:00
|
|
|
}
|
2021-11-04 23:42:21 +01:00
|
|
|
|
|
|
|
response, err := client.RenameNamespace(ctx, request)
|
2021-10-16 17:20:06 +02:00
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf(
|
|
|
|
"Cannot rename namespace: %s",
|
|
|
|
status.Convert(err).Message(),
|
|
|
|
),
|
|
|
|
output,
|
|
|
|
)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-10-16 17:20:06 +02:00
|
|
|
return
|
|
|
|
}
|
2021-11-04 23:42:21 +01:00
|
|
|
|
|
|
|
SuccessOutput(response.Namespace, "Namespace renamed", output)
|
2021-10-16 17:20:06 +02:00
|
|
|
},
|
|
|
|
}
|