2015-01-16 03:29:16 +03:00
|
|
|
// Copyright 2015 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package buildlet contains client tools for working with a buildlet
|
|
|
|
// server.
|
2015-01-21 09:25:37 +03:00
|
|
|
package buildlet // import "golang.org/x/build/buildlet"
|
2015-01-16 03:29:16 +03:00
|
|
|
|
|
|
|
import (
|
2015-02-10 00:12:20 +03:00
|
|
|
"bufio"
|
2015-04-06 15:09:20 +03:00
|
|
|
"encoding/json"
|
2015-01-16 20:54:03 +03:00
|
|
|
"errors"
|
2015-01-16 03:29:16 +03:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2015-09-18 03:15:30 +03:00
|
|
|
"log"
|
2015-02-03 14:09:50 +03:00
|
|
|
"net"
|
2015-01-16 03:29:16 +03:00
|
|
|
"net/http"
|
2015-01-16 20:54:03 +03:00
|
|
|
"net/url"
|
2015-02-10 15:25:40 +03:00
|
|
|
"os"
|
2015-01-16 03:29:16 +03:00
|
|
|
"strings"
|
2015-06-10 23:59:42 +03:00
|
|
|
"sync"
|
2015-02-03 15:39:25 +03:00
|
|
|
"time"
|
|
|
|
|
2018-03-07 01:22:17 +03:00
|
|
|
"context"
|
2015-02-03 15:39:25 +03:00
|
|
|
"golang.org/x/oauth2"
|
2015-01-16 03:29:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewClient returns a *Client that will manipulate ipPort,
|
|
|
|
// authenticated using the provided keypair.
|
|
|
|
//
|
|
|
|
// This constructor returns immediately without testing the host or auth.
|
2015-01-16 23:59:14 +03:00
|
|
|
func NewClient(ipPort string, kp KeyPair) *Client {
|
2016-08-23 01:12:49 +03:00
|
|
|
tr := &http.Transport{
|
|
|
|
Dial: defaultDialer(),
|
|
|
|
DialTLS: kp.tlsDialer(),
|
|
|
|
IdleConnTimeout: time.Minute,
|
|
|
|
}
|
2015-07-06 02:22:16 +03:00
|
|
|
c := &Client{
|
2016-08-23 01:12:49 +03:00
|
|
|
ipPort: ipPort,
|
|
|
|
tls: kp,
|
|
|
|
password: kp.Password(),
|
|
|
|
httpClient: &http.Client{Transport: tr},
|
|
|
|
closeFuncs: []func(){tr.CloseIdleConnections},
|
2015-01-16 03:29:16 +03:00
|
|
|
}
|
2015-07-06 02:22:16 +03:00
|
|
|
c.setCommon()
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) setCommon() {
|
|
|
|
c.peerDead = make(chan struct{})
|
2016-05-10 06:51:02 +03:00
|
|
|
c.ctx, c.ctxCancel = context.WithCancel(context.Background())
|
2015-01-16 03:29:16 +03:00
|
|
|
}
|
|
|
|
|
2015-09-16 01:18:37 +03:00
|
|
|
// SetOnHeartbeatFailure sets a function to be called when heartbeats
|
|
|
|
// against this builder fail, or when the client is destroyed with
|
|
|
|
// Close. The function fn is never called more than once.
|
|
|
|
// SetOnHeartbeatFailure must be set before any use of the buildlet.
|
|
|
|
func (c *Client) SetOnHeartbeatFailure(fn func()) {
|
|
|
|
c.heartbeatFailure = fn
|
2015-03-27 19:45:12 +03:00
|
|
|
}
|
|
|
|
|
2015-09-14 21:33:55 +03:00
|
|
|
var ErrClosed = errors.New("buildlet: Client closed")
|
|
|
|
|
2015-09-16 01:18:37 +03:00
|
|
|
// Closes destroys and closes down the buildlet, destroying all state
|
|
|
|
// immediately.
|
2015-03-27 19:45:12 +03:00
|
|
|
func (c *Client) Close() error {
|
2015-09-18 04:29:23 +03:00
|
|
|
// TODO(bradfitz): have a Client-wide Done channel we set on
|
|
|
|
// all outbound HTTP Requests and close it in the once here?
|
|
|
|
// Then if something was in-flight and somebody else Closes,
|
|
|
|
// the local http.Transport notices, rather than noticing via
|
|
|
|
// the remote machine dying or timing out.
|
2015-09-16 01:18:37 +03:00
|
|
|
c.closeOnce.Do(func() {
|
|
|
|
// Send a best-effort notification to the server to destroy itself.
|
|
|
|
// Don't want too long (since it's likely in a broken state anyway).
|
|
|
|
// Ignore the return value, since we're about to forcefully destroy
|
|
|
|
// it anyway.
|
|
|
|
req, err := http.NewRequest("POST", c.URL()+"/halt", nil)
|
|
|
|
if err != nil {
|
|
|
|
// ignore.
|
|
|
|
} else {
|
|
|
|
_, err = c.doHeaderTimeout(req, 2*time.Second)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
err = ErrClosed
|
|
|
|
}
|
2016-08-23 01:12:49 +03:00
|
|
|
for _, fn := range c.closeFuncs {
|
|
|
|
fn()
|
|
|
|
}
|
2015-09-16 01:18:37 +03:00
|
|
|
c.setPeerDead(err) // which will also cause c.heartbeatFailure to run
|
|
|
|
})
|
|
|
|
return nil
|
2015-03-27 19:45:12 +03:00
|
|
|
}
|
|
|
|
|
2015-06-12 20:31:58 +03:00
|
|
|
func (c *Client) setPeerDead(err error) {
|
|
|
|
c.setPeerDeadOnce.Do(func() {
|
2015-09-14 21:33:55 +03:00
|
|
|
c.MarkBroken()
|
|
|
|
if err == nil {
|
|
|
|
err = errors.New("peer dead (no specific error)")
|
|
|
|
}
|
2015-06-12 20:31:58 +03:00
|
|
|
c.deadErr = err
|
|
|
|
close(c.peerDead)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-03-27 19:45:12 +03:00
|
|
|
// SetDescription sets a short description of where the buildlet
|
|
|
|
// connection came from. This is used by the build coordinator status
|
|
|
|
// page, mostly for debugging.
|
|
|
|
func (c *Client) SetDescription(v string) {
|
|
|
|
c.desc = v
|
|
|
|
}
|
|
|
|
|
2015-04-06 15:09:20 +03:00
|
|
|
// SetHTTPClient replaces the underlying HTTP client.
|
2015-06-12 20:31:58 +03:00
|
|
|
// It should only be called before the Client is used.
|
2015-04-06 15:09:20 +03:00
|
|
|
func (c *Client) SetHTTPClient(httpClient *http.Client) {
|
|
|
|
c.httpClient = httpClient
|
|
|
|
}
|
|
|
|
|
cmd/coordinator, cmd/buildlet, cmd/gomote: add SSH support
This adds an SSH server to farmer.golang.org on port 2222 that proxies
SSH connections to users' gomote-created buildlet instances.
For example:
$ gomote create openbsd-amd64-60
user-bradfitz-openbsd-amd64-60-1
$ gomote ssh user-bradfitz-openbsd-amd64-60-1
Warning: Permanently added '[localhost]:33351' (ECDSA) to the list of known hosts.
OpenBSD 6.0 (GENERIC.MP) golang/go#2319: Tue Jul 26 13:00:43 MDT 2016
Welcome to OpenBSD: The proactively secure Unix-like operating system.
Please use the sendbug(1) utility to report bugs in the system.
Before reporting a bug, please try to reproduce it with the latest
version of the code. With bug reports, please try to ensure that
enough information to reproduce the problem is enclosed, and if a
known fix for it exists, include that as well.
$
As before, if the coordinator process is restarted (or crashes, is
evicted, etc), all gomote instances die.
Not yet supported:
* scp (help wanted)
* not all host types are configured. most are. some will need slight
config tweaks to the Docker image (e.g. adding openssh-server)
Supports currently:
* linux-amd64 (host type shared by 386, nacl)
* linux-arm
* linux-arm64
* darwin
* freebsd
* openbsd
* plan9-386
* windows
Implementation details:
* the ssh server process listens on port 2222 in the coordinator
(farmer.golang.org), which is behind a GKE TCP load balancer.
* the ssh server library is github.com/gliderlabs/ssh
* authentication is done via Github users' public keys. It's assumed
that gomote user == github user. But there's a mapping in the code
for known exceptions.
* we can't give out access to this too widely. too many things are
accessible from within the host environment if you look in the right
places. Details omitted. But the Go team and other trusted gomote
users can use this.
* the buildlet binary has a new /connect-ssh handler that acts like a
CONNECT request but instead of taking an explicit host:port, just
says "give me your machine's SSH connection". The buildlet can also
start sshd if needed for the environment. The /connect-ssh handler
also installs the coordinator's public key.
* a new buildlet client library method "ConnectSSH" hits the /connect-ssh
handler and returns a net.Conn.
* the coordinator's ssh.Handler is just running the OpenSSH ssh client.
* because the OpenSSH ssh child process can't connect to a net.Conn,
an emphemeral localhost port is created on the coordinator to proxy
between the ssh client and the net.Conn returned by ConnectSSH.
* The /connect-ssh handler requires http.Hijacker, which requires
fully compliant net.Conn implementations as of Go 1.8. So I needed
to flesh out revdial too, testing it with the
golang.org/x/net/nettest package.
* plan9 doesn't have an ssh server, so we use 0intro's new conterm
program (drawterm without GUI support) to connect to plan9 from the
coordinator ssh proxy instead of using the OpenSSH ssh client
binary.
* windows doesn't have an ssh server, so we enable the telnet service
and the coordinator ssh proxy uses telnet instead on the backend
on the private network. (There is a Windows ssh server but only in
new versions.)
Happy debugging over ssh!
Fixes golang/go#19956
Change-Id: I80a62064c5f85af1f195f980c862ba29af4015f0
Reviewed-on: https://go-review.googlesource.com/50750
Reviewed-by: Herbie Ong <herbie@google.com>
Reviewed-by: Jessie Frazelle <me@jessfraz.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-07-22 22:15:56 +03:00
|
|
|
// SetDialer sets the function that creates a new connection to the buildlet.
|
|
|
|
// By default, net.Dial is used.
|
|
|
|
func (c *Client) SetDialer(dialer func() (net.Conn, error)) {
|
|
|
|
c.dialer = dialer
|
|
|
|
}
|
|
|
|
|
2015-02-03 14:09:50 +03:00
|
|
|
// defaultDialer returns the net/http package's default Dial function.
|
|
|
|
// Notably, this sets TCP keep-alive values, so when we kill VMs
|
|
|
|
// (whose TCP stacks stop replying, forever), we don't leak file
|
|
|
|
// descriptors for otherwise forever-stalled TCP connections.
|
|
|
|
func defaultDialer() func(network, addr string) (net.Conn, error) {
|
|
|
|
if fn := http.DefaultTransport.(*http.Transport).Dial; fn != nil {
|
|
|
|
return fn
|
|
|
|
}
|
|
|
|
return net.Dial
|
|
|
|
}
|
|
|
|
|
2015-01-16 03:29:16 +03:00
|
|
|
// A Client interacts with a single buildlet.
|
|
|
|
type Client struct {
|
2015-07-06 02:22:16 +03:00
|
|
|
ipPort string // required, unless remoteBuildlet+baseURL is set
|
|
|
|
tls KeyPair
|
|
|
|
httpClient *http.Client
|
cmd/coordinator, cmd/buildlet, cmd/gomote: add SSH support
This adds an SSH server to farmer.golang.org on port 2222 that proxies
SSH connections to users' gomote-created buildlet instances.
For example:
$ gomote create openbsd-amd64-60
user-bradfitz-openbsd-amd64-60-1
$ gomote ssh user-bradfitz-openbsd-amd64-60-1
Warning: Permanently added '[localhost]:33351' (ECDSA) to the list of known hosts.
OpenBSD 6.0 (GENERIC.MP) golang/go#2319: Tue Jul 26 13:00:43 MDT 2016
Welcome to OpenBSD: The proactively secure Unix-like operating system.
Please use the sendbug(1) utility to report bugs in the system.
Before reporting a bug, please try to reproduce it with the latest
version of the code. With bug reports, please try to ensure that
enough information to reproduce the problem is enclosed, and if a
known fix for it exists, include that as well.
$
As before, if the coordinator process is restarted (or crashes, is
evicted, etc), all gomote instances die.
Not yet supported:
* scp (help wanted)
* not all host types are configured. most are. some will need slight
config tweaks to the Docker image (e.g. adding openssh-server)
Supports currently:
* linux-amd64 (host type shared by 386, nacl)
* linux-arm
* linux-arm64
* darwin
* freebsd
* openbsd
* plan9-386
* windows
Implementation details:
* the ssh server process listens on port 2222 in the coordinator
(farmer.golang.org), which is behind a GKE TCP load balancer.
* the ssh server library is github.com/gliderlabs/ssh
* authentication is done via Github users' public keys. It's assumed
that gomote user == github user. But there's a mapping in the code
for known exceptions.
* we can't give out access to this too widely. too many things are
accessible from within the host environment if you look in the right
places. Details omitted. But the Go team and other trusted gomote
users can use this.
* the buildlet binary has a new /connect-ssh handler that acts like a
CONNECT request but instead of taking an explicit host:port, just
says "give me your machine's SSH connection". The buildlet can also
start sshd if needed for the environment. The /connect-ssh handler
also installs the coordinator's public key.
* a new buildlet client library method "ConnectSSH" hits the /connect-ssh
handler and returns a net.Conn.
* the coordinator's ssh.Handler is just running the OpenSSH ssh client.
* because the OpenSSH ssh child process can't connect to a net.Conn,
an emphemeral localhost port is created on the coordinator to proxy
between the ssh client and the net.Conn returned by ConnectSSH.
* The /connect-ssh handler requires http.Hijacker, which requires
fully compliant net.Conn implementations as of Go 1.8. So I needed
to flesh out revdial too, testing it with the
golang.org/x/net/nettest package.
* plan9 doesn't have an ssh server, so we use 0intro's new conterm
program (drawterm without GUI support) to connect to plan9 from the
coordinator ssh proxy instead of using the OpenSSH ssh client
binary.
* windows doesn't have an ssh server, so we enable the telnet service
and the coordinator ssh proxy uses telnet instead on the backend
on the private network. (There is a Windows ssh server but only in
new versions.)
Happy debugging over ssh!
Fixes golang/go#19956
Change-Id: I80a62064c5f85af1f195f980c862ba29af4015f0
Reviewed-on: https://go-review.googlesource.com/50750
Reviewed-by: Herbie Ong <herbie@google.com>
Reviewed-by: Jessie Frazelle <me@jessfraz.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-07-22 22:15:56 +03:00
|
|
|
dialer func() (net.Conn, error) // nil means to use net.Dial
|
|
|
|
baseURL string // optional baseURL (used by remote buildlets)
|
|
|
|
authUser string // defaults to "gomote", if password is non-empty
|
|
|
|
password string // basic auth password or empty for none
|
|
|
|
remoteBuildlet string // non-empty if for remote buildlets
|
2015-03-27 19:45:12 +03:00
|
|
|
|
2017-02-13 20:38:27 +03:00
|
|
|
closeFuncs []func() // optional extra code to run on close
|
|
|
|
releaseMode bool
|
2016-08-23 01:12:49 +03:00
|
|
|
|
2016-05-07 00:06:13 +03:00
|
|
|
ctx context.Context
|
|
|
|
ctxCancel context.CancelFunc
|
2015-09-16 01:18:37 +03:00
|
|
|
heartbeatFailure func() // optional
|
|
|
|
desc string
|
2015-06-10 23:59:42 +03:00
|
|
|
|
2015-09-16 01:18:37 +03:00
|
|
|
closeOnce sync.Once
|
2015-06-12 20:31:58 +03:00
|
|
|
initHeartbeatOnce sync.Once
|
|
|
|
setPeerDeadOnce sync.Once
|
|
|
|
peerDead chan struct{} // closed on peer death
|
|
|
|
deadErr error // guarded by peerDead's close
|
|
|
|
|
2015-06-10 23:59:42 +03:00
|
|
|
mu sync.Mutex
|
|
|
|
broken bool // client is broken in some way
|
2015-03-27 19:45:12 +03:00
|
|
|
}
|
|
|
|
|
2017-02-13 20:38:27 +03:00
|
|
|
// SetReleaseMode sets whether this client is being used in "release
|
2017-02-13 21:02:50 +03:00
|
|
|
// mode", to prepare the final binaries to be shipped to users. All
|
2017-02-13 20:38:27 +03:00
|
|
|
// this mode does for now is disable pargzip multi-stream gzip
|
|
|
|
// files. See golang.org/issue/19052.
|
|
|
|
//
|
|
|
|
// SetReleaseMode must be set before using the client.
|
|
|
|
func (c *Client) SetReleaseMode(v bool) {
|
|
|
|
c.releaseMode = v
|
|
|
|
}
|
|
|
|
|
2015-03-27 19:45:12 +03:00
|
|
|
func (c *Client) String() string {
|
|
|
|
if c == nil {
|
|
|
|
return "(nil *buildlet.Client)"
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(c.URL() + " " + c.desc)
|
2015-01-16 03:29:16 +03:00
|
|
|
}
|
|
|
|
|
2015-07-06 02:22:16 +03:00
|
|
|
// RemoteName returns the name of this client's buildlet on the
|
|
|
|
// coordinator. If this buildlet isn't a remote buildlet created via
|
|
|
|
// a buildlet, this returns the empty string.
|
|
|
|
func (c *Client) RemoteName() string {
|
|
|
|
return c.remoteBuildlet
|
|
|
|
}
|
|
|
|
|
2015-01-16 03:29:16 +03:00
|
|
|
// URL returns the buildlet's URL prefix, without a trailing slash.
|
|
|
|
func (c *Client) URL() string {
|
2015-07-06 02:22:16 +03:00
|
|
|
if c.baseURL != "" {
|
|
|
|
return strings.TrimRight(c.baseURL, "/")
|
|
|
|
}
|
2015-01-16 23:59:14 +03:00
|
|
|
if !c.tls.IsZero() {
|
|
|
|
return "https://" + strings.TrimSuffix(c.ipPort, ":443")
|
|
|
|
}
|
|
|
|
return "http://" + strings.TrimSuffix(c.ipPort, ":80")
|
|
|
|
}
|
|
|
|
|
2015-05-28 07:51:25 +03:00
|
|
|
func (c *Client) IPPort() string { return c.ipPort }
|
|
|
|
|
2015-09-14 21:33:55 +03:00
|
|
|
func (c *Client) Name() string {
|
|
|
|
if c.ipPort != "" {
|
|
|
|
return c.ipPort
|
|
|
|
}
|
|
|
|
return "(unnamed-buildlet)"
|
|
|
|
}
|
|
|
|
|
2015-06-10 23:59:42 +03:00
|
|
|
// MarkBroken marks this client as broken in some way.
|
|
|
|
func (c *Client) MarkBroken() {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
c.broken = true
|
2016-05-07 00:06:13 +03:00
|
|
|
c.ctxCancel()
|
2015-06-10 23:59:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsBroken reports whether this client is broken in some way.
|
|
|
|
func (c *Client) IsBroken() bool {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
return c.broken
|
|
|
|
}
|
|
|
|
|
2015-07-06 02:22:16 +03:00
|
|
|
func (c *Client) authUsername() string {
|
|
|
|
if c.authUser != "" {
|
|
|
|
return c.authUser
|
|
|
|
}
|
|
|
|
return "gomote"
|
|
|
|
}
|
|
|
|
|
2015-01-16 23:59:14 +03:00
|
|
|
func (c *Client) do(req *http.Request) (*http.Response, error) {
|
2016-05-07 00:06:13 +03:00
|
|
|
if req.Cancel == nil {
|
|
|
|
req.Cancel = c.ctx.Done()
|
|
|
|
}
|
2015-06-12 20:31:58 +03:00
|
|
|
c.initHeartbeatOnce.Do(c.initHeartbeats)
|
2015-01-16 23:59:14 +03:00
|
|
|
if c.password != "" {
|
2015-07-06 02:22:16 +03:00
|
|
|
req.SetBasicAuth(c.authUsername(), c.password)
|
|
|
|
}
|
|
|
|
if c.remoteBuildlet != "" {
|
|
|
|
req.Header.Set("X-Buildlet-Proxy", c.remoteBuildlet)
|
2015-01-16 03:29:16 +03:00
|
|
|
}
|
2015-01-16 23:59:14 +03:00
|
|
|
return c.httpClient.Do(req)
|
2015-01-16 03:29:16 +03:00
|
|
|
}
|
|
|
|
|
2015-07-06 02:22:16 +03:00
|
|
|
// ProxyRoundTripper returns a RoundTripper that sends HTTP requests directly
|
|
|
|
// through to the underlying buildlet, adding auth and X-Buildlet-Proxy headers
|
|
|
|
// as necessary. This is really only intended for use by the coordinator.
|
|
|
|
func (c *Client) ProxyRoundTripper() http.RoundTripper {
|
|
|
|
return proxyRoundTripper{c}
|
|
|
|
}
|
|
|
|
|
|
|
|
type proxyRoundTripper struct {
|
|
|
|
c *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p proxyRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
|
|
return p.c.do(req)
|
|
|
|
}
|
|
|
|
|
2015-06-12 20:31:58 +03:00
|
|
|
func (c *Client) initHeartbeats() {
|
|
|
|
go c.heartbeatLoop()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) heartbeatLoop() {
|
2015-06-16 23:15:54 +03:00
|
|
|
failInARow := 0
|
2015-06-12 20:31:58 +03:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c.peerDead:
|
2015-09-16 01:18:37 +03:00
|
|
|
// Dead for whatever reason (heartbeat, remote
|
|
|
|
// side closed, caller Closed
|
|
|
|
// normally). Regardless, we call the
|
|
|
|
// heartbeatFailure func if set.
|
|
|
|
if c.heartbeatFailure != nil {
|
|
|
|
c.heartbeatFailure()
|
|
|
|
}
|
2015-06-12 20:31:58 +03:00
|
|
|
return
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t0 := time.Now()
|
|
|
|
if _, err := c.Status(); err != nil {
|
2015-06-16 23:15:54 +03:00
|
|
|
failInARow++
|
|
|
|
if failInARow == 3 {
|
2015-09-18 03:15:30 +03:00
|
|
|
log.Printf("Buildlet %v failed three heartbeats; final error: %v", c, err)
|
2015-06-16 23:15:54 +03:00
|
|
|
c.setPeerDead(fmt.Errorf("Buildlet %v failed heartbeat after %v; marking dead; err=%v", c, time.Since(t0), err))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
failInARow = 0
|
2015-06-12 20:31:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-09 19:12:17 +03:00
|
|
|
var errHeaderTimeout = errors.New("timeout waiting for headers")
|
|
|
|
|
|
|
|
// doHeaderTimeout calls c.do(req) and returns its results, or
|
|
|
|
// errHeaderTimeout if max elapses first.
|
|
|
|
func (c *Client) doHeaderTimeout(req *http.Request, max time.Duration) (res *http.Response, err error) {
|
|
|
|
type resErr struct {
|
|
|
|
res *http.Response
|
|
|
|
err error
|
|
|
|
}
|
2015-09-17 23:03:59 +03:00
|
|
|
|
|
|
|
if req.Cancel != nil {
|
|
|
|
panic("use of Request.Cancel inside the buildlet package is reserved for doHeaderTimeout")
|
|
|
|
}
|
|
|
|
cancelc := make(chan struct{}) // closed to abort
|
|
|
|
req.Cancel = cancelc
|
|
|
|
|
2015-06-09 19:12:17 +03:00
|
|
|
resErrc := make(chan resErr, 1)
|
|
|
|
go func() {
|
|
|
|
res, err := c.do(req)
|
|
|
|
resErrc <- resErr{res, err}
|
|
|
|
}()
|
|
|
|
|
|
|
|
timer := time.NewTimer(max)
|
|
|
|
defer timer.Stop()
|
|
|
|
|
2015-06-12 20:31:58 +03:00
|
|
|
cleanup := func() {
|
2015-09-17 23:03:59 +03:00
|
|
|
close(cancelc)
|
2015-06-12 20:31:58 +03:00
|
|
|
if re := <-resErrc; re.res != nil {
|
|
|
|
re.res.Body.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-09 19:12:17 +03:00
|
|
|
select {
|
|
|
|
case re := <-resErrc:
|
|
|
|
return re.res, re.err
|
2015-06-12 20:31:58 +03:00
|
|
|
case <-c.peerDead:
|
|
|
|
go cleanup()
|
|
|
|
return nil, c.deadErr
|
2015-06-09 19:12:17 +03:00
|
|
|
case <-timer.C:
|
2015-06-12 20:31:58 +03:00
|
|
|
go cleanup()
|
2015-06-09 19:12:17 +03:00
|
|
|
return nil, errHeaderTimeout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-20 06:43:25 +03:00
|
|
|
// doOK sends the request and expects a 200 OK response.
|
|
|
|
func (c *Client) doOK(req *http.Request) error {
|
2015-01-16 23:59:14 +03:00
|
|
|
res, err := c.do(req)
|
2015-01-16 03:29:16 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
2015-01-20 06:43:25 +03:00
|
|
|
if res.StatusCode != http.StatusOK {
|
2015-01-16 03:29:16 +03:00
|
|
|
slurp, _ := ioutil.ReadAll(io.LimitReader(res.Body, 4<<10))
|
|
|
|
return fmt.Errorf("%v; body: %s", res.Status, slurp)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-01-16 20:54:03 +03:00
|
|
|
|
2015-01-20 06:43:25 +03:00
|
|
|
// PutTar writes files to the remote buildlet, rooted at the relative
|
|
|
|
// directory dir.
|
|
|
|
// If dir is empty, they're placed at the root of the buildlet's work directory.
|
|
|
|
// The dir is created if necessary.
|
|
|
|
// The Reader must be of a tar.gz file.
|
|
|
|
func (c *Client) PutTar(r io.Reader, dir string) error {
|
|
|
|
req, err := http.NewRequest("PUT", c.URL()+"/writetgz?dir="+url.QueryEscape(dir), r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.doOK(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutTarFromURL tells the buildlet to download the tar.gz file from tarURL
|
|
|
|
// and write it to dir, a relative directory from the workdir.
|
|
|
|
// If dir is empty, they're placed at the root of the buildlet's work directory.
|
|
|
|
// The dir is created if necessary.
|
|
|
|
// The url must be of a tar.gz file.
|
|
|
|
func (c *Client) PutTarFromURL(tarURL, dir string) error {
|
|
|
|
form := url.Values{
|
|
|
|
"url": {tarURL},
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("POST", c.URL()+"/writetgz?dir="+url.QueryEscape(dir), strings.NewReader(form.Encode()))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
return c.doOK(req)
|
|
|
|
}
|
|
|
|
|
2015-02-10 15:25:40 +03:00
|
|
|
// Put writes the provided file to path (relative to workdir) and sets mode.
|
|
|
|
func (c *Client) Put(r io.Reader, path string, mode os.FileMode) error {
|
|
|
|
param := url.Values{
|
|
|
|
"path": {path},
|
|
|
|
"mode": {fmt.Sprint(int64(mode))},
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("PUT", c.URL()+"/write?"+param.Encode(), r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.doOK(req)
|
|
|
|
}
|
|
|
|
|
2015-01-20 07:53:34 +03:00
|
|
|
// GetTar returns a .tar.gz stream of the given directory, relative to the buildlet's work dir.
|
|
|
|
// The provided dir may be empty to get everything.
|
2017-02-12 12:08:39 +03:00
|
|
|
func (c *Client) GetTar(ctx context.Context, dir string) (io.ReadCloser, error) {
|
2017-02-13 20:38:27 +03:00
|
|
|
var args string
|
|
|
|
if c.releaseMode {
|
|
|
|
args = "&pargzip=0"
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("GET", c.URL()+"/tgz?dir="+url.QueryEscape(dir)+args, nil)
|
2015-01-20 07:53:34 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-12 12:08:39 +03:00
|
|
|
res, err := c.do(req.WithContext(ctx))
|
2015-01-20 07:53:34 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if res.StatusCode != http.StatusOK {
|
|
|
|
slurp, _ := ioutil.ReadAll(io.LimitReader(res.Body, 4<<10))
|
|
|
|
res.Body.Close()
|
|
|
|
return nil, fmt.Errorf("%v; body: %s", res.Status, slurp)
|
|
|
|
}
|
|
|
|
return res.Body, nil
|
|
|
|
}
|
|
|
|
|
2015-01-16 20:54:03 +03:00
|
|
|
// ExecOpts are options for a remote command invocation.
|
|
|
|
type ExecOpts struct {
|
|
|
|
// Output is the output of stdout and stderr.
|
|
|
|
// If nil, the output is discarded.
|
|
|
|
Output io.Writer
|
|
|
|
|
2015-02-11 03:17:46 +03:00
|
|
|
// Dir is the directory from which to execute the command.
|
|
|
|
// It is optional. If not specified, it defaults to the directory of
|
|
|
|
// the command, or the work directory if SystemLevel is set.
|
|
|
|
Dir string
|
|
|
|
|
2015-01-20 07:53:34 +03:00
|
|
|
// Args are the arguments to pass to the cmd given to Client.Exec.
|
|
|
|
Args []string
|
|
|
|
|
2015-01-22 02:15:48 +03:00
|
|
|
// ExtraEnv are KEY=VALUE pairs to append to the buildlet
|
|
|
|
// process's environment.
|
|
|
|
ExtraEnv []string
|
|
|
|
|
2015-05-21 06:01:10 +03:00
|
|
|
// Path, if non-nil, specifies the PATH variable of the executed
|
|
|
|
// process's environment. A non-nil empty list clears the path.
|
|
|
|
// The following expansions apply:
|
|
|
|
// - the string "$PATH" expands to any existing PATH element(s)
|
|
|
|
// - the substring "$WORKDIR" expands to buildlet's temp workdir
|
|
|
|
// After expansions, the list is joined with an OS-specific list
|
|
|
|
// separator and supplied to the executed process as its PATH
|
|
|
|
// environment variable.
|
|
|
|
Path []string
|
|
|
|
|
2015-01-20 07:53:34 +03:00
|
|
|
// SystemLevel controls whether the command is run outside of
|
|
|
|
// the buildlet's environment.
|
|
|
|
SystemLevel bool
|
|
|
|
|
2015-02-10 00:12:20 +03:00
|
|
|
// Debug, if true, instructs to the buildlet to print extra debug
|
|
|
|
// info to the output before the command begins executing.
|
|
|
|
Debug bool
|
|
|
|
|
2015-01-16 20:54:03 +03:00
|
|
|
// OnStartExec is an optional hook that runs after the 200 OK
|
|
|
|
// response from the buildlet, but before the output begins
|
|
|
|
// writing to Output.
|
|
|
|
OnStartExec func()
|
2015-06-12 20:31:58 +03:00
|
|
|
|
|
|
|
// Timeout is an optional duration before ErrTimeout is returned.
|
|
|
|
Timeout time.Duration
|
2015-01-16 20:54:03 +03:00
|
|
|
}
|
|
|
|
|
2015-06-12 20:31:58 +03:00
|
|
|
var ErrTimeout = errors.New("buildlet: timeout waiting for command to complete")
|
|
|
|
|
2015-01-16 20:54:03 +03:00
|
|
|
// Exec runs cmd on the buildlet.
|
|
|
|
//
|
|
|
|
// Two errors are returned: one is whether the command succeeded
|
|
|
|
// remotely (remoteErr), and the second (execErr) is whether there
|
|
|
|
// were system errors preventing the command from being started or
|
|
|
|
// seen to completition. If execErr is non-nil, the remoteErr is
|
|
|
|
// meaningless.
|
|
|
|
func (c *Client) Exec(cmd string, opts ExecOpts) (remoteErr, execErr error) {
|
2015-01-20 07:53:34 +03:00
|
|
|
var mode string
|
|
|
|
if opts.SystemLevel {
|
|
|
|
mode = "sys"
|
|
|
|
}
|
2015-05-21 06:01:10 +03:00
|
|
|
path := opts.Path
|
|
|
|
if len(path) == 0 && path != nil {
|
|
|
|
// url.Values doesn't distinguish between a nil slice and
|
|
|
|
// a non-nil zero-length slice, so use this sentinel value.
|
|
|
|
path = []string{"$EMPTY"}
|
|
|
|
}
|
2015-01-16 23:59:14 +03:00
|
|
|
form := url.Values{
|
2015-01-20 07:53:34 +03:00
|
|
|
"cmd": {cmd},
|
|
|
|
"mode": {mode},
|
2015-02-11 03:17:46 +03:00
|
|
|
"dir": {opts.Dir},
|
2015-01-20 07:53:34 +03:00
|
|
|
"cmdArg": opts.Args,
|
2015-01-22 02:15:48 +03:00
|
|
|
"env": opts.ExtraEnv,
|
2015-05-21 06:01:10 +03:00
|
|
|
"path": path,
|
2015-02-10 00:12:20 +03:00
|
|
|
"debug": {fmt.Sprint(opts.Debug)},
|
2015-01-16 23:59:14 +03:00
|
|
|
}
|
|
|
|
req, err := http.NewRequest("POST", c.URL()+"/exec", strings.NewReader(form.Encode()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
2015-06-09 19:12:17 +03:00
|
|
|
|
|
|
|
// The first thing the buildlet's exec handler does is flush the headers, so
|
2015-06-12 20:31:58 +03:00
|
|
|
// 10 seconds should be plenty of time, regardless of where on the planet
|
2015-06-09 19:12:17 +03:00
|
|
|
// (Atlanta, Paris, etc) the reverse buildlet is:
|
2015-06-12 20:31:58 +03:00
|
|
|
res, err := c.doHeaderTimeout(req, 10*time.Second)
|
2015-06-09 19:12:17 +03:00
|
|
|
if err == errHeaderTimeout {
|
2015-06-10 23:59:42 +03:00
|
|
|
c.MarkBroken()
|
2015-06-09 19:12:17 +03:00
|
|
|
return nil, errors.New("buildlet: timeout waiting for exec header response")
|
|
|
|
}
|
2015-01-16 20:54:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != http.StatusOK {
|
|
|
|
slurp, _ := ioutil.ReadAll(io.LimitReader(res.Body, 4<<10))
|
|
|
|
return nil, fmt.Errorf("buildlet: HTTP status %v: %s", res.Status, slurp)
|
|
|
|
}
|
|
|
|
condRun(opts.OnStartExec)
|
|
|
|
|
2015-06-12 20:31:58 +03:00
|
|
|
type errs struct {
|
|
|
|
remoteErr, execErr error
|
2015-01-16 20:54:03 +03:00
|
|
|
}
|
2015-06-12 20:31:58 +03:00
|
|
|
resc := make(chan errs, 1)
|
|
|
|
go func() {
|
|
|
|
// Stream the output:
|
|
|
|
out := opts.Output
|
|
|
|
if out == nil {
|
|
|
|
out = ioutil.Discard
|
|
|
|
}
|
|
|
|
if _, err := io.Copy(out, res.Body); err != nil {
|
|
|
|
resc <- errs{execErr: fmt.Errorf("error copying response: %v", err)}
|
|
|
|
return
|
|
|
|
}
|
2015-01-16 20:54:03 +03:00
|
|
|
|
2015-06-12 20:31:58 +03:00
|
|
|
// Don't record to the dashboard unless we heard the trailer from
|
|
|
|
// the buildlet, otherwise it was probably some unrelated error
|
|
|
|
// (like the VM being killed, or the buildlet crashing due to
|
|
|
|
// e.g. https://golang.org/issue/9309, since we require a tip
|
|
|
|
// build of the buildlet to get Trailers support)
|
|
|
|
state := res.Trailer.Get("Process-State")
|
|
|
|
if state == "" {
|
|
|
|
resc <- errs{execErr: errors.New("missing Process-State trailer from HTTP response; buildlet built with old (<= 1.4) Go?")}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if state != "ok" {
|
|
|
|
resc <- errs{remoteErr: errors.New(state)}
|
|
|
|
} else {
|
|
|
|
resc <- errs{} // success
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
var timer <-chan time.Time
|
|
|
|
if opts.Timeout > 0 {
|
|
|
|
t := time.NewTimer(opts.Timeout)
|
|
|
|
defer t.Stop()
|
|
|
|
timer = t.C
|
2015-01-16 20:54:03 +03:00
|
|
|
}
|
2015-06-12 20:31:58 +03:00
|
|
|
select {
|
|
|
|
case <-timer:
|
|
|
|
c.MarkBroken()
|
|
|
|
return nil, ErrTimeout
|
|
|
|
case res := <-resc:
|
|
|
|
return res.remoteErr, res.execErr
|
|
|
|
case <-c.peerDead:
|
|
|
|
return nil, c.deadErr
|
2015-01-16 20:54:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-02 15:04:07 +03:00
|
|
|
// RemoveAll deletes the provided paths, relative to the work directory.
|
|
|
|
func (c *Client) RemoveAll(paths ...string) error {
|
2015-02-10 00:12:20 +03:00
|
|
|
if len(paths) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2015-02-02 15:04:07 +03:00
|
|
|
form := url.Values{"path": paths}
|
|
|
|
req, err := http.NewRequest("POST", c.URL()+"/removeall", strings.NewReader(form.Encode()))
|
2015-01-16 23:59:14 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-04 18:54:43 +03:00
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
2015-02-02 15:04:07 +03:00
|
|
|
return c.doOK(req)
|
2015-01-16 23:59:14 +03:00
|
|
|
}
|
|
|
|
|
2015-02-03 15:39:25 +03:00
|
|
|
// DestroyVM shuts down the buildlet and destroys the VM instance.
|
|
|
|
func (c *Client) DestroyVM(ts oauth2.TokenSource, proj, zone, instance string) error {
|
2015-09-16 01:18:37 +03:00
|
|
|
// TODO(bradfitz): move GCE stuff out of this package?
|
2015-02-03 15:39:25 +03:00
|
|
|
gceErrc := make(chan error, 1)
|
|
|
|
buildletErrc := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
gceErrc <- DestroyVM(ts, proj, zone, instance)
|
|
|
|
}()
|
|
|
|
go func() {
|
2015-09-16 01:18:37 +03:00
|
|
|
buildletErrc <- c.Close()
|
2015-02-03 15:39:25 +03:00
|
|
|
}()
|
|
|
|
timeout := time.NewTimer(5 * time.Second)
|
|
|
|
defer timeout.Stop()
|
|
|
|
|
|
|
|
var retErr error
|
|
|
|
var gceDone, buildletDone bool
|
|
|
|
for !gceDone || !buildletDone {
|
|
|
|
select {
|
|
|
|
case err := <-gceErrc:
|
|
|
|
if err != nil {
|
|
|
|
retErr = err
|
|
|
|
}
|
|
|
|
gceDone = true
|
|
|
|
case err := <-buildletErrc:
|
|
|
|
if err != nil {
|
|
|
|
retErr = err
|
|
|
|
}
|
|
|
|
buildletDone = true
|
|
|
|
case <-timeout.C:
|
|
|
|
e := ""
|
|
|
|
if !buildletDone {
|
|
|
|
e = "timeout asking buildlet to shut down"
|
|
|
|
}
|
|
|
|
if !gceDone {
|
|
|
|
if e != "" {
|
|
|
|
e += " and "
|
|
|
|
}
|
|
|
|
e += "timeout asking GCE to delete builder VM"
|
|
|
|
}
|
|
|
|
return errors.New(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return retErr
|
|
|
|
}
|
|
|
|
|
2015-04-08 02:46:19 +03:00
|
|
|
// Status provides status information about the buildlet.
|
2015-04-06 15:09:20 +03:00
|
|
|
//
|
|
|
|
// A coordinator can use the provided information to decide what, if anything,
|
|
|
|
// to do with a buildlet.
|
2015-04-08 02:46:19 +03:00
|
|
|
type Status struct {
|
|
|
|
Version int // buildlet version, coordinator rejects any value less than 1.
|
2015-04-06 15:09:20 +03:00
|
|
|
}
|
|
|
|
|
2015-04-08 02:46:19 +03:00
|
|
|
// Status returns an Status value describing this buildlet.
|
|
|
|
func (c *Client) Status() (Status, error) {
|
2015-09-14 21:33:55 +03:00
|
|
|
select {
|
|
|
|
case <-c.peerDead:
|
|
|
|
return Status{}, c.deadErr
|
|
|
|
default:
|
|
|
|
// Continue below.
|
|
|
|
}
|
2015-04-08 02:46:19 +03:00
|
|
|
req, err := http.NewRequest("GET", c.URL()+"/status", nil)
|
2015-04-06 15:09:20 +03:00
|
|
|
if err != nil {
|
2015-04-08 02:46:19 +03:00
|
|
|
return Status{}, err
|
2015-04-06 15:09:20 +03:00
|
|
|
}
|
2015-06-12 20:31:58 +03:00
|
|
|
resp, err := c.doHeaderTimeout(req, 10*time.Second) // plenty of time
|
2015-04-06 15:09:20 +03:00
|
|
|
if err != nil {
|
2015-04-08 02:46:19 +03:00
|
|
|
return Status{}, err
|
2015-04-06 15:09:20 +03:00
|
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2015-04-08 02:46:19 +03:00
|
|
|
return Status{}, errors.New(resp.Status)
|
2015-04-06 15:09:20 +03:00
|
|
|
}
|
|
|
|
b, err := ioutil.ReadAll(resp.Body)
|
|
|
|
resp.Body.Close()
|
|
|
|
if err != nil {
|
2015-04-08 02:46:19 +03:00
|
|
|
return Status{}, err
|
2015-04-06 15:09:20 +03:00
|
|
|
}
|
2015-04-08 02:46:19 +03:00
|
|
|
var status Status
|
|
|
|
if err := json.Unmarshal(b, &status); err != nil {
|
|
|
|
return Status{}, err
|
2015-04-06 15:09:20 +03:00
|
|
|
}
|
2015-04-08 02:46:19 +03:00
|
|
|
return status, nil
|
2015-04-06 15:09:20 +03:00
|
|
|
}
|
|
|
|
|
2015-02-04 18:54:43 +03:00
|
|
|
// WorkDir returns the absolute path to the buildlet work directory.
|
|
|
|
func (c *Client) WorkDir() (string, error) {
|
|
|
|
req, err := http.NewRequest("GET", c.URL()+"/workdir", nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-06-12 20:31:58 +03:00
|
|
|
resp, err := c.doHeaderTimeout(req, 10*time.Second) // plenty of time
|
2015-02-04 18:54:43 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return "", errors.New(resp.Status)
|
|
|
|
}
|
|
|
|
b, err := ioutil.ReadAll(resp.Body)
|
|
|
|
resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(b), nil
|
|
|
|
}
|
|
|
|
|
2015-02-10 00:12:20 +03:00
|
|
|
// DirEntry is the information about a file on a buildlet.
|
|
|
|
type DirEntry struct {
|
|
|
|
// line is of the form "drw-rw-rw\t<name>" and then if a regular file,
|
|
|
|
// also "\t<size>\t<modtime>". in either case, without trailing newline.
|
|
|
|
// TODO: break into parsed fields?
|
|
|
|
line string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (de DirEntry) String() string {
|
|
|
|
return de.line
|
|
|
|
}
|
|
|
|
|
|
|
|
func (de DirEntry) Name() string {
|
|
|
|
f := strings.Split(de.line, "\t")
|
|
|
|
if len(f) < 2 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return f[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (de DirEntry) Digest() string {
|
|
|
|
f := strings.Split(de.line, "\t")
|
|
|
|
if len(f) < 5 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return f[4]
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListDirOpts are options for Client.ListDir.
|
|
|
|
type ListDirOpts struct {
|
|
|
|
// Recursive controls whether the directory is listed
|
|
|
|
// recursively.
|
|
|
|
Recursive bool
|
|
|
|
|
|
|
|
// Skip are the directories to skip, relative to the directory
|
|
|
|
// passed to ListDir. Each item should contain only forward
|
|
|
|
// slashes and not start or end in slashes.
|
|
|
|
Skip []string
|
|
|
|
|
|
|
|
// Digest controls whether the SHA-1 digests of regular files
|
|
|
|
// are returned.
|
|
|
|
Digest bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListDir lists the contents of a directory.
|
|
|
|
// The fn callback is run for each entry.
|
|
|
|
func (c *Client) ListDir(dir string, opts ListDirOpts, fn func(DirEntry)) error {
|
|
|
|
param := url.Values{
|
|
|
|
"dir": {dir},
|
|
|
|
"recursive": {fmt.Sprint(opts.Recursive)},
|
|
|
|
"skip": opts.Skip,
|
|
|
|
"digest": {fmt.Sprint(opts.Digest)},
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("GET", c.URL()+"/ls?"+param.Encode(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp, err := c.do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2015-07-06 02:22:16 +03:00
|
|
|
slurp, _ := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<10))
|
|
|
|
return fmt.Errorf("%s: %s", resp.Status, slurp)
|
2015-02-10 00:12:20 +03:00
|
|
|
}
|
|
|
|
sc := bufio.NewScanner(resp.Body)
|
|
|
|
for sc.Scan() {
|
|
|
|
line := strings.TrimSpace(sc.Text())
|
|
|
|
fn(DirEntry{line: line})
|
|
|
|
}
|
|
|
|
return sc.Err()
|
|
|
|
}
|
|
|
|
|
cmd/coordinator, cmd/buildlet, cmd/gomote: add SSH support
This adds an SSH server to farmer.golang.org on port 2222 that proxies
SSH connections to users' gomote-created buildlet instances.
For example:
$ gomote create openbsd-amd64-60
user-bradfitz-openbsd-amd64-60-1
$ gomote ssh user-bradfitz-openbsd-amd64-60-1
Warning: Permanently added '[localhost]:33351' (ECDSA) to the list of known hosts.
OpenBSD 6.0 (GENERIC.MP) golang/go#2319: Tue Jul 26 13:00:43 MDT 2016
Welcome to OpenBSD: The proactively secure Unix-like operating system.
Please use the sendbug(1) utility to report bugs in the system.
Before reporting a bug, please try to reproduce it with the latest
version of the code. With bug reports, please try to ensure that
enough information to reproduce the problem is enclosed, and if a
known fix for it exists, include that as well.
$
As before, if the coordinator process is restarted (or crashes, is
evicted, etc), all gomote instances die.
Not yet supported:
* scp (help wanted)
* not all host types are configured. most are. some will need slight
config tweaks to the Docker image (e.g. adding openssh-server)
Supports currently:
* linux-amd64 (host type shared by 386, nacl)
* linux-arm
* linux-arm64
* darwin
* freebsd
* openbsd
* plan9-386
* windows
Implementation details:
* the ssh server process listens on port 2222 in the coordinator
(farmer.golang.org), which is behind a GKE TCP load balancer.
* the ssh server library is github.com/gliderlabs/ssh
* authentication is done via Github users' public keys. It's assumed
that gomote user == github user. But there's a mapping in the code
for known exceptions.
* we can't give out access to this too widely. too many things are
accessible from within the host environment if you look in the right
places. Details omitted. But the Go team and other trusted gomote
users can use this.
* the buildlet binary has a new /connect-ssh handler that acts like a
CONNECT request but instead of taking an explicit host:port, just
says "give me your machine's SSH connection". The buildlet can also
start sshd if needed for the environment. The /connect-ssh handler
also installs the coordinator's public key.
* a new buildlet client library method "ConnectSSH" hits the /connect-ssh
handler and returns a net.Conn.
* the coordinator's ssh.Handler is just running the OpenSSH ssh client.
* because the OpenSSH ssh child process can't connect to a net.Conn,
an emphemeral localhost port is created on the coordinator to proxy
between the ssh client and the net.Conn returned by ConnectSSH.
* The /connect-ssh handler requires http.Hijacker, which requires
fully compliant net.Conn implementations as of Go 1.8. So I needed
to flesh out revdial too, testing it with the
golang.org/x/net/nettest package.
* plan9 doesn't have an ssh server, so we use 0intro's new conterm
program (drawterm without GUI support) to connect to plan9 from the
coordinator ssh proxy instead of using the OpenSSH ssh client
binary.
* windows doesn't have an ssh server, so we enable the telnet service
and the coordinator ssh proxy uses telnet instead on the backend
on the private network. (There is a Windows ssh server but only in
new versions.)
Happy debugging over ssh!
Fixes golang/go#19956
Change-Id: I80a62064c5f85af1f195f980c862ba29af4015f0
Reviewed-on: https://go-review.googlesource.com/50750
Reviewed-by: Herbie Ong <herbie@google.com>
Reviewed-by: Jessie Frazelle <me@jessfraz.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-07-22 22:15:56 +03:00
|
|
|
func (c *Client) getDialer() func() (net.Conn, error) {
|
|
|
|
if c.dialer != nil {
|
|
|
|
return c.dialer
|
|
|
|
}
|
|
|
|
return c.dialWithNetDial
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) dialWithNetDial() (net.Conn, error) {
|
|
|
|
// TODO: contexts? the tedious part will be adding it to
|
|
|
|
// revdial.Dial. For now just do a 5 second timeout. Probably
|
|
|
|
// fine. This is currently only used for ssh connections.
|
|
|
|
d := net.Dialer{Timeout: 5 * time.Second}
|
|
|
|
return d.Dial("tcp", c.ipPort)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectSSH opens an SSH connection to the buildlet for the given username.
|
|
|
|
// The authorizedPubKey must be a line from an ~/.ssh/authorized_keys file
|
|
|
|
// and correspond to the private key to be used to communicate over the net.Conn.
|
|
|
|
func (c *Client) ConnectSSH(user, authorizedPubKey string) (net.Conn, error) {
|
|
|
|
conn, err := c.getDialer()()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error dialing HTTP connection before SSH upgrade: %v", err)
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("POST", "/connect-ssh", nil)
|
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Add("X-Go-Ssh-User", user)
|
|
|
|
req.Header.Add("X-Go-Authorized-Key", authorizedPubKey)
|
|
|
|
if err := req.Write(conn); err != nil {
|
|
|
|
conn.Close()
|
|
|
|
return nil, fmt.Errorf("writing /connect-ssh HTTP request failed: %v", err)
|
|
|
|
}
|
|
|
|
bufr := bufio.NewReader(conn)
|
|
|
|
res, err := http.ReadResponse(bufr, req)
|
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
|
|
|
return nil, fmt.Errorf("reading /connect-ssh response: %v", err)
|
|
|
|
}
|
|
|
|
if res.StatusCode != http.StatusSwitchingProtocols {
|
|
|
|
slurp, _ := ioutil.ReadAll(res.Body)
|
|
|
|
return nil, fmt.Errorf("unexpected /connect-ssh response: %v, %s", res.Status, slurp)
|
|
|
|
}
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
2015-01-16 20:54:03 +03:00
|
|
|
func condRun(fn func()) {
|
|
|
|
if fn != nil {
|
|
|
|
fn()
|
|
|
|
}
|
|
|
|
}
|