2013-03-21 13:10:14 -04:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2013-03-22 04:25:27 -04:00
|
|
|
"github.com/dotcloud/docker/auth"
|
2013-03-21 13:10:14 -04:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
//FIXME: Set the endpoint in a conf file or via commandline
|
|
|
|
//const REGISTRY_ENDPOINT = "http://registry-creack.dotcloud.com/v1"
|
2013-03-22 04:25:27 -04:00
|
|
|
const REGISTRY_ENDPOINT = auth.REGISTRY_SERVER + "/v1"
|
2013-03-21 13:10:14 -04:00
|
|
|
|
|
|
|
// Build an Image object from raw json data
|
|
|
|
func NewImgJson(src []byte) (*Image, error) {
|
|
|
|
ret := &Image{}
|
|
|
|
|
|
|
|
fmt.Printf("Json string: {%s}\n", src)
|
|
|
|
// FIXME: Is there a cleaner way to "puryfy" the input json?
|
|
|
|
src = []byte(strings.Replace(string(src), "null", "\"\"", -1))
|
|
|
|
|
|
|
|
if err := json.Unmarshal(src, ret); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build an Image object list from a raw json data
|
|
|
|
// FIXME: Do this in "stream" mode
|
|
|
|
func NewMultipleImgJson(src []byte) ([]*Image, error) {
|
|
|
|
ret := []*Image{}
|
|
|
|
|
|
|
|
dec := json.NewDecoder(strings.NewReader(strings.Replace(string(src), "null", "\"\"", -1)))
|
|
|
|
for {
|
|
|
|
m := &Image{}
|
|
|
|
if err := dec.Decode(m); err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret = append(ret, m)
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the history of a given image from the Registry.
|
|
|
|
// Return a list of the parent's json (requested image included)
|
2013-03-22 05:57:28 -04:00
|
|
|
func (graph *Graph) getRemoteHistory(imgId string, authConfig *auth.AuthConfig) ([]*Image, error) {
|
|
|
|
client := &http.Client{}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", REGISTRY_ENDPOINT+"/images/"+imgId+"/history", nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.SetBasicAuth(authConfig.Username, authConfig.Password)
|
|
|
|
res, err := client.Do(req)
|
2013-03-21 13:10:14 -04:00
|
|
|
if err != nil {
|
2013-03-22 05:57:28 -04:00
|
|
|
return nil, err
|
2013-03-21 13:10:14 -04:00
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
jsonString, err := ioutil.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error while reading the http response: %s\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
history, err := NewMultipleImgJson(jsonString)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error while parsing the json: %s\n", err)
|
|
|
|
}
|
|
|
|
return history, nil
|
|
|
|
}
|
|
|
|
|
2013-03-22 04:25:27 -04:00
|
|
|
// Check if an image exists in the Registry
|
2013-03-22 05:57:28 -04:00
|
|
|
func (graph *Graph) LookupRemoteImage(imgId string, authConfig *auth.AuthConfig) bool {
|
|
|
|
client := &http.Client{}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", REGISTRY_ENDPOINT+"/images/"+imgId+"/json", nil)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
req.SetBasicAuth(authConfig.Username, authConfig.Password)
|
|
|
|
res, err := client.Do(req)
|
2013-03-22 04:25:27 -04:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return res.StatusCode == 307
|
|
|
|
}
|
|
|
|
|
2013-03-21 13:10:14 -04:00
|
|
|
// Retrieve an image from the Registry.
|
|
|
|
// Returns the Image object as well as the layer as an Archive (io.Reader)
|
2013-03-22 05:57:28 -04:00
|
|
|
func (graph *Graph) getRemoteImage(imgId string, authConfig *auth.AuthConfig) (*Image, Archive, error) {
|
|
|
|
client := &http.Client{}
|
|
|
|
|
2013-03-21 13:10:14 -04:00
|
|
|
// Get the Json
|
2013-03-22 05:57:28 -04:00
|
|
|
req, err := http.NewRequest("GET", REGISTRY_ENDPOINT+"/images/"+imgId+"/json", nil)
|
2013-03-21 13:10:14 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("Error while getting from the server: %s\n", err)
|
|
|
|
}
|
2013-03-22 05:57:28 -04:00
|
|
|
req.SetBasicAuth(authConfig.Username, authConfig.Password)
|
|
|
|
res, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2013-03-21 13:10:14 -04:00
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
jsonString, err := ioutil.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("Error while reading the http response: %s\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
img, err := NewImgJson(jsonString)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("Error while parsing the json: %s\n", err)
|
|
|
|
}
|
|
|
|
img.Id = imgId
|
|
|
|
|
|
|
|
// Get the layer
|
|
|
|
res, err = http.Get(REGISTRY_ENDPOINT + "/images/" + imgId + "/layer")
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("Error while getting from the server: %s\n", err)
|
|
|
|
}
|
|
|
|
return img, res.Body, nil
|
|
|
|
}
|
|
|
|
|
2013-03-22 05:57:28 -04:00
|
|
|
func (graph *Graph) PullImage(imgId string, authConfig *auth.AuthConfig) error {
|
|
|
|
history, err := graph.getRemoteHistory(imgId, authConfig)
|
2013-03-21 13:10:14 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// FIXME: Try to stream the images?
|
|
|
|
// FIXME: Lunch the getRemoteImage() in goroutines
|
|
|
|
for _, j := range history {
|
|
|
|
if !graph.Exists(j.Id) {
|
2013-03-22 05:57:28 -04:00
|
|
|
img, layer, err := graph.getRemoteImage(j.Id, authConfig)
|
2013-03-21 13:10:14 -04:00
|
|
|
if err != nil {
|
|
|
|
// FIXME: Keep goging in case of error?
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = graph.Register(layer, img); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-03-22 04:25:27 -04:00
|
|
|
// FIXME: Handle the askedTag parameter
|
2013-03-22 05:19:39 -04:00
|
|
|
func (graph *Graph) PullRepository(user, repoName, askedTag string, repositories *TagStore, authConfig *auth.AuthConfig) error {
|
2013-03-22 04:25:27 -04:00
|
|
|
client := &http.Client{}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", REGISTRY_ENDPOINT+"/users/"+user+"/"+repoName, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-03-22 05:19:39 -04:00
|
|
|
req.SetBasicAuth(authConfig.Username, authConfig.Password)
|
2013-03-22 04:25:27 -04:00
|
|
|
res, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-03-22 05:57:28 -04:00
|
|
|
defer res.Body.Close()
|
2013-03-22 04:25:27 -04:00
|
|
|
rawJson, err := ioutil.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t := map[string]string{}
|
|
|
|
if err = json.Unmarshal(rawJson, &t); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for tag, rev := range t {
|
2013-03-22 05:57:28 -04:00
|
|
|
if err = graph.PullImage(rev, authConfig); err != nil {
|
2013-03-22 04:25:27 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-03-22 21:27:18 -04:00
|
|
|
if err = repositories.Set(repoName, tag, rev, true); err != nil {
|
2013-03-22 04:25:27 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err = repositories.Save(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-03-21 13:10:14 -04:00
|
|
|
// Push a local image to the registry with its history if needed
|
2013-03-22 05:57:28 -04:00
|
|
|
func (graph *Graph) PushImage(imgOrig *Image, authConfig *auth.AuthConfig) error {
|
2013-03-21 13:10:14 -04:00
|
|
|
client := &http.Client{}
|
|
|
|
|
|
|
|
// FIXME: Factorize the code
|
|
|
|
// FIXME: Do the puts in goroutines
|
|
|
|
if err := imgOrig.WalkHistory(func(img *Image) error {
|
|
|
|
|
|
|
|
jsonRaw, err := ioutil.ReadFile(path.Join(graph.Root, img.Id, "json"))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error while retreiving the path for {%s}: %s", img.Id, err)
|
|
|
|
}
|
2013-03-22 04:25:27 -04:00
|
|
|
// FIXME: try json with UTF8
|
2013-03-21 13:10:14 -04:00
|
|
|
jsonData := strings.NewReader(string(jsonRaw))
|
|
|
|
req, err := http.NewRequest("PUT", REGISTRY_ENDPOINT+"/images/"+img.Id+"/json", jsonData)
|
2013-03-22 04:25:27 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-03-22 06:22:36 -04:00
|
|
|
req.Header.Add("Content-type", "application/json")
|
2013-03-22 05:57:28 -04:00
|
|
|
req.SetBasicAuth(authConfig.Username, authConfig.Password)
|
2013-03-21 13:10:14 -04:00
|
|
|
res, err := client.Do(req)
|
|
|
|
if err != nil || res.StatusCode != 200 {
|
|
|
|
if res == nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error: Internal server error trying to push image {%s} (json): %s",
|
|
|
|
img.Id, err)
|
|
|
|
}
|
|
|
|
switch res.StatusCode {
|
|
|
|
case 204:
|
|
|
|
// Case where the image is already on the Registry
|
|
|
|
// FIXME: Do not be silent?
|
|
|
|
return nil
|
|
|
|
case 400:
|
|
|
|
return fmt.Errorf("Error: Invalid Json")
|
|
|
|
default:
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error: Internal server error trying to push image {%s} (json): %s (%d)\n",
|
|
|
|
img.Id, err, res.StatusCode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
req2, err := http.NewRequest("PUT", REGISTRY_ENDPOINT+"/images/"+img.Id+"/layer", nil)
|
2013-03-22 05:57:28 -04:00
|
|
|
req2.SetBasicAuth(authConfig.Username, authConfig.Password)
|
2013-03-21 13:10:14 -04:00
|
|
|
res2, err := client.Do(req2)
|
|
|
|
if err != nil || res2.StatusCode != 307 {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error trying to push image {%s} (layer 1): %s\n",
|
|
|
|
img.Id, err)
|
|
|
|
}
|
|
|
|
url, err := res2.Location()
|
|
|
|
if err != nil || url == nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Fail to retrieve layer storage URL for image {%s}: %s\n",
|
|
|
|
img.Id, err)
|
|
|
|
}
|
|
|
|
// FIXME: Don't do this :D. Check the S3 requierement and implement chunks of 5MB
|
|
|
|
// FIXME2: I won't stress it enough, DON'T DO THIS! very high priority
|
|
|
|
layerData2, err := Tar(path.Join(graph.Root, img.Id, "layer"), Gzip)
|
|
|
|
layerData, err := Tar(path.Join(graph.Root, img.Id, "layer"), Gzip)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error while retrieving layer for {%s}: %s\n",
|
|
|
|
img.Id, err)
|
|
|
|
}
|
|
|
|
req3, err := http.NewRequest("PUT", url.String(), layerData)
|
2013-03-22 05:57:28 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tmp, err := ioutil.ReadAll(layerData2)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-03-21 13:10:14 -04:00
|
|
|
req3.ContentLength = int64(len(tmp))
|
|
|
|
|
|
|
|
req3.TransferEncoding = []string{"none"}
|
|
|
|
res3, err := client.Do(req3)
|
|
|
|
if err != nil || res3.StatusCode != 200 {
|
|
|
|
if res3 == nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error trying to push image {%s} (layer 2): %s\n",
|
|
|
|
img.Id, err)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error trying to push image {%s} (layer 2): %s (%d)\n",
|
|
|
|
img.Id, err, res3.StatusCode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2013-03-22 04:25:27 -04:00
|
|
|
|
2013-03-22 05:57:28 -04:00
|
|
|
func (graph *Graph) pushTag(user, repo, revision, tag string, authConfig *auth.AuthConfig) error {
|
2013-03-22 04:25:27 -04:00
|
|
|
|
|
|
|
if tag == "" {
|
|
|
|
tag = "lastest"
|
|
|
|
}
|
|
|
|
|
|
|
|
revision = "\"" + revision + "\""
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
req, err := http.NewRequest("PUT", REGISTRY_ENDPOINT+"/users/"+user+"/"+repo+"/"+tag, strings.NewReader(revision))
|
|
|
|
req.Header.Add("Content-type", "application/json")
|
2013-03-22 05:57:28 -04:00
|
|
|
req.SetBasicAuth(authConfig.Username, authConfig.Password)
|
2013-03-22 04:25:27 -04:00
|
|
|
res, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Printf("Result of push tag: %d\n", res.StatusCode)
|
|
|
|
switch res.StatusCode {
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Error %d\n", res.StatusCode)
|
|
|
|
case 200:
|
|
|
|
case 201:
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-03-22 05:57:28 -04:00
|
|
|
func (graph *Graph) PushRepository(user, repoName string, repo Repository, authConfig *auth.AuthConfig) error {
|
2013-03-22 04:25:27 -04:00
|
|
|
for tag, imgId := range repo {
|
|
|
|
fmt.Printf("tag: %s, imgId: %s\n", tag, imgId)
|
|
|
|
img, err := graph.Get(imgId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-03-22 05:57:28 -04:00
|
|
|
if err = graph.PushImage(img, authConfig); err != nil {
|
2013-03-22 04:25:27 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-03-22 05:57:28 -04:00
|
|
|
if err = graph.pushTag(user, repoName, imgId, tag, authConfig); err != nil {
|
2013-03-22 04:25:27 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|