added params to cmd

This commit is contained in:
marvzhang
2021-07-10 21:47:30 +08:00
parent bbe631011c
commit 178711829c
11 changed files with 203 additions and 24 deletions

View File

@@ -2,11 +2,26 @@ package cmd
import (
"crawlab/apps"
"fmt"
"github.com/crawlab-team/crawlab-core/entity"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
runOnMaster bool
masterConfigPath string
masterGrpcAddress string
)
func init() {
rootCmd.AddCommand(masterCmd)
masterCmd.PersistentFlags().StringVarP(&masterConfigPath, "config-path", "c", "", "Config path of master node")
_ = viper.BindPFlag("configPath", masterCmd.PersistentFlags().Lookup("configPath"))
masterCmd.PersistentFlags().StringVarP(&masterGrpcAddress, "grpc-address", "g", "", "gRPC address of master node")
_ = viper.BindPFlag("grpcAddress", masterCmd.PersistentFlags().Lookup("grpcAddress"))
}
var masterCmd = &cobra.Command{
@@ -16,9 +31,26 @@ var masterCmd = &cobra.Command{
Long: `Start a master instance of Crawlab
which runs api and assign tasks to worker nodes`,
Run: func(cmd *cobra.Command, args []string) {
master := apps.NewMaster(
apps.WithRunOnMaster(runOnMaster),
)
// options
var opts []apps.MasterOption
if masterConfigPath != "" {
opts = append(opts, apps.WithMasterConfigPath(masterConfigPath))
viper.Set("config.path", masterConfigPath)
}
opts = append(opts, apps.WithRunOnMaster(runOnMaster))
if masterGrpcAddress != "" {
address, err := entity.NewAddressFromString(masterGrpcAddress)
if err != nil {
fmt.Println(fmt.Sprintf("invalid grpc-address: %s", masterGrpcAddress))
}
opts = append(opts, apps.WithMasterGrpcAddress(address))
viper.Set("grpc.client.address", masterGrpcAddress)
}
// app
master := apps.NewMaster(opts...)
// start
apps.Start(master)
},
}

View File

@@ -10,8 +10,7 @@ import (
var (
// Used for flags.
cfgFile string
runOnMaster bool
cfgFile string
rootCmd = &cobra.Command{
Use: "crawlab",
@@ -30,9 +29,6 @@ func Execute() error {
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().BoolVar(&runOnMaster, "runOnMaster", false, "Whether to run tasks on master node (default: false)")
_ = viper.BindPFlag("runOnMaster", rootCmd.PersistentFlags().Lookup("runOnMaster"))
}
func initConfig() {

View File

@@ -2,11 +2,25 @@ package cmd
import (
"crawlab/apps"
"fmt"
"github.com/crawlab-team/crawlab-core/entity"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
workerConfigPath string
workerGrpcAddress string
)
func init() {
rootCmd.AddCommand(workerCmd)
workerCmd.PersistentFlags().StringVarP(&workerConfigPath, "config-path", "c", "", "Config path of worker node")
_ = viper.BindPFlag("configPath", workerCmd.PersistentFlags().Lookup("configPath"))
workerCmd.PersistentFlags().StringVarP(&workerGrpcAddress, "grpc-address", "g", "", "gRPC address of worker node")
_ = viper.BindPFlag("grpcAddress", workerCmd.PersistentFlags().Lookup("grpcAddress"))
}
var workerCmd = &cobra.Command{
@@ -17,7 +31,26 @@ var workerCmd = &cobra.Command{
serving in the worker node and executes tasks
assigned by the master node`,
Run: func(cmd *cobra.Command, args []string) {
worker := apps.NewWorker()
apps.Start(worker)
// options
var opts []apps.WorkerOption
if workerConfigPath != "" {
opts = append(opts, apps.WithWorkerConfigPath(workerConfigPath))
viper.Set("config.path", workerConfigPath)
}
if workerGrpcAddress != "" {
address, err := entity.NewAddressFromString(workerGrpcAddress)
if err != nil {
fmt.Println(fmt.Sprintf("invalid grpc-address: %s", workerGrpcAddress))
return
}
opts = append(opts, apps.WithWorkerGrpcAddress(address))
viper.Set("grpc.client.address", workerGrpcAddress)
}
// app
master := apps.NewWorker(opts...)
// start
apps.Start(master)
},
}