serve: add option to choose which components to run

This commit is contained in:
Simone Gotti 2019-05-06 17:49:57 +02:00
parent afae185e11
commit 4d19ce1633
1 changed files with 84 additions and 24 deletions

View File

@ -24,8 +24,10 @@ import (
"github.com/sorintlab/agola/internal/services/gateway" "github.com/sorintlab/agola/internal/services/gateway"
"github.com/sorintlab/agola/internal/services/gitserver" "github.com/sorintlab/agola/internal/services/gitserver"
"github.com/sorintlab/agola/internal/services/runservice/executor" "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" rsscheduler "github.com/sorintlab/agola/internal/services/runservice/scheduler"
"github.com/sorintlab/agola/internal/services/scheduler" "github.com/sorintlab/agola/internal/services/scheduler"
"github.com/sorintlab/agola/internal/util"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -37,6 +39,16 @@ var (
gatewayURL = fmt.Sprintf("http://%s:%d", "localhost", 8000) gatewayURL = fmt.Sprintf("http://%s:%d", "localhost", 8000)
) )
var componentsNames = []string{
"all",
"gateway",
"scheduler",
"runservicescheduler",
"runserviceexecutor",
"configstore",
"gitserver",
}
var cmdServe = &cobra.Command{ var cmdServe = &cobra.Command{
Use: "serve", Use: "serve",
Short: "serve", Short: "serve",
@ -50,6 +62,7 @@ var cmdServe = &cobra.Command{
type serveOptions struct { type serveOptions struct {
config string config string
components []string
embeddedEtcd bool embeddedEtcd bool
embeddedEtcdDataDir string embeddedEtcdDataDir string
} }
@ -60,6 +73,7 @@ func init() {
flags := cmdServe.PersistentFlags() flags := cmdServe.PersistentFlags()
flags.StringVar(&serveOpts.config, "config", "./config.yml", "config file path") 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.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") 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 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 { func serve(cmd *cobra.Command, args []string) error {
ctx := context.Background() 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) c, err := config.Parse(serveOpts.config)
if err != nil { if err != nil {
return errors.Wrapf(err, "config error") 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) var rssched *rsscheduler.Scheduler
if isComponentEnabled("runservicescheduler") {
rssched, err = rsscheduler.NewScheduler(ctx, &c.RunServiceScheduler)
if err != nil { if err != nil {
return errors.Wrapf(err, "failed to start run service scheduler") return errors.Wrapf(err, "failed to start run service scheduler")
} }
}
rsex1, err := executor.NewExecutor(&c.RunServiceExecutor) var rsex *rsexecutor.Executor
if isComponentEnabled("runserviceexecutor") {
rsex, err = executor.NewExecutor(&c.RunServiceExecutor)
if err != nil { if err != nil {
return errors.Wrapf(err, "failed to start run service executor") return errors.Wrapf(err, "failed to start run service executor")
} }
}
cs, err := configstore.NewConfigStore(ctx, &c.ConfigStore) var cs *configstore.ConfigStore
if isComponentEnabled("configstore") {
cs, err = configstore.NewConfigStore(ctx, &c.ConfigStore)
if err != nil { if err != nil {
return errors.Wrapf(err, "failed to start config store") return errors.Wrapf(err, "failed to start config store")
} }
}
sched1, err := scheduler.NewScheduler(&c.Scheduler) var sched *scheduler.Scheduler
if isComponentEnabled("scheduler") {
sched, err = scheduler.NewScheduler(&c.Scheduler)
if err != nil { if err != nil {
return errors.Wrapf(err, "failed to start scheduler") return errors.Wrapf(err, "failed to start scheduler")
} }
}
gateway, err := gateway.NewGateway(c) var gw *gateway.Gateway
if isComponentEnabled("gateway") {
gw, err = gateway.NewGateway(c)
if err != nil { if err != nil {
return errors.Wrapf(err, "failed to start gateway") return errors.Wrapf(err, "failed to start gateway")
} }
}
gitserver, err := gitserver.NewGitServer(&c.GitServer) var gs *gitserver.GitServer
if isComponentEnabled("gitserver") {
gs, err = gitserver.NewGitServer(&c.GitServer)
if err != nil { if err != nil {
return errors.Wrapf(err, "failed to start git server") return errors.Wrapf(err, "failed to start git server")
} }
}
errCh := make(chan error) errCh := make(chan error)
go func() { errCh <- rsex1.Run(ctx) }() if rssched != nil {
go func() { errCh <- rssched1.Run(ctx) }() go func() { errCh <- rssched.Run(ctx) }()
}
if rsex != nil {
go func() { errCh <- rsex.Run(ctx) }()
}
if cs != nil {
go func() { errCh <- cs.Run(ctx) }() go func() { errCh <- cs.Run(ctx) }()
go func() { errCh <- gateway.Run(ctx) }() }
go func() { errCh <- gitserver.Run(ctx) }() if sched != nil {
go func() { errCh <- sched1.Run(ctx) }() 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 return <-errCh
} }