1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
package test
import (
"archive/tar"
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"io/ioutil"
"os"
"path"
"github.com/appc/docker2aci/lib/common"
"github.com/appc/docker2aci/lib/internal/typesV2"
)
type Layer map[*tar.Header][]byte
type Docker22Image struct {
RepoTags []string
Layers []Layer
Config typesV2.ImageConfig
}
func GenerateDocker22(destPath string, img Docker22Image) error {
layerHashes, err := GenLayers(destPath, img.Layers)
if err != nil {
return err
}
configHash, err := GenDocker22Config(destPath, img.Config, layerHashes)
if err != nil {
return err
}
err = GenDocker22Manifest(destPath, configHash, layerHashes)
if err != nil {
return err
}
return nil
}
func GenLayers(destPath string, layers []Layer) ([]string, error) {
var layerHashes []string
for _, l := range layers {
layerBuffer := &bytes.Buffer{}
tw := tar.NewWriter(layerBuffer)
for hdr, contents := range l {
hdr.Size = int64(len(contents))
err := tw.WriteHeader(hdr)
if err != nil {
tw.Close()
return nil, err
}
_, err = tw.Write(contents)
if err != nil {
tw.Close()
return nil, err
}
}
tw.Close()
layerTarBlob := layerBuffer.Bytes()
h := sha256.New()
h.Write(layerTarBlob)
hashStr := hex.EncodeToString(h.Sum(nil))
layerHashes = append(layerHashes, hashStr)
err := ioutil.WriteFile(path.Join(destPath, hashStr), layerTarBlob, 0644)
if err != nil {
return nil, err
}
}
return layerHashes, nil
}
func GenDocker22Config(destPath string, conf typesV2.ImageConfig, layerHashes []string) (string, error) {
conf.RootFS = &typesV2.ImageConfigRootFS{}
conf.RootFS.Type = "layers"
for _, h := range layerHashes {
conf.RootFS.DiffIDs = append(conf.RootFS.DiffIDs, "sha256:"+h)
}
confblob, err := json.Marshal(conf)
if err != nil {
return "", err
}
h := sha256.New()
h.Write(confblob)
hashStr := hex.EncodeToString(h.Sum(nil))
err = ioutil.WriteFile(path.Join(destPath, hashStr), confblob, 0644)
if err != nil {
return "", err
}
return hashStr, nil
}
func GenDocker22Manifest(destPath, configHash string, layerHashes []string) error {
getDigestSize := func(digest string) (int64, error) {
fi, err := os.Stat(path.Join(destPath, digest))
if err != nil {
return 0, err
}
return fi.Size(), nil
}
configSize, err := getDigestSize(configHash)
if err != nil {
return err
}
manifest := &typesV2.ImageManifest{
SchemaVersion: 2,
MediaType: common.MediaTypeDockerV22Manifest,
Config: &typesV2.ImageManifestDigest{
MediaType: common.MediaTypeDockerV22Config,
Size: int(configSize),
Digest: "sha256:" + configHash,
},
}
for _, h := range layerHashes {
layerSize, err := getDigestSize(h)
if err != nil {
return err
}
manifest.Layers = append(manifest.Layers,
&typesV2.ImageManifestDigest{
MediaType: common.MediaTypeDockerV22RootFS,
Size: int(layerSize),
Digest: "sha256:" + h,
})
}
manblob, err := json.Marshal(manifest)
if err != nil {
return err
}
err = ioutil.WriteFile(path.Join(destPath, "manifest.json"), manblob, 0644)
if err != nil {
return err
}
return nil
}
|