feat(runners): add explicit registration

This commit is contained in:
Denis Gukov 2024-09-29 21:07:15 +05:00
parent 1ca883f20c
commit 0078297d25
3 changed files with 54 additions and 20 deletions

View File

@ -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()
},
}

View File

@ -54,6 +54,24 @@ func (p *JobPool) hasRunningJobs() bool {
return false 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) { func (p *JobPool) Unregister() (err error) {
if util.Config.Runner.Token == "" { if util.Config.Runner.Token == "" {
@ -87,6 +105,11 @@ func (p *JobPool) Unregister() (err error) {
} }
func (p *JobPool) Run() { 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) queueTicker := time.NewTicker(5 * time.Second)
requestTimer := time.NewTicker(1 * time.Second) requestTimer := time.NewTicker(1 * time.Second)
p.runningJobs = make(map[int]*runningJob) p.runningJobs = make(map[int]*runningJob)
@ -96,18 +119,6 @@ func (p *JobPool) Run() {
requestTimer.Stop() requestTimer.Stop()
}() }()
for {
if p.tryRegisterRunner() {
log.Info("The runner has been started")
break
}
time.Sleep(5_000_000_000)
}
for { for {
select { select {
@ -231,12 +242,6 @@ func (p *JobPool) tryRegisterRunner() bool {
log.Info("Attempting to register on the server") 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 != "" { if util.Config.Runner.Token != "" {
p.token = &util.Config.Runner.Token p.token = &util.Config.Runner.Token
return true return true

View File

@ -195,11 +195,11 @@ type ConfigType struct {
UseRemoteRunner bool `json:"use_remote_runner" env:"SEMAPHORE_USE_REMOTE_RUNNER"` 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"` IntegrationAlias string `json:"global_integration_alias" env:"SEMAPHORE_INTEGRATION_ALIAS"`
Apps map[string]App `json:"apps" env:"SEMAPHORE_APPS"` Apps map[string]App `json:"apps" env:"SEMAPHORE_APPS"`
Runner RunnerSettings `json:"runner"`
} }
// Config exposes the application configuration storage for use in the application // Config exposes the application configuration storage for use in the application