コード例 #1
0
ファイル: repository2.go プロジェクト: joshix/acbuild
func (rb *RepositoryBackend) getLayerV2(layerID string, dockerURL *types.ParsedDockerURL, tmpDir string, copier *progressutil.CopyProgressPrinter) (*os.File, io.ReadCloser, error) {
	url := rb.schema + path.Join(dockerURL.IndexURL, "v2", dockerURL.ImageName, "blobs", layerID)
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return nil, nil, err
	}

	rb.setBasicAuth(req)

	res, err := rb.makeRequest(req, dockerURL.ImageName)
	if err != nil {
		return nil, nil, err
	}

	if res.StatusCode == http.StatusTemporaryRedirect || res.StatusCode == http.StatusFound {
		location := res.Header.Get("Location")
		if location != "" {
			req, err = http.NewRequest("GET", location, nil)
			if err != nil {
				return nil, nil, err
			}
			res, err = rb.makeRequest(req, dockerURL.ImageName)
			if err != nil {
				return nil, nil, err
			}
			defer res.Body.Close()
		}
	}

	if res.StatusCode != http.StatusOK {
		return nil, nil, fmt.Errorf("HTTP code: %d. URL: %s", res.StatusCode, req.URL)
	}

	var in io.Reader
	in = res.Body

	var size int64

	if hdr := res.Header.Get("Content-Length"); hdr != "" {
		size, err = strconv.ParseInt(hdr, 10, 64)
		if err != nil {
			return nil, nil, err
		}
	}

	name := "Downloading " + layerID[:18]

	layerFile, err := ioutil.TempFile(tmpDir, "dockerlayer-")
	if err != nil {
		return nil, nil, err
	}

	err = copier.AddCopy(in, name, size, layerFile)
	if err != nil {
		return nil, nil, err
	}

	return layerFile, res.Body, nil
}
func blockDumpAction(cmd *cobra.Command, args []string) {
	if len(args) != 2 {
		cmd.Usage()
		os.Exit(1)
	}
	output, err := getWriterFromArg(args[1])
	if err != nil {
		die("couldn't open output: %v", err)
	}
	srv := createServer()
	defer srv.Close()
	blockvol, err := block.OpenBlockVolume(srv, args[0])
	if err != nil {
		die("couldn't open block volume %s: %v", args[0], err)
	}
	tempsnap := fmt.Sprintf("temp-dump-%d", os.Getpid())
	err = blockvol.SaveSnapshot(tempsnap)
	if err != nil {
		die("couldn't snapshot: %v", err)
	}
	bf, err := blockvol.OpenSnapshot(tempsnap)
	if err != nil {
		die("couldn't open snapshot: %v", err)
	}

	size := int64(bf.Size())
	if progress {
		pb := new(progressutil.CopyProgressPrinter)
		pb.AddCopy(bf, path.Base(args[0]), size, output)
		err := pb.PrintAndWait(os.Stdout, 500*time.Millisecond, nil)
		if err != nil {
			die("couldn't copy: %v", err)
		}
	} else {
		n, err := io.Copy(output, bf)
		if err != nil {
			die("couldn't copy: %v", err)
		}

		if n != size {
			die("short read of %q", args[0])
		}
	}

	err = blockvol.DeleteSnapshot(tempsnap)
	if err != nil {
		die("couldn't delete snapshot: %v", err)
	}
	fmt.Printf("copied %d bytes\n", size)
}
コード例 #3
0
ファイル: repository2.go プロジェクト: intelsdi-x/rkt
func (rb *RepositoryBackend) getLayerV2(layerID string, dockerURL *common.ParsedDockerURL, tmpDir string, copier *progressutil.CopyProgressPrinter) (*os.File, io.ReadCloser, error) {
	var (
		err error
		res *http.Response
		url = rb.schema + path.Join(dockerURL.IndexURL, "v2", dockerURL.ImageName, "blobs", layerID)
	)
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return nil, nil, err
	}

	rb.setBasicAuth(req)

	accepting := []string{
		typesV2.MediaTypeDockerV22RootFS,
		typesV2.MediaTypeOCILayer,
	}

	res, err = rb.makeRequest(req, dockerURL.ImageName, accepting)
	if err != nil {
		return nil, nil, err
	}

	defer func() {
		if err != nil && res != nil {
			res.Body.Close()
		}
	}()

	if res.StatusCode == http.StatusTemporaryRedirect || res.StatusCode == http.StatusFound {
		location := res.Header.Get("Location")
		if location != "" {
			req, err = http.NewRequest("GET", location, nil)
			if err != nil {
				return nil, nil, err
			}
			res.Body.Close()
			res = nil
			res, err = rb.makeRequest(req, dockerURL.ImageName, accepting)
			if err != nil {
				return nil, nil, err
			}
		}
	}

	if res.StatusCode != http.StatusOK {
		return nil, nil, &httpStatusErr{res.StatusCode, req.URL}
	}

	var in io.Reader
	in = res.Body

	var size int64

	if hdr := res.Header.Get("Content-Length"); hdr != "" {
		size, err = strconv.ParseInt(hdr, 10, 64)
		if err != nil {
			return nil, nil, err
		}
	}

	name := "Downloading " + layerID[:18]

	layerFile, err := ioutil.TempFile(tmpDir, "dockerlayer-")
	if err != nil {
		return nil, nil, err
	}

	err = copier.AddCopy(in, name, size, layerFile)
	if err != nil {
		return nil, nil, err
	}

	return layerFile, res.Body, nil
}
func blockLoadAction(cmd *cobra.Command, args []string) {
	if len(args) != 2 && len(args) != 3 {
		cmd.Usage()
		os.Exit(1)
	}
	input, err := getReaderFromArg(args[0])
	if err != nil {
		die("couldn't open input: %v", err)
	}
	srv := createServer()
	defer srv.Close()
	fi, err := input.Stat()
	if err != nil {
		die("couldn't stat input file: %v", err)
	}

	size := uint64(fi.Size())
	if len(args) == 3 {
		expSize, err := humanize.ParseBytes(args[2])
		if err != nil {
			die("error parsing size %s: %v", args[2], err)
		}

		if expSize < size {
			die("size must be larger than input file")
		}

		size = expSize
	}

	err = block.CreateBlockVolume(srv.MDS, args[1], size)
	if err != nil {
		die("couldn't create block volume %s: %v", args[1], err)
	}
	blockvol, err := block.OpenBlockVolume(srv, args[1])
	if err != nil {
		die("couldn't open block volume %s: %v", args[1], err)
	}
	f, err := blockvol.OpenBlockFile()
	if err != nil {
		die("couldn't open blockfile %s: %v", args[1], err)
	}

	if progress {
		pb := new(progressutil.CopyProgressPrinter)
		pb.AddCopy(input, path.Base(args[0]), fi.Size(), f)
		err := pb.PrintAndWait(os.Stdout, 500*time.Millisecond, nil)
		if err != nil {
			die("couldn't copy: %v", err)
		}
	} else {
		_, err := io.Copy(f, input)
		if err != nil {
			die("couldn't copy: %v", err)
		}
	}

	err = f.Sync()
	if err != nil {
		die("couldn't sync: %v", err)
	}
	err = f.Close()
	if err != nil {
		die("couldn't close: %v", err)
	}
	fmt.Printf("copied %d bytes\n", fi.Size())
}