expose agent name to pipeline

This commit is contained in:
Brad Rydzewski 2017-09-14 09:39:52 -07:00
parent 913d8701f2
commit 63001a7c66

View file

@ -105,7 +105,12 @@ func loop(c *cli.Context) error {
if sigterm.IsSet() {
return
}
if err := run(ctx, client, filter); err != nil {
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
}
@ -125,7 +130,13 @@ const (
maxFileUpload = 1000000
)
func run(ctx context.Context, client rpc.Peer, filter rpc.Filter) error {
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")
@ -133,7 +144,7 @@ func run(ctx context.Context, client rpc.Peer, filter rpc.Filter) error {
ctxmeta := metadata.NewOutgoingContext(context.Background(), meta)
// get the next job from the queue
work, err := client.Next(ctx, filter)
work, err := r.client.Next(ctx, r.filter)
if err != nil {
return err
}
@ -181,7 +192,7 @@ func run(ctx context.Context, client rpc.Peer, filter rpc.Filter) error {
logger.Debug().
Msg("listen for cancel signal")
if werr := client.Wait(ctx, work.ID); werr != nil {
if werr := r.client.Wait(ctx, work.ID); werr != nil {
cancelled.SetTo(true)
logger.Warn().
Err(werr).
@ -206,7 +217,7 @@ func run(ctx context.Context, client rpc.Peer, filter rpc.Filter) error {
logger.Debug().
Msg("pipeline lease renewed")
client.Extend(ctx, work.ID)
r.client.Extend(ctx, work.ID)
}
}
}()
@ -214,7 +225,7 @@ func run(ctx context.Context, client rpc.Peer, filter rpc.Filter) error {
state := rpc.State{}
state.Started = time.Now().Unix()
err = client.Init(ctxmeta, work.ID, state)
err = r.client.Init(ctxmeta, work.ID, state)
if err != nil {
logger.Error().
Err(err).
@ -245,7 +256,7 @@ func run(ctx context.Context, client rpc.Peer, filter rpc.Filter) error {
loglogger.Debug().Msg("log stream opened")
limitedPart := io.LimitReader(part, maxLogsUpload)
logstream := rpc.NewLineWriter(client, work.ID, proc.Alias, secrets...)
logstream := rpc.NewLineWriter(r.client, work.ID, proc.Alias, secrets...)
io.Copy(logstream, limitedPart)
loglogger.Debug().Msg("log stream copied")
@ -261,7 +272,7 @@ func run(ctx context.Context, client rpc.Peer, filter rpc.Filter) error {
loglogger.Debug().
Msg("log stream uploading")
if serr := client.Upload(ctxmeta, work.ID, file); serr != nil {
if serr := r.client.Upload(ctxmeta, work.ID, file); serr != nil {
loglogger.Error().
Err(serr).
Msg("log stream upload error")
@ -301,7 +312,7 @@ func run(ctx context.Context, client rpc.Peer, filter rpc.Filter) error {
Str("mime", file.Mime).
Msg("file stream uploading")
if serr := client.Upload(ctxmeta, work.ID, file); serr != nil {
if serr := r.client.Upload(ctxmeta, work.ID, file); serr != nil {
loglogger.Error().
Err(serr).
Str("file", file.Name).
@ -335,7 +346,7 @@ func run(ctx context.Context, client rpc.Peer, filter rpc.Filter) error {
proclogger.Debug().
Msg("update step status")
if uerr := client.Update(ctxmeta, work.ID, procState); uerr != nil {
if uerr := r.client.Update(ctxmeta, work.ID, procState); uerr != nil {
proclogger.Debug().
Err(uerr).
Msg("update step status error")
@ -350,6 +361,8 @@ func run(ctx context.Context, client rpc.Peer, filter rpc.Filter) error {
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)
@ -413,7 +426,7 @@ func run(ctx context.Context, client rpc.Peer, filter rpc.Filter) error {
Int("exit_code", state.ExitCode).
Msg("updating pipeline status")
err = client.Done(ctxmeta, work.ID, state)
err = r.client.Done(ctxmeta, work.ID, state)
if err != nil {
logger.Error().Err(err).
Msg("updating pipeline status failed")