Skip to content

Commit

Permalink
More logging and refactor (#457)
Browse files Browse the repository at this point in the history
* only use "context"
* enable 'h2' support at server
* trace log  remote and database config
* log loglevel on start
  • Loading branch information
6543 committed Oct 19, 2021
1 parent 2778bfe commit 5990d32
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 89 deletions.
3 changes: 1 addition & 2 deletions cmd/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/rs/zerolog/log"
"github.com/tevino/abool"
"github.com/urfave/cli"
oldcontext "golang.org/x/net/context"
"google.golang.org/grpc"
grpccredentials "google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
Expand Down Expand Up @@ -160,7 +159,7 @@ type credentials struct {
password string
}

func (c *credentials) GetRequestMetadata(oldcontext.Context, ...string) (map[string]string, error) {
func (c *credentials) GetRequestMetadata(context.Context, ...string) (map[string]string, error) {
return map[string]string{
"username": c.username,
"password": c.password,
Expand Down
15 changes: 9 additions & 6 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/rs/zerolog/log"
"github.com/urfave/cli"
"golang.org/x/crypto/acme/autocert"
oldcontext "golang.org/x/net/context"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
Expand Down Expand Up @@ -68,7 +67,6 @@ func loop(c *cli.Context) error {
log.Warn().Msg("--debug is deprecated, use --log-level instead")
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}

if c.IsSet("log-level") {
logLevelFlag := c.String("log-level")
lvl, err := zerolog.ParseLevel(logLevelFlag)
Expand All @@ -77,6 +75,7 @@ func loop(c *cli.Context) error {
}
zerolog.SetGlobalLevel(lvl)
}
log.Log().Msgf("LogLevel = %s", zerolog.GlobalLevel().String())

if c.String("server-host") == "" {
log.Fatal().Msg("WOODPECKER_HOST is not properly configured")
Expand Down Expand Up @@ -105,7 +104,11 @@ func loop(c *cli.Context) error {
log.Fatal().Err(err).Msg("")
}

store_ := setupStore(c)
store_, err := setupStore(c)
if err != nil {
log.Fatal().Err(err).Msg("")
}

setupEvilGlobals(c, store_, remote_)

proxyWebUI := c.String("www-proxy")
Expand Down Expand Up @@ -189,7 +192,7 @@ func loop(c *cli.Context) error {
Addr: ":https",
Handler: handler,
TLSConfig: &tls.Config{
NextProtos: []string{"http/1.1"}, // disable h2 because Safari :(
NextProtos: []string{"h2", "http/1.1"},
},
}
return serve.ListenAndServeTLS(
Expand Down Expand Up @@ -232,7 +235,7 @@ func loop(c *cli.Context) error {
Handler: handler,
TLSConfig: &tls.Config{
GetCertificate: manager.GetCertificate,
NextProtos: []string{"http/1.1"}, // disable h2 because Safari :(
NextProtos: []string{"h2", "http/1.1"},
},
}
return serve.ListenAndServeTLS("", "")
Expand Down Expand Up @@ -296,7 +299,7 @@ func (a *authorizer) streamInterceptor(srv interface{}, stream grpc.ServerStream
return handler(srv, stream)
}

func (a *authorizer) unaryIntercaptor(ctx oldcontext.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
func (a *authorizer) unaryIntercaptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
if err := a.authorize(ctx); err != nil {
return nil, err
}
Expand Down
47 changes: 30 additions & 17 deletions cmd/server/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ import (
"github.com/woodpecker-ci/woodpecker/server/web"
)

func setupStore(c *cli.Context) store.Store {
return datastore.New(
c.String("driver"),
c.String("datasource"),
)
func setupStore(c *cli.Context) (store.Store, error) {
opts := &datastore.Opts{
Driver: c.String("driver"),
Config: c.String("datasource"),
}
log.Trace().Msgf("setup datastore: %#v", opts)
return datastore.New(opts)
}

func setupQueue(c *cli.Context, s store.Store) queue.Queue {
Expand Down Expand Up @@ -98,21 +100,25 @@ func SetupRemote(c *cli.Context) (remote.Remote, error) {

// helper function to setup the Bitbucket remote from the CLI arguments.
func setupBitbucket(c *cli.Context) (remote.Remote, error) {
return bitbucket.New(
c.String("bitbucket-client"),
c.String("bitbucket-secret"),
), nil
opts := &bitbucket.Opts{
Client: c.String("bitbucket-client"),
Secret: c.String("bitbucket-secret"),
}
log.Trace().Msgf("Remote (bitbucket) opts: %#v", opts)
return bitbucket.New(opts)
}

// helper function to setup the Gogs remote from the CLI arguments.
func setupGogs(c *cli.Context) (remote.Remote, error) {
return gogs.New(gogs.Opts{
opts := gogs.Opts{
URL: c.String("gogs-server"),
Username: c.String("gogs-git-username"),
Password: c.String("gogs-git-password"),
PrivateMode: c.Bool("gogs-private-mode"),
SkipVerify: c.Bool("gogs-skip-verify"),
})
}
log.Trace().Msgf("Remote (gogs) opts: %#v", opts)
return gogs.New(opts)
}

// helper function to setup the Gitea remote from the CLI arguments.
Expand All @@ -130,20 +136,23 @@ func setupGitea(c *cli.Context) (remote.Remote, error) {
if len(opts.URL) == 0 {
log.Fatal().Msg("WOODPECKER_GITEA_URL must be set")
}
log.Trace().Msgf("Remote (gitea) opts: %#v", opts)
return gitea.New(opts)
}

// helper function to setup the Stash remote from the CLI arguments.
func setupStash(c *cli.Context) (remote.Remote, error) {
return bitbucketserver.New(bitbucketserver.Opts{
opts := bitbucketserver.Opts{
URL: c.String("stash-server"),
Username: c.String("stash-git-username"),
Password: c.String("stash-git-password"),
ConsumerKey: c.String("stash-consumer-key"),
ConsumerRSA: c.String("stash-consumer-rsa"),
ConsumerRSAString: c.String("stash-consumer-rsa-string"),
SkipVerify: c.Bool("stash-skip-verify"),
})
}
log.Trace().Msgf("Remote (bitbucketserver) opts: %#v", opts)
return bitbucketserver.New(opts)
}

// helper function to setup the Gitlab remote from the CLI arguments.
Expand All @@ -161,7 +170,7 @@ func setupGitlab(c *cli.Context) (remote.Remote, error) {

// helper function to setup the GitHub remote from the CLI arguments.
func setupGithub(c *cli.Context) (remote.Remote, error) {
return github.New(github.Opts{
opts := github.Opts{
URL: c.String("github-server"),
Context: c.String("github-context"),
Client: c.String("github-client"),
Expand All @@ -172,12 +181,14 @@ func setupGithub(c *cli.Context) (remote.Remote, error) {
PrivateMode: c.Bool("github-private-mode"),
SkipVerify: c.Bool("github-skip-verify"),
MergeRef: c.BoolT("github-merge-ref"),
})
}
log.Trace().Msgf("Remote (github) opts: %#v", opts)
return github.New(opts)
}

// helper function to setup the Coding remote from the CLI arguments.
func setupCoding(c *cli.Context) (remote.Remote, error) {
return coding.New(coding.Opts{
opts := coding.Opts{
URL: c.String("coding-server"),
Client: c.String("coding-client"),
Secret: c.String("coding-secret"),
Expand All @@ -186,7 +197,9 @@ func setupCoding(c *cli.Context) (remote.Remote, error) {
Username: c.String("coding-git-username"),
Password: c.String("coding-git-password"),
SkipVerify: c.Bool("coding-skip-verify"),
})
}
log.Trace().Msgf("Remote (coding) opts: %#v", opts)
return coding.New(opts)
}

func setupTree(c *cli.Context) *gin.Engine {
Expand Down
17 changes: 8 additions & 9 deletions server/grpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
oldcontext "golang.org/x/net/context"
grpcMetadata "google.golang.org/grpc/metadata"

"github.com/woodpecker-ci/expr"
Expand Down Expand Up @@ -505,7 +504,7 @@ func NewWoodpeckerServer(remote remote.Remote, queue queue.Queue, logger logging
return &WoodpeckerServer{peer: peer}
}

func (s *WoodpeckerServer) Next(c oldcontext.Context, req *proto.NextRequest) (*proto.NextReply, error) {
func (s *WoodpeckerServer) Next(c context.Context, req *proto.NextRequest) (*proto.NextReply, error) {
filter := rpc.Filter{
Labels: req.GetFilter().GetLabels(),
Expr: req.GetFilter().GetExpr(),
Expand All @@ -528,7 +527,7 @@ func (s *WoodpeckerServer) Next(c oldcontext.Context, req *proto.NextRequest) (*
return res, err
}

func (s *WoodpeckerServer) Init(c oldcontext.Context, req *proto.InitRequest) (*proto.Empty, error) {
func (s *WoodpeckerServer) Init(c context.Context, req *proto.InitRequest) (*proto.Empty, error) {
state := rpc.State{
Error: req.GetState().GetError(),
ExitCode: int(req.GetState().GetExitCode()),
Expand All @@ -542,7 +541,7 @@ func (s *WoodpeckerServer) Init(c oldcontext.Context, req *proto.InitRequest) (*
return res, err
}

func (s *WoodpeckerServer) Update(c oldcontext.Context, req *proto.UpdateRequest) (*proto.Empty, error) {
func (s *WoodpeckerServer) Update(c context.Context, req *proto.UpdateRequest) (*proto.Empty, error) {
state := rpc.State{
Error: req.GetState().GetError(),
ExitCode: int(req.GetState().GetExitCode()),
Expand All @@ -556,7 +555,7 @@ func (s *WoodpeckerServer) Update(c oldcontext.Context, req *proto.UpdateRequest
return res, err
}

func (s *WoodpeckerServer) Upload(c oldcontext.Context, req *proto.UploadRequest) (*proto.Empty, error) {
func (s *WoodpeckerServer) Upload(c context.Context, req *proto.UploadRequest) (*proto.Empty, error) {
file := &rpc.File{
Data: req.GetFile().GetData(),
Mime: req.GetFile().GetMime(),
Expand All @@ -572,7 +571,7 @@ func (s *WoodpeckerServer) Upload(c oldcontext.Context, req *proto.UploadRequest
return res, err
}

func (s *WoodpeckerServer) Done(c oldcontext.Context, req *proto.DoneRequest) (*proto.Empty, error) {
func (s *WoodpeckerServer) Done(c context.Context, req *proto.DoneRequest) (*proto.Empty, error) {
state := rpc.State{
Error: req.GetState().GetError(),
ExitCode: int(req.GetState().GetExitCode()),
Expand All @@ -586,19 +585,19 @@ func (s *WoodpeckerServer) Done(c oldcontext.Context, req *proto.DoneRequest) (*
return res, err
}

func (s *WoodpeckerServer) Wait(c oldcontext.Context, req *proto.WaitRequest) (*proto.Empty, error) {
func (s *WoodpeckerServer) Wait(c context.Context, req *proto.WaitRequest) (*proto.Empty, error) {
res := new(proto.Empty)
err := s.peer.Wait(c, req.GetId())
return res, err
}

func (s *WoodpeckerServer) Extend(c oldcontext.Context, req *proto.ExtendRequest) (*proto.Empty, error) {
func (s *WoodpeckerServer) Extend(c context.Context, req *proto.ExtendRequest) (*proto.Empty, error) {
res := new(proto.Empty)
err := s.peer.Extend(c, req.GetId())
return res, err
}

func (s *WoodpeckerServer) Log(c oldcontext.Context, req *proto.LogRequest) (*proto.Empty, error) {
func (s *WoodpeckerServer) Log(c context.Context, req *proto.LogRequest) (*proto.Empty, error) {
line := &rpc.Line{
Out: req.GetLine().GetOut(),
Pos: int(req.GetLine().GetPos()),
Expand Down
15 changes: 11 additions & 4 deletions server/remote/bitbucket/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ const (
DefaultURL = "https://bitbucket.org"
)

// Opts are remote options for bitbucket
type Opts struct {
Client string
Secret string
}

type config struct {
API string
URL string
Expand All @@ -43,13 +49,14 @@ type config struct {

// New returns a new remote Configuration for integrating with the Bitbucket
// repository hosting service at https://bitbucket.org
func New(client, secret string) remote.Remote {
func New(opts *Opts) (remote.Remote, error) {
return &config{
API: DefaultAPI,
URL: DefaultURL,
Client: client,
Secret: secret,
}
Client: opts.Client,
Secret: opts.Secret,
}, nil
// TODO: add checks
}

// Login authenticates an account with Bitbucket using the oauth2 protocol. The
Expand Down
4 changes: 2 additions & 2 deletions server/remote/bitbucket/bitbucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ func Test_bitbucket(t *testing.T) {
})

g.It("Should return client with default endpoint", func() {
remote := New("4vyW6b49Z", "a5012f6c6")
remote, _ := New(&Opts{Client: "4vyW6b49Z", Secret: "a5012f6c6"})
g.Assert(remote.(*config).URL).Equal(DefaultURL)
g.Assert(remote.(*config).API).Equal(DefaultAPI)
g.Assert(remote.(*config).Client).Equal("4vyW6b49Z")
g.Assert(remote.(*config).Secret).Equal("a5012f6c6")
})

g.It("Should return the netrc file", func() {
remote := New("", "")
remote, _ := New(&Opts{})
netrc, _ := remote.Netrc(fakeUser, nil)
g.Assert(netrc.Machine).Equal("bitbucket.org")
g.Assert(netrc.Login).Equal("x-token-auth")
Expand Down
2 changes: 1 addition & 1 deletion server/remote/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package remote

import (
"golang.org/x/net/context"
"context"
)

const key = "remote"
Expand Down
1 change: 1 addition & 0 deletions server/shared/configFetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func (cf *configFetcher) Fetch(ctx context.Context) (files []*remote.FileMeta, e
// try to fetch 3 times, timeout is one second longer each time
for i := 0; i < 3; i++ {
files, err = cf.fetch(ctx, time.Second*time.Duration(configFetchTimeout), strings.TrimSpace(cf.repo.Config))
log.Trace().Msgf("%d try failed: %v", i, err)
if errors.Is(err, context.DeadlineExceeded) {
continue
}
Expand Down
2 changes: 1 addition & 1 deletion server/store/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package store

import (
"golang.org/x/net/context"
"context"
)

const key = "store"
Expand Down
Loading

0 comments on commit 5990d32

Please sign in to comment.