From 378966e45ff4d26f2ab2ba7a17944067978c097f Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 16 Oct 2022 23:51:53 +0800 Subject: [PATCH] add forgeinstance --- cmd/daemon.go | 11 ++++++----- cmd/root.go | 4 ++-- config/config.go | 12 +++++++----- core/runner.go | 9 +++++---- register/register.go | 1 + runtime/runtime.go | 9 +++++---- runtime/task.go | 4 ++-- 7 files changed, 28 insertions(+), 22 deletions(-) diff --git a/cmd/daemon.go b/cmd/daemon.go index 4dbbadf..fe59232 100644 --- a/cmd/daemon.go +++ b/cmd/daemon.go @@ -20,11 +20,11 @@ import ( "golang.org/x/sync/errgroup" ) -func runDaemon(ctx context.Context, task *runtime.Task) func(cmd *cobra.Command, args []string) error { +func runDaemon(ctx context.Context, envFile string) func(cmd *cobra.Command, args []string) error { return func(cmd *cobra.Command, args []string) error { log.Infoln("Starting runner daemon") - _ = godotenv.Load(task.Input.EnvFile) + _ = godotenv.Load(envFile) cfg, err := config.FromEnviron() if err != nil { log.WithError(err). @@ -101,9 +101,10 @@ func runDaemon(ctx context.Context, task *runtime.Task) func(cmd *cobra.Command, ) runner := &runtime.Runner{ - Client: cli, - Machine: cfg.Runner.Name, - Environ: cfg.Runner.Environ, + Client: cli, + Machine: cfg.Runner.Name, + ForgeInstance: cfg.ForgeInstance, + Environ: cfg.Runner.Environ, } poller := poller.New( diff --git a/cmd/root.go b/cmd/root.go index 09bab0d..8933a31 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -33,7 +33,7 @@ func initLogging(cfg config.Config) { } func Execute(ctx context.Context) { - task := runtime.NewTask(0, nil) + task := runtime.NewTask("gitea", 0, nil) // ./act_runner rootCmd := &cobra.Command{ @@ -54,7 +54,7 @@ func Execute(ctx context.Context) { Aliases: []string{"daemon"}, Use: "execute runner daemon", Args: cobra.MaximumNArgs(1), - RunE: runDaemon(ctx, task), + RunE: runDaemon(ctx, task.Input.EnvFile), } // add all command rootCmd.AddCommand(daemonCmd) diff --git a/config/config.go b/config/config.go index edcf8ec..db560c6 100644 --- a/config/config.go +++ b/config/config.go @@ -16,11 +16,12 @@ import ( type ( // Config provides the system configuration. Config struct { - Debug bool `envconfig:"GITEA_DEBUG"` - Trace bool `envconfig:"GITEA_TRACE"` - Client Client - Runner Runner - Platform Platform + Debug bool `envconfig:"GITEA_DEBUG"` + Trace bool `envconfig:"GITEA_TRACE"` + Client Client + Runner Runner + Platform Platform + ForgeInstance string } Client struct { @@ -68,6 +69,7 @@ func FromEnviron() (Config, error) { if runner.UUID != "" { cfg.Runner.UUID = runner.UUID } + cfg.ForgeInstance = runner.ForgeInstance } // runner config diff --git a/core/runner.go b/core/runner.go index d4ca3b7..94093a8 100644 --- a/core/runner.go +++ b/core/runner.go @@ -4,8 +4,9 @@ const UUIDHeader = "x-runner-uuid" // Runner struct type Runner struct { - ID int64 `json:"id"` - UUID string `json:"uuid"` - Name string `json:"name"` - Token string `json:"token"` + ID int64 `json:"id"` + UUID string `json:"uuid"` + Name string `json:"name"` + Token string `json:"token"` + ForgeInstance string `json:"forge_instance"` } diff --git a/register/register.go b/register/register.go index fb8e285..f8b9286 100644 --- a/register/register.go +++ b/register/register.go @@ -46,6 +46,7 @@ func (p *Register) Register(ctx context.Context, cfg config.Runner) (*core.Runne UUID: resp.Msg.Runner.Uuid, Name: resp.Msg.Runner.Name, Token: resp.Msg.Runner.Token, + // ForgeInstance: resp.Msg.Runner.ForgeInstance, TODO: add me } file, err := json.MarshalIndent(data, "", " ") diff --git a/runtime/runtime.go b/runtime/runtime.go index 18bee20..71ed4f8 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -12,9 +12,10 @@ import ( // Runner runs the pipeline. type Runner struct { - Machine string - Environ map[string]string - Client client.Client + Machine string + ForgeInstance string + Environ map[string]string + Client client.Client } // Run runs the pipeline stage. @@ -48,5 +49,5 @@ func (s *Runner) Run(ctx context.Context, task *runnerv1.Task) error { l.Info("update runner status to idle") }() - return NewTask(task.Id, s.Client).Run(ctx, task) + return NewTask(s.ForgeInstance, task.Id, s.Client).Run(ctx, task) } diff --git a/runtime/task.go b/runtime/task.go index 15b27f9..fb15b5e 100644 --- a/runtime/task.go +++ b/runtime/task.go @@ -86,11 +86,11 @@ type Task struct { } // NewTask creates a new task -func NewTask(buildID int64, client client.Client) *Task { +func NewTask(forgeInstance string, buildID int64, client client.Client) *Task { task := &Task{ Input: &TaskInput{ reuseContainers: true, - ForgeInstance: "gitea", + ForgeInstance: forgeInstance, }, BuildID: buildID,