Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] profile switching (multiple networks) #2401

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions client/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,25 @@ var loginCmd = &cobra.Command{

client := proto.NewDaemonServiceClient(conn)

if profileName != "" {
profilesDir, err := getUserProfilesDir()
if err != nil {
return err
}

usesSetupKey := setupKey != ""

_, err = client.SwitchProfile(ctx, &proto.SwitchProfileRequest{
Profile: profileName,
UserProfilesPath: profilesDir,
IsNewSystemProfile: &usesSetupKey,
})

if err != nil {
return err
}
}

loginRequest := proto.LoginRequest{
SetupKey: providedSetupKey,
ManagementUrl: managementURL,
Expand Down Expand Up @@ -135,6 +154,10 @@ var loginCmd = &cobra.Command{
},
}

func init() {
logCmd.PersistentFlags().StringVar(&profileName, profileNameFlag, "", "the profile to use")
}

func foregroundLogin(ctx context.Context, cmd *cobra.Command, config *internal.Config, setupKey string) error {
needsLogin := false

Expand Down
102 changes: 102 additions & 0 deletions client/cmd/profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package cmd

import (
"fmt"
"os"
"path"

"github.com/netbirdio/netbird/client/internal"
"github.com/netbirdio/netbird/client/proto"
"github.com/spf13/cobra"
)

func getUserProfilesDir() (string, error) {
config, err := os.UserConfigDir()
if err != nil {
return "", err
}

profilesDir := path.Join(config, "netbird", "profiles")

if err := os.MkdirAll(profilesDir, os.ModeDir); err != nil {
return "", err
}

return profilesDir, nil
}

var (
profileCmd = &cobra.Command{
Use: "profile [newProfile]",
Short: "switch to profile newProfile or get the current one",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := internal.CtxInitState(cmd.Context())

conn, err := DialClientGRPCServer(ctx, daemonAddr)
if err != nil {
return fmt.Errorf("failed to connect to service CLI interface %v", err)
}

defer conn.Close()

daemonClient := proto.NewDaemonServiceClient(conn)

profilesDir, err := getUserProfilesDir()
if err != nil {
return err
}

if len(args) == 1 {
if _, err := daemonClient.SwitchProfile(ctx, &proto.SwitchProfileRequest{Profile: args[0], UserProfilesPath: profilesDir}); err != nil {
return err
}

return nil
}

resp, err := daemonClient.GetProfile(ctx, &proto.GetProfileRequest{})

if err != nil {
return err
}

cmd.Println(resp.Profile)

return nil
},
}

profilesCmd = &cobra.Command{
Use: "profiles",
Short: "list all profiles",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := internal.CtxInitState(cmd.Context())

conn, err := DialClientGRPCServer(ctx, daemonAddr)
if err != nil {
return fmt.Errorf("failed to connect to service CLI interface %v", err)
}

profilesDir, err := getUserProfilesDir()
if err != nil {
return err
}

defer conn.Close()

daemonClient := proto.NewDaemonServiceClient(conn)
resp, err := daemonClient.ListProfiles(ctx, &proto.ListProfilesRequest{UserProfilesPath: profilesDir})

if err != nil {
return err
}

for _, profile := range resp.Profiles {
cmd.Println(profile)
}

return nil
},
}
)
4 changes: 4 additions & 0 deletions client/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
extraIFaceBlackListFlag = "extra-iface-blacklist"
dnsRouteIntervalFlag = "dns-router-interval"
systemInfoFlag = "system-info"
profileNameFlag = "profile"
)

var (
Expand Down Expand Up @@ -73,6 +74,7 @@ var (
anonymizeFlag bool
debugSystemInfoFlag bool
dnsRouteInterval time.Duration
profileName string

rootCmd = &cobra.Command{
Use: "netbird",
Expand Down Expand Up @@ -144,6 +146,8 @@ func init() {
rootCmd.AddCommand(sshCmd)
rootCmd.AddCommand(routesCmd)
rootCmd.AddCommand(debugCmd)
rootCmd.AddCommand(profileCmd)
rootCmd.AddCommand(profilesCmd)

serviceCmd.AddCommand(runCmd, startCmd, stopCmd, restartCmd) // service control commands are subcommands of service
serviceCmd.AddCommand(installCmd, uninstallCmd) // service installer commands are subcommands of service
Expand Down
20 changes: 20 additions & 0 deletions client/cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func init() {
)
upCmd.PersistentFlags().StringSliceVar(&extraIFaceBlackList, extraIFaceBlackListFlag, nil, "Extra list of default interfaces to ignore for listening")
upCmd.PersistentFlags().DurationVar(&dnsRouteInterval, dnsRouteIntervalFlag, time.Minute, "DNS route update interval")
upCmd.PersistentFlags().StringVar(&profileName, profileNameFlag, "", "the profile to use")
}

func upFunc(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -194,6 +195,25 @@ func runInDaemonMode(ctx context.Context, cmd *cobra.Command) error {

client := proto.NewDaemonServiceClient(conn)

if profileName != "" {
profilesDir, err := getUserProfilesDir()
if err != nil {
return err
}

usesSetupKey := setupKey != ""

_, err = client.SwitchProfile(ctx, &proto.SwitchProfileRequest{
Profile: profileName,
UserProfilesPath: profilesDir,
IsNewSystemProfile: &usesSetupKey,
})

if err != nil {
return err
}
}

status, err := client.Status(ctx, &proto.StatusRequest{})
if err != nil {
return fmt.Errorf("unable to get daemon status: %v", err)
Expand Down
Loading
Loading