|
@@ -2,14 +2,14 @@ package remotecontext
|
|
|
|
|
|
import (
|
|
import (
|
|
"bytes"
|
|
"bytes"
|
|
- "errors"
|
|
|
|
"fmt"
|
|
"fmt"
|
|
"io"
|
|
"io"
|
|
"io/ioutil"
|
|
"io/ioutil"
|
|
|
|
+ "net/http"
|
|
"regexp"
|
|
"regexp"
|
|
|
|
|
|
"github.com/docker/docker/builder"
|
|
"github.com/docker/docker/builder"
|
|
- "github.com/docker/docker/pkg/httputils"
|
|
|
|
|
|
+ "github.com/pkg/errors"
|
|
)
|
|
)
|
|
|
|
|
|
// When downloading remote contexts, limit the amount (in bytes)
|
|
// When downloading remote contexts, limit the amount (in bytes)
|
|
@@ -30,7 +30,7 @@ var mimeRe = regexp.MustCompile(acceptableRemoteMIME)
|
|
// to be returned. If no match is found, it is assumed the body is a tar stream (compressed or not).
|
|
// to be returned. If no match is found, it is assumed the body is a tar stream (compressed or not).
|
|
// In either case, an (assumed) tar stream is passed to MakeTarSumContext whose result is returned.
|
|
// In either case, an (assumed) tar stream is passed to MakeTarSumContext whose result is returned.
|
|
func MakeRemoteContext(remoteURL string, contentTypeHandlers map[string]func(io.ReadCloser) (io.ReadCloser, error)) (builder.Source, error) {
|
|
func MakeRemoteContext(remoteURL string, contentTypeHandlers map[string]func(io.ReadCloser) (io.ReadCloser, error)) (builder.Source, error) {
|
|
- f, err := httputils.Download(remoteURL)
|
|
|
|
|
|
+ f, err := GetWithStatusError(remoteURL)
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error downloading remote context %s: %v", remoteURL, err)
|
|
return nil, fmt.Errorf("error downloading remote context %s: %v", remoteURL, err)
|
|
}
|
|
}
|
|
@@ -66,6 +66,24 @@ func MakeRemoteContext(remoteURL string, contentTypeHandlers map[string]func(io.
|
|
return MakeTarSumContext(contextReader)
|
|
return MakeTarSumContext(contextReader)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// GetWithStatusError does an http.Get() and returns an error if the
|
|
|
|
+// status code is 4xx or 5xx.
|
|
|
|
+func GetWithStatusError(url string) (resp *http.Response, err error) {
|
|
|
|
+ if resp, err = http.Get(url); err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ if resp.StatusCode < 400 {
|
|
|
|
+ return resp, nil
|
|
|
|
+ }
|
|
|
|
+ msg := fmt.Sprintf("failed to GET %s with status %s", url, resp.Status)
|
|
|
|
+ body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
+ resp.Body.Close()
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, errors.Wrapf(err, msg+": error reading body")
|
|
|
|
+ }
|
|
|
|
+ return nil, errors.Errorf(msg+": %s", bytes.TrimSpace(body))
|
|
|
|
+}
|
|
|
|
+
|
|
// inspectResponse looks into the http response data at r to determine whether its
|
|
// inspectResponse looks into the http response data at r to determine whether its
|
|
// content-type is on the list of acceptable content types for remote build contexts.
|
|
// content-type is on the list of acceptable content types for remote build contexts.
|
|
// This function returns:
|
|
// This function returns:
|