woodpecker/shared/build/docker/image.go

125 lines
3 KiB
Go
Raw Normal View History

2014-02-07 10:10:01 +00:00
package docker
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"time"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/parsers"
2014-02-07 10:10:01 +00:00
)
type Images struct {
ID string `json:"Id"`
RepoTags []string `json:",omitempty"`
Created int64
Size int64
VirtualSize int64
ParentId string `json:",omitempty"`
// DEPRECATED
Repository string `json:",omitempty"`
Tag string `json:",omitempty"`
}
type Image struct {
ID string `json:"id"`
Parent string `json:"parent,omitempty"`
Comment string `json:"comment,omitempty"`
Created time.Time `json:"created"`
Container string `json:"container,omitempty"`
ContainerConfig Config `json:"container_config,omitempty"`
DockerVersion string `json:"docker_version,omitempty"`
Author string `json:"author,omitempty"`
Config *Config `json:"config,omitempty"`
Architecture string `json:"architecture,omitempty"`
OS string `json:"os,omitempty"`
Size int64
}
type Delete struct {
Deleted string `json:",omitempty"`
Untagged string `json:",omitempty"`
}
type ImageService struct {
*Client
}
// List Images
func (c *ImageService) List() ([]*Images, error) {
images := []*Images{}
err := c.do("GET", "/images/json?all=0", nil, &images)
return images, err
}
// Create an image, either by pull it from the registry or by importing it.
func (c *ImageService) Create(image string) error {
2014-02-10 11:49:12 +00:00
return c.do("POST", fmt.Sprintf("/images/create?fromImage=%s", image), nil, nil)
2014-02-07 10:10:01 +00:00
}
func (c *ImageService) Pull(image string) error {
name, tag := parsers.ParseRepositoryTag(image)
2014-02-07 10:10:01 +00:00
if len(tag) == 0 {
tag = DEFAULTTAG
}
return c.PullTag(name, tag)
}
func (c *ImageService) PullTag(name, tag string) error {
var out io.Writer
if Logging {
out = os.Stdout
}
path := fmt.Sprintf("/images/create?fromImage=%s&tag=%s", name, tag)
return c.stream("POST", path, nil, out, http.Header{})
}
// Remove the image name from the filesystem
func (c *ImageService) Remove(image string) ([]*Delete, error) {
resp := []*Delete{}
err := c.do("DELETE", fmt.Sprintf("/images/%s", image), nil, &resp)
return resp, err
}
// Inspect the image
func (c *ImageService) Inspect(name string) (*Image, error) {
image := Image{}
err := c.do("GET", fmt.Sprintf("/images/%s/json", name), nil, &image)
return &image, err
}
// Build the Image
func (c *ImageService) Build(tag, dir string) error {
// tar the file
context, err := archive.Tar(dir, archive.Uncompressed)
if err != nil {
return err
}
var body io.Reader
body = ioutil.NopCloser(context)
// Upload the build context
v := url.Values{}
v.Set("t", tag)
v.Set("q", "1")
v.Set("rm", "1")
2014-02-07 10:10:01 +00:00
// url path
path := fmt.Sprintf("/build?%s", v.Encode())
// set content type to tar file
headers := http.Header{}
headers.Set("Content-Type", "application/tar")
// make the request
2014-07-09 07:46:15 +00:00
return c.stream("POST", path, body /*os.Stdout*/, nil, headers)
2014-02-07 10:10:01 +00:00
}