diff --git a/go.mod b/go.mod index 4b8b13ca8e..4a753a9ae3 100644 --- a/go.mod +++ b/go.mod @@ -6,8 +6,8 @@ require ( code.gitea.io/gitea-vet v0.2.2-0.20220122151748-48ebc902541b code.gitea.io/sdk/gitea v0.15.1 codeberg.org/gusted/mcaptcha v0.0.0-20220723083913-4f3072e1d570 - gitea.com/gitea/proto-go v0.0.0-20220817054638-17fb0016dd41 - gitea.com/go-chi/binding v0.0.0-20221013104517-b29891619681 + gitea.com/gitea/proto-go v0.0.0-20220828031749-616e40329b57 + gitea.com/go-chi/binding v0.0.0-20220309004920-114340dabecb gitea.com/go-chi/cache v0.2.0 gitea.com/go-chi/captcha v0.0.0-20211013065431-70641c1a35d5 gitea.com/go-chi/session v0.0.0-20211218221615-e3605d8b28b8 diff --git a/go.sum b/go.sum index 680c382a32..58da3fc581 100644 --- a/go.sum +++ b/go.sum @@ -89,10 +89,10 @@ git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGy git.apache.org/thrift.git v0.12.0/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg= git.sr.ht/~mariusor/go-xsd-duration v0.0.0-20220703122237-02e73435a078 h1:cliQ4HHsCo6xi2oWZYKWW4bly/Ory9FuTpFPRxj/mAg= git.sr.ht/~mariusor/go-xsd-duration v0.0.0-20220703122237-02e73435a078/go.mod h1:g/V2Hjas6Z1UHUp4yIx6bATpNzJ7DYtD0FG3+xARWxs= -gitea.com/gitea/proto-go v0.0.0-20220817054638-17fb0016dd41 h1:FIGF6szYd3lBIwvbeedfU5Lc7uG1Xpzi7bkktS6Vdvg= -gitea.com/gitea/proto-go v0.0.0-20220817054638-17fb0016dd41/go.mod h1:hD8YwSHusjwjEEgubW6XFvnZuNhMZTHz6lwjfltEt/Y= -gitea.com/go-chi/binding v0.0.0-20221013104517-b29891619681 h1:MMSPgnVULVwV9kEBgvyEUhC9v/uviZ55hPJEMjpbNR4= -gitea.com/go-chi/binding v0.0.0-20221013104517-b29891619681/go.mod h1:77TZu701zMXWJFvB8gvTbQ92zQ3DQq/H7l5wAEjQRKc= +gitea.com/gitea/proto-go v0.0.0-20220828031749-616e40329b57 h1:eVM6m3h5KpmJM2+LEqroENFaMs2kAo8QNIPyMgho9jg= +gitea.com/gitea/proto-go v0.0.0-20220828031749-616e40329b57/go.mod h1:hD8YwSHusjwjEEgubW6XFvnZuNhMZTHz6lwjfltEt/Y= +gitea.com/go-chi/binding v0.0.0-20220309004920-114340dabecb h1:Yy0Bxzc8R2wxiwXoG/rECGplJUSpXqCsog9PuJFgiHs= +gitea.com/go-chi/binding v0.0.0-20220309004920-114340dabecb/go.mod h1:77TZu701zMXWJFvB8gvTbQ92zQ3DQq/H7l5wAEjQRKc= gitea.com/go-chi/cache v0.0.0-20210110083709-82c4c9ce2d5e/go.mod h1:k2V/gPDEtXGjjMGuBJiapffAXTv76H4snSmlJRLUhH0= gitea.com/go-chi/cache v0.2.0 h1:E0npuTfDW6CT1yD8NMDVc1SK6IeRjfmRL2zlEsCEd7w= gitea.com/go-chi/cache v0.2.0/go.mod h1:iQlVK2aKTZ/rE9UcHyz9pQWGvdP9i1eI2spOpzgCrtE= diff --git a/models/bots/runner.go b/models/bots/runner.go index 24ac953492..ef7f5d3773 100644 --- a/models/bots/runner.go +++ b/models/bots/runner.go @@ -5,6 +5,7 @@ package bots import ( + "context" "fmt" "code.gitea.io/gitea/models/db" @@ -17,11 +18,16 @@ import ( // ErrRunnerNotExist represents an error for bot runner not exist type ErrRunnerNotExist struct { - UUID string + UUID string + Token string } func (err ErrRunnerNotExist) Error() string { - return fmt.Sprintf("Bot runner [%s] is not exist", err.UUID) + if err.UUID != "" { + return fmt.Sprintf("Bot runner ID [%s] is not exist", err.UUID) + } + + return fmt.Sprintf("Bot runner token [%s] is not exist", err.Token) } // Runner represents runner machines @@ -40,6 +46,7 @@ type Runner struct { Base int // 0 native 1 docker 2 virtual machine RepoRange string // glob match which repositories could use this runner Token string + Capacity int64 LastOnline timeutil.TimeStamp `xorm:"index"` Created timeutil.TimeStamp `xorm:"created"` } @@ -128,6 +135,32 @@ func GetRunnerByUUID(uuid string) (*Runner, error) { return &runner, nil } +// GetRunnerByToken returns a bot runner via token +func GetRunnerByToken(token string) (*Runner, error) { + var runner Runner + has, err := db.GetEngine(db.DefaultContext).Where("token=?", token).Get(&runner) + if err != nil { + return nil, err + } else if !has { + return nil, ErrRunnerNotExist{ + UUID: "", + } + } + return &runner, nil +} + +// UpdateRunner updates runner's information. +func UpdateRunner(ctx context.Context, r *Runner, cols ...string) (err error) { + e := db.GetEngine(ctx) + + if len(cols) == 0 { + _, err = e.ID(r.ID).AllCols().Update(r) + } else { + _, err = e.ID(r.ID).Cols(cols...).Update(r) + } + return err +} + // FindRunnersByRepoID returns all workers for the repository func FindRunnersByRepoID(repoID int64) ([]*Runner, error) { var runners []*Runner diff --git a/routers/api/bots/runner/runner.go b/routers/api/bots/runner/runner.go index e07b7ce67a..1a84cb7c88 100644 --- a/routers/api/bots/runner/runner.go +++ b/routers/api/bots/runner/runner.go @@ -6,27 +6,55 @@ package runner import ( "context" + "errors" + bots_model "code.gitea.io/gitea/models/bots" "code.gitea.io/gitea/modules/log" runnerv1 "gitea.com/gitea/proto-go/runner/v1" + "gitea.com/gitea/proto-go/runner/v1/runnerv1connect" "github.com/bufbuild/connect-go" ) -type Service struct{} +type Service struct { + runnerv1connect.UnimplementedRunnerServiceHandler +} -func (s *Service) Connect( +func (s *Service) Register( ctx context.Context, - req *connect.Request[runnerv1.ConnectRequest], -) (*connect.Response[runnerv1.ConnectResponse], error) { + req *connect.Request[runnerv1.RegisterRequest], +) (*connect.Response[runnerv1.RegisterResponse], error) { log.Info("Request headers: %v", req.Header()) - res := connect.NewResponse(&runnerv1.ConnectResponse{ - Stage: &runnerv1.Stage{ - RunnerUuid: "foobar", - BuildUuid: "foobar", + + token := req.Header().Get("X-Runner-Token") + log.Info("token: %v", token) + + if token == "" { + return nil, errors.New("missing runner token") + } + + runner, err := bots_model.GetRunnerByToken(token) + if err != nil { + return nil, errors.New("runner not found") + } + + // update runner information + runner.Arch = req.Msg.Arch + runner.OS = req.Msg.Os + runner.Capacity = req.Msg.Capacity + if err := bots_model.UpdateRunner(ctx, runner, []string{"arch", "os", "capacity"}...); err != nil { + return nil, errors.New("can't update runner") + } + + res := connect.NewResponse(&runnerv1.RegisterResponse{ + Runner: &runnerv1.Runner{ + Uuid: runner.UUID, + Os: req.Msg.Os, + Arch: req.Msg.Arch, + Capacity: req.Msg.Capacity, }, }) - res.Header().Set("Gitea-Version", "runnerv1") + return res, nil }