2016-09-06 14:46:37 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
)
|
|
|
|
|
|
|
|
var headerRegexp = regexp.MustCompile(`\ADocker/.+\s\((.+)\)\z`)
|
|
|
|
|
|
|
|
// ImageBuild sends request to the daemon to build images.
|
|
|
|
// The Body in the response implement an io.ReadCloser and it's up to the caller to
|
|
|
|
// close it.
|
|
|
|
func (cli *Client) ImageBuild(ctx context.Context, buildContext io.Reader, options types.ImageBuildOptions) (types.ImageBuildResponse, error) {
|
2016-11-02 20:43:32 -04:00
|
|
|
query, err := cli.imageBuildOptionsToQuery(options)
|
2016-09-06 14:46:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return types.ImageBuildResponse{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
headers := http.Header(make(map[string][]string))
|
|
|
|
buf, err := json.Marshal(options.AuthConfigs)
|
|
|
|
if err != nil {
|
|
|
|
return types.ImageBuildResponse{}, err
|
|
|
|
}
|
|
|
|
headers.Add("X-Registry-Config", base64.URLEncoding.EncodeToString(buf))
|
|
|
|
headers.Set("Content-Type", "application/tar")
|
|
|
|
|
|
|
|
serverResp, err := cli.postRaw(ctx, "/build", query, buildContext, headers)
|
|
|
|
if err != nil {
|
|
|
|
return types.ImageBuildResponse{}, err
|
|
|
|
}
|
|
|
|
|
2016-09-07 19:08:51 -04:00
|
|
|
osType := GetDockerOS(serverResp.header.Get("Server"))
|
2016-09-06 14:46:37 -04:00
|
|
|
|
|
|
|
return types.ImageBuildResponse{
|
|
|
|
Body: serverResp.body,
|
|
|
|
OSType: osType,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-11-02 20:43:32 -04:00
|
|
|
func (cli *Client) imageBuildOptionsToQuery(options types.ImageBuildOptions) (url.Values, error) {
|
2016-09-06 14:46:37 -04:00
|
|
|
query := url.Values{
|
2016-06-07 15:15:50 -04:00
|
|
|
"t": options.Tags,
|
|
|
|
"securityopt": options.SecurityOpt,
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
if options.SuppressOutput {
|
|
|
|
query.Set("q", "1")
|
|
|
|
}
|
|
|
|
if options.RemoteContext != "" {
|
|
|
|
query.Set("remote", options.RemoteContext)
|
|
|
|
}
|
|
|
|
if options.NoCache {
|
|
|
|
query.Set("nocache", "1")
|
|
|
|
}
|
|
|
|
if options.Remove {
|
|
|
|
query.Set("rm", "1")
|
|
|
|
} else {
|
|
|
|
query.Set("rm", "0")
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.ForceRemove {
|
|
|
|
query.Set("forcerm", "1")
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.PullParent {
|
|
|
|
query.Set("pull", "1")
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.Squash {
|
2016-11-02 20:43:32 -04:00
|
|
|
if err := cli.NewVersionError("1.25", "squash"); err != nil {
|
|
|
|
return query, err
|
|
|
|
}
|
2016-09-06 14:46:37 -04:00
|
|
|
query.Set("squash", "1")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !container.Isolation.IsDefault(options.Isolation) {
|
|
|
|
query.Set("isolation", string(options.Isolation))
|
|
|
|
}
|
|
|
|
|
|
|
|
query.Set("cpusetcpus", options.CPUSetCPUs)
|
2016-03-06 07:29:23 -05:00
|
|
|
query.Set("networkmode", options.NetworkMode)
|
2016-09-06 14:46:37 -04:00
|
|
|
query.Set("cpusetmems", options.CPUSetMems)
|
|
|
|
query.Set("cpushares", strconv.FormatInt(options.CPUShares, 10))
|
|
|
|
query.Set("cpuquota", strconv.FormatInt(options.CPUQuota, 10))
|
|
|
|
query.Set("cpuperiod", strconv.FormatInt(options.CPUPeriod, 10))
|
|
|
|
query.Set("memory", strconv.FormatInt(options.Memory, 10))
|
|
|
|
query.Set("memswap", strconv.FormatInt(options.MemorySwap, 10))
|
|
|
|
query.Set("cgroupparent", options.CgroupParent)
|
|
|
|
query.Set("shmsize", strconv.FormatInt(options.ShmSize, 10))
|
|
|
|
query.Set("dockerfile", options.Dockerfile)
|
|
|
|
|
|
|
|
ulimitsJSON, err := json.Marshal(options.Ulimits)
|
|
|
|
if err != nil {
|
|
|
|
return query, err
|
|
|
|
}
|
|
|
|
query.Set("ulimits", string(ulimitsJSON))
|
|
|
|
|
|
|
|
buildArgsJSON, err := json.Marshal(options.BuildArgs)
|
|
|
|
if err != nil {
|
|
|
|
return query, err
|
|
|
|
}
|
|
|
|
query.Set("buildargs", string(buildArgsJSON))
|
|
|
|
|
|
|
|
labelsJSON, err := json.Marshal(options.Labels)
|
|
|
|
if err != nil {
|
|
|
|
return query, err
|
|
|
|
}
|
|
|
|
query.Set("labels", string(labelsJSON))
|
2016-09-22 17:38:00 -04:00
|
|
|
|
|
|
|
cacheFromJSON, err := json.Marshal(options.CacheFrom)
|
|
|
|
if err != nil {
|
|
|
|
return query, err
|
|
|
|
}
|
|
|
|
query.Set("cachefrom", string(cacheFromJSON))
|
|
|
|
|
2016-09-06 14:46:37 -04:00
|
|
|
return query, nil
|
|
|
|
}
|
|
|
|
|
2016-09-07 19:08:51 -04:00
|
|
|
// GetDockerOS returns the operating system based on the server header from the daemon.
|
|
|
|
func GetDockerOS(serverHeader string) string {
|
2016-09-06 14:46:37 -04:00
|
|
|
var osType string
|
|
|
|
matches := headerRegexp.FindStringSubmatch(serverHeader)
|
|
|
|
if len(matches) > 0 {
|
|
|
|
osType = matches[1]
|
|
|
|
}
|
|
|
|
return osType
|
|
|
|
}
|