Brad Fitzpatrick | f3c0193 | 2015-01-15 16:29:16 -0800 | [diff] [blame] | 1 | // Copyright 2015 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Brad Fitzpatrick | f3c0193 | 2015-01-15 16:29:16 -0800 | [diff] [blame] | 5 | // Package buildlet contains client tools for working with a buildlet |
| 6 | // server. |
Andrew Gerrand | fa8373a | 2015-01-21 17:25:37 +1100 | [diff] [blame] | 7 | package buildlet // import "golang.org/x/build/buildlet" |
Brad Fitzpatrick | f3c0193 | 2015-01-15 16:29:16 -0800 | [diff] [blame] | 8 | |
| 9 | import ( |
Brad Fitzpatrick | de8994a | 2015-02-09 13:12:20 -0800 | [diff] [blame] | 10 | "bufio" |
David Crawshaw | 581ddd1 | 2015-04-06 08:09:20 -0400 | [diff] [blame] | 11 | "encoding/json" |
Brad Fitzpatrick | f8c2484 | 2015-01-16 09:54:03 -0800 | [diff] [blame] | 12 | "errors" |
Brad Fitzpatrick | f3c0193 | 2015-01-15 16:29:16 -0800 | [diff] [blame] | 13 | "fmt" |
| 14 | "io" |
| 15 | "io/ioutil" |
Brad Fitzpatrick | f68e9f5 | 2015-02-03 11:09:50 +0000 | [diff] [blame] | 16 | "net" |
Brad Fitzpatrick | f3c0193 | 2015-01-15 16:29:16 -0800 | [diff] [blame] | 17 | "net/http" |
Brad Fitzpatrick | f8c2484 | 2015-01-16 09:54:03 -0800 | [diff] [blame] | 18 | "net/url" |
Andrew Gerrand | 91d984c | 2015-02-10 23:25:40 +1100 | [diff] [blame] | 19 | "os" |
Brad Fitzpatrick | f3c0193 | 2015-01-15 16:29:16 -0800 | [diff] [blame] | 20 | "strings" |
Brad Fitzpatrick | 378fb29 | 2015-06-10 13:59:42 -0700 | [diff] [blame] | 21 | "sync" |
Andrew Gerrand | 376b01d | 2015-02-03 12:39:25 +0000 | [diff] [blame] | 22 | "time" |
| 23 | |
| 24 | "golang.org/x/oauth2" |
Brad Fitzpatrick | f3c0193 | 2015-01-15 16:29:16 -0800 | [diff] [blame] | 25 | ) |
| 26 | |
Brad Fitzpatrick | f3c0193 | 2015-01-15 16:29:16 -0800 | [diff] [blame] | 27 | // NewClient returns a *Client that will manipulate ipPort, |
| 28 | // authenticated using the provided keypair. |
| 29 | // |
| 30 | // This constructor returns immediately without testing the host or auth. |
Brad Fitzpatrick | 874c083 | 2015-01-16 12:59:14 -0800 | [diff] [blame] | 31 | func NewClient(ipPort string, kp KeyPair) *Client { |
Brad Fitzpatrick | f3c0193 | 2015-01-15 16:29:16 -0800 | [diff] [blame] | 32 | return &Client{ |
Brad Fitzpatrick | 874c083 | 2015-01-16 12:59:14 -0800 | [diff] [blame] | 33 | ipPort: ipPort, |
| 34 | tls: kp, |
| 35 | password: kp.Password(), |
Brad Fitzpatrick | d4ea014 | 2015-06-12 10:31:58 -0700 | [diff] [blame^] | 36 | peerDead: make(chan struct{}), |
Brad Fitzpatrick | 874c083 | 2015-01-16 12:59:14 -0800 | [diff] [blame] | 37 | httpClient: &http.Client{ |
| 38 | Transport: &http.Transport{ |
Brad Fitzpatrick | f68e9f5 | 2015-02-03 11:09:50 +0000 | [diff] [blame] | 39 | Dial: defaultDialer(), |
Brad Fitzpatrick | 874c083 | 2015-01-16 12:59:14 -0800 | [diff] [blame] | 40 | DialTLS: kp.tlsDialer(), |
| 41 | }, |
| 42 | }, |
Brad Fitzpatrick | f3c0193 | 2015-01-15 16:29:16 -0800 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
Brad Fitzpatrick | 7b2f9d7 | 2015-03-27 17:45:12 +0100 | [diff] [blame] | 46 | // SetCloseFunc sets a function to be called when c.Close is called. |
| 47 | // SetCloseFunc must not be called concurrently with Close. |
| 48 | func (c *Client) SetCloseFunc(fn func() error) { |
| 49 | c.closeFunc = fn |
| 50 | } |
| 51 | |
| 52 | func (c *Client) Close() error { |
Brad Fitzpatrick | d4ea014 | 2015-06-12 10:31:58 -0700 | [diff] [blame^] | 53 | c.setPeerDead(errors.New("Close called")) |
Brad Fitzpatrick | 7b2f9d7 | 2015-03-27 17:45:12 +0100 | [diff] [blame] | 54 | var err error |
| 55 | if c.closeFunc != nil { |
| 56 | err = c.closeFunc() |
| 57 | c.closeFunc = nil |
| 58 | } |
| 59 | return err |
| 60 | } |
| 61 | |
Brad Fitzpatrick | d4ea014 | 2015-06-12 10:31:58 -0700 | [diff] [blame^] | 62 | // To be called only via c.setPeerDeadOnce.Do(s.setPeerDead) |
| 63 | func (c *Client) setPeerDead(err error) { |
| 64 | c.setPeerDeadOnce.Do(func() { |
| 65 | c.deadErr = err |
| 66 | close(c.peerDead) |
| 67 | }) |
| 68 | } |
| 69 | |
Brad Fitzpatrick | 7b2f9d7 | 2015-03-27 17:45:12 +0100 | [diff] [blame] | 70 | // SetDescription sets a short description of where the buildlet |
| 71 | // connection came from. This is used by the build coordinator status |
| 72 | // page, mostly for debugging. |
| 73 | func (c *Client) SetDescription(v string) { |
| 74 | c.desc = v |
| 75 | } |
| 76 | |
David Crawshaw | 581ddd1 | 2015-04-06 08:09:20 -0400 | [diff] [blame] | 77 | // SetHTTPClient replaces the underlying HTTP client. |
Brad Fitzpatrick | d4ea014 | 2015-06-12 10:31:58 -0700 | [diff] [blame^] | 78 | // It should only be called before the Client is used. |
David Crawshaw | 581ddd1 | 2015-04-06 08:09:20 -0400 | [diff] [blame] | 79 | func (c *Client) SetHTTPClient(httpClient *http.Client) { |
| 80 | c.httpClient = httpClient |
| 81 | } |
| 82 | |
Brad Fitzpatrick | d4ea014 | 2015-06-12 10:31:58 -0700 | [diff] [blame^] | 83 | // EnableHeartbeats enables background heartbeating |
| 84 | // against the peer. |
| 85 | // It should only be called before the Client is used. |
| 86 | func (c *Client) EnableHeartbeats() { |
| 87 | // TODO(bradfitz): make this always enabled, once the |
| 88 | // reverse buildlet connection model supports |
| 89 | // multiple connections at once. |
| 90 | c.heartbeat = true |
| 91 | } |
| 92 | |
Brad Fitzpatrick | f68e9f5 | 2015-02-03 11:09:50 +0000 | [diff] [blame] | 93 | // defaultDialer returns the net/http package's default Dial function. |
| 94 | // Notably, this sets TCP keep-alive values, so when we kill VMs |
| 95 | // (whose TCP stacks stop replying, forever), we don't leak file |
| 96 | // descriptors for otherwise forever-stalled TCP connections. |
| 97 | func defaultDialer() func(network, addr string) (net.Conn, error) { |
| 98 | if fn := http.DefaultTransport.(*http.Transport).Dial; fn != nil { |
| 99 | return fn |
| 100 | } |
| 101 | return net.Dial |
| 102 | } |
| 103 | |
Brad Fitzpatrick | f3c0193 | 2015-01-15 16:29:16 -0800 | [diff] [blame] | 104 | // A Client interacts with a single buildlet. |
| 105 | type Client struct { |
Brad Fitzpatrick | 874c083 | 2015-01-16 12:59:14 -0800 | [diff] [blame] | 106 | ipPort string |
| 107 | tls KeyPair |
| 108 | password string // basic auth password or empty for none |
| 109 | httpClient *http.Client |
Brad Fitzpatrick | d4ea014 | 2015-06-12 10:31:58 -0700 | [diff] [blame^] | 110 | heartbeat bool // whether to heartbeat in the background |
Brad Fitzpatrick | 7b2f9d7 | 2015-03-27 17:45:12 +0100 | [diff] [blame] | 111 | |
| 112 | closeFunc func() error |
| 113 | desc string |
Brad Fitzpatrick | 378fb29 | 2015-06-10 13:59:42 -0700 | [diff] [blame] | 114 | |
Brad Fitzpatrick | d4ea014 | 2015-06-12 10:31:58 -0700 | [diff] [blame^] | 115 | initHeartbeatOnce sync.Once |
| 116 | setPeerDeadOnce sync.Once |
| 117 | peerDead chan struct{} // closed on peer death |
| 118 | deadErr error // guarded by peerDead's close |
| 119 | |
Brad Fitzpatrick | 378fb29 | 2015-06-10 13:59:42 -0700 | [diff] [blame] | 120 | mu sync.Mutex |
| 121 | broken bool // client is broken in some way |
Brad Fitzpatrick | 7b2f9d7 | 2015-03-27 17:45:12 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | func (c *Client) String() string { |
| 125 | if c == nil { |
| 126 | return "(nil *buildlet.Client)" |
| 127 | } |
| 128 | return strings.TrimSpace(c.URL() + " " + c.desc) |
Brad Fitzpatrick | f3c0193 | 2015-01-15 16:29:16 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // URL returns the buildlet's URL prefix, without a trailing slash. |
| 132 | func (c *Client) URL() string { |
Brad Fitzpatrick | 874c083 | 2015-01-16 12:59:14 -0800 | [diff] [blame] | 133 | if !c.tls.IsZero() { |
| 134 | return "https://" + strings.TrimSuffix(c.ipPort, ":443") |
Brad Fitzpatrick | f3c0193 | 2015-01-15 16:29:16 -0800 | [diff] [blame] | 135 | } |
Brad Fitzpatrick | 874c083 | 2015-01-16 12:59:14 -0800 | [diff] [blame] | 136 | return "http://" + strings.TrimSuffix(c.ipPort, ":80") |
| 137 | } |
| 138 | |
Brad Fitzpatrick | 79f3fc0 | 2015-05-27 21:51:25 -0700 | [diff] [blame] | 139 | func (c *Client) IPPort() string { return c.ipPort } |
| 140 | |
Brad Fitzpatrick | 378fb29 | 2015-06-10 13:59:42 -0700 | [diff] [blame] | 141 | // MarkBroken marks this client as broken in some way. |
| 142 | func (c *Client) MarkBroken() { |
| 143 | c.mu.Lock() |
| 144 | defer c.mu.Unlock() |
| 145 | c.broken = true |
| 146 | } |
| 147 | |
| 148 | // IsBroken reports whether this client is broken in some way. |
| 149 | func (c *Client) IsBroken() bool { |
| 150 | c.mu.Lock() |
| 151 | defer c.mu.Unlock() |
| 152 | return c.broken |
| 153 | } |
| 154 | |
Brad Fitzpatrick | 874c083 | 2015-01-16 12:59:14 -0800 | [diff] [blame] | 155 | func (c *Client) do(req *http.Request) (*http.Response, error) { |
Brad Fitzpatrick | d4ea014 | 2015-06-12 10:31:58 -0700 | [diff] [blame^] | 156 | c.initHeartbeatOnce.Do(c.initHeartbeats) |
Brad Fitzpatrick | 874c083 | 2015-01-16 12:59:14 -0800 | [diff] [blame] | 157 | if c.password != "" { |
| 158 | req.SetBasicAuth("gomote", c.password) |
| 159 | } |
| 160 | return c.httpClient.Do(req) |
Brad Fitzpatrick | f3c0193 | 2015-01-15 16:29:16 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Brad Fitzpatrick | d4ea014 | 2015-06-12 10:31:58 -0700 | [diff] [blame^] | 163 | func (c *Client) initHeartbeats() { |
| 164 | if !c.heartbeat { |
| 165 | // TODO(bradfitz): make this always enabled later, once |
| 166 | // reverse buildlets are fixed. |
| 167 | return |
| 168 | } |
| 169 | go c.heartbeatLoop() |
| 170 | } |
| 171 | |
| 172 | func (c *Client) heartbeatLoop() { |
| 173 | for { |
| 174 | select { |
| 175 | case <-c.peerDead: |
| 176 | // Already dead by something else. |
| 177 | // Most likely: c.Close was called. |
| 178 | return |
| 179 | case <-time.After(10 * time.Second): |
| 180 | t0 := time.Now() |
| 181 | if _, err := c.Status(); err != nil { |
| 182 | err := fmt.Errorf("Buildlet %v failed heartbeat after %v; marking dead; err=%v", c, time.Since(t0), err) |
| 183 | c.MarkBroken() |
| 184 | c.setPeerDead(err) |
| 185 | return |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
Brad Fitzpatrick | 0802522 | 2015-06-09 09:12:17 -0700 | [diff] [blame] | 191 | var errHeaderTimeout = errors.New("timeout waiting for headers") |
| 192 | |
| 193 | // doHeaderTimeout calls c.do(req) and returns its results, or |
| 194 | // errHeaderTimeout if max elapses first. |
| 195 | func (c *Client) doHeaderTimeout(req *http.Request, max time.Duration) (res *http.Response, err error) { |
| 196 | type resErr struct { |
| 197 | res *http.Response |
| 198 | err error |
| 199 | } |
| 200 | resErrc := make(chan resErr, 1) |
| 201 | go func() { |
| 202 | res, err := c.do(req) |
| 203 | resErrc <- resErr{res, err} |
| 204 | }() |
| 205 | |
| 206 | timer := time.NewTimer(max) |
| 207 | defer timer.Stop() |
| 208 | |
Brad Fitzpatrick | d4ea014 | 2015-06-12 10:31:58 -0700 | [diff] [blame^] | 209 | cleanup := func() { |
| 210 | if re := <-resErrc; re.res != nil { |
| 211 | re.res.Body.Close() |
| 212 | } |
| 213 | } |
| 214 | |
Brad Fitzpatrick | 0802522 | 2015-06-09 09:12:17 -0700 | [diff] [blame] | 215 | select { |
| 216 | case re := <-resErrc: |
| 217 | return re.res, re.err |
Brad Fitzpatrick | d4ea014 | 2015-06-12 10:31:58 -0700 | [diff] [blame^] | 218 | case <-c.peerDead: |
| 219 | go cleanup() |
| 220 | return nil, c.deadErr |
Brad Fitzpatrick | 0802522 | 2015-06-09 09:12:17 -0700 | [diff] [blame] | 221 | case <-timer.C: |
Brad Fitzpatrick | d4ea014 | 2015-06-12 10:31:58 -0700 | [diff] [blame^] | 222 | go cleanup() |
Brad Fitzpatrick | 0802522 | 2015-06-09 09:12:17 -0700 | [diff] [blame] | 223 | return nil, errHeaderTimeout |
| 224 | } |
| 225 | } |
| 226 | |
Brad Fitzpatrick | 0cc0461 | 2015-01-19 19:43:25 -0800 | [diff] [blame] | 227 | // doOK sends the request and expects a 200 OK response. |
| 228 | func (c *Client) doOK(req *http.Request) error { |
Brad Fitzpatrick | 874c083 | 2015-01-16 12:59:14 -0800 | [diff] [blame] | 229 | res, err := c.do(req) |
Brad Fitzpatrick | f3c0193 | 2015-01-15 16:29:16 -0800 | [diff] [blame] | 230 | if err != nil { |
| 231 | return err |
| 232 | } |
| 233 | defer res.Body.Close() |
Brad Fitzpatrick | 0cc0461 | 2015-01-19 19:43:25 -0800 | [diff] [blame] | 234 | if res.StatusCode != http.StatusOK { |
Brad Fitzpatrick | f3c0193 | 2015-01-15 16:29:16 -0800 | [diff] [blame] | 235 | slurp, _ := ioutil.ReadAll(io.LimitReader(res.Body, 4<<10)) |
| 236 | return fmt.Errorf("%v; body: %s", res.Status, slurp) |
| 237 | } |
| 238 | return nil |
| 239 | } |
Brad Fitzpatrick | f8c2484 | 2015-01-16 09:54:03 -0800 | [diff] [blame] | 240 | |
Brad Fitzpatrick | 0cc0461 | 2015-01-19 19:43:25 -0800 | [diff] [blame] | 241 | // PutTar writes files to the remote buildlet, rooted at the relative |
| 242 | // directory dir. |
| 243 | // If dir is empty, they're placed at the root of the buildlet's work directory. |
| 244 | // The dir is created if necessary. |
| 245 | // The Reader must be of a tar.gz file. |
| 246 | func (c *Client) PutTar(r io.Reader, dir string) error { |
| 247 | req, err := http.NewRequest("PUT", c.URL()+"/writetgz?dir="+url.QueryEscape(dir), r) |
| 248 | if err != nil { |
| 249 | return err |
| 250 | } |
| 251 | return c.doOK(req) |
| 252 | } |
| 253 | |
| 254 | // PutTarFromURL tells the buildlet to download the tar.gz file from tarURL |
| 255 | // and write it to dir, a relative directory from the workdir. |
| 256 | // If dir is empty, they're placed at the root of the buildlet's work directory. |
| 257 | // The dir is created if necessary. |
| 258 | // The url must be of a tar.gz file. |
| 259 | func (c *Client) PutTarFromURL(tarURL, dir string) error { |
| 260 | form := url.Values{ |
| 261 | "url": {tarURL}, |
| 262 | } |
| 263 | req, err := http.NewRequest("POST", c.URL()+"/writetgz?dir="+url.QueryEscape(dir), strings.NewReader(form.Encode())) |
| 264 | if err != nil { |
| 265 | return err |
| 266 | } |
| 267 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 268 | return c.doOK(req) |
| 269 | } |
| 270 | |
Andrew Gerrand | 91d984c | 2015-02-10 23:25:40 +1100 | [diff] [blame] | 271 | // Put writes the provided file to path (relative to workdir) and sets mode. |
| 272 | func (c *Client) Put(r io.Reader, path string, mode os.FileMode) error { |
| 273 | param := url.Values{ |
| 274 | "path": {path}, |
| 275 | "mode": {fmt.Sprint(int64(mode))}, |
| 276 | } |
| 277 | req, err := http.NewRequest("PUT", c.URL()+"/write?"+param.Encode(), r) |
| 278 | if err != nil { |
| 279 | return err |
| 280 | } |
| 281 | return c.doOK(req) |
| 282 | } |
| 283 | |
Brad Fitzpatrick | b35ba9f | 2015-01-19 20:53:34 -0800 | [diff] [blame] | 284 | // GetTar returns a .tar.gz stream of the given directory, relative to the buildlet's work dir. |
| 285 | // The provided dir may be empty to get everything. |
| 286 | func (c *Client) GetTar(dir string) (tgz io.ReadCloser, err error) { |
| 287 | req, err := http.NewRequest("GET", c.URL()+"/tgz?dir="+url.QueryEscape(dir), nil) |
| 288 | if err != nil { |
| 289 | return nil, err |
| 290 | } |
| 291 | res, err := c.do(req) |
| 292 | if err != nil { |
| 293 | return nil, err |
| 294 | } |
| 295 | if res.StatusCode != http.StatusOK { |
| 296 | slurp, _ := ioutil.ReadAll(io.LimitReader(res.Body, 4<<10)) |
| 297 | res.Body.Close() |
| 298 | return nil, fmt.Errorf("%v; body: %s", res.Status, slurp) |
| 299 | } |
| 300 | return res.Body, nil |
| 301 | } |
| 302 | |
Brad Fitzpatrick | f8c2484 | 2015-01-16 09:54:03 -0800 | [diff] [blame] | 303 | // ExecOpts are options for a remote command invocation. |
| 304 | type ExecOpts struct { |
| 305 | // Output is the output of stdout and stderr. |
| 306 | // If nil, the output is discarded. |
| 307 | Output io.Writer |
| 308 | |
Andrew Gerrand | 6097812 | 2015-02-11 11:17:46 +1100 | [diff] [blame] | 309 | // Dir is the directory from which to execute the command. |
| 310 | // It is optional. If not specified, it defaults to the directory of |
| 311 | // the command, or the work directory if SystemLevel is set. |
| 312 | Dir string |
| 313 | |
Brad Fitzpatrick | b35ba9f | 2015-01-19 20:53:34 -0800 | [diff] [blame] | 314 | // Args are the arguments to pass to the cmd given to Client.Exec. |
| 315 | Args []string |
| 316 | |
Brad Fitzpatrick | 32d0520 | 2015-01-21 15:15:48 -0800 | [diff] [blame] | 317 | // ExtraEnv are KEY=VALUE pairs to append to the buildlet |
| 318 | // process's environment. |
| 319 | ExtraEnv []string |
| 320 | |
Andrew Gerrand | 1fc56ca | 2015-05-21 13:01:10 +1000 | [diff] [blame] | 321 | // Path, if non-nil, specifies the PATH variable of the executed |
| 322 | // process's environment. A non-nil empty list clears the path. |
| 323 | // The following expansions apply: |
| 324 | // - the string "$PATH" expands to any existing PATH element(s) |
| 325 | // - the substring "$WORKDIR" expands to buildlet's temp workdir |
| 326 | // After expansions, the list is joined with an OS-specific list |
| 327 | // separator and supplied to the executed process as its PATH |
| 328 | // environment variable. |
| 329 | Path []string |
| 330 | |
Brad Fitzpatrick | b35ba9f | 2015-01-19 20:53:34 -0800 | [diff] [blame] | 331 | // SystemLevel controls whether the command is run outside of |
| 332 | // the buildlet's environment. |
| 333 | SystemLevel bool |
| 334 | |
Brad Fitzpatrick | de8994a | 2015-02-09 13:12:20 -0800 | [diff] [blame] | 335 | // Debug, if true, instructs to the buildlet to print extra debug |
| 336 | // info to the output before the command begins executing. |
| 337 | Debug bool |
| 338 | |
Brad Fitzpatrick | f8c2484 | 2015-01-16 09:54:03 -0800 | [diff] [blame] | 339 | // OnStartExec is an optional hook that runs after the 200 OK |
| 340 | // response from the buildlet, but before the output begins |
| 341 | // writing to Output. |
| 342 | OnStartExec func() |
Brad Fitzpatrick | d4ea014 | 2015-06-12 10:31:58 -0700 | [diff] [blame^] | 343 | |
| 344 | // Timeout is an optional duration before ErrTimeout is returned. |
| 345 | Timeout time.Duration |
Brad Fitzpatrick | f8c2484 | 2015-01-16 09:54:03 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Brad Fitzpatrick | d4ea014 | 2015-06-12 10:31:58 -0700 | [diff] [blame^] | 348 | var ErrTimeout = errors.New("buildlet: timeout waiting for command to complete") |
| 349 | |
Brad Fitzpatrick | f8c2484 | 2015-01-16 09:54:03 -0800 | [diff] [blame] | 350 | // Exec runs cmd on the buildlet. |
| 351 | // |
| 352 | // Two errors are returned: one is whether the command succeeded |
| 353 | // remotely (remoteErr), and the second (execErr) is whether there |
| 354 | // were system errors preventing the command from being started or |
| 355 | // seen to completition. If execErr is non-nil, the remoteErr is |
| 356 | // meaningless. |
| 357 | func (c *Client) Exec(cmd string, opts ExecOpts) (remoteErr, execErr error) { |
Brad Fitzpatrick | b35ba9f | 2015-01-19 20:53:34 -0800 | [diff] [blame] | 358 | var mode string |
| 359 | if opts.SystemLevel { |
| 360 | mode = "sys" |
| 361 | } |
Andrew Gerrand | 1fc56ca | 2015-05-21 13:01:10 +1000 | [diff] [blame] | 362 | path := opts.Path |
| 363 | if len(path) == 0 && path != nil { |
| 364 | // url.Values doesn't distinguish between a nil slice and |
| 365 | // a non-nil zero-length slice, so use this sentinel value. |
| 366 | path = []string{"$EMPTY"} |
| 367 | } |
Brad Fitzpatrick | 874c083 | 2015-01-16 12:59:14 -0800 | [diff] [blame] | 368 | form := url.Values{ |
Brad Fitzpatrick | b35ba9f | 2015-01-19 20:53:34 -0800 | [diff] [blame] | 369 | "cmd": {cmd}, |
| 370 | "mode": {mode}, |
Andrew Gerrand | 6097812 | 2015-02-11 11:17:46 +1100 | [diff] [blame] | 371 | "dir": {opts.Dir}, |
Brad Fitzpatrick | b35ba9f | 2015-01-19 20:53:34 -0800 | [diff] [blame] | 372 | "cmdArg": opts.Args, |
Brad Fitzpatrick | 32d0520 | 2015-01-21 15:15:48 -0800 | [diff] [blame] | 373 | "env": opts.ExtraEnv, |
Andrew Gerrand | 1fc56ca | 2015-05-21 13:01:10 +1000 | [diff] [blame] | 374 | "path": path, |
Brad Fitzpatrick | de8994a | 2015-02-09 13:12:20 -0800 | [diff] [blame] | 375 | "debug": {fmt.Sprint(opts.Debug)}, |
Brad Fitzpatrick | 874c083 | 2015-01-16 12:59:14 -0800 | [diff] [blame] | 376 | } |
| 377 | req, err := http.NewRequest("POST", c.URL()+"/exec", strings.NewReader(form.Encode())) |
| 378 | if err != nil { |
| 379 | return nil, err |
| 380 | } |
| 381 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
Brad Fitzpatrick | 0802522 | 2015-06-09 09:12:17 -0700 | [diff] [blame] | 382 | |
| 383 | // The first thing the buildlet's exec handler does is flush the headers, so |
Brad Fitzpatrick | d4ea014 | 2015-06-12 10:31:58 -0700 | [diff] [blame^] | 384 | // 10 seconds should be plenty of time, regardless of where on the planet |
Brad Fitzpatrick | 0802522 | 2015-06-09 09:12:17 -0700 | [diff] [blame] | 385 | // (Atlanta, Paris, etc) the reverse buildlet is: |
Brad Fitzpatrick | d4ea014 | 2015-06-12 10:31:58 -0700 | [diff] [blame^] | 386 | res, err := c.doHeaderTimeout(req, 10*time.Second) |
Brad Fitzpatrick | 0802522 | 2015-06-09 09:12:17 -0700 | [diff] [blame] | 387 | if err == errHeaderTimeout { |
Brad Fitzpatrick | 378fb29 | 2015-06-10 13:59:42 -0700 | [diff] [blame] | 388 | c.MarkBroken() |
Brad Fitzpatrick | 0802522 | 2015-06-09 09:12:17 -0700 | [diff] [blame] | 389 | return nil, errors.New("buildlet: timeout waiting for exec header response") |
| 390 | } |
Brad Fitzpatrick | f8c2484 | 2015-01-16 09:54:03 -0800 | [diff] [blame] | 391 | if err != nil { |
| 392 | return nil, err |
| 393 | } |
| 394 | defer res.Body.Close() |
| 395 | if res.StatusCode != http.StatusOK { |
| 396 | slurp, _ := ioutil.ReadAll(io.LimitReader(res.Body, 4<<10)) |
| 397 | return nil, fmt.Errorf("buildlet: HTTP status %v: %s", res.Status, slurp) |
| 398 | } |
| 399 | condRun(opts.OnStartExec) |
| 400 | |
Brad Fitzpatrick | d4ea014 | 2015-06-12 10:31:58 -0700 | [diff] [blame^] | 401 | type errs struct { |
| 402 | remoteErr, execErr error |
Brad Fitzpatrick | f8c2484 | 2015-01-16 09:54:03 -0800 | [diff] [blame] | 403 | } |
Brad Fitzpatrick | d4ea014 | 2015-06-12 10:31:58 -0700 | [diff] [blame^] | 404 | resc := make(chan errs, 1) |
| 405 | go func() { |
| 406 | // Stream the output: |
| 407 | out := opts.Output |
| 408 | if out == nil { |
| 409 | out = ioutil.Discard |
| 410 | } |
| 411 | if _, err := io.Copy(out, res.Body); err != nil { |
| 412 | resc <- errs{execErr: fmt.Errorf("error copying response: %v", err)} |
| 413 | return |
| 414 | } |
Brad Fitzpatrick | f8c2484 | 2015-01-16 09:54:03 -0800 | [diff] [blame] | 415 | |
Brad Fitzpatrick | d4ea014 | 2015-06-12 10:31:58 -0700 | [diff] [blame^] | 416 | // Don't record to the dashboard unless we heard the trailer from |
| 417 | // the buildlet, otherwise it was probably some unrelated error |
| 418 | // (like the VM being killed, or the buildlet crashing due to |
| 419 | // e.g. https://golang.org/issue/9309, since we require a tip |
| 420 | // build of the buildlet to get Trailers support) |
| 421 | state := res.Trailer.Get("Process-State") |
| 422 | if state == "" { |
| 423 | resc <- errs{execErr: errors.New("missing Process-State trailer from HTTP response; buildlet built with old (<= 1.4) Go?")} |
| 424 | return |
| 425 | } |
| 426 | if state != "ok" { |
| 427 | resc <- errs{remoteErr: errors.New(state)} |
| 428 | } else { |
| 429 | resc <- errs{} // success |
| 430 | } |
| 431 | }() |
| 432 | var timer <-chan time.Time |
| 433 | if opts.Timeout > 0 { |
| 434 | t := time.NewTimer(opts.Timeout) |
| 435 | defer t.Stop() |
| 436 | timer = t.C |
Brad Fitzpatrick | f8c2484 | 2015-01-16 09:54:03 -0800 | [diff] [blame] | 437 | } |
Brad Fitzpatrick | d4ea014 | 2015-06-12 10:31:58 -0700 | [diff] [blame^] | 438 | select { |
| 439 | case <-timer: |
| 440 | c.MarkBroken() |
| 441 | return nil, ErrTimeout |
| 442 | case res := <-resc: |
| 443 | return res.remoteErr, res.execErr |
| 444 | case <-c.peerDead: |
| 445 | return nil, c.deadErr |
Brad Fitzpatrick | f8c2484 | 2015-01-16 09:54:03 -0800 | [diff] [blame] | 446 | } |
Brad Fitzpatrick | f8c2484 | 2015-01-16 09:54:03 -0800 | [diff] [blame] | 447 | } |
| 448 | |
Brad Fitzpatrick | 874c083 | 2015-01-16 12:59:14 -0800 | [diff] [blame] | 449 | // Destroy shuts down the buildlet, destroying all state immediately. |
| 450 | func (c *Client) Destroy() error { |
| 451 | req, err := http.NewRequest("POST", c.URL()+"/halt", nil) |
| 452 | if err != nil { |
| 453 | return err |
| 454 | } |
Andrew Gerrand | 212bff2 | 2015-02-02 12:04:07 +0000 | [diff] [blame] | 455 | return c.doOK(req) |
| 456 | } |
| 457 | |
| 458 | // RemoveAll deletes the provided paths, relative to the work directory. |
| 459 | func (c *Client) RemoveAll(paths ...string) error { |
Brad Fitzpatrick | de8994a | 2015-02-09 13:12:20 -0800 | [diff] [blame] | 460 | if len(paths) == 0 { |
| 461 | return nil |
| 462 | } |
Andrew Gerrand | 212bff2 | 2015-02-02 12:04:07 +0000 | [diff] [blame] | 463 | form := url.Values{"path": paths} |
| 464 | req, err := http.NewRequest("POST", c.URL()+"/removeall", strings.NewReader(form.Encode())) |
Brad Fitzpatrick | 874c083 | 2015-01-16 12:59:14 -0800 | [diff] [blame] | 465 | if err != nil { |
| 466 | return err |
| 467 | } |
Andrew Gerrand | 84aecb2 | 2015-02-04 15:54:43 +0000 | [diff] [blame] | 468 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
Andrew Gerrand | 212bff2 | 2015-02-02 12:04:07 +0000 | [diff] [blame] | 469 | return c.doOK(req) |
Brad Fitzpatrick | 874c083 | 2015-01-16 12:59:14 -0800 | [diff] [blame] | 470 | } |
| 471 | |
Andrew Gerrand | 376b01d | 2015-02-03 12:39:25 +0000 | [diff] [blame] | 472 | // DestroyVM shuts down the buildlet and destroys the VM instance. |
| 473 | func (c *Client) DestroyVM(ts oauth2.TokenSource, proj, zone, instance string) error { |
| 474 | gceErrc := make(chan error, 1) |
| 475 | buildletErrc := make(chan error, 1) |
| 476 | go func() { |
| 477 | gceErrc <- DestroyVM(ts, proj, zone, instance) |
| 478 | }() |
| 479 | go func() { |
| 480 | buildletErrc <- c.Destroy() |
| 481 | }() |
| 482 | timeout := time.NewTimer(5 * time.Second) |
| 483 | defer timeout.Stop() |
| 484 | |
| 485 | var retErr error |
| 486 | var gceDone, buildletDone bool |
| 487 | for !gceDone || !buildletDone { |
| 488 | select { |
| 489 | case err := <-gceErrc: |
| 490 | if err != nil { |
| 491 | retErr = err |
| 492 | } |
| 493 | gceDone = true |
| 494 | case err := <-buildletErrc: |
| 495 | if err != nil { |
| 496 | retErr = err |
| 497 | } |
| 498 | buildletDone = true |
| 499 | case <-timeout.C: |
| 500 | e := "" |
| 501 | if !buildletDone { |
| 502 | e = "timeout asking buildlet to shut down" |
| 503 | } |
| 504 | if !gceDone { |
| 505 | if e != "" { |
| 506 | e += " and " |
| 507 | } |
| 508 | e += "timeout asking GCE to delete builder VM" |
| 509 | } |
| 510 | return errors.New(e) |
| 511 | } |
| 512 | } |
| 513 | return retErr |
| 514 | } |
| 515 | |
David Crawshaw | a3dce2c | 2015-04-07 19:46:19 -0400 | [diff] [blame] | 516 | // Status provides status information about the buildlet. |
David Crawshaw | 581ddd1 | 2015-04-06 08:09:20 -0400 | [diff] [blame] | 517 | // |
| 518 | // A coordinator can use the provided information to decide what, if anything, |
| 519 | // to do with a buildlet. |
David Crawshaw | a3dce2c | 2015-04-07 19:46:19 -0400 | [diff] [blame] | 520 | type Status struct { |
| 521 | Version int // buildlet version, coordinator rejects any value less than 1. |
David Crawshaw | 581ddd1 | 2015-04-06 08:09:20 -0400 | [diff] [blame] | 522 | } |
| 523 | |
David Crawshaw | a3dce2c | 2015-04-07 19:46:19 -0400 | [diff] [blame] | 524 | // Status returns an Status value describing this buildlet. |
| 525 | func (c *Client) Status() (Status, error) { |
| 526 | req, err := http.NewRequest("GET", c.URL()+"/status", nil) |
David Crawshaw | 581ddd1 | 2015-04-06 08:09:20 -0400 | [diff] [blame] | 527 | if err != nil { |
David Crawshaw | a3dce2c | 2015-04-07 19:46:19 -0400 | [diff] [blame] | 528 | return Status{}, err |
David Crawshaw | 581ddd1 | 2015-04-06 08:09:20 -0400 | [diff] [blame] | 529 | } |
Brad Fitzpatrick | d4ea014 | 2015-06-12 10:31:58 -0700 | [diff] [blame^] | 530 | resp, err := c.doHeaderTimeout(req, 10*time.Second) // plenty of time |
David Crawshaw | 581ddd1 | 2015-04-06 08:09:20 -0400 | [diff] [blame] | 531 | if err != nil { |
David Crawshaw | a3dce2c | 2015-04-07 19:46:19 -0400 | [diff] [blame] | 532 | return Status{}, err |
David Crawshaw | 581ddd1 | 2015-04-06 08:09:20 -0400 | [diff] [blame] | 533 | } |
| 534 | if resp.StatusCode != http.StatusOK { |
David Crawshaw | a3dce2c | 2015-04-07 19:46:19 -0400 | [diff] [blame] | 535 | return Status{}, errors.New(resp.Status) |
David Crawshaw | 581ddd1 | 2015-04-06 08:09:20 -0400 | [diff] [blame] | 536 | } |
| 537 | b, err := ioutil.ReadAll(resp.Body) |
| 538 | resp.Body.Close() |
| 539 | if err != nil { |
David Crawshaw | a3dce2c | 2015-04-07 19:46:19 -0400 | [diff] [blame] | 540 | return Status{}, err |
David Crawshaw | 581ddd1 | 2015-04-06 08:09:20 -0400 | [diff] [blame] | 541 | } |
David Crawshaw | a3dce2c | 2015-04-07 19:46:19 -0400 | [diff] [blame] | 542 | var status Status |
| 543 | if err := json.Unmarshal(b, &status); err != nil { |
| 544 | return Status{}, err |
David Crawshaw | 581ddd1 | 2015-04-06 08:09:20 -0400 | [diff] [blame] | 545 | } |
David Crawshaw | a3dce2c | 2015-04-07 19:46:19 -0400 | [diff] [blame] | 546 | return status, nil |
David Crawshaw | 581ddd1 | 2015-04-06 08:09:20 -0400 | [diff] [blame] | 547 | } |
| 548 | |
Andrew Gerrand | 84aecb2 | 2015-02-04 15:54:43 +0000 | [diff] [blame] | 549 | // WorkDir returns the absolute path to the buildlet work directory. |
| 550 | func (c *Client) WorkDir() (string, error) { |
| 551 | req, err := http.NewRequest("GET", c.URL()+"/workdir", nil) |
| 552 | if err != nil { |
| 553 | return "", err |
| 554 | } |
Brad Fitzpatrick | d4ea014 | 2015-06-12 10:31:58 -0700 | [diff] [blame^] | 555 | resp, err := c.doHeaderTimeout(req, 10*time.Second) // plenty of time |
Andrew Gerrand | 84aecb2 | 2015-02-04 15:54:43 +0000 | [diff] [blame] | 556 | if err != nil { |
| 557 | return "", err |
| 558 | } |
| 559 | if resp.StatusCode != http.StatusOK { |
| 560 | return "", errors.New(resp.Status) |
| 561 | } |
| 562 | b, err := ioutil.ReadAll(resp.Body) |
| 563 | resp.Body.Close() |
| 564 | if err != nil { |
| 565 | return "", err |
| 566 | } |
| 567 | return string(b), nil |
| 568 | } |
| 569 | |
Brad Fitzpatrick | de8994a | 2015-02-09 13:12:20 -0800 | [diff] [blame] | 570 | // DirEntry is the information about a file on a buildlet. |
| 571 | type DirEntry struct { |
| 572 | // line is of the form "drw-rw-rw\t<name>" and then if a regular file, |
| 573 | // also "\t<size>\t<modtime>". in either case, without trailing newline. |
| 574 | // TODO: break into parsed fields? |
| 575 | line string |
| 576 | } |
| 577 | |
| 578 | func (de DirEntry) String() string { |
| 579 | return de.line |
| 580 | } |
| 581 | |
| 582 | func (de DirEntry) Name() string { |
| 583 | f := strings.Split(de.line, "\t") |
| 584 | if len(f) < 2 { |
| 585 | return "" |
| 586 | } |
| 587 | return f[1] |
| 588 | } |
| 589 | |
| 590 | func (de DirEntry) Digest() string { |
| 591 | f := strings.Split(de.line, "\t") |
| 592 | if len(f) < 5 { |
| 593 | return "" |
| 594 | } |
| 595 | return f[4] |
| 596 | } |
| 597 | |
| 598 | // ListDirOpts are options for Client.ListDir. |
| 599 | type ListDirOpts struct { |
| 600 | // Recursive controls whether the directory is listed |
| 601 | // recursively. |
| 602 | Recursive bool |
| 603 | |
| 604 | // Skip are the directories to skip, relative to the directory |
| 605 | // passed to ListDir. Each item should contain only forward |
| 606 | // slashes and not start or end in slashes. |
| 607 | Skip []string |
| 608 | |
| 609 | // Digest controls whether the SHA-1 digests of regular files |
| 610 | // are returned. |
| 611 | Digest bool |
| 612 | } |
| 613 | |
| 614 | // ListDir lists the contents of a directory. |
| 615 | // The fn callback is run for each entry. |
| 616 | func (c *Client) ListDir(dir string, opts ListDirOpts, fn func(DirEntry)) error { |
| 617 | param := url.Values{ |
| 618 | "dir": {dir}, |
| 619 | "recursive": {fmt.Sprint(opts.Recursive)}, |
| 620 | "skip": opts.Skip, |
| 621 | "digest": {fmt.Sprint(opts.Digest)}, |
| 622 | } |
| 623 | req, err := http.NewRequest("GET", c.URL()+"/ls?"+param.Encode(), nil) |
| 624 | if err != nil { |
| 625 | return err |
| 626 | } |
| 627 | resp, err := c.do(req) |
| 628 | if err != nil { |
| 629 | return err |
| 630 | } |
| 631 | defer resp.Body.Close() |
| 632 | if resp.StatusCode != http.StatusOK { |
| 633 | return errors.New(resp.Status) |
| 634 | } |
| 635 | sc := bufio.NewScanner(resp.Body) |
| 636 | for sc.Scan() { |
| 637 | line := strings.TrimSpace(sc.Text()) |
| 638 | fn(DirEntry{line: line}) |
| 639 | } |
| 640 | return sc.Err() |
| 641 | } |
| 642 | |
Brad Fitzpatrick | f8c2484 | 2015-01-16 09:54:03 -0800 | [diff] [blame] | 643 | func condRun(fn func()) { |
| 644 | if fn != nil { |
| 645 | fn() |
| 646 | } |
| 647 | } |