chore(runner): refactor register flow

Signed-off-by: Bo-Yi.Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi.Wu
2022-10-02 12:33:17 +08:00
committed by Jason Song
parent 7486d2ab91
commit 3a1503138b
8 changed files with 165 additions and 97 deletions

View File

@ -1,74 +0,0 @@
package cmd
import (
"fmt"
"os"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
)
type (
// Config provides the system configuration.
Config struct {
Debug bool `envconfig:"GITEA_DEBUG"`
Trace bool `envconfig:"GITEA_TRACE"`
Client struct {
Address string `ignored:"true"`
Proto string `envconfig:"GITEA_RPC_PROTO" default:"http"`
Host string `envconfig:"GITEA_RPC_HOST" required:"true"`
Secret string `envconfig:"GITEA_RPC_SECRET" required:"true"`
SkipVerify bool `envconfig:"GITEA_RPC_SKIP_VERIFY"`
GRPC bool `envconfig:"GITEA_RPC_GRPC" default:"true"`
GRPCWeb bool `envconfig:"GITEA_RPC_GRPC_WEB"`
}
Runner struct {
Name string `envconfig:"GITEA_RUNNER_NAME"`
Capacity int `envconfig:"GITEA_RUNNER_CAPACITY" default:"2"`
Procs int64 `envconfig:"GITEA_RUNNER_MAX_PROCS"`
Environ map[string]string `envconfig:"GITEA_RUNNER_ENVIRON"`
EnvFile string `envconfig:"GITEA_RUNNER_ENV_FILE"`
}
Platform struct {
OS string `envconfig:"GITEA_PLATFORM_OS" default:"linux"`
Arch string `envconfig:"GITEA_PLATFORM_ARCH" default:"amd64"`
Kernel string `envconfig:"GITEA_PLATFORM_KERNEL"`
Variant string `envconfig:"GITEA_PLATFORM_VARIANT"`
}
}
)
// fromEnviron returns the settings from the environment.
func fromEnviron() (Config, error) {
cfg := Config{}
err := envconfig.Process("", &cfg)
// runner config
if cfg.Runner.Environ == nil {
cfg.Runner.Environ = map[string]string{}
}
if cfg.Runner.Name == "" {
cfg.Runner.Name, _ = os.Hostname()
}
cfg.Client.Address = fmt.Sprintf(
"%s://%s",
cfg.Client.Proto,
cfg.Client.Host,
)
if file := cfg.Runner.EnvFile; file != "" {
envs, err := godotenv.Read(file)
if err != nil {
return cfg, err
}
for k, v := range envs {
cfg.Runner.Environ[k] = v
}
}
return cfg, err
}

View File

@ -5,6 +5,7 @@ import (
"time"
"gitea.com/gitea/act_runner/client"
"gitea.com/gitea/act_runner/config"
"gitea.com/gitea/act_runner/engine"
"gitea.com/gitea/act_runner/poller"
"gitea.com/gitea/act_runner/runtime"
@ -22,7 +23,7 @@ func runDaemon(ctx context.Context, task *runtime.Task) func(cmd *cobra.Command,
log.Infoln("Starting runner daemon")
_ = godotenv.Load(task.Input.EnvFile)
cfg, err := fromEnviron()
cfg, err := config.FromEnviron()
if err != nil {
log.WithError(err).
Fatalln("invalid configuration")
@ -79,9 +80,9 @@ func runDaemon(ctx context.Context, task *runtime.Task) func(cmd *cobra.Command,
cli,
runner.Run,
&client.Filter{
OS: cfg.Platform.OS,
Arch: cfg.Platform.Arch,
Capacity: cfg.Runner.Capacity,
OS: cfg.Platform.OS,
Arch: cfg.Platform.Arch,
Labels: cfg.Runner.Labels,
},
)
@ -92,6 +93,11 @@ func runDaemon(ctx context.Context, task *runtime.Task) func(cmd *cobra.Command,
WithField("arch", cfg.Platform.Arch).
Infoln("polling the remote server")
// register new runner
if err := poller.Register(ctx, cfg.Runner); err != nil {
return err
}
return poller.Poll(ctx, cfg.Runner.Capacity)
})

View File

@ -5,8 +5,10 @@ import (
"os"
"strconv"
"gitea.com/gitea/act_runner/config"
"gitea.com/gitea/act_runner/engine"
"gitea.com/gitea/act_runner/runtime"
"github.com/mattn/go-isatty"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
@ -15,7 +17,7 @@ import (
const version = "0.1"
// initLogging setup the global logrus logger.
func initLogging(cfg Config) {
func initLogging(cfg config.Config) {
isTerm := isatty.IsTerminal(os.Stdout.Fd())
log.SetFormatter(&log.TextFormatter{
DisableColors: !isTerm,
@ -76,7 +78,7 @@ func runRoot(ctx context.Context, task *runtime.Task) func(cmd *cobra.Command, a
}
task.BuildID, _ = strconv.ParseInt(jobID, 10, 64)
task.Run(ctx, nil)
_ = task.Run(ctx, nil)
return nil
}
}