From 4d7ef95d402d3e53d175e262e01af224f4c58da7 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sat, 13 Aug 2022 22:41:01 +0800 Subject: [PATCH] chore(proto): Add ping request. Signed-off-by: Bo-Yi Wu --- .gitignore | 3 +- client/client.go | 11 +++++++ client/http.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++ cmd/config.go | 49 +++++++++++++++++++++++++++++- cmd/damon.go | 29 +++++++++++++++++- engine/options.go | 2 +- go.mod | 6 +++- go.sum | 10 +++++-- 8 files changed, 179 insertions(+), 7 deletions(-) create mode 100644 client/client.go create mode 100644 client/http.go diff --git a/.gitignore b/.gitignore index f3f4db5..0a9cdb8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -act_runner \ No newline at end of file +act_runner +.env diff --git a/client/client.go b/client/client.go new file mode 100644 index 0000000..15fd42e --- /dev/null +++ b/client/client.go @@ -0,0 +1,11 @@ +package client + +import ( + "context" +) + +// A Client manages communication with the runner. +type Client interface { + // Ping sends a ping message to the server to test connectivity. + Ping(ctx context.Context, machine string) error +} diff --git a/client/http.go b/client/http.go new file mode 100644 index 0000000..d7efbde --- /dev/null +++ b/client/http.go @@ -0,0 +1,76 @@ +package client + +import ( + "context" + "crypto/tls" + "net" + "net/http" + "time" + + v1 "gitea.com/gitea/proto/gen/proto/v1" + "gitea.com/gitea/proto/gen/proto/v1/v1connect" + + "github.com/bufbuild/connect-go" + "golang.org/x/net/http2" +) + +// New returns a new runner client. +func New(endpoint, secret string, skipverify bool) *HTTPClient { + client := &HTTPClient{ + Endpoint: endpoint, + Secret: secret, + SkipVerify: skipverify, + } + + client.Client = &http.Client{ + Timeout: 5 * time.Second, + CheckRedirect: func(*http.Request, []*http.Request) error { + return http.ErrUseLastResponse + }, + Transport: &http2.Transport{ + AllowHTTP: true, + DialTLS: func(netw, addr string, cfg *tls.Config) (net.Conn, error) { + return net.Dial(netw, addr) + }, + }, + } + + if skipverify { + client.Client = &http.Client{ + CheckRedirect: func(*http.Request, []*http.Request) error { + return http.ErrUseLastResponse + }, + Transport: &http.Transport{ + Proxy: http.ProxyFromEnvironment, + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: true, + }, + }, + } + } + return client +} + +// An HTTPClient manages communication with the runner API. +type HTTPClient struct { + Client *http.Client + Endpoint string + Secret string + SkipVerify bool +} + +// Ping sends a ping message to the server to test connectivity. +func (p *HTTPClient) Ping(ctx context.Context, machine string) error { + client := v1connect.NewPingServiceClient( + p.Client, + p.Endpoint, + ) + req := connect.NewRequest(&v1.PingRequest{ + Data: machine, + }) + + req.Header().Set("X-Gitea-Token", p.Secret) + + _, err := client.Ping(ctx, req) + return err +} diff --git a/cmd/config.go b/cmd/config.go index 1e1452a..e7964ae 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -1,12 +1,34 @@ package cmd -import "github.com/kelseyhightower/envconfig" +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"` + } + + 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"` + } } ) @@ -14,5 +36,30 @@ type ( 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 } diff --git a/cmd/damon.go b/cmd/damon.go index d739c45..29485de 100644 --- a/cmd/damon.go +++ b/cmd/damon.go @@ -4,6 +4,7 @@ import ( "context" "time" + "gitea.com/gitea/act_runner/client" "gitea.com/gitea/act_runner/engine" "github.com/joho/godotenv" @@ -164,7 +165,33 @@ func runDaemon(ctx context.Context, input *Input) func(cmd *cobra.Command, args } time.Sleep(time.Second) } else { - log.Debugln("successfully pinged the docker daemon") + log.Infoln("successfully pinged the docker daemon") + break + } + } + + cli := client.New( + cfg.Client.Address, + cfg.Client.Secret, + cfg.Client.SkipVerify, + ) + + for { + err := cli.Ping(ctx, cfg.Runner.Name) + select { + case <-ctx.Done(): + return nil + default: + } + if ctx.Err() != nil { + break + } + if err != nil { + log.WithError(err). + Errorln("cannot ping the remote server") + time.Sleep(time.Second) + } else { + log.Infoln("successfully pinged the remote server") break } } diff --git a/engine/options.go b/engine/options.go index 791c6b1..46c6f9d 100644 --- a/engine/options.go +++ b/engine/options.go @@ -22,7 +22,7 @@ func WithClient(c client.APIClient) Option { }) } -// WithHidePull set custom client +// WithHidePull hide pull event. func WithHidePull(v bool) Option { return OptionFunc(func(q *Docker) { q.hidePull = v diff --git a/go.mod b/go.mod index 1bfd777..19a9e17 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,8 @@ module gitea.com/gitea/act_runner go 1.18 require ( + gitea.com/gitea/proto v0.0.0-20220813120843-ce4b5dd68c1f + github.com/bufbuild/connect-go v0.3.0 github.com/docker/docker v20.10.17+incompatible github.com/joho/godotenv v1.4.0 github.com/kelseyhightower/envconfig v1.4.0 @@ -10,6 +12,7 @@ require ( github.com/nektos/act v0.2.26 github.com/sirupsen/logrus v1.9.0 github.com/spf13/cobra v1.5.0 + golang.org/x/net v0.0.0-20220403103023-749bd193bc2b ) require ( @@ -58,10 +61,11 @@ require ( github.com/xanzy/ssh-agent v0.3.1 // indirect go.opencensus.io v0.23.0 // indirect golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29 // indirect - golang.org/x/net v0.0.0-20220403103023-749bd193bc2b // indirect golang.org/x/sync v0.0.0-20220513210516-0976fa681c29 // indirect golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect + golang.org/x/text v0.3.7 // indirect + google.golang.org/protobuf v1.28.1 // indirect gopkg.in/warnings.v0 v0.1.2 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 0327107..430ea39 100644 --- a/go.sum +++ b/go.sum @@ -25,6 +25,8 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= gitea.com/gitea/act v0.0.0-20220812010840-c467b06265ee h1:T0wftx4RaYqbTH4t0A7bXGXxemZloCrjReA7xJvIVdY= gitea.com/gitea/act v0.0.0-20220812010840-c467b06265ee/go.mod h1:G37Vfz4J6kJ5NbcPI5xQUkeWPVkUCP5J+MFkaWU9jNY= +gitea.com/gitea/proto v0.0.0-20220813120843-ce4b5dd68c1f h1:o1fHWLbdhicM5Q6EXvpBLrpu/fVqlobYw1sU1YKSreM= +gitea.com/gitea/proto v0.0.0-20220813120843-ce4b5dd68c1f/go.mod h1:LWD9G0VCMxaDY4I+J3vSqJF5OYNum33pQtRpx43516s= github.com/Azure/azure-sdk-for-go v16.2.1+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 h1:w+iIsaOQNcT7OZ575w+acHgRric5iCyQh+xv+KJ4HB8= github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8= @@ -101,6 +103,8 @@ github.com/blang/semver v3.1.0+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnweb github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk= github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4= github.com/bshuster-repo/logrus-logstash-hook v0.4.1/go.mod h1:zsTqEiSzDgAa/8GZR7E1qaXrhYNDKBYy5/dWPTIflbk= +github.com/bufbuild/connect-go v0.3.0 h1:B0vyZrTR11SNEYpodL6P0NzluDCsuqmr8CNKblXvVto= +github.com/bufbuild/connect-go v0.3.0/go.mod h1:4efZ2eXFENwd4p7tuLaL9m0qtTsCOzuBvrohvRGevDM= github.com/buger/jsonparser v0.0.0-20180808090653-f4dd9f5a6b44/go.mod h1:bbYlZJ7hK1yFx9hf58LP0zeX7UjIGs20ufpu3evjr+s= github.com/bugsnag/bugsnag-go v0.0.0-20141110184014-b1d153021fcd/go.mod h1:2oa8nejYd4cQ/b0hMIopN0lCRxU0bueqREvZLWFrtK8= github.com/bugsnag/osext v0.0.0-20130617224835-0dd3f918b21b/go.mod h1:obH5gd0BsqsP2LwDJ9aOkm/6J86V6lyAXCoQWGw3K50= @@ -395,7 +399,7 @@ github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= +github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= github.com/google/go-containerregistry v0.5.1/go.mod h1:Ct15B4yir3PLOP5jsy0GNeYVaIZs/MK/Jz5any1wFW0= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= @@ -952,6 +956,7 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -1007,7 +1012,6 @@ golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/api v0.0.0-20160322025152-9bf6e6e569ff/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0= google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= @@ -1084,6 +1088,8 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= +google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/airbrake/gobrake.v2 v2.0.9/go.mod h1:/h5ZAUhDkGaJfjzjKLSjv6zCL6O0LLBxU4K+aSYdM/U= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=