2022-10-15 20:03:33 +08:00
|
|
|
package register
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
2023-02-15 16:51:14 +08:00
|
|
|
"strconv"
|
2022-11-24 11:55:52 +08:00
|
|
|
"strings"
|
2022-10-15 20:03:33 +08:00
|
|
|
|
2022-12-06 16:37:38 +08:00
|
|
|
runnerv1 "code.gitea.io/actions-proto-go/runner/v1"
|
2022-10-15 20:03:33 +08:00
|
|
|
"gitea.com/gitea/act_runner/client"
|
|
|
|
"gitea.com/gitea/act_runner/config"
|
|
|
|
"gitea.com/gitea/act_runner/core"
|
|
|
|
|
|
|
|
"github.com/bufbuild/connect-go"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2022-11-24 11:55:52 +08:00
|
|
|
func New(cli client.Client) *Register {
|
2022-10-15 20:03:33 +08:00
|
|
|
return &Register{
|
|
|
|
Client: cli,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Register struct {
|
|
|
|
Client client.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Register) Register(ctx context.Context, cfg config.Runner) (*core.Runner, error) {
|
2022-11-24 11:55:52 +08:00
|
|
|
labels := make([]string, len(cfg.Labels))
|
|
|
|
for i, v := range cfg.Labels {
|
|
|
|
labels[i] = strings.SplitN(v, ":", 2)[0]
|
|
|
|
}
|
2022-10-15 20:03:33 +08:00
|
|
|
// register new runner.
|
|
|
|
resp, err := p.Client.Register(ctx, connect.NewRequest(&runnerv1.RegisterRequest{
|
2022-11-24 11:55:52 +08:00
|
|
|
Name: cfg.Name,
|
|
|
|
Token: cfg.Token,
|
|
|
|
AgentLabels: labels,
|
2022-10-15 20:03:33 +08:00
|
|
|
}))
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("poller: cannot register new runner")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
data := &core.Runner{
|
2023-02-15 16:51:14 +08:00
|
|
|
ID: resp.Msg.Runner.Id,
|
|
|
|
UUID: resp.Msg.Runner.Uuid,
|
|
|
|
Name: resp.Msg.Runner.Name,
|
|
|
|
Token: resp.Msg.Runner.Token,
|
|
|
|
Address: p.Client.Address(),
|
|
|
|
Insecure: strconv.FormatBool(p.Client.Insecure()),
|
|
|
|
Labels: cfg.Labels,
|
2022-10-15 20:03:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
file, err := json.MarshalIndent(data, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("poller: cannot marshal the json input")
|
|
|
|
return data, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// store runner config in .runner file
|
|
|
|
return data, os.WriteFile(cfg.File, file, 0o644)
|
|
|
|
}
|