github.com/marinho/drone@v0.2.1-0.20140504195434-d3ba962e89a7/pkg/build/docker/image.go (about) 1 package docker 2 3 import ( 4 "fmt" 5 "io" 6 "io/ioutil" 7 "net/http" 8 "net/url" 9 "os" 10 "time" 11 12 "github.com/dotcloud/docker/archive" 13 "github.com/dotcloud/docker/utils" 14 ) 15 16 type Images struct { 17 ID string `json:"Id"` 18 RepoTags []string `json:",omitempty"` 19 Created int64 20 Size int64 21 VirtualSize int64 22 ParentId string `json:",omitempty"` 23 24 // DEPRECATED 25 Repository string `json:",omitempty"` 26 Tag string `json:",omitempty"` 27 } 28 29 type Image struct { 30 ID string `json:"id"` 31 Parent string `json:"parent,omitempty"` 32 Comment string `json:"comment,omitempty"` 33 Created time.Time `json:"created"` 34 Container string `json:"container,omitempty"` 35 ContainerConfig Config `json:"container_config,omitempty"` 36 DockerVersion string `json:"docker_version,omitempty"` 37 Author string `json:"author,omitempty"` 38 Config *Config `json:"config,omitempty"` 39 Architecture string `json:"architecture,omitempty"` 40 OS string `json:"os,omitempty"` 41 Size int64 42 } 43 44 type Delete struct { 45 Deleted string `json:",omitempty"` 46 Untagged string `json:",omitempty"` 47 } 48 49 type ImageService struct { 50 *Client 51 } 52 53 // List Images 54 func (c *ImageService) List() ([]*Images, error) { 55 images := []*Images{} 56 err := c.do("GET", "/images/json?all=0", nil, &images) 57 return images, err 58 } 59 60 // Create an image, either by pull it from the registry or by importing it. 61 func (c *ImageService) Create(image string) error { 62 return c.do("POST", fmt.Sprintf("/images/create?fromImage=%s", image), nil, nil) 63 } 64 65 func (c *ImageService) Pull(image string) error { 66 name, tag := utils.ParseRepositoryTag(image) 67 if len(tag) == 0 { 68 tag = DEFAULTTAG 69 } 70 return c.PullTag(name, tag) 71 } 72 73 func (c *ImageService) PullTag(name, tag string) error { 74 var out io.Writer 75 if Logging { 76 out = os.Stdout 77 } 78 79 path := fmt.Sprintf("/images/create?fromImage=%s&tag=%s", name, tag) 80 return c.stream("POST", path, nil, out, http.Header{}) 81 } 82 83 // Remove the image name from the filesystem 84 func (c *ImageService) Remove(image string) ([]*Delete, error) { 85 resp := []*Delete{} 86 err := c.do("DELETE", fmt.Sprintf("/images/%s", image), nil, &resp) 87 return resp, err 88 } 89 90 // Inspect the image 91 func (c *ImageService) Inspect(name string) (*Image, error) { 92 image := Image{} 93 err := c.do("GET", fmt.Sprintf("/images/%s/json", name), nil, &image) 94 return &image, err 95 } 96 97 // Build the Image 98 func (c *ImageService) Build(tag, dir string) error { 99 100 // tar the file 101 context, err := archive.Tar(dir, archive.Uncompressed) 102 if err != nil { 103 return err 104 } 105 106 var body io.Reader 107 body = ioutil.NopCloser(context) 108 109 // Upload the build context 110 v := url.Values{} 111 v.Set("t", tag) 112 v.Set("q", "1") 113 v.Set("rm", "1") 114 115 // url path 116 path := fmt.Sprintf("/build?%s", v.Encode()) 117 118 // set content type to tar file 119 headers := http.Header{} 120 headers.Set("Content-Type", "application/tar") 121 122 // make the request 123 return c.stream("POST", path, body, os.Stdout, headers) 124 }