From 4d19ce163363313e0e58667adbc595d81e23328e Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Mon, 6 May 2019 17:49:57 +0200 Subject: [PATCH] serve: add option to choose which components to run --- cmd/agola/cmd/serve.go | 108 ++++++++++++++++++++++++++++++++--------- 1 file changed, 84 insertions(+), 24 deletions(-) diff --git a/cmd/agola/cmd/serve.go b/cmd/agola/cmd/serve.go index 75969e9..b23f048 100644 --- a/cmd/agola/cmd/serve.go +++ b/cmd/agola/cmd/serve.go @@ -24,8 +24,10 @@ import ( "github.com/sorintlab/agola/internal/services/gateway" "github.com/sorintlab/agola/internal/services/gitserver" "github.com/sorintlab/agola/internal/services/runservice/executor" + rsexecutor "github.com/sorintlab/agola/internal/services/runservice/executor" rsscheduler "github.com/sorintlab/agola/internal/services/runservice/scheduler" "github.com/sorintlab/agola/internal/services/scheduler" + "github.com/sorintlab/agola/internal/util" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -37,6 +39,16 @@ var ( gatewayURL = fmt.Sprintf("http://%s:%d", "localhost", 8000) ) +var componentsNames = []string{ + "all", + "gateway", + "scheduler", + "runservicescheduler", + "runserviceexecutor", + "configstore", + "gitserver", +} + var cmdServe = &cobra.Command{ Use: "serve", Short: "serve", @@ -50,6 +62,7 @@ var cmdServe = &cobra.Command{ type serveOptions struct { config string + components []string embeddedEtcd bool embeddedEtcdDataDir string } @@ -60,6 +73,7 @@ func init() { flags := cmdServe.PersistentFlags() flags.StringVar(&serveOpts.config, "config", "./config.yml", "config file path") + flags.StringSliceVar(&serveOpts.components, "components", []string{}, `list of components to start (specify "all" to start all components)`) flags.BoolVar(&serveOpts.embeddedEtcd, "embedded-etcd", false, "start and use an embedded etcd, only for testing purpose") flags.StringVar(&serveOpts.embeddedEtcdDataDir, "embedded-etcd-data-dir", "/tmp/agola/etcd", "embedded etcd data dir, only for testing purpose") @@ -95,9 +109,25 @@ func embeddedEtcd(ctx context.Context) error { return nil } +func isComponentEnabled(name string) bool { + if util.StringInSlice(serveOpts.components, "all") { + return true + } + return util.StringInSlice(serveOpts.components, name) +} + func serve(cmd *cobra.Command, args []string) error { ctx := context.Background() + if len(serveOpts.components) == 0 { + return errors.Errorf("no enabled components") + } + for _, ec := range serveOpts.components { + if !util.StringInSlice(componentsNames, ec) { + return errors.Errorf("unkown component name %q", ec) + } + } + c, err := config.Parse(serveOpts.config) if err != nil { return errors.Wrapf(err, "config error") @@ -109,44 +139,74 @@ func serve(cmd *cobra.Command, args []string) error { } } - rssched1, err := rsscheduler.NewScheduler(ctx, &c.RunServiceScheduler) - if err != nil { - return errors.Wrapf(err, "failed to start run service scheduler") + var rssched *rsscheduler.Scheduler + if isComponentEnabled("runservicescheduler") { + rssched, err = rsscheduler.NewScheduler(ctx, &c.RunServiceScheduler) + if err != nil { + return errors.Wrapf(err, "failed to start run service scheduler") + } } - rsex1, err := executor.NewExecutor(&c.RunServiceExecutor) - if err != nil { - return errors.Wrapf(err, "failed to start run service executor") + var rsex *rsexecutor.Executor + if isComponentEnabled("runserviceexecutor") { + rsex, err = executor.NewExecutor(&c.RunServiceExecutor) + if err != nil { + return errors.Wrapf(err, "failed to start run service executor") + } } - cs, err := configstore.NewConfigStore(ctx, &c.ConfigStore) - if err != nil { - return errors.Wrapf(err, "failed to start config store") + var cs *configstore.ConfigStore + if isComponentEnabled("configstore") { + cs, err = configstore.NewConfigStore(ctx, &c.ConfigStore) + if err != nil { + return errors.Wrapf(err, "failed to start config store") + } } - sched1, err := scheduler.NewScheduler(&c.Scheduler) - if err != nil { - return errors.Wrapf(err, "failed to start scheduler") + var sched *scheduler.Scheduler + if isComponentEnabled("scheduler") { + sched, err = scheduler.NewScheduler(&c.Scheduler) + if err != nil { + return errors.Wrapf(err, "failed to start scheduler") + } } - gateway, err := gateway.NewGateway(c) - if err != nil { - return errors.Wrapf(err, "failed to start gateway") + var gw *gateway.Gateway + if isComponentEnabled("gateway") { + gw, err = gateway.NewGateway(c) + if err != nil { + return errors.Wrapf(err, "failed to start gateway") + } } - gitserver, err := gitserver.NewGitServer(&c.GitServer) - if err != nil { - return errors.Wrapf(err, "failed to start git server") + var gs *gitserver.GitServer + if isComponentEnabled("gitserver") { + gs, err = gitserver.NewGitServer(&c.GitServer) + if err != nil { + return errors.Wrapf(err, "failed to start git server") + } } errCh := make(chan error) - go func() { errCh <- rsex1.Run(ctx) }() - go func() { errCh <- rssched1.Run(ctx) }() - go func() { errCh <- cs.Run(ctx) }() - go func() { errCh <- gateway.Run(ctx) }() - go func() { errCh <- gitserver.Run(ctx) }() - go func() { errCh <- sched1.Run(ctx) }() + if rssched != nil { + go func() { errCh <- rssched.Run(ctx) }() + } + if rsex != nil { + go func() { errCh <- rsex.Run(ctx) }() + } + if cs != nil { + go func() { errCh <- cs.Run(ctx) }() + } + if sched != nil { + go func() { errCh <- sched.Run(ctx) }() + } + if gw != nil { + go func() { errCh <- gw.Run(ctx) }() + } + if gs != nil { + go func() { errCh <- gs.Run(ctx) }() + } return <-errCh }