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{}
|
|
|
|
|
2013-03-22 16:21:44 -04:00
|
|
|
Debugf("Json string: {%s}\n", src)
|
2013-03-30 03:22:24 -04:00
|
|
|
// FIXME: Is there a cleaner way to "purify" the input json?
|
2013-03-21 13:10:14 -04:00
|
|
|
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{}
|
|
|
|
|
2013-04-02 21:37:29 -04:00
|
|
|
dec := json.NewDecoder(strings.NewReader(string(src)))
|
2013-03-21 13:10:14 -04:00
|
|
|
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-22 07:44:07 -04:00
|
|
|
if err != nil || res.StatusCode != 200 {
|
|
|
|
if res != nil {
|
|
|
|
return nil, fmt.Errorf("Internal server error: %d trying to fetch remote history for %s", res.StatusCode, imgId)
|
|
|
|
}
|
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 {
|
2013-03-22 13:22:22 -04:00
|
|
|
rt := &http.Transport{Proxy: http.ProxyFromEnvironment}
|
2013-03-22 05:57:28 -04:00
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", REGISTRY_ENDPOINT+"/images/"+imgId+"/json", nil)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
req.SetBasicAuth(authConfig.Username, authConfig.Password)
|
2013-03-22 13:22:22 -04:00
|
|
|
res, err := rt.RoundTrip(req)
|
2013-03-22 07:44:07 -04:00
|
|
|
if err != nil || res.StatusCode != 307 {
|
2013-03-22 04:25:27 -04:00
|
|
|
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-25 21:48:57 -04:00
|
|
|
func (graph *Graph) getRemoteImage(stdout io.Writer, imgId string, authConfig *auth.AuthConfig) (*Image, Archive, error) {
|
2013-03-22 05:57:28 -04:00
|
|
|
client := &http.Client{}
|
|
|
|
|
2013-04-10 22:13:15 -04:00
|
|
|
fmt.Fprintf(stdout, "Pulling %s metadata\r\n", imgId)
|
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 {
|
2013-03-25 21:48:57 -04:00
|
|
|
return nil, nil, fmt.Errorf("Failed to download json: %s", err)
|
2013-03-21 13:10:14 -04:00
|
|
|
}
|
2013-03-22 05:57:28 -04:00
|
|
|
req.SetBasicAuth(authConfig.Username, authConfig.Password)
|
|
|
|
res, err := client.Do(req)
|
2013-03-25 21:48:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("Failed to download json: %s", err)
|
|
|
|
}
|
|
|
|
if res.StatusCode != 200 {
|
|
|
|
return nil, nil, fmt.Errorf("HTTP code %d", res.StatusCode)
|
2013-03-22 05:57:28 -04:00
|
|
|
}
|
2013-03-21 13:10:14 -04:00
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
jsonString, err := ioutil.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
2013-03-25 21:48:57 -04:00
|
|
|
return nil, nil, fmt.Errorf("Failed to download json: %s", err)
|
2013-03-21 13:10:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
img, err := NewImgJson(jsonString)
|
|
|
|
if err != nil {
|
2013-03-25 21:48:57 -04:00
|
|
|
return nil, nil, fmt.Errorf("Failed to parse json: %s", err)
|
2013-03-21 13:10:14 -04:00
|
|
|
}
|
|
|
|
img.Id = imgId
|
|
|
|
|
|
|
|
// Get the layer
|
2013-04-10 22:13:15 -04:00
|
|
|
fmt.Fprintf(stdout, "Pulling %s fs layer\r\n", imgId)
|
2013-03-22 10:56:44 -04:00
|
|
|
req, err = http.NewRequest("GET", REGISTRY_ENDPOINT+"/images/"+imgId+"/layer", 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 10:56:44 -04:00
|
|
|
req.SetBasicAuth(authConfig.Username, authConfig.Password)
|
|
|
|
res, err = client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2013-03-31 16:04:41 -04:00
|
|
|
return img, ProgressReader(res.Body, int(res.ContentLength), stdout), nil
|
2013-03-21 13:10:14 -04:00
|
|
|
}
|
|
|
|
|
2013-03-25 21:48:57 -04:00
|
|
|
func (graph *Graph) PullImage(stdout io.Writer, imgId string, authConfig *auth.AuthConfig) error {
|
2013-03-22 05:57:28 -04:00
|
|
|
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-25 21:48:57 -04:00
|
|
|
img, layer, err := graph.getRemoteImage(stdout, 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 16:21:44 -04:00
|
|
|
func (graph *Graph) PullRepository(stdout io.Writer, remote, askedTag string, repositories *TagStore, authConfig *auth.AuthConfig) error {
|
2013-03-22 04:25:27 -04:00
|
|
|
client := &http.Client{}
|
|
|
|
|
2013-04-10 22:13:15 -04:00
|
|
|
fmt.Fprintf(stdout, "Pulling repository %s\r\n", remote)
|
2013-03-22 10:56:44 -04:00
|
|
|
|
2013-03-22 20:41:48 -04:00
|
|
|
var repositoryTarget string
|
|
|
|
// If we are asking for 'root' repository, lookup on the Library's registry
|
|
|
|
if strings.Index(remote, "/") == -1 {
|
|
|
|
repositoryTarget = REGISTRY_ENDPOINT + "/library/" + remote
|
|
|
|
} else {
|
|
|
|
repositoryTarget = REGISTRY_ENDPOINT + "/users/" + remote
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", repositoryTarget, nil)
|
2013-03-22 04:25:27 -04:00
|
|
|
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)
|
2013-03-25 21:48:57 -04:00
|
|
|
if err != nil {
|
2013-03-22 04:25:27 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-04-01 09:49:51 -04:00
|
|
|
defer res.Body.Close()
|
2013-03-25 21:48:57 -04:00
|
|
|
if res.StatusCode != 200 {
|
|
|
|
return fmt.Errorf("HTTP code: %d", res.StatusCode)
|
|
|
|
}
|
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-04-10 22:13:15 -04:00
|
|
|
fmt.Fprintf(stdout, "Pulling tag %s:%s\r\n", remote, tag)
|
2013-03-25 21:48:57 -04:00
|
|
|
if err = graph.PullImage(stdout, rev, authConfig); err != nil {
|
2013-03-22 04:25:27 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-03-22 10:56:44 -04:00
|
|
|
if err = repositories.Set(remote, 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 16:21:44 -04:00
|
|
|
func (graph *Graph) PushImage(stdout io.Writer, 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 14:44:12 -04:00
|
|
|
|
2013-04-10 22:13:15 -04:00
|
|
|
fmt.Fprintf(stdout, "Pushing %s metadata\r\n", img.Id)
|
2013-03-22 14:44:12 -04:00
|
|
|
|
2013-03-22 04:25:27 -04:00
|
|
|
// FIXME: try json with UTF8
|
2013-04-02 21:37:29 -04:00
|
|
|
jsonData := strings.NewReader(string(jsonRaw))
|
2013-03-21 13:10:14 -04:00
|
|
|
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)
|
2013-03-25 20:20:55 -04:00
|
|
|
if err != nil {
|
2013-03-25 21:48:57 -04:00
|
|
|
return fmt.Errorf("Failed to upload metadata: %s", err)
|
2013-03-25 20:20:55 -04:00
|
|
|
}
|
2013-04-01 09:49:51 -04:00
|
|
|
defer res.Body.Close()
|
2013-03-25 20:20:55 -04:00
|
|
|
if res.StatusCode != 200 {
|
2013-03-21 13:10:14 -04:00
|
|
|
switch res.StatusCode {
|
|
|
|
case 204:
|
|
|
|
// Case where the image is already on the Registry
|
|
|
|
// FIXME: Do not be silent?
|
|
|
|
return nil
|
|
|
|
default:
|
2013-03-25 21:50:02 -04:00
|
|
|
errBody, err := ioutil.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
errBody = []byte(err.Error())
|
|
|
|
}
|
|
|
|
return fmt.Errorf("HTTP code %d while uploading metadata: %s", res.StatusCode, errBody)
|
2013-03-21 13:10:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-10 22:13:15 -04:00
|
|
|
fmt.Fprintf(stdout, "Pushing %s fs layer\r\n", img.Id)
|
2013-03-21 13:10:14 -04:00
|
|
|
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)
|
2013-04-01 09:49:51 -04:00
|
|
|
if err != nil {
|
2013-03-25 20:20:26 -04:00
|
|
|
return fmt.Errorf("Registry returned error: %s", err)
|
2013-03-21 13:10:14 -04:00
|
|
|
}
|
2013-04-01 09:49:51 -04:00
|
|
|
res2.Body.Close()
|
|
|
|
if res2.StatusCode != 307 {
|
|
|
|
return fmt.Errorf("Registry returned unexpected HTTP status code %d, expected 307", res2.StatusCode)
|
|
|
|
}
|
2013-03-21 13:10:14 -04:00
|
|
|
url, err := res2.Location()
|
|
|
|
if err != nil || url == nil {
|
2013-03-25 20:20:26 -04:00
|
|
|
return fmt.Errorf("Failed to retrieve layer upload location: %s", err)
|
2013-03-21 13:10:14 -04:00
|
|
|
}
|
2013-03-22 14:44:12 -04:00
|
|
|
|
2013-03-21 13:10:14 -04:00
|
|
|
// 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
|
2013-04-01 19:19:34 -04:00
|
|
|
layerData2, err := Tar(path.Join(graph.Root, img.Id, "layer"), Xz)
|
2013-03-31 16:53:47 -04:00
|
|
|
tmp, err := ioutil.ReadAll(layerData2)
|
2013-03-21 13:10:14 -04:00
|
|
|
if err != nil {
|
2013-03-31 16:53:47 -04:00
|
|
|
return err
|
2013-03-21 13:10:14 -04:00
|
|
|
}
|
2013-03-31 16:53:47 -04:00
|
|
|
layerLength := len(tmp)
|
|
|
|
|
2013-04-01 19:19:34 -04:00
|
|
|
layerData, err := Tar(path.Join(graph.Root, img.Id, "layer"), Xz)
|
2013-03-22 05:57:28 -04:00
|
|
|
if err != nil {
|
2013-03-31 16:53:47 -04:00
|
|
|
return fmt.Errorf("Failed to generate layer archive: %s", err)
|
2013-03-22 05:57:28 -04:00
|
|
|
}
|
2013-03-31 16:53:47 -04:00
|
|
|
req3, err := http.NewRequest("PUT", url.String(), ProgressReader(layerData.(io.ReadCloser), layerLength, stdout))
|
2013-03-22 05:57:28 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-03-31 16:53:47 -04:00
|
|
|
req3.ContentLength = int64(layerLength)
|
2013-03-21 13:10:14 -04:00
|
|
|
|
|
|
|
req3.TransferEncoding = []string{"none"}
|
|
|
|
res3, err := client.Do(req3)
|
2013-03-25 20:20:55 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to upload layer: %s", err)
|
|
|
|
}
|
2013-04-01 09:49:51 -04:00
|
|
|
res3.Body.Close()
|
2013-03-25 20:20:55 -04:00
|
|
|
if res3.StatusCode != 200 {
|
|
|
|
return fmt.Errorf("Received HTTP code %d while uploading layer", res3.StatusCode)
|
2013-03-21 13:10:14 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2013-03-22 04:25:27 -04:00
|
|
|
|
2013-03-22 09:38:54 -04:00
|
|
|
// push a tag on the registry.
|
|
|
|
// Remote has the format '<user>/<repo>
|
|
|
|
func (graph *Graph) pushTag(remote, revision, tag string, authConfig *auth.AuthConfig) error {
|
2013-03-22 04:25:27 -04:00
|
|
|
|
2013-03-22 09:38:54 -04:00
|
|
|
// Keep this for backward compatibility
|
2013-03-22 04:25:27 -04:00
|
|
|
if tag == "" {
|
|
|
|
tag = "lastest"
|
|
|
|
}
|
|
|
|
|
2013-03-22 09:38:54 -04:00
|
|
|
// "jsonify" the string
|
2013-03-22 04:25:27 -04:00
|
|
|
revision = "\"" + revision + "\""
|
|
|
|
|
2013-03-22 14:44:12 -04:00
|
|
|
Debugf("Pushing tags for rev [%s] on {%s}\n", revision, REGISTRY_ENDPOINT+"/users/"+remote+"/"+tag)
|
|
|
|
|
2013-03-22 04:25:27 -04:00
|
|
|
client := &http.Client{}
|
2013-03-22 09:38:54 -04:00
|
|
|
req, err := http.NewRequest("PUT", REGISTRY_ENDPOINT+"/users/"+remote+"/"+tag, strings.NewReader(revision))
|
2013-03-22 04:25:27 -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-22 04:25:27 -04:00
|
|
|
res, err := client.Do(req)
|
2013-04-01 09:49:51 -04:00
|
|
|
if err != nil {
|
2013-03-22 04:25:27 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-04-01 09:49:51 -04:00
|
|
|
res.Body.Close()
|
|
|
|
if res.StatusCode != 200 && res.StatusCode != 201 {
|
|
|
|
return fmt.Errorf("Internal server error: %d trying to push tag %s on %s", res.StatusCode, tag, remote)
|
|
|
|
}
|
2013-03-22 16:21:44 -04:00
|
|
|
Debugf("Result of push tag: %d\n", res.StatusCode)
|
2013-03-22 04:25:27 -04:00
|
|
|
switch res.StatusCode {
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Error %d\n", res.StatusCode)
|
|
|
|
case 200:
|
|
|
|
case 201:
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-03-22 13:42:13 -04:00
|
|
|
func (graph *Graph) LookupRemoteRepository(remote string, authConfig *auth.AuthConfig) bool {
|
|
|
|
rt := &http.Transport{Proxy: http.ProxyFromEnvironment}
|
|
|
|
|
2013-03-26 07:11:13 -04:00
|
|
|
var repositoryTarget string
|
|
|
|
// If we are asking for 'root' repository, lookup on the Library's registry
|
|
|
|
if strings.Index(remote, "/") == -1 {
|
|
|
|
repositoryTarget = REGISTRY_ENDPOINT + "/library/" + remote + "/lookup"
|
|
|
|
} else {
|
|
|
|
repositoryTarget = REGISTRY_ENDPOINT + "/users/" + remote + "/lookup"
|
|
|
|
}
|
|
|
|
Debugf("Checking for permissions on: %s", repositoryTarget)
|
|
|
|
req, err := http.NewRequest("PUT", repositoryTarget, strings.NewReader("\"\""))
|
2013-03-22 13:42:13 -04:00
|
|
|
if err != nil {
|
2013-03-26 07:11:13 -04:00
|
|
|
Debugf("%s\n", err)
|
2013-03-22 13:42:13 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
req.SetBasicAuth(authConfig.Username, authConfig.Password)
|
2013-03-26 07:11:13 -04:00
|
|
|
req.Header.Add("Content-type", "application/json")
|
2013-03-22 13:42:13 -04:00
|
|
|
res, err := rt.RoundTrip(req)
|
2013-03-26 07:11:13 -04:00
|
|
|
if err != nil || res.StatusCode != 404 {
|
|
|
|
errBody, err := ioutil.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
errBody = []byte(err.Error())
|
|
|
|
}
|
|
|
|
Debugf("Lookup status code: %d (body: %s)", res.StatusCode, errBody)
|
2013-03-22 13:42:13 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2013-03-25 20:20:26 -04:00
|
|
|
// FIXME: this should really be PushTag
|
2013-03-22 16:21:44 -04:00
|
|
|
func (graph *Graph) pushPrimitive(stdout io.Writer, remote, tag, imgId string, authConfig *auth.AuthConfig) error {
|
2013-03-25 18:29:00 -04:00
|
|
|
// Check if the local impage exists
|
2013-03-22 13:42:13 -04:00
|
|
|
img, err := graph.Get(imgId)
|
|
|
|
if err != nil {
|
2013-04-10 22:13:15 -04:00
|
|
|
fmt.Fprintf(stdout, "Skipping tag %s:%s: %s does not exist\r\n", remote, tag, imgId)
|
2013-03-25 18:29:00 -04:00
|
|
|
return nil
|
2013-03-22 13:42:13 -04:00
|
|
|
}
|
2013-04-10 22:13:15 -04:00
|
|
|
fmt.Fprintf(stdout, "Pushing tag %s:%s\r\n", remote, tag)
|
2013-03-22 13:42:13 -04:00
|
|
|
// Push the image
|
2013-03-22 16:21:44 -04:00
|
|
|
if err = graph.PushImage(stdout, img, authConfig); err != nil {
|
2013-03-22 13:42:13 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-04-10 22:13:15 -04:00
|
|
|
fmt.Fprintf(stdout, "Registering tag %s:%s\r\n", remote, tag)
|
2013-03-22 13:42:13 -04:00
|
|
|
// And then the tag
|
|
|
|
if err = graph.pushTag(remote, imgId, tag, authConfig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-03-22 09:38:54 -04:00
|
|
|
// Push a repository to the registry.
|
|
|
|
// Remote has the format '<user>/<repo>
|
2013-03-22 16:21:44 -04:00
|
|
|
func (graph *Graph) PushRepository(stdout io.Writer, remote string, localRepo Repository, authConfig *auth.AuthConfig) error {
|
2013-03-26 00:01:42 -04:00
|
|
|
// Check if the remote repository exists/if we have the permission
|
|
|
|
if !graph.LookupRemoteRepository(remote, authConfig) {
|
|
|
|
return fmt.Errorf("Permission denied on repository %s\n", remote)
|
|
|
|
}
|
2013-03-22 13:42:13 -04:00
|
|
|
|
2013-04-10 22:13:15 -04:00
|
|
|
fmt.Fprintf(stdout, "Pushing repository %s (%d tags)\r\n", remote, len(localRepo))
|
2013-03-22 13:42:13 -04:00
|
|
|
// For each image within the repo, push them
|
2013-03-22 09:38:54 -04:00
|
|
|
for tag, imgId := range localRepo {
|
2013-03-22 16:21:44 -04:00
|
|
|
if err := graph.pushPrimitive(stdout, remote, tag, imgId, authConfig); err != nil {
|
2013-03-22 13:42:13 -04:00
|
|
|
// FIXME: Continue on error?
|
2013-03-22 04:25:27 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|