github.com/walkingsparrow/docker@v1.4.2-0.20151218153551-b708a2249bfa/api/client/lib/image_build.go (about)

     1  package lib
     2  
     3  import (
     4  	"encoding/base64"
     5  	"encoding/json"
     6  	"net/http"
     7  	"net/url"
     8  	"regexp"
     9  	"strconv"
    10  
    11  	"github.com/docker/docker/api/types"
    12  	"github.com/docker/docker/runconfig"
    13  	"github.com/docker/go-units"
    14  )
    15  
    16  var headerRegexp = regexp.MustCompile(`\ADocker/.+\s\((.+)\)\z`)
    17  
    18  // ImageBuild sends request to the daemon to build images.
    19  // The Body in the response implement an io.ReadCloser and it's up to the caller to
    20  // close it.
    21  func (cli *Client) ImageBuild(options types.ImageBuildOptions) (types.ImageBuildResponse, error) {
    22  	query, err := imageBuildOptionsToQuery(options)
    23  	if err != nil {
    24  		return types.ImageBuildResponse{}, err
    25  	}
    26  
    27  	headers := http.Header(make(map[string][]string))
    28  	buf, err := json.Marshal(options.AuthConfigs)
    29  	if err != nil {
    30  		return types.ImageBuildResponse{}, err
    31  	}
    32  	headers.Add("X-Registry-Config", base64.URLEncoding.EncodeToString(buf))
    33  	headers.Set("Content-Type", "application/tar")
    34  
    35  	serverResp, err := cli.postRaw("/build", query, options.Context, headers)
    36  	if err != nil {
    37  		return types.ImageBuildResponse{}, err
    38  	}
    39  
    40  	osType := getDockerOS(serverResp.header.Get("Server"))
    41  
    42  	return types.ImageBuildResponse{
    43  		Body:   serverResp.body,
    44  		OSType: osType,
    45  	}, nil
    46  }
    47  
    48  func imageBuildOptionsToQuery(options types.ImageBuildOptions) (url.Values, error) {
    49  	query := url.Values{
    50  		"t": options.Tags,
    51  	}
    52  	if options.SuppressOutput {
    53  		query.Set("q", "1")
    54  	}
    55  	if options.RemoteContext != "" {
    56  		query.Set("remote", options.RemoteContext)
    57  	}
    58  	if options.NoCache {
    59  		query.Set("nocache", "1")
    60  	}
    61  	if options.Remove {
    62  		query.Set("rm", "1")
    63  	} else {
    64  		query.Set("rm", "0")
    65  	}
    66  
    67  	if options.ForceRemove {
    68  		query.Set("forcerm", "1")
    69  	}
    70  
    71  	if options.PullParent {
    72  		query.Set("pull", "1")
    73  	}
    74  
    75  	if !runconfig.IsolationLevel.IsDefault(runconfig.IsolationLevel(options.Isolation)) {
    76  		query.Set("isolation", options.Isolation)
    77  	}
    78  
    79  	query.Set("cpusetcpus", options.CPUSetCPUs)
    80  	query.Set("cpusetmems", options.CPUSetMems)
    81  	query.Set("cpushares", strconv.FormatInt(options.CPUShares, 10))
    82  	query.Set("cpuquota", strconv.FormatInt(options.CPUQuota, 10))
    83  	query.Set("cpuperiod", strconv.FormatInt(options.CPUPeriod, 10))
    84  	query.Set("memory", strconv.FormatInt(options.Memory, 10))
    85  	query.Set("memswap", strconv.FormatInt(options.MemorySwap, 10))
    86  	query.Set("cgroupparent", options.CgroupParent)
    87  
    88  	if options.ShmSize != "" {
    89  		parsedShmSize, err := units.RAMInBytes(options.ShmSize)
    90  		if err != nil {
    91  			return query, err
    92  		}
    93  		query.Set("shmsize", strconv.FormatInt(parsedShmSize, 10))
    94  	}
    95  
    96  	query.Set("dockerfile", options.Dockerfile)
    97  
    98  	ulimitsJSON, err := json.Marshal(options.Ulimits)
    99  	if err != nil {
   100  		return query, err
   101  	}
   102  	query.Set("ulimits", string(ulimitsJSON))
   103  
   104  	buildArgs := runconfig.ConvertKVStringsToMap(options.BuildArgs)
   105  	buildArgsJSON, err := json.Marshal(buildArgs)
   106  	if err != nil {
   107  		return query, err
   108  	}
   109  	query.Set("buildargs", string(buildArgsJSON))
   110  
   111  	return query, nil
   112  }
   113  
   114  func getDockerOS(serverHeader string) string {
   115  	var osType string
   116  	matches := headerRegexp.FindStringSubmatch(serverHeader)
   117  	if len(matches) > 0 {
   118  		osType = matches[1]
   119  	}
   120  	return osType
   121  }