mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-10-22 02:35:22 +00:00
Rename cmd agent and server folders and binaries (#330)
Renamed `cmd/drone-agent` to `cmd/agent` and `cmd/drone-server` to `cmd/server` and binaries to get rid of the drone name.
This commit is contained in:
492
cmd/agent/agent.go
Normal file
492
cmd/agent/agent.go
Normal file
@@ -0,0 +1,492 @@
|
||||
// Copyright 2018 Drone.IO Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
grpccredentials "google.golang.org/grpc/credentials"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/keepalive"
|
||||
"google.golang.org/grpc/metadata"
|
||||
|
||||
"github.com/woodpecker-ci/woodpecker/cncd/pipeline/pipeline"
|
||||
"github.com/woodpecker-ci/woodpecker/cncd/pipeline/pipeline/backend"
|
||||
"github.com/woodpecker-ci/woodpecker/cncd/pipeline/pipeline/backend/docker"
|
||||
"github.com/woodpecker-ci/woodpecker/cncd/pipeline/pipeline/multipart"
|
||||
"github.com/woodpecker-ci/woodpecker/cncd/pipeline/pipeline/rpc"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/tevino/abool"
|
||||
"github.com/urfave/cli"
|
||||
oldcontext "golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func loop(c *cli.Context) error {
|
||||
filter := rpc.Filter{
|
||||
Labels: map[string]string{
|
||||
"platform": c.String("platform"),
|
||||
},
|
||||
Expr: c.String("filter"),
|
||||
}
|
||||
|
||||
hostname := c.String("hostname")
|
||||
if len(hostname) == 0 {
|
||||
hostname, _ = os.Hostname()
|
||||
}
|
||||
|
||||
if c.BoolT("debug") {
|
||||
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
||||
} else {
|
||||
zerolog.SetGlobalLevel(zerolog.WarnLevel)
|
||||
}
|
||||
|
||||
if c.Bool("pretty") {
|
||||
log.Logger = log.Output(
|
||||
zerolog.ConsoleWriter{
|
||||
Out: os.Stderr,
|
||||
NoColor: c.BoolT("nocolor"),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
counter.Polling = c.Int("max-procs")
|
||||
counter.Running = 0
|
||||
|
||||
if c.BoolT("healthcheck") {
|
||||
go http.ListenAndServe(":3000", nil)
|
||||
}
|
||||
|
||||
// TODO pass version information to grpc server
|
||||
// TODO authenticate to grpc server
|
||||
|
||||
// grpc.Dial(target, ))
|
||||
|
||||
var transport = grpc.WithInsecure()
|
||||
|
||||
if c.Bool("secure-grpc") {
|
||||
transport = grpc.WithTransportCredentials(grpccredentials.NewTLS(&tls.Config{InsecureSkipVerify: c.Bool("skip-insecure-grpc")}))
|
||||
}
|
||||
|
||||
conn, err := grpc.Dial(
|
||||
c.String("server"),
|
||||
transport,
|
||||
grpc.WithPerRPCCredentials(&credentials{
|
||||
username: c.String("username"),
|
||||
password: c.String("password"),
|
||||
}),
|
||||
grpc.WithKeepaliveParams(keepalive.ClientParameters{
|
||||
Time: c.Duration("keepalive-time"),
|
||||
Timeout: c.Duration("keepalive-timeout"),
|
||||
}),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
client := rpc.NewGrpcClient(conn)
|
||||
|
||||
sigterm := abool.New()
|
||||
ctx := metadata.NewOutgoingContext(
|
||||
context.Background(),
|
||||
metadata.Pairs("hostname", hostname),
|
||||
)
|
||||
ctx = WithContextFunc(ctx, func() {
|
||||
println("ctrl+c received, terminating process")
|
||||
sigterm.Set()
|
||||
})
|
||||
|
||||
var wg sync.WaitGroup
|
||||
parallel := c.Int("max-procs")
|
||||
wg.Add(parallel)
|
||||
|
||||
for i := 0; i < parallel; i++ {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for {
|
||||
if sigterm.IsSet() {
|
||||
return
|
||||
}
|
||||
r := runner{
|
||||
client: client,
|
||||
filter: filter,
|
||||
hostname: hostname,
|
||||
}
|
||||
if err := r.run(ctx); err != nil {
|
||||
log.Error().Err(err).Msg("pipeline done with error")
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
return nil
|
||||
}
|
||||
|
||||
// NOTE we need to limit the size of the logs and files that we upload.
|
||||
// The maximum grpc payload size is 4194304. So until we implement streaming
|
||||
// for uploads, we need to set these limits below the maximum.
|
||||
const (
|
||||
maxLogsUpload = 2000000 // this is per step
|
||||
maxFileUpload = 1000000
|
||||
)
|
||||
|
||||
type runner struct {
|
||||
client rpc.Peer
|
||||
filter rpc.Filter
|
||||
hostname string
|
||||
}
|
||||
|
||||
func (r *runner) run(ctx context.Context) error {
|
||||
log.Debug().
|
||||
Msg("request next execution")
|
||||
|
||||
meta, _ := metadata.FromOutgoingContext(ctx)
|
||||
ctxmeta := metadata.NewOutgoingContext(context.Background(), meta)
|
||||
|
||||
// get the next job from the queue
|
||||
work, err := r.client.Next(ctx, r.filter)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if work == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
timeout := time.Hour
|
||||
if minutes := work.Timeout; minutes != 0 {
|
||||
timeout = time.Duration(minutes) * time.Minute
|
||||
}
|
||||
|
||||
counter.Add(
|
||||
work.ID,
|
||||
timeout,
|
||||
extractRepositoryName(work.Config), // hack
|
||||
extractBuildNumber(work.Config), // hack
|
||||
)
|
||||
defer counter.Done(work.ID)
|
||||
|
||||
logger := log.With().
|
||||
Str("repo", extractRepositoryName(work.Config)). // hack
|
||||
Str("build", extractBuildNumber(work.Config)). // hack
|
||||
Str("id", work.ID).
|
||||
Logger()
|
||||
|
||||
logger.Debug().
|
||||
Msg("received execution")
|
||||
|
||||
// new docker engine
|
||||
engine, err := docker.NewEnv()
|
||||
if err != nil {
|
||||
logger.Error().
|
||||
Err(err).
|
||||
Msg("cannot create docker client")
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(ctxmeta, timeout)
|
||||
defer cancel()
|
||||
|
||||
cancelled := abool.New()
|
||||
go func() {
|
||||
logger.Debug().
|
||||
Msg("listen for cancel signal")
|
||||
|
||||
if werr := r.client.Wait(ctx, work.ID); werr != nil {
|
||||
cancelled.SetTo(true)
|
||||
logger.Warn().
|
||||
Err(werr).
|
||||
Msg("cancel signal received")
|
||||
|
||||
cancel()
|
||||
} else {
|
||||
logger.Debug().
|
||||
Msg("stop listening for cancel signal")
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
logger.Debug().
|
||||
Msg("pipeline done")
|
||||
|
||||
return
|
||||
case <-time.After(time.Minute):
|
||||
logger.Debug().
|
||||
Msg("pipeline lease renewed")
|
||||
|
||||
r.client.Extend(ctx, work.ID)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
state := rpc.State{}
|
||||
state.Started = time.Now().Unix()
|
||||
|
||||
err = r.client.Init(ctxmeta, work.ID, state)
|
||||
if err != nil {
|
||||
logger.Error().
|
||||
Err(err).
|
||||
Msg("pipeline initialization failed")
|
||||
}
|
||||
|
||||
var uploads sync.WaitGroup
|
||||
defaultLogger := pipeline.LogFunc(func(proc *backend.Step, rc multipart.Reader) error {
|
||||
|
||||
loglogger := logger.With().
|
||||
Str("image", proc.Image).
|
||||
Str("stage", proc.Alias).
|
||||
Logger()
|
||||
|
||||
part, rerr := rc.NextPart()
|
||||
if rerr != nil {
|
||||
return rerr
|
||||
}
|
||||
uploads.Add(1)
|
||||
|
||||
var secrets []string
|
||||
for _, secret := range work.Config.Secrets {
|
||||
if secret.Mask {
|
||||
secrets = append(secrets, secret.Value)
|
||||
}
|
||||
}
|
||||
|
||||
loglogger.Debug().Msg("log stream opened")
|
||||
|
||||
limitedPart := io.LimitReader(part, maxLogsUpload)
|
||||
logstream := rpc.NewLineWriter(r.client, work.ID, proc.Alias, secrets...)
|
||||
io.Copy(logstream, limitedPart)
|
||||
|
||||
loglogger.Debug().Msg("log stream copied")
|
||||
|
||||
file := &rpc.File{}
|
||||
file.Mime = "application/json+logs"
|
||||
file.Proc = proc.Alias
|
||||
file.Name = "logs.json"
|
||||
file.Data, _ = json.Marshal(logstream.Lines())
|
||||
file.Size = len(file.Data)
|
||||
file.Time = time.Now().Unix()
|
||||
|
||||
loglogger.Debug().
|
||||
Msg("log stream uploading")
|
||||
|
||||
if serr := r.client.Upload(ctxmeta, work.ID, file); serr != nil {
|
||||
loglogger.Error().
|
||||
Err(serr).
|
||||
Msg("log stream upload error")
|
||||
}
|
||||
|
||||
loglogger.Debug().
|
||||
Msg("log stream upload complete")
|
||||
|
||||
defer func() {
|
||||
loglogger.Debug().
|
||||
Msg("log stream closed")
|
||||
|
||||
uploads.Done()
|
||||
}()
|
||||
|
||||
part, rerr = rc.NextPart()
|
||||
if rerr != nil {
|
||||
return nil
|
||||
}
|
||||
// TODO should be configurable
|
||||
limitedPart = io.LimitReader(part, maxFileUpload)
|
||||
file = &rpc.File{}
|
||||
file.Mime = part.Header().Get("Content-Type")
|
||||
file.Proc = proc.Alias
|
||||
file.Name = part.FileName()
|
||||
file.Data, _ = ioutil.ReadAll(limitedPart)
|
||||
file.Size = len(file.Data)
|
||||
file.Time = time.Now().Unix()
|
||||
file.Meta = map[string]string{}
|
||||
|
||||
for key, value := range part.Header() {
|
||||
file.Meta[key] = value[0]
|
||||
}
|
||||
|
||||
loglogger.Debug().
|
||||
Str("file", file.Name).
|
||||
Str("mime", file.Mime).
|
||||
Msg("file stream uploading")
|
||||
|
||||
if serr := r.client.Upload(ctxmeta, work.ID, file); serr != nil {
|
||||
loglogger.Error().
|
||||
Err(serr).
|
||||
Str("file", file.Name).
|
||||
Str("mime", file.Mime).
|
||||
Msg("file stream upload error")
|
||||
}
|
||||
|
||||
loglogger.Debug().
|
||||
Str("file", file.Name).
|
||||
Str("mime", file.Mime).
|
||||
Msg("file stream upload complete")
|
||||
return nil
|
||||
})
|
||||
|
||||
defaultTracer := pipeline.TraceFunc(func(state *pipeline.State) error {
|
||||
proclogger := logger.With().
|
||||
Str("image", state.Pipeline.Step.Image).
|
||||
Str("stage", state.Pipeline.Step.Alias).
|
||||
Int("exit_code", state.Process.ExitCode).
|
||||
Bool("exited", state.Process.Exited).
|
||||
Logger()
|
||||
|
||||
procState := rpc.State{
|
||||
Proc: state.Pipeline.Step.Alias,
|
||||
Exited: state.Process.Exited,
|
||||
ExitCode: state.Process.ExitCode,
|
||||
Started: time.Now().Unix(), // TODO do not do this
|
||||
Finished: time.Now().Unix(),
|
||||
}
|
||||
defer func() {
|
||||
proclogger.Debug().
|
||||
Msg("update step status")
|
||||
|
||||
if uerr := r.client.Update(ctxmeta, work.ID, procState); uerr != nil {
|
||||
proclogger.Debug().
|
||||
Err(uerr).
|
||||
Msg("update step status error")
|
||||
}
|
||||
|
||||
proclogger.Debug().
|
||||
Msg("update step status complete")
|
||||
}()
|
||||
if state.Process.Exited {
|
||||
return nil
|
||||
}
|
||||
if state.Pipeline.Step.Environment == nil {
|
||||
state.Pipeline.Step.Environment = map[string]string{}
|
||||
}
|
||||
|
||||
state.Pipeline.Step.Environment["DRONE_MACHINE"] = r.hostname
|
||||
state.Pipeline.Step.Environment["CI_BUILD_STATUS"] = "success"
|
||||
state.Pipeline.Step.Environment["CI_BUILD_STARTED"] = strconv.FormatInt(state.Pipeline.Time, 10)
|
||||
state.Pipeline.Step.Environment["CI_BUILD_FINISHED"] = strconv.FormatInt(time.Now().Unix(), 10)
|
||||
state.Pipeline.Step.Environment["DRONE_BUILD_STATUS"] = "success"
|
||||
state.Pipeline.Step.Environment["DRONE_BUILD_STARTED"] = strconv.FormatInt(state.Pipeline.Time, 10)
|
||||
state.Pipeline.Step.Environment["DRONE_BUILD_FINISHED"] = strconv.FormatInt(time.Now().Unix(), 10)
|
||||
|
||||
state.Pipeline.Step.Environment["CI_JOB_STATUS"] = "success"
|
||||
state.Pipeline.Step.Environment["CI_JOB_STARTED"] = strconv.FormatInt(state.Pipeline.Time, 10)
|
||||
state.Pipeline.Step.Environment["CI_JOB_FINISHED"] = strconv.FormatInt(time.Now().Unix(), 10)
|
||||
state.Pipeline.Step.Environment["DRONE_JOB_STATUS"] = "success"
|
||||
state.Pipeline.Step.Environment["DRONE_JOB_STARTED"] = strconv.FormatInt(state.Pipeline.Time, 10)
|
||||
state.Pipeline.Step.Environment["DRONE_JOB_FINISHED"] = strconv.FormatInt(time.Now().Unix(), 10)
|
||||
|
||||
if state.Pipeline.Error != nil {
|
||||
state.Pipeline.Step.Environment["CI_BUILD_STATUS"] = "failure"
|
||||
state.Pipeline.Step.Environment["CI_JOB_STATUS"] = "failure"
|
||||
state.Pipeline.Step.Environment["DRONE_BUILD_STATUS"] = "failure"
|
||||
state.Pipeline.Step.Environment["DRONE_JOB_STATUS"] = "failure"
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
err = pipeline.New(work.Config,
|
||||
pipeline.WithContext(ctx),
|
||||
pipeline.WithLogger(defaultLogger),
|
||||
pipeline.WithTracer(defaultTracer),
|
||||
pipeline.WithEngine(engine),
|
||||
).Run()
|
||||
|
||||
state.Finished = time.Now().Unix()
|
||||
state.Exited = true
|
||||
if err != nil {
|
||||
switch xerr := err.(type) {
|
||||
case *pipeline.ExitError:
|
||||
state.ExitCode = xerr.Code
|
||||
default:
|
||||
state.ExitCode = 1
|
||||
state.Error = err.Error()
|
||||
}
|
||||
if cancelled.IsSet() {
|
||||
state.ExitCode = 137
|
||||
}
|
||||
}
|
||||
|
||||
logger.Debug().
|
||||
Str("error", state.Error).
|
||||
Int("exit_code", state.ExitCode).
|
||||
Msg("pipeline complete")
|
||||
|
||||
logger.Debug().
|
||||
Msg("uploading logs")
|
||||
|
||||
uploads.Wait()
|
||||
|
||||
logger.Debug().
|
||||
Msg("uploading logs complete")
|
||||
|
||||
logger.Debug().
|
||||
Str("error", state.Error).
|
||||
Int("exit_code", state.ExitCode).
|
||||
Msg("updating pipeline status")
|
||||
|
||||
err = r.client.Done(ctxmeta, work.ID, state)
|
||||
if err != nil {
|
||||
logger.Error().Err(err).
|
||||
Msg("updating pipeline status failed")
|
||||
} else {
|
||||
logger.Debug().
|
||||
Msg("updating pipeline status complete")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type credentials struct {
|
||||
username string
|
||||
password string
|
||||
}
|
||||
|
||||
func (c *credentials) GetRequestMetadata(oldcontext.Context, ...string) (map[string]string, error) {
|
||||
return map[string]string{
|
||||
"username": c.username,
|
||||
"password": c.password,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *credentials) RequireTransportSecurity() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// extract repository name from the configuration
|
||||
func extractRepositoryName(config *backend.Config) string {
|
||||
return config.Stages[0].Steps[0].Environment["DRONE_REPO"]
|
||||
}
|
||||
|
||||
// extract build number from the configuration
|
||||
func extractBuildNumber(config *backend.Config) string {
|
||||
return config.Stages[0].Steps[0].Environment["DRONE_BUILD_NUMBER"]
|
||||
}
|
104
cmd/agent/flags.go
Normal file
104
cmd/agent/flags.go
Normal file
@@ -0,0 +1,104 @@
|
||||
// Copyright 2019 Laszlo Fogas
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
var flags = []cli.Flag{
|
||||
cli.StringFlag{
|
||||
EnvVar: "DRONE_SERVER,WOODPECKER_SERVER",
|
||||
Name: "server",
|
||||
Usage: "drone server address",
|
||||
Value: "localhost:9000",
|
||||
},
|
||||
cli.StringFlag{
|
||||
EnvVar: "DRONE_USERNAME,WOODPECKER_USERNAME",
|
||||
Name: "username",
|
||||
Usage: "drone auth username",
|
||||
Value: "x-oauth-basic",
|
||||
},
|
||||
cli.StringFlag{
|
||||
EnvVar: "DRONE_PASSWORD,DRONE_SECRET,WOODPECKER_PASSWORD,WOODPECKER_SECRET",
|
||||
Name: "password",
|
||||
Usage: "server-agent shared password",
|
||||
},
|
||||
cli.BoolTFlag{
|
||||
EnvVar: "DRONE_DEBUG,WOODPECKER_DEBUG",
|
||||
Name: "debug",
|
||||
Usage: "enable agent debug mode",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
EnvVar: "DRONE_DEBUG_PRETTY,WOODPECKER_DEBUG_PRETTY",
|
||||
Name: "pretty",
|
||||
Usage: "enable pretty-printed debug output",
|
||||
},
|
||||
cli.BoolTFlag{
|
||||
EnvVar: "DRONE_DEBUG_NOCOLOR,WOODPECKER_DEBUG_NOCOLOR",
|
||||
Name: "nocolor",
|
||||
Usage: "disable colored debug output",
|
||||
},
|
||||
cli.StringFlag{
|
||||
EnvVar: "DRONE_HOSTNAME,HOSTNAME,WOODPECKER_HOSTNAME,HOSTNAME",
|
||||
Name: "hostname",
|
||||
Usage: "agent hostname",
|
||||
},
|
||||
cli.StringFlag{
|
||||
EnvVar: "DRONE_PLATFORM,WOODPECKER_PLATFORM",
|
||||
Name: "platform",
|
||||
Usage: "restrict builds by platform conditions",
|
||||
Value: "linux/amd64",
|
||||
},
|
||||
cli.StringFlag{
|
||||
EnvVar: "DRONE_FILTER,WOODPECKER_FILTER",
|
||||
Name: "filter",
|
||||
Usage: "filter expression to restrict builds by label",
|
||||
},
|
||||
cli.IntFlag{
|
||||
EnvVar: "DRONE_MAX_PROCS,WOODPECKER_MAX_PROCS",
|
||||
Name: "max-procs",
|
||||
Usage: "agent parallel builds",
|
||||
Value: 1,
|
||||
},
|
||||
cli.BoolTFlag{
|
||||
EnvVar: "DRONE_HEALTHCHECK,WOODPECKER_HEALTHCHECK",
|
||||
Name: "healthcheck",
|
||||
Usage: "enable healthcheck endpoint",
|
||||
},
|
||||
cli.DurationFlag{
|
||||
EnvVar: "DRONE_KEEPALIVE_TIME,WOODPECKER_KEEPALIVE_TIME",
|
||||
Name: "keepalive-time",
|
||||
Usage: "after a duration of this time of no activity, the agent pings the server to check if the transport is still alive",
|
||||
},
|
||||
cli.DurationFlag{
|
||||
EnvVar: "DRONE_KEEPALIVE_TIMEOUT,WOODPECKER_KEEPALIVE_TIMEOUT",
|
||||
Name: "keepalive-timeout",
|
||||
Usage: "after pinging for a keepalive check, the agent waits for a duration of this time before closing the connection if no activity",
|
||||
Value: time.Second * 20,
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "secure-grpc",
|
||||
Usage: "should the connection to DRONE_SERVER be made using a secure transport",
|
||||
EnvVar: "DRONE_GRPC_SECURE,WOODPECKER_GRPC_SECURE",
|
||||
},
|
||||
cli.BoolTFlag{
|
||||
Name: "skip-insecure-grpc",
|
||||
Usage: "should the grpc server certificate be verified, only valid when DRONE_GRPC_SECURE is true",
|
||||
EnvVar: "DRONE_GRPC_VERIFY,WOODPECKER_GRPC_VERIFY",
|
||||
},
|
||||
}
|
145
cmd/agent/health.go
Normal file
145
cmd/agent/health.go
Normal file
@@ -0,0 +1,145 @@
|
||||
// Copyright 2018 Drone.IO Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/urfave/cli"
|
||||
"github.com/woodpecker-ci/woodpecker/version"
|
||||
)
|
||||
|
||||
// the file implements some basic healthcheck logic based on the
|
||||
// following specification:
|
||||
// https://github.com/mozilla-services/Dockerflow
|
||||
|
||||
func init() {
|
||||
http.HandleFunc("/varz", handleStats)
|
||||
http.HandleFunc("/healthz", handleHeartbeat)
|
||||
http.HandleFunc("/version", handleVersion)
|
||||
}
|
||||
|
||||
func handleHeartbeat(w http.ResponseWriter, r *http.Request) {
|
||||
if counter.Healthy() {
|
||||
w.WriteHeader(200)
|
||||
} else {
|
||||
w.WriteHeader(500)
|
||||
}
|
||||
}
|
||||
|
||||
func handleVersion(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(200)
|
||||
w.Header().Add("Content-Type", "text/json")
|
||||
json.NewEncoder(w).Encode(versionResp{
|
||||
Source: "https://github.com/woodpecker-ci/woodpecker",
|
||||
Version: version.String(),
|
||||
})
|
||||
}
|
||||
|
||||
func handleStats(w http.ResponseWriter, r *http.Request) {
|
||||
if counter.Healthy() {
|
||||
w.WriteHeader(200)
|
||||
} else {
|
||||
w.WriteHeader(500)
|
||||
}
|
||||
w.Header().Add("Content-Type", "text/json")
|
||||
counter.writeTo(w)
|
||||
}
|
||||
|
||||
type versionResp struct {
|
||||
Version string `json:"version"`
|
||||
Source string `json:"source"`
|
||||
}
|
||||
|
||||
// default statistics counter
|
||||
var counter = &state{
|
||||
Metadata: map[string]info{},
|
||||
}
|
||||
|
||||
type state struct {
|
||||
sync.Mutex `json:"-"`
|
||||
Polling int `json:"polling_count"`
|
||||
Running int `json:"running_count"`
|
||||
Metadata map[string]info `json:"running"`
|
||||
}
|
||||
|
||||
type info struct {
|
||||
ID string `json:"id"`
|
||||
Repo string `json:"repository"`
|
||||
Build string `json:"build_number"`
|
||||
Started time.Time `json:"build_started"`
|
||||
Timeout time.Duration `json:"build_timeout"`
|
||||
}
|
||||
|
||||
func (s *state) Add(id string, timeout time.Duration, repo, build string) {
|
||||
s.Lock()
|
||||
s.Polling--
|
||||
s.Running++
|
||||
s.Metadata[id] = info{
|
||||
ID: id,
|
||||
Repo: repo,
|
||||
Build: build,
|
||||
Timeout: timeout,
|
||||
Started: time.Now().UTC(),
|
||||
}
|
||||
s.Unlock()
|
||||
}
|
||||
|
||||
func (s *state) Done(id string) {
|
||||
s.Lock()
|
||||
s.Polling++
|
||||
s.Running--
|
||||
delete(s.Metadata, id)
|
||||
s.Unlock()
|
||||
}
|
||||
|
||||
func (s *state) Healthy() bool {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
now := time.Now()
|
||||
buf := time.Hour // 1 hour buffer
|
||||
for _, item := range s.Metadata {
|
||||
if now.After(item.Started.Add(item.Timeout).Add(buf)) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *state) writeTo(w io.Writer) (int, error) {
|
||||
s.Lock()
|
||||
out, _ := json.Marshal(s)
|
||||
s.Unlock()
|
||||
return w.Write(out)
|
||||
}
|
||||
|
||||
// handles pinging the endpoint and returns an error if the
|
||||
// agent is in an unhealthy state.
|
||||
func pinger(c *cli.Context) error {
|
||||
resp, err := http.Get("http://localhost:3000/healthz")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != 200 {
|
||||
return fmt.Errorf("agent returned non-200 status code")
|
||||
}
|
||||
return nil
|
||||
}
|
59
cmd/agent/health_test.go
Normal file
59
cmd/agent/health_test.go
Normal file
@@ -0,0 +1,59 @@
|
||||
// Copyright 2018 Drone.IO Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestHealthy(t *testing.T) {
|
||||
s := state{}
|
||||
s.Metadata = map[string]info{}
|
||||
|
||||
s.Add("1", time.Hour, "octocat/hello-world", "42")
|
||||
|
||||
if got, want := s.Metadata["1"].ID, "1"; got != want {
|
||||
t.Errorf("got ID %s, want %s", got, want)
|
||||
}
|
||||
if got, want := s.Metadata["1"].Timeout, time.Hour; got != want {
|
||||
t.Errorf("got duration %v, want %v", got, want)
|
||||
}
|
||||
if got, want := s.Metadata["1"].Repo, "octocat/hello-world"; got != want {
|
||||
t.Errorf("got repository name %s, want %s", got, want)
|
||||
}
|
||||
|
||||
s.Metadata["1"] = info{
|
||||
Timeout: time.Hour,
|
||||
Started: time.Now().UTC(),
|
||||
}
|
||||
if s.Healthy() == false {
|
||||
t.Error("want healthy status when timeout not exceeded, got false")
|
||||
}
|
||||
|
||||
s.Metadata["1"] = info{
|
||||
Started: time.Now().UTC().Add(-(time.Minute * 30)),
|
||||
}
|
||||
if s.Healthy() == false {
|
||||
t.Error("want healthy status when timeout+buffer not exceeded, got false")
|
||||
}
|
||||
|
||||
s.Metadata["1"] = info{
|
||||
Started: time.Now().UTC().Add(-(time.Hour + time.Minute)),
|
||||
}
|
||||
if s.Healthy() == true {
|
||||
t.Error("want unhealthy status when timeout+buffer not exceeded, got true")
|
||||
}
|
||||
}
|
46
cmd/agent/main.go
Normal file
46
cmd/agent/main.go
Normal file
@@ -0,0 +1,46 @@
|
||||
// Copyright 2018 Drone.IO Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/woodpecker-ci/woodpecker/version"
|
||||
|
||||
_ "github.com/joho/godotenv/autoload"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := cli.NewApp()
|
||||
app.Name = "woodpecker-agent"
|
||||
app.Version = version.String()
|
||||
app.Usage = "woodpecker agent"
|
||||
app.Action = loop
|
||||
app.Commands = []cli.Command{
|
||||
{
|
||||
Name: "ping",
|
||||
Usage: "ping the agent",
|
||||
Action: pinger,
|
||||
},
|
||||
}
|
||||
app.Flags = flags
|
||||
|
||||
if err := app.Run(os.Args); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
42
cmd/agent/signal.go
Normal file
42
cmd/agent/signal.go
Normal file
@@ -0,0 +1,42 @@
|
||||
// Copyright 2017 Drone.IO Inc.
|
||||
//
|
||||
// This file is licensed under the terms of the MIT license.
|
||||
// For a copy, see https://opensource.org/licenses/MIT.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// WithContext returns a copy of parent context whose Done channel is closed
|
||||
// when an os interrupt signal is received.
|
||||
func WithContext(ctx context.Context) context.Context {
|
||||
return WithContextFunc(ctx, func() {
|
||||
println("interrupt received, terminating process")
|
||||
})
|
||||
}
|
||||
|
||||
// WithContextFunc returns a copy of parent context that is cancelled when
|
||||
// an os interrupt signal is received. The callback function f is invoked
|
||||
// before cancellation.
|
||||
func WithContextFunc(ctx context.Context, f func()) context.Context {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
go func() {
|
||||
c := make(chan os.Signal)
|
||||
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
|
||||
defer signal.Stop(c)
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
case <-c:
|
||||
f()
|
||||
cancel()
|
||||
}
|
||||
}()
|
||||
|
||||
return ctx
|
||||
}
|
Reference in New Issue
Block a user