2023-04-04 08:32:04 -05:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2023-06-05 08:11:23 -05:00
|
|
|
"path"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-04-04 08:32:04 -05:00
|
|
|
|
|
|
|
"github.com/mattn/go-isatty"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"gitea.com/gitea/act_runner/internal/app/poll"
|
|
|
|
"gitea.com/gitea/act_runner/internal/app/run"
|
|
|
|
"gitea.com/gitea/act_runner/internal/pkg/client"
|
|
|
|
"gitea.com/gitea/act_runner/internal/pkg/config"
|
|
|
|
"gitea.com/gitea/act_runner/internal/pkg/envcheck"
|
|
|
|
"gitea.com/gitea/act_runner/internal/pkg/labels"
|
|
|
|
"gitea.com/gitea/act_runner/internal/pkg/ver"
|
|
|
|
)
|
|
|
|
|
|
|
|
func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command, args []string) error {
|
|
|
|
return func(cmd *cobra.Command, args []string) error {
|
|
|
|
cfg, err := config.LoadDefault(*configFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid configuration: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
initLogging(cfg)
|
2023-06-05 08:11:23 -05:00
|
|
|
log.Infoln("Starting runner daemon")
|
2023-04-04 08:32:04 -05:00
|
|
|
|
|
|
|
reg, err := config.LoadRegistration(cfg.Runner.File)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
log.Error("registration file not found, please register the runner first")
|
|
|
|
return err
|
|
|
|
} else if err != nil {
|
|
|
|
return fmt.Errorf("failed to load registration file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ls := labels.Labels{}
|
|
|
|
for _, l := range reg.Labels {
|
|
|
|
label, err := labels.Parse(l)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Warnf("ignored invalid label %q", l)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ls = append(ls, label)
|
|
|
|
}
|
|
|
|
if len(ls) == 0 {
|
|
|
|
log.Warn("no labels configured, runner may not be able to pick up jobs")
|
|
|
|
}
|
|
|
|
|
|
|
|
if ls.RequireDocker() {
|
|
|
|
if err := envcheck.CheckIfDockerRunning(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cli := client.New(
|
|
|
|
reg.Address,
|
|
|
|
cfg.Runner.Insecure,
|
|
|
|
reg.UUID,
|
|
|
|
reg.Token,
|
|
|
|
ver.Version(),
|
|
|
|
)
|
|
|
|
|
|
|
|
runner := run.NewRunner(cfg, reg, cli)
|
|
|
|
poller := poll.New(cfg, cli, runner)
|
|
|
|
|
|
|
|
poller.Poll(ctx)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// initLogging setup the global logrus logger.
|
|
|
|
func initLogging(cfg *config.Config) {
|
|
|
|
isTerm := isatty.IsTerminal(os.Stdout.Fd())
|
2023-06-05 08:11:23 -05:00
|
|
|
format := &log.TextFormatter{
|
2023-04-04 08:32:04 -05:00
|
|
|
DisableColors: !isTerm,
|
|
|
|
FullTimestamp: true,
|
2023-06-05 08:11:23 -05:00
|
|
|
}
|
|
|
|
log.SetFormatter(format)
|
2023-04-04 08:32:04 -05:00
|
|
|
|
|
|
|
if l := cfg.Log.Level; l != "" {
|
|
|
|
level, err := log.ParseLevel(l)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).
|
|
|
|
Errorf("invalid log level: %q", l)
|
|
|
|
}
|
2023-06-05 08:11:23 -05:00
|
|
|
|
|
|
|
// debug level
|
|
|
|
if level == log.DebugLevel {
|
|
|
|
log.SetReportCaller(true)
|
|
|
|
format.CallerPrettyfier = func(f *runtime.Frame) (string, string) {
|
|
|
|
// get function name
|
|
|
|
s := strings.Split(f.Function, ".")
|
|
|
|
funcname := "[" + s[len(s)-1] + "]"
|
|
|
|
// get file name and line number
|
|
|
|
_, filename := path.Split(f.File)
|
|
|
|
filename = "[" + filename + ":" + strconv.Itoa(f.Line) + "]"
|
|
|
|
return funcname, filename
|
|
|
|
}
|
|
|
|
log.SetFormatter(format)
|
|
|
|
}
|
|
|
|
|
2023-04-04 08:32:04 -05:00
|
|
|
if log.GetLevel() != level {
|
|
|
|
log.Infof("log level changed to %v", level)
|
|
|
|
log.SetLevel(level)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|