diff --git a/cli/cmd/runner_register.go b/cli/cmd/runner_register.go new file mode 100644 index 00000000..d83efce6 --- /dev/null +++ b/cli/cmd/runner_register.go @@ -0,0 +1,29 @@ +package cmd + +import ( + "github.com/ansible-semaphore/semaphore/services/runners" + "github.com/ansible-semaphore/semaphore/util" + "github.com/spf13/cobra" +) + +func init() { + runnerCmd.AddCommand(runnerRegisterCmd) +} + +func registerRunner() { + util.ConfigInit(configPath, noConfig) + + taskPool := runners.JobPool{} + err := taskPool.Register() + if err != nil { + panic(err) + } +} + +var runnerRegisterCmd = &cobra.Command{ + Use: "register", + Short: "Register runner on the server", + Run: func(cmd *cobra.Command, args []string) { + registerRunner() + }, +} diff --git a/services/runners/job_pool.go b/services/runners/job_pool.go index e641f78d..b5ae67b8 100644 --- a/services/runners/job_pool.go +++ b/services/runners/job_pool.go @@ -54,6 +54,24 @@ func (p *JobPool) hasRunningJobs() bool { return false } +func (p *JobPool) Register() (err error) { + if util.Config.Runner.RegistrationToken == "" { + return fmt.Errorf("runner registration token required") + } + + if util.Config.Runner.TokenFile == "" { + return fmt.Errorf("runner token file required") + } + + ok := p.tryRegisterRunner() + + if !ok { + return fmt.Errorf("runner registration failed") + } + + return +} + func (p *JobPool) Unregister() (err error) { if util.Config.Runner.Token == "" { @@ -87,6 +105,11 @@ func (p *JobPool) Unregister() (err error) { } func (p *JobPool) Run() { + + if p.token == nil { + panic("runner token required. Please register runner first or create it from web interface.") + } + queueTicker := time.NewTicker(5 * time.Second) requestTimer := time.NewTicker(1 * time.Second) p.runningJobs = make(map[int]*runningJob) @@ -96,18 +119,6 @@ func (p *JobPool) Run() { requestTimer.Stop() }() - for { - - if p.tryRegisterRunner() { - - log.Info("The runner has been started") - - break - } - - time.Sleep(5_000_000_000) - } - for { select { @@ -231,12 +242,6 @@ func (p *JobPool) tryRegisterRunner() bool { log.Info("Attempting to register on the server") - //config, err := util.LoadRunnerSettings(util.Config.Runner.ConfigFile) - // - //if err != nil { - // panic(err) - //} - if util.Config.Runner.Token != "" { p.token = &util.Config.Runner.Token return true diff --git a/util/config.go b/util/config.go index 43ec5455..37837839 100644 --- a/util/config.go +++ b/util/config.go @@ -195,11 +195,11 @@ type ConfigType struct { UseRemoteRunner bool `json:"use_remote_runner" env:"SEMAPHORE_USE_REMOTE_RUNNER"` - Runner RunnerSettings `json:"runner"` - IntegrationAlias string `json:"global_integration_alias" env:"SEMAPHORE_INTEGRATION_ALIAS"` Apps map[string]App `json:"apps" env:"SEMAPHORE_APPS"` + + Runner RunnerSettings `json:"runner"` } // Config exposes the application configuration storage for use in the application