Skip to content

Commit

Permalink
Add file to sqlite store migration command
Browse files Browse the repository at this point in the history
  • Loading branch information
surik committed Aug 24, 2023
1 parent 9428ab6 commit dd93f14
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
65 changes: 65 additions & 0 deletions management/cmd/migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cmd

import (
"errors"
"flag"
"fmt"
"os"
"path"

"github.com/netbirdio/netbird/management/server"
"github.com/netbirdio/netbird/util"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var short = "Migrate JSON file store to SQLite store. Please make a backup of the store json file before running this command."

var migrationCmd = &cobra.Command{
Use: "migration [--datadir directory] [--log-file console]",
Short: short,
Long: short +
"\n\n" +
"This command reads the content of {datadir}/store.json and migrates it to {datadir}/store.db that can be used by SQLite store driver.",
RunE: func(cmd *cobra.Command, args []string) error {
flag.Parse()
err := util.InitLog(logLevel, logFile)
if err != nil {
return fmt.Errorf("failed initializing log %v", err)
}

fileStorePath := path.Join(mgmtDataDir, "store.json")
if _, err := os.Stat(fileStorePath); errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("%s doesn't exist, couldn't continue the operation", fileStorePath)
}

sqlStorePath := path.Join(mgmtDataDir, "store.db")
if _, err := os.Stat(sqlStorePath); err == nil {
return fmt.Errorf("%s already exists, couldn't continue the operation", sqlStorePath)
}

fstore, err := server.NewFileStore(mgmtDataDir, nil)
if err != nil {
return fmt.Errorf("failed creating file store: %s: %v", mgmtDataDir, err)
}

fsStoreAccounts := len(fstore.GetAllAccounts())
log.Infof("%d account will be migrated from file store %s to sqlite store %s",
fsStoreAccounts, fileStorePath, sqlStorePath)

store, err := server.NewSqliteStoreFromFileStore(fstore, mgmtDataDir, nil)
if err != nil {
return fmt.Errorf("failed creating file store: %s: %v", mgmtDataDir, err)
}

sqliteStoreAccounts := len(store.GetAllAccounts())
if fsStoreAccounts != sqliteStoreAccounts {
return fmt.Errorf("failed to migrate accounts from file to sqlite. Expected accounts: %d, got: %d",
fsStoreAccounts, sqliteStoreAccounts)
}

log.Info("Migration finished successfully")

return nil
},
}
4 changes: 4 additions & 0 deletions management/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func init() {
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "")
rootCmd.PersistentFlags().StringVar(&logFile, "log-file", defaultLogFile, "sets Netbird log path. If console is specified the the log will be output to stdout")
rootCmd.AddCommand(mgmtCmd)

migrationCmd.Flags().StringVar(&mgmtDataDir, "datadir", defaultMgmtDataDir, "server data directory location")
migrationCmd.MarkFlagRequired("datadir") //nolint
rootCmd.AddCommand(migrationCmd)
}

// SetupCloseHandler handles SIGTERM signal and exits with success
Expand Down

0 comments on commit dd93f14

Please sign in to comment.