-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
planner-api: Split the main into commands
This commit splits the main file into commands to have another command _migrate_ along with the _run_ command. The _migrate_ command is needed to migrate db during unit tests. Signed-off-by: Cosmin Tupangiu <[email protected]>
- Loading branch information
Showing
6 changed files
with
169 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,10 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
"github.com/kubev2v/migration-planner/internal/api_server/agentserver" | ||
"github.com/kubev2v/migration-planner/internal/config" | ||
"github.com/kubev2v/migration-planner/pkg/log" | ||
"github.com/sirupsen/logrus" | ||
|
||
apiserver "github.com/kubev2v/migration-planner/internal/api_server" | ||
|
||
"github.com/kubev2v/migration-planner/internal/store" | ||
) | ||
import "os" | ||
|
||
func main() { | ||
log := log.InitLogs() | ||
log.Println("Starting API service") | ||
defer log.Println("API service stopped") | ||
|
||
cfg, err := config.LoadOrGenerate(config.ConfigFile()) | ||
err := rootCmd.Execute() | ||
if err != nil { | ||
log.Fatalf("reading configuration: %v", err) | ||
} | ||
log.Printf("Using config: %s", cfg) | ||
|
||
logLvl, err := logrus.ParseLevel(cfg.Service.LogLevel) | ||
if err != nil { | ||
logLvl = logrus.InfoLevel | ||
} | ||
log.SetLevel(logLvl) | ||
|
||
log.Println("Initializing data store") | ||
db, err := store.InitDB(cfg, log) | ||
if err != nil { | ||
log.Fatalf("initializing data store: %v", err) | ||
} | ||
|
||
store := store.NewStore(db, log.WithField("pkg", "store")) | ||
defer store.Close() | ||
|
||
if err := store.InitialMigration(); err != nil { | ||
log.Fatalf("running initial migration: %v", err) | ||
} | ||
|
||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGQUIT) | ||
go func() { | ||
listener, err := newListener(cfg.Service.Address) | ||
if err != nil { | ||
log.Fatalf("creating listener: %s", err) | ||
} | ||
|
||
server := apiserver.New(log, cfg, store, listener) | ||
if err := server.Run(ctx); err != nil { | ||
log.Fatalf("Error running server: %s", err) | ||
} | ||
cancel() | ||
}() | ||
|
||
go func() { | ||
listener, err := newListener(cfg.Service.AgentEndpointAddress) | ||
if err != nil { | ||
log.Fatalf("creating listener: %s", err) | ||
} | ||
|
||
agentserver := agentserver.New(log, cfg, store, listener) | ||
if err := agentserver.Run(ctx); err != nil { | ||
log.Fatalf("Error running server: %s", err) | ||
} | ||
cancel() | ||
}() | ||
|
||
<-ctx.Done() | ||
} | ||
|
||
func newListener(address string) (net.Listener, error) { | ||
if address == "" { | ||
address = "localhost:0" | ||
os.Exit(1) | ||
} | ||
return net.Listen("tcp", address) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/kubev2v/migration-planner/internal/config" | ||
"github.com/kubev2v/migration-planner/internal/store" | ||
"github.com/kubev2v/migration-planner/pkg/log" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
dbUrl string | ||
) | ||
|
||
var migrateCmd = &cobra.Command{ | ||
Use: "migrate", | ||
Short: "Migrate the db", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
log := log.InitLogs() | ||
log.Println("Starting API service") | ||
defer log.Println("Db migrated") | ||
|
||
if configFile == "" { | ||
configFile = config.ConfigFile() | ||
} | ||
|
||
cfg, err := config.Load(configFile) | ||
if err != nil { | ||
log.Fatalf("reading configuration: %v", err) | ||
} | ||
log.Printf("Using config: %s", cfg) | ||
|
||
logLvl, err := logrus.ParseLevel(cfg.Service.LogLevel) | ||
if err != nil { | ||
logLvl = logrus.InfoLevel | ||
} | ||
log.SetLevel(logLvl) | ||
|
||
log.Println("Initializing data store") | ||
db, err := store.InitDB(cfg, log) | ||
if err != nil { | ||
log.Fatalf("initializing data store: %v", err) | ||
} | ||
|
||
store := store.NewStore(db, log.WithField("pkg", "store")) | ||
defer store.Close() | ||
|
||
if err := store.InitialMigration(); err != nil { | ||
log.Fatalf("running initial migration: %v", err) | ||
} | ||
|
||
return nil | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package main | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
var ( | ||
configFile string | ||
) | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "planner-api", | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(migrateCmd) | ||
rootCmd.AddCommand(runCmd) | ||
|
||
rootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "", "Path to configuration file") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
apiserver "github.com/kubev2v/migration-planner/internal/api_server" | ||
"github.com/kubev2v/migration-planner/internal/api_server/agentserver" | ||
"github.com/kubev2v/migration-planner/internal/config" | ||
"github.com/kubev2v/migration-planner/internal/store" | ||
"github.com/kubev2v/migration-planner/pkg/log" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var runCmd = &cobra.Command{ | ||
Use: "run", | ||
Short: "Run the planner api", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
log := log.InitLogs() | ||
log.Println("Starting API service") | ||
defer log.Println("API service stopped") | ||
|
||
if configFile == "" { | ||
configFile = config.ConfigFile() | ||
} | ||
cfg, err := config.NewFromFile(configFile) | ||
if err != nil { | ||
log.Fatalf("reading configuration: %v", err) | ||
} | ||
log.Printf("Using config: %s", cfg) | ||
|
||
logLvl, err := logrus.ParseLevel(cfg.Service.LogLevel) | ||
if err != nil { | ||
logLvl = logrus.InfoLevel | ||
} | ||
log.SetLevel(logLvl) | ||
|
||
log.Println("Initializing data store") | ||
db, err := store.InitDB(cfg, log) | ||
if err != nil { | ||
log.Fatalf("initializing data store: %v", err) | ||
} | ||
|
||
store := store.NewStore(db, log.WithField("pkg", "store")) | ||
defer store.Close() | ||
|
||
if err := store.InitialMigration(); err != nil { | ||
log.Fatalf("running initial migration: %v", err) | ||
} | ||
|
||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGQUIT) | ||
go func() { | ||
defer cancel() | ||
listener, err := newListener(cfg.Service.Address) | ||
if err != nil { | ||
log.Fatalf("creating listener: %s", err) | ||
} | ||
|
||
server := apiserver.New(log, cfg, store, listener) | ||
if err := server.Run(ctx); err != nil { | ||
log.Fatalf("Error running server: %s", err) | ||
} | ||
}() | ||
|
||
go func() { | ||
defer cancel() | ||
listener, err := newListener(cfg.Service.AgentEndpointAddress) | ||
if err != nil { | ||
log.Fatalf("creating listener: %s", err) | ||
} | ||
|
||
agentserver := agentserver.New(log, cfg, store, listener) | ||
if err := agentserver.Run(ctx); err != nil { | ||
log.Fatalf("Error running server: %s", err) | ||
} | ||
}() | ||
|
||
<-ctx.Done() | ||
return nil | ||
}, | ||
} | ||
|
||
func newListener(address string) (net.Listener, error) { | ||
if address == "" { | ||
address = "localhost:0" | ||
} | ||
return net.Listen("tcp", address) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters