Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(533)

Side by Side Diff: go/src/infra/tools/kitchen/cook_logdog.go

Issue 2146403002: Kitchen: Generate LogDog prefix from Swarming. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Kitchen: Generate LogDog prefix from Swarming. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The LUCI Authors. All rights reserved. 1 // Copyright 2016 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0 2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file. 3 // that can be found in the LICENSE file.
4 4
5 package main 5 package main
6 6
7 import ( 7 import (
8 "io" 8 "io"
9 "os/exec" 9 "os/exec"
10 "sync" 10 "sync"
11 "time" 11 "time"
12 12
13 "github.com/luci/luci-go/client/logdog/annotee" 13 "github.com/luci/luci-go/client/logdog/annotee"
14 "github.com/luci/luci-go/client/logdog/annotee/annotation" 14 "github.com/luci/luci-go/client/logdog/annotee/annotation"
15 "github.com/luci/luci-go/client/logdog/butler" 15 "github.com/luci/luci-go/client/logdog/butler"
16 "github.com/luci/luci-go/client/logdog/butler/bootstrap" 16 "github.com/luci/luci-go/client/logdog/butler/bootstrap"
17 "github.com/luci/luci-go/client/logdog/butler/output" 17 "github.com/luci/luci-go/client/logdog/butler/output"
18 fileOut "github.com/luci/luci-go/client/logdog/butler/output/file" 18 fileOut "github.com/luci/luci-go/client/logdog/butler/output/file"
19 out "github.com/luci/luci-go/client/logdog/butler/output/logdog" 19 out "github.com/luci/luci-go/client/logdog/butler/output/logdog"
20 "github.com/luci/luci-go/client/logdog/butlerlib/streamclient" 20 "github.com/luci/luci-go/client/logdog/butlerlib/streamclient"
21 "github.com/luci/luci-go/common/auth" 21 "github.com/luci/luci-go/common/auth"
22 "github.com/luci/luci-go/common/config" 22 "github.com/luci/luci-go/common/config"
23 "github.com/luci/luci-go/common/ctxcmd" 23 "github.com/luci/luci-go/common/ctxcmd"
24 "github.com/luci/luci-go/common/environ" 24 "github.com/luci/luci-go/common/environ"
25 "github.com/luci/luci-go/common/errors" 25 "github.com/luci/luci-go/common/errors"
26 "github.com/luci/luci-go/common/logdog/types"
26 log "github.com/luci/luci-go/common/logging" 27 log "github.com/luci/luci-go/common/logging"
27 28
28 "github.com/golang/protobuf/proto" 29 "github.com/golang/protobuf/proto"
29 "golang.org/x/net/context" 30 "golang.org/x/net/context"
30 ) 31 )
31 32
33 // buildLogdogPrefix constructs a LogDog Prefix string from Swarming task
34 // parameters.
35 func buildLogdogPrefix(host, taskID string) (types.StreamName, error) {
36 return types.MakeStreamName("", "swarm", host, taskID)
37 }
38
39 func getLogdogPrefix(env environ.Env) (types.StreamName, error) {
40 // Construct our LogDog prefix from the Swarming task parameters.
41 host, taskID, err := getSwarmingTaskParameters(env)
42 if err != nil {
43 return "", errors.Annotate(err).Reason("failed to get swarming t ask parameters").Err()
44 }
45 prefix, err := buildLogdogPrefix(host, taskID)
46 if err != nil {
47 return "", errors.Annotate(err).Reason("failed to build LogDog p refix.").Err()
48 }
49 return prefix, nil
50 }
51
32 // runWithLogdogButler rus the supplied command through the a LogDog Butler 52 // runWithLogdogButler rus the supplied command through the a LogDog Butler
33 // engine instance. This involves: 53 // engine instance. This involves:
34 // - Determine a LogDog Prefix. 54 // - Determine a LogDog Prefix.
35 // - Configuring / setting up the Butler. 55 // - Configuring / setting up the Butler.
36 // - Initiating a LogDog Pub/Sub Output, registering with remote server. 56 // - Initiating a LogDog Pub/Sub Output, registering with remote server.
37 // - Running the recipe process. 57 // - Running the recipe process.
38 // - Optionally, hook its output streams up through an Annotee processor. 58 // - Optionally, hook its output streams up through an Annotee processor.
39 // - Otherwise, wait for the process to finish. 59 // - Otherwise, wait for the process to finish.
40 // - Shut down the Butler instance. 60 // - Shut down the Butler instance.
41 func (c *cookRun) runWithLogdogButler(ctx context.Context, cmd *exec.Cmd) (rc in t, err error) { 61 func (c *cookRun) runWithLogdogButler(ctx context.Context, cmd *exec.Cmd) (rc in t, err error) {
42 » _ = auth.Authenticator{} 62 » // Get our task's environment.
63 » var env environ.Env
64 » if cmd.Env != nil {
65 » » env = environ.New(cmd.Env)
66 » } else {
67 » » env = environ.System()
68 » }
69
70 » prefix := c.logdog.prefix
71 » if prefix == "" {
72 » » var err error
73 » » prefix, err = getLogdogPrefix(env)
74 » » if err != nil {
75 » » » return 0, errors.Annotate(err).Err()
76 » » }
77 » » log.Fields{
78 » » » "prefix": prefix,
79 » » }.Infof(ctx, "Generated LogDog prefix [%q] from Swarming paramet ers.", prefix)
80 » }
43 81
44 authenticator := auth.NewAuthenticator(ctx, auth.SilentLogin, auth.Optio ns{ 82 authenticator := auth.NewAuthenticator(ctx, auth.SilentLogin, auth.Optio ns{
45 Scopes: out.Scopes(), 83 Scopes: out.Scopes(),
46 }) 84 })
47 85
48 // Register and instantiate our LogDog Output. 86 // Register and instantiate our LogDog Output.
49 var o output.Output 87 var o output.Output
50 if c.logdog.filePath == "" { 88 if c.logdog.filePath == "" {
51 ocfg := out.Config{ 89 ocfg := out.Config{
52 Auth: authenticator, 90 Auth: authenticator,
53 Host: c.logdog.host, 91 Host: c.logdog.host,
54 Project: config.ProjectName(c.logdog.project), 92 Project: config.ProjectName(c.logdog.project),
55 » » » Prefix: c.logdog.prefix, 93 » » » Prefix: prefix,
56 SourceInfo: []string{ 94 SourceInfo: []string{
57 "Kitchen", 95 "Kitchen",
58 }, 96 },
59 PublishContext: withNonCancel(ctx), 97 PublishContext: withNonCancel(ctx),
60 } 98 }
61 99
62 var err error 100 var err error
63 if o, err = ocfg.Register(ctx); err != nil { 101 if o, err = ocfg.Register(ctx); err != nil {
64 return 0, errors.Annotate(err).Reason("failed to create LogDog Output instance").Err() 102 return 0, errors.Annotate(err).Reason("failed to create LogDog Output instance").Err()
65 } 103 }
(...skipping 28 matching lines...) Expand all
94 err = ierr 132 err = ierr
95 } 133 }
96 } 134 }
97 }() 135 }()
98 136
99 // Wrap our incoming command in a CtxCmd. 137 // Wrap our incoming command in a CtxCmd.
100 proc := ctxcmd.CtxCmd{ 138 proc := ctxcmd.CtxCmd{
101 Cmd: cmd, 139 Cmd: cmd,
102 } 140 }
103 141
104 var env environ.Env
105 if proc.Env != nil {
106 env = environ.New(proc.Env)
107 } else {
108 env = environ.System()
109 }
110
111 // Augment our environment with Butler parameters. 142 // Augment our environment with Butler parameters.
112 bsEnv := bootstrap.Environment{ 143 bsEnv := bootstrap.Environment{
113 Project: config.ProjectName(c.logdog.project), 144 Project: config.ProjectName(c.logdog.project),
114 Prefix: c.logdog.prefix, 145 Prefix: c.logdog.prefix,
115 } 146 }
116 bsEnv.Augment(env) 147 bsEnv.Augment(env)
117 proc.Env = env.Sorted() 148 proc.Env = env.Sorted()
118 149
119 // Build pipes for our STDOUT and STDERR streams. 150 // Build pipes for our STDOUT and STDERR streams.
120 stdout, err := proc.StdoutPipe() 151 stdout, err := proc.StdoutPipe()
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 // callbackReadCloser invokes a callback method when closed. 286 // callbackReadCloser invokes a callback method when closed.
256 type callbackReadCloser struct { 287 type callbackReadCloser struct {
257 io.ReadCloser 288 io.ReadCloser
258 callback func() 289 callback func()
259 } 290 }
260 291
261 func (c *callbackReadCloser) Close() error { 292 func (c *callbackReadCloser) Close() error {
262 defer c.callback() 293 defer c.callback()
263 return c.ReadCloser.Close() 294 return c.ReadCloser.Close()
264 } 295 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698