Skip to content

Commit

Permalink
update runOnce
Browse files Browse the repository at this point in the history
  • Loading branch information
ncode committed Dec 5, 2023
1 parent 6649d7d commit bab6b8c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 19 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var logger = slog.New(
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "bedel",
Short: "Small utility to sync redis acls with a master instance",
Short: "Small utility to sync redis acls with a primary instance",
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand Down
2 changes: 1 addition & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
// runCmd represents the run command
var runCmd = &cobra.Command{
Use: "run",
Short: "Run the acl manager in mood loop, it will sync the follower with the master",
Short: "Run the acl manager in mood loop, it will sync the follower with the primary",
Run: func(cmd *cobra.Command, args []string) {
mgr := aclmanager.New(viper.GetString("address"), viper.GetString("username"), viper.GetString("password"))
ctx := cmd.Context()
Expand Down
28 changes: 24 additions & 4 deletions cmd/runOnce.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,39 @@ package cmd
import (
"github.com/ncode/bedel/pkg/aclmanager"
"github.com/spf13/viper"
"log/slog"
"os"

"github.com/spf13/cobra"
)

// runOnceCmd represents the runOnce command
var runOnceCmd = &cobra.Command{
Use: "runOnce",
Short: "Run the acl manager once, it will sync the follower with the master",
Short: "Run the acl manager once, it will sync the follower with the primary",
Run: func(cmd *cobra.Command, args []string) {
mgr := aclmanager.New(viper.GetString("address"), viper.GetString("username"), viper.GetString("password"))
err := mgr.SyncAcls()
ctx := cmd.Context()
aclManager := aclmanager.New(viper.GetString("address"), viper.GetString("username"), viper.GetString("password"))
function, err := aclManager.CurrentFunction(ctx)
if err != nil {
panic(err)
slog.Warn("unable to check if it's a Primary", "message", err)
os.Exit(1)
}
if function == aclmanager.Follower {
primary, err := aclManager.Primary(ctx)
if err != nil {
slog.Warn("unable to find Primary", "message", err)
os.Exit(1)
}
var added, deleted []string
added, deleted, err = aclManager.SyncAcls(ctx, primary)
if err != nil {
slog.Warn("unable to sync acls from Primary", "message", err)
os.Exit(1)
}
slog.Info("Synced acls from Primary", "added", added, "deleted", deleted)
} else {
slog.Info("Not a follower, nothing to do")
}
},
}
Expand Down
18 changes: 10 additions & 8 deletions pkg/aclmanager/aclmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,17 @@ func (a *AclManager) Primary(ctx context.Context) (primary *AclManager, err erro
}

// SyncAcls connects to master node and syncs the acls to the current node
func (a *AclManager) SyncAcls(ctx context.Context, primary *AclManager) (err error) {
func (a *AclManager) SyncAcls(ctx context.Context, primary *AclManager) (added []string, deleted []string, err error) {
slog.Debug("Syncing acls")
if primary == nil {
return fmt.Errorf("no primary found")
return added, deleted, fmt.Errorf("no primary found")
}
added, deleted, err := mirrorAcls(ctx, primary.RedisClient, a.RedisClient)
added, deleted, err = mirrorAcls(ctx, primary.RedisClient, a.RedisClient)
if err != nil {
return fmt.Errorf("error syncing acls: %v", err)
return added, deleted, fmt.Errorf("error syncing acls: %v", err)
}
slog.Info("Synced acls", "added", added, "deleted", deleted)

return err
return added, deleted, err
}

// Close closes the redis client
Expand Down Expand Up @@ -254,11 +253,14 @@ func (a *AclManager) Loop(ctx context.Context) (err error) {
slog.Warn("unable to find Primary", "message", e)
continue
}
e = a.SyncAcls(ctx, primary)
var added, deleted []string
added, deleted, err = a.SyncAcls(ctx, primary)
if err != nil {
slog.Warn("unable to sync acls from Primary", "message", e)
err = fmt.Errorf("unable to sync acls from Primary: %w", e)
continue
}
err = fmt.Errorf("unable to sync acls from Primary: %w", e)
slog.Info("Synced acls from Primary", "added", added, "deleted", deleted)
}
}
}
Expand Down
5 changes: 0 additions & 5 deletions pkg/aclmanager/aclmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ repl_backlog_first_byte_offset:1
repl_backlog_histlen:434`
)

type NodeInfo struct {
Address string
Function int
}

func TestFindNodes(t *testing.T) {
// Sample master and slave output for testing

Expand Down

0 comments on commit bab6b8c

Please sign in to comment.