summaryrefslogtreecommitdiff
path: root/migrate
diff options
context:
space:
mode:
Diffstat (limited to 'migrate')
-rw-r--r--migrate/v1/migratev1.go506
-rw-r--r--migrate/v1/migratev1_test.go448
2 files changed, 954 insertions, 0 deletions
diff --git a/migrate/v1/migratev1.go b/migrate/v1/migratev1.go
new file mode 100644
index 0000000..3cb8828
--- /dev/null
+++ b/migrate/v1/migratev1.go
@@ -0,0 +1,506 @@
+package v1
+
+import (
+ "errors"
+ "fmt"
+ "io/ioutil"
+ "os"
+ "path/filepath"
+ "runtime"
+ "strconv"
+ "sync"
+ "time"
+
+ "encoding/json"
+
+ "github.com/Sirupsen/logrus"
+ "github.com/docker/distribution/reference"
+ "github.com/docker/docker/distribution/metadata"
+ "github.com/docker/docker/image"
+ imagev1 "github.com/docker/docker/image/v1"
+ "github.com/docker/docker/layer"
+ "github.com/docker/docker/pkg/ioutils"
+ refstore "github.com/docker/docker/reference"
+ "github.com/opencontainers/go-digest"
+)
+
+type graphIDRegistrar interface {
+ RegisterByGraphID(string, layer.ChainID, layer.DiffID, string, int64) (layer.Layer, error)
+ Release(layer.Layer) ([]layer.Metadata, error)
+}
+
+type graphIDMounter interface {
+ CreateRWLayerByGraphID(string, string, layer.ChainID) error
+}
+
+type checksumCalculator interface {
+ ChecksumForGraphID(id, parent, oldTarDataPath, newTarDataPath string) (diffID layer.DiffID, size int64, err error)
+}
+
+const (
+ graphDirName = "graph"
+ tarDataFileName = "tar-data.json.gz"
+ migrationFileName = ".migration-v1-images.json"
+ migrationTagsFileName = ".migration-v1-tags"
+ migrationDiffIDFileName = ".migration-diffid"
+ migrationSizeFileName = ".migration-size"
+ migrationTarDataFileName = ".migration-tardata"
+ containersDirName = "containers"
+ configFileNameLegacy = "config.json"
+ configFileName = "config.v2.json"
+ repositoriesFilePrefixLegacy = "repositories-"
+)
+
+var (
+ errUnsupported = errors.New("migration is not supported")
+)
+
+// Migrate takes an old graph directory and transforms the metadata into the
+// new format.
+func Migrate(root, driverName string, ls layer.Store, is image.Store, rs refstore.Store, ms metadata.Store) error {
+ graphDir := filepath.Join(root, graphDirName)
+ if _, err := os.Lstat(graphDir); os.IsNotExist(err) {
+ return nil
+ }
+
+ mappings, err := restoreMappings(root)
+ if err != nil {
+ return err
+ }
+
+ if cc, ok := ls.(checksumCalculator); ok {
+ CalculateLayerChecksums(root, cc, mappings)
+ }
+
+ if registrar, ok := ls.(graphIDRegistrar); !ok {
+ return errUnsupported
+ } else if err := migrateImages(root, registrar, is, ms, mappings); err != nil {
+ return err
+ }
+
+ err = saveMappings(root, mappings)
+ if err != nil {
+ return err
+ }
+
+ if mounter, ok := ls.(graphIDMounter); !ok {
+ return errUnsupported
+ } else if err := migrateContainers(root, mounter, is, mappings); err != nil {
+ return err
+ }
+
+ if err := migrateRefs(root, driverName, rs, mappings); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+// CalculateLayerChecksums walks an old graph directory and calculates checksums
+// for each layer. These checksums are later used for migration.
+func CalculateLayerChecksums(root string, ls checksumCalculator, mappings map[string]image.ID) {
+ graphDir := filepath.Join(root, graphDirName)
+ // spawn some extra workers also for maximum performance because the process is bounded by both cpu and io
+ workers := runtime.NumCPU() * 3
+ workQueue := make(chan string, workers)
+
+ wg := sync.WaitGroup{}
+
+ for i := 0; i < workers; i++ {
+ wg.Add(1)
+ go func() {
+ for id := range workQueue {
+ start := time.Now()
+ if err := calculateLayerChecksum(graphDir, id, ls); err != nil {
+ logrus.Errorf("could not calculate checksum for %q, %q", id, err)
+ }
+ elapsed := time.Since(start)
+ logrus.Debugf("layer %s took %.2f seconds", id, elapsed.Seconds())
+ }
+ wg.Done()
+ }()
+ }
+
+ dir, err := ioutil.ReadDir(graphDir)
+ if err != nil {
+ logrus.Errorf("could not read directory %q", graphDir)
+ return
+ }
+ for _, v := range dir {
+ v1ID := v.Name()
+ if err := imagev1.ValidateID(v1ID); err != nil {
+ continue
+ }
+ if _, ok := mappings[v1ID]; ok { // support old migrations without helper files
+ continue
+ }
+ workQueue <- v1ID
+ }
+ close(workQueue)
+ wg.Wait()
+}
+
+func calculateLayerChecksum(graphDir, id string, ls checksumCalculator) error {
+ diffIDFile := filepath.Join(graphDir, id, migrationDiffIDFileName)
+ if _, err := os.Lstat(diffIDFile); err == nil {
+ return nil
+ } else if !os.IsNotExist(err) {
+ return err
+ }
+
+ parent, err := getParent(filepath.Join(graphDir, id))
+ if err != nil {
+ return err
+ }
+
+ diffID, size, err := ls.ChecksumForGraphID(id, parent, filepath.Join(graphDir, id, tarDataFileName), filepath.Join(graphDir, id, migrationTarDataFileName))
+ if err != nil {
+ return err
+ }
+
+ if err := ioutil.WriteFile(filepath.Join(graphDir, id, migrationSizeFileName), []byte(strconv.Itoa(int(size))), 0600); err != nil {
+ return err
+ }
+
+ if err := ioutils.AtomicWriteFile(filepath.Join(graphDir, id, migrationDiffIDFileName), []byte(diffID), 0600); err != nil {
+ return err
+ }
+
+ logrus.Infof("calculated checksum for layer %s: %s", id, diffID)
+ return nil
+}
+
+func restoreMappings(root string) (map[string]image.ID, error) {
+ mappings := make(map[string]image.ID)
+
+ mfile := filepath.Join(root, migrationFileName)
+ f, err := os.Open(mfile)
+ if err != nil && !os.IsNotExist(err) {
+ return nil, err
+ } else if err == nil {
+ err := json.NewDecoder(f).Decode(&mappings)
+ if err != nil {
+ f.Close()
+ return nil, err
+ }
+ f.Close()
+ }
+
+ return mappings, nil
+}
+
+func saveMappings(root string, mappings map[string]image.ID) error {
+ mfile := filepath.Join(root, migrationFileName)
+ f, err := os.OpenFile(mfile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
+ if err != nil {
+ return err
+ }
+ defer f.Close()
+ return json.NewEncoder(f).Encode(mappings)
+}
+
+func migrateImages(root string, ls graphIDRegistrar, is image.Store, ms metadata.Store, mappings map[string]image.ID) error {
+ graphDir := filepath.Join(root, graphDirName)
+
+ dir, err := ioutil.ReadDir(graphDir)
+ if err != nil {
+ return err
+ }
+ for _, v := range dir {
+ v1ID := v.Name()
+ if err := imagev1.ValidateID(v1ID); err != nil {
+ continue
+ }
+ if _, exists := mappings[v1ID]; exists {
+ continue
+ }
+ if err := migrateImage(v1ID, root, ls, is, ms, mappings); err != nil {
+ continue
+ }
+ }
+
+ return nil
+}
+
+func migrateContainers(root string, ls graphIDMounter, is image.Store, imageMappings map[string]image.ID) error {
+ containersDir := filepath.Join(root, containersDirName)
+ dir, err := ioutil.ReadDir(containersDir)
+ if err != nil {
+ return err
+ }
+ for _, v := range dir {
+ id := v.Name()
+
+ if _, err := os.Stat(filepath.Join(containersDir, id, configFileName)); err == nil {
+ continue
+ }
+
+ containerJSON, err := ioutil.ReadFile(filepath.Join(containersDir, id, configFileNameLegacy))
+ if err != nil {
+ logrus.Errorf("migrate container error: %v", err)
+ continue
+ }
+
+ var c map[string]*json.RawMessage
+ if err := json.Unmarshal(containerJSON, &c); err != nil {
+ logrus.Errorf("migrate container error: %v", err)
+ continue
+ }
+
+ imageStrJSON, ok := c["Image"]
+ if !ok {
+ return fmt.Errorf("invalid container configuration for %v", id)
+ }
+
+ var image string
+ if err := json.Unmarshal([]byte(*imageStrJSON), &image); err != nil {
+ logrus.Errorf("migrate container error: %v", err)
+ continue
+ }
+
+ imageID, ok := imageMappings[image]
+ if !ok {
+ logrus.Errorf("image not migrated %v", imageID) // non-fatal error
+ continue
+ }
+
+ c["Image"] = rawJSON(imageID)
+
+ containerJSON, err = json.Marshal(c)
+ if err != nil {
+ return err
+ }
+
+ if err := ioutil.WriteFile(filepath.Join(containersDir, id, configFileName), containerJSON, 0600); err != nil {
+ return err
+ }
+
+ img, err := is.Get(imageID)
+ if err != nil {
+ return err
+ }
+
+ if err := ls.CreateRWLayerByGraphID(id, id, img.RootFS.ChainID()); err != nil {
+ logrus.Errorf("migrate container error: %v", err)
+ continue
+ }
+
+ logrus.Infof("migrated container %s to point to %s", id, imageID)
+
+ }
+ return nil
+}
+
+type refAdder interface {
+ AddTag(ref reference.Named, id digest.Digest, force bool) error
+ AddDigest(ref reference.Canonical, id digest.Digest, force bool) error
+}
+
+func migrateRefs(root, driverName string, rs refAdder, mappings map[string]image.ID) error {
+ migrationFile := filepath.Join(root, migrationTagsFileName)
+ if _, err := os.Lstat(migrationFile); !os.IsNotExist(err) {
+ return err
+ }
+
+ type repositories struct {
+ Repositories map[string]map[string]string
+ }
+
+ var repos repositories
+
+ f, err := os.Open(filepath.Join(root, repositoriesFilePrefixLegacy+driverName))
+ if err != nil {
+ if os.IsNotExist(err) {
+ return nil
+ }
+ return err
+ }
+ defer f.Close()
+ if err := json.NewDecoder(f).Decode(&repos); err != nil {
+ return err
+ }
+
+ for name, repo := range repos.Repositories {
+ for tag, id := range repo {
+ if strongID, exists := mappings[id]; exists {
+ ref, err := reference.ParseNormalizedNamed(name)
+ if err != nil {
+ logrus.Errorf("migrate tags: invalid name %q, %q", name, err)
+ continue
+ }
+ if !reference.IsNameOnly(ref) {
+ logrus.Errorf("migrate tags: invalid name %q, unexpected tag or digest", name)
+ continue
+ }
+ if dgst, err := digest.Parse(tag); err == nil {
+ canonical, err := reference.WithDigest(reference.TrimNamed(ref), dgst)
+ if err != nil {
+ logrus.Errorf("migrate tags: invalid digest %q, %q", dgst, err)
+ continue
+ }
+ if err := rs.AddDigest(canonical, strongID.Digest(), false); err != nil {
+ logrus.Errorf("can't migrate digest %q for %q, err: %q", reference.FamiliarString(ref), strongID, err)
+ }
+ } else {
+ tagRef, err := reference.WithTag(ref, tag)
+ if err != nil {
+ logrus.Errorf("migrate tags: invalid tag %q, %q", tag, err)
+ continue
+ }
+ if err := rs.AddTag(tagRef, strongID.Digest(), false); err != nil {
+ logrus.Errorf("can't migrate tag %q for %q, err: %q", reference.FamiliarString(ref), strongID, err)
+ }
+ }
+ logrus.Infof("migrated tag %s:%s to point to %s", name, tag, strongID)
+ }
+ }
+ }
+
+ mf, err := os.Create(migrationFile)
+ if err != nil {
+ return err
+ }
+ mf.Close()
+
+ return nil
+}
+
+func getParent(confDir string) (string, error) {
+ jsonFile := filepath.Join(confDir, "json")
+ imageJSON, err := ioutil.ReadFile(jsonFile)
+ if err != nil {
+ return "", err
+ }
+ var parent struct {
+ Parent string
+ ParentID digest.Digest `json:"parent_id"`
+ }
+ if err := json.Unmarshal(imageJSON, &parent); err != nil {
+ return "", err
+ }
+ if parent.Parent == "" && parent.ParentID != "" { // v1.9
+ parent.Parent = parent.ParentID.Hex()
+ }
+ // compatibilityID for parent
+ parentCompatibilityID, err := ioutil.ReadFile(filepath.Join(confDir, "parent"))
+ if err == nil && len(parentCompatibilityID) > 0 {
+ parent.Parent = string(parentCompatibilityID)
+ }
+ return parent.Parent, nil
+}
+
+func migrateImage(id, root string, ls graphIDRegistrar, is image.Store, ms metadata.Store, mappings map[string]image.ID) (err error) {
+ defer func() {
+ if err != nil {
+ logrus.Errorf("migration failed for %v, err: %v", id, err)
+ }
+ }()
+
+ parent, err := getParent(filepath.Join(root, graphDirName, id))
+ if err != nil {
+ return err
+ }
+
+ var parentID image.ID
+ if parent != "" {
+ var exists bool
+ if parentID, exists = mappings[parent]; !exists {
+ if err := migrateImage(parent, root, ls, is, ms, mappings); err != nil {
+ // todo: fail or allow broken chains?
+ return err
+ }
+ parentID = mappings[parent]
+ }
+ }
+
+ rootFS := image.NewRootFS()
+ var history []image.History
+
+ if parentID != "" {
+ parentImg, err := is.Get(parentID)
+ if err != nil {
+ return err
+ }
+
+ rootFS = parentImg.RootFS
+ history = parentImg.History
+ }
+
+ diffIDData, err := ioutil.ReadFile(filepath.Join(root, graphDirName, id, migrationDiffIDFileName))
+ if err != nil {
+ return err
+ }
+ diffID, err := digest.Parse(string(diffIDData))
+ if err != nil {
+ return err
+ }
+
+ sizeStr, err := ioutil.ReadFile(filepath.Join(root, graphDirName, id, migrationSizeFileName))
+ if err != nil {
+ return err
+ }
+ size, err := strconv.ParseInt(string(sizeStr), 10, 64)
+ if err != nil {
+ return err
+ }
+
+ layer, err := ls.RegisterByGraphID(id, rootFS.ChainID(), layer.DiffID(diffID), filepath.Join(root, graphDirName, id, migrationTarDataFileName), size)
+ if err != nil {
+ return err
+ }
+ logrus.Infof("migrated layer %s to %s", id, layer.DiffID())
+
+ jsonFile := filepath.Join(root, graphDirName, id, "json")
+ imageJSON, err := ioutil.ReadFile(jsonFile)
+ if err != nil {
+ return err
+ }
+
+ h, err := imagev1.HistoryFromConfig(imageJSON, false)
+ if err != nil {
+ return err
+ }
+ history = append(history, h)
+
+ rootFS.Append(layer.DiffID())
+
+ config, err := imagev1.MakeConfigFromV1Config(imageJSON, rootFS, history)
+ if err != nil {
+ return err
+ }
+ strongID, err := is.Create(config)
+ if err != nil {
+ return err
+ }
+ logrus.Infof("migrated image %s to %s", id, strongID)
+
+ if parentID != "" {
+ if err := is.SetParent(strongID, parentID); err != nil {
+ return err
+ }
+ }
+
+ checksum, err := ioutil.ReadFile(filepath.Join(root, graphDirName, id, "checksum"))
+ if err == nil { // best effort
+ dgst, err := digest.Parse(string(checksum))
+ if err == nil {
+ V2MetadataService := metadata.NewV2MetadataService(ms)
+ V2MetadataService.Add(layer.DiffID(), metadata.V2Metadata{Digest: dgst})
+ }
+ }
+ _, err = ls.Release(layer)
+ if err != nil {
+ return err
+ }
+
+ mappings[id] = strongID
+ return
+}
+
+func rawJSON(value interface{}) *json.RawMessage {
+ jsonval, err := json.Marshal(value)
+ if err != nil {
+ return nil
+ }
+ return (*json.RawMessage)(&jsonval)
+}
diff --git a/migrate/v1/migratev1_test.go b/migrate/v1/migratev1_test.go
new file mode 100644
index 0000000..ef4ac27
--- /dev/null
+++ b/migrate/v1/migratev1_test.go
@@ -0,0 +1,448 @@
+package v1
+
+import (
+ "crypto/rand"
+ "encoding/hex"
+ "encoding/json"
+ "fmt"
+ "io"
+ "io/ioutil"
+ "os"
+ "path/filepath"
+ "reflect"
+ "runtime"
+ "testing"
+
+ "github.com/docker/distribution/reference"
+ "github.com/docker/docker/distribution/metadata"
+ "github.com/docker/docker/image"
+ "github.com/docker/docker/layer"
+ "github.com/docker/docker/pkg/ioutils"
+ "github.com/opencontainers/go-digest"
+)
+
+func TestMigrateRefs(t *testing.T) {
+ tmpdir, err := ioutil.TempDir("", "migrate-tags")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer os.RemoveAll(tmpdir)
+
+ ioutil.WriteFile(filepath.Join(tmpdir, "repositories-generic"), []byte(`{"Repositories":{"busybox":{"latest":"b3ca410aa2c115c05969a7b2c8cf8a9fcf62c1340ed6a601c9ee50df337ec108","sha256:16a2a52884c2a9481ed267c2d46483eac7693b813a63132368ab098a71303f8a":"b3ca410aa2c115c05969a7b2c8cf8a9fcf62c1340ed6a601c9ee50df337ec108"},"registry":{"2":"5d165b8e4b203685301c815e95663231691d383fd5e3d3185d1ce3f8dddead3d","latest":"8d5547a9f329b1d3f93198cd661fb5117e5a96b721c5cf9a2c389e7dd4877128"}}}`), 0600)
+
+ ta := &mockTagAdder{}
+ err = migrateRefs(tmpdir, "generic", ta, map[string]image.ID{
+ "5d165b8e4b203685301c815e95663231691d383fd5e3d3185d1ce3f8dddead3d": image.ID("sha256:2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"),
+ "b3ca410aa2c115c05969a7b2c8cf8a9fcf62c1340ed6a601c9ee50df337ec108": image.ID("sha256:fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9"),
+ "abcdef3434c115c05969a7b2c8cf8a9fcf62c1340ed6a601c9ee50df337ec108": image.ID("sha256:56434342345ae68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"),
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ expected := map[string]string{
+ "docker.io/library/busybox:latest": "sha256:fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9",
+ "docker.io/library/busybox@sha256:16a2a52884c2a9481ed267c2d46483eac7693b813a63132368ab098a71303f8a": "sha256:fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9",
+ "docker.io/library/registry:2": "sha256:2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae",
+ }
+
+ if !reflect.DeepEqual(expected, ta.refs) {
+ t.Fatalf("Invalid migrated tags: expected %q, got %q", expected, ta.refs)
+ }
+
+ // second migration is no-op
+ ioutil.WriteFile(filepath.Join(tmpdir, "repositories-generic"), []byte(`{"Repositories":{"busybox":{"latest":"b3ca410aa2c115c05969a7b2c8cf8a9fcf62c1340ed6a601c9ee50df337ec108"`), 0600)
+ err = migrateRefs(tmpdir, "generic", ta, map[string]image.ID{
+ "b3ca410aa2c115c05969a7b2c8cf8a9fcf62c1340ed6a601c9ee50df337ec108": image.ID("sha256:fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9"),
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !reflect.DeepEqual(expected, ta.refs) {
+ t.Fatalf("Invalid migrated tags: expected %q, got %q", expected, ta.refs)
+ }
+}
+
+func TestMigrateContainers(t *testing.T) {
+ // TODO Windows: Figure out why this is failing
+ if runtime.GOOS == "windows" {
+ t.Skip("Failing on Windows")
+ }
+ if runtime.GOARCH != "amd64" {
+ t.Skip("Test tailored to amd64 architecture")
+ }
+ tmpdir, err := ioutil.TempDir("", "migrate-containers")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer os.RemoveAll(tmpdir)
+
+ err = addContainer(tmpdir, `{"State":{"Running":false,"Paused":false,"Restarting":false,"OOMKilled":false,"Dead":false,"Pid":0,"ExitCode":0,"Error":"","StartedAt":"2015-11-10T21:42:40.604267436Z","FinishedAt":"2015-11-10T21:42:41.869265487Z"},"ID":"f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c","Created":"2015-11-10T21:42:40.433831551Z","Path":"sh","Args":[],"Config":{"Hostname":"f780ee3f80e6","Domainname":"","User":"","AttachStdin":true,"AttachStdout":true,"AttachStderr":true,"Tty":true,"OpenStdin":true,"StdinOnce":true,"Env":null,"Cmd":["sh"],"Image":"busybox","Volumes":null,"WorkingDir":"","Entrypoint":null,"OnBuild":null,"Labels":{}},"Image":"2c5ac3f849df8627fcf2822727f87c57f38b7129d3604fbc11d861fe856ff093","NetworkSettings":{"Bridge":"","EndpointID":"","Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"HairpinMode":false,"IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","LinkLocalIPv6Address":"","LinkLocalIPv6PrefixLen":0,"MacAddress":"","NetworkID":"","PortMapping":null,"Ports":null,"SandboxKey":"","SecondaryIPAddresses":null,"SecondaryIPv6Addresses":null},"ResolvConfPath":"/var/lib/docker/containers/f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c/resolv.conf","HostnamePath":"/var/lib/docker/containers/f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c/hostname","HostsPath":"/var/lib/docker/containers/f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c/hosts","LogPath":"/var/lib/docker/containers/f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c/f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c-json.log","Name":"/determined_euclid","Driver":"overlay","ExecDriver":"native-0.2","MountLabel":"","ProcessLabel":"","RestartCount":0,"UpdateDns":false,"HasBeenStartedBefore":false,"MountPoints":{},"Volumes":{},"VolumesRW":{},"AppArmorProfile":""}`)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ // container with invalid image
+ err = addContainer(tmpdir, `{"State":{"Running":false,"Paused":false,"Restarting":false,"OOMKilled":false,"Dead":false,"Pid":0,"ExitCode":0,"Error":"","StartedAt":"2015-11-10T21:42:40.604267436Z","FinishedAt":"2015-11-10T21:42:41.869265487Z"},"ID":"e780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c","Created":"2015-11-10T21:42:40.433831551Z","Path":"sh","Args":[],"Config":{"Hostname":"f780ee3f80e6","Domainname":"","User":"","AttachStdin":true,"AttachStdout":true,"AttachStderr":true,"Tty":true,"OpenStdin":true,"StdinOnce":true,"Env":null,"Cmd":["sh"],"Image":"busybox","Volumes":null,"WorkingDir":"","Entrypoint":null,"OnBuild":null,"Labels":{}},"Image":"4c5ac3f849df8627fcf2822727f87c57f38b7129d3604fbc11d861fe856ff093","NetworkSettings":{"Bridge":"","EndpointID":"","Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"HairpinMode":false,"IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","LinkLocalIPv6Address":"","LinkLocalIPv6PrefixLen":0,"MacAddress":"","NetworkID":"","PortMapping":null,"Ports":null,"SandboxKey":"","SecondaryIPAddresses":null,"SecondaryIPv6Addresses":null},"ResolvConfPath":"/var/lib/docker/containers/f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c/resolv.conf","HostnamePath":"/var/lib/docker/containers/f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c/hostname","HostsPath":"/var/lib/docker/containers/f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c/hosts","LogPath":"/var/lib/docker/containers/f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c/f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c-json.log","Name":"/determined_euclid","Driver":"overlay","ExecDriver":"native-0.2","MountLabel":"","ProcessLabel":"","RestartCount":0,"UpdateDns":false,"HasBeenStartedBefore":false,"MountPoints":{},"Volumes":{},"VolumesRW":{},"AppArmorProfile":""}`)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ ls := &mockMounter{}
+
+ ifs, err := image.NewFSStoreBackend(filepath.Join(tmpdir, "imagedb"))
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ is, err := image.NewImageStore(ifs, runtime.GOOS, ls)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ imgID, err := is.Create([]byte(`{"architecture":"amd64","config":{"AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"Cmd":["sh"],"Entrypoint":null,"Env":null,"Hostname":"23304fc829f9","Image":"d1592a710ac323612bd786fa8ac20727c58d8a67847e5a65177c594f43919498","Labels":null,"OnBuild":null,"OpenStdin":false,"StdinOnce":false,"Tty":false,"Volumes":null,"WorkingDir":"","Domainname":"","User":""},"container":"349b014153779e30093d94f6df2a43c7a0a164e05aa207389917b540add39b51","container_config":{"AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"Cmd":["/bin/sh","-c","#(nop) CMD [\"sh\"]"],"Entrypoint":null,"Env":null,"Hostname":"23304fc829f9","Image":"d1592a710ac323612bd786fa8ac20727c58d8a67847e5a65177c594f43919498","Labels":null,"OnBuild":null,"OpenStdin":false,"StdinOnce":false,"Tty":false,"Volumes":null,"WorkingDir":"","Domainname":"","User":""},"created":"2015-10-31T22:22:55.613815829Z","docker_version":"1.8.2","history":[{"created":"2015-10-31T22:22:54.690851953Z","created_by":"/bin/sh -c #(nop) ADD file:a3bc1e842b69636f9df5256c49c5374fb4eef1e281fe3f282c65fb853ee171c5 in /"},{"created":"2015-10-31T22:22:55.613815829Z","created_by":"/bin/sh -c #(nop) CMD [\"sh\"]"}],"os":"linux","rootfs":{"type":"layers","diff_ids":["sha256:c6f988f4874bb0add23a778f753c65efe992244e148a1d2ec2a8b664fb66bbd1","sha256:5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef"]}}`))
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ err = migrateContainers(tmpdir, ls, is, map[string]image.ID{
+ "2c5ac3f849df8627fcf2822727f87c57f38b7129d3604fbc11d861fe856ff093": imgID,
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ expected := []mountInfo{{
+ "f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c",
+ "f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c",
+ "sha256:c3191d32a37d7159b2e30830937d2e30268ad6c375a773a8994911a3aba9b93f",
+ }}
+ if !reflect.DeepEqual(expected, ls.mounts) {
+ t.Fatalf("invalid mounts: expected %q, got %q", expected, ls.mounts)
+ }
+
+ if actual, expected := ls.count, 0; actual != expected {
+ t.Fatalf("invalid active mounts: expected %d, got %d", expected, actual)
+ }
+
+ config2, err := ioutil.ReadFile(filepath.Join(tmpdir, "containers", "f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c", "config.v2.json"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ var config struct{ Image string }
+ err = json.Unmarshal(config2, &config)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if actual, expected := config.Image, string(imgID); actual != expected {
+ t.Fatalf("invalid image pointer in migrated config: expected %q, got %q", expected, actual)
+ }
+
+}
+
+func TestMigrateImages(t *testing.T) {
+ // TODO Windows: Figure out why this is failing
+ if runtime.GOOS == "windows" {
+ t.Skip("Failing on Windows")
+ }
+ if runtime.GOARCH != "amd64" {
+ t.Skip("Test tailored to amd64 architecture")
+ }
+ tmpdir, err := ioutil.TempDir("", "migrate-images")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer os.RemoveAll(tmpdir)
+
+ // busybox from 1.9
+ id1, err := addImage(tmpdir, `{"architecture":"amd64","config":{"Hostname":"23304fc829f9","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":null,"Cmd":null,"Image":"","Volumes":null,"WorkingDir":"","Entrypoint":null,"OnBuild":null,"Labels":null},"container":"23304fc829f9b9349416f6eb1afec162907eba3a328f51d53a17f8986f865d65","container_config":{"Hostname":"23304fc829f9","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":null,"Cmd":["/bin/sh","-c","#(nop) ADD file:a3bc1e842b69636f9df5256c49c5374fb4eef1e281fe3f282c65fb853ee171c5 in /"],"Image":"","Volumes":null,"WorkingDir":"","Entrypoint":null,"OnBuild":null,"Labels":null},"created":"2015-10-31T22:22:54.690851953Z","docker_version":"1.8.2","layer_id":"sha256:55dc925c23d1ed82551fd018c27ac3ee731377b6bad3963a2a4e76e753d70e57","os":"linux"}`, "", "")
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ id2, err := addImage(tmpdir, `{"architecture":"amd64","config":{"Hostname":"23304fc829f9","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":null,"Cmd":["sh"],"Image":"d1592a710ac323612bd786fa8ac20727c58d8a67847e5a65177c594f43919498","Volumes":null,"WorkingDir":"","Entrypoint":null,"OnBuild":null,"Labels":null},"container":"349b014153779e30093d94f6df2a43c7a0a164e05aa207389917b540add39b51","container_config":{"Hostname":"23304fc829f9","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":null,"Cmd":["/bin/sh","-c","#(nop) CMD [\"sh\"]"],"Image":"d1592a710ac323612bd786fa8ac20727c58d8a67847e5a65177c594f43919498","Volumes":null,"WorkingDir":"","Entrypoint":null,"OnBuild":null,"Labels":null},"created":"2015-10-31T22:22:55.613815829Z","docker_version":"1.8.2","layer_id":"sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4","os":"linux","parent_id":"sha256:039b63dd2cbaa10d6015ea574392530571ed8d7b174090f032211285a71881d0"}`, id1, "")
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ ls := &mockRegistrar{}
+
+ ifs, err := image.NewFSStoreBackend(filepath.Join(tmpdir, "imagedb"))
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ is, err := image.NewImageStore(ifs, runtime.GOOS, ls)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ ms, err := metadata.NewFSMetadataStore(filepath.Join(tmpdir, "distribution"), runtime.GOOS)
+ if err != nil {
+ t.Fatal(err)
+ }
+ mappings := make(map[string]image.ID)
+
+ err = migrateImages(tmpdir, ls, is, ms, mappings)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ expected := map[string]image.ID{
+ id1: image.ID("sha256:ca406eaf9c26898414ff5b7b3a023c33310759d6203be0663dbf1b3a712f432d"),
+ id2: image.ID("sha256:a488bec94bb96b26a968f913d25ef7d8d204d727ca328b52b4b059c7d03260b6"),
+ }
+
+ if !reflect.DeepEqual(mappings, expected) {
+ t.Fatalf("invalid image mappings: expected %q, got %q", expected, mappings)
+ }
+
+ if actual, expected := ls.count, 2; actual != expected {
+ t.Fatalf("invalid register count: expected %q, got %q", expected, actual)
+ }
+ ls.count = 0
+
+ // next images are busybox from 1.8.2
+ _, err = addImage(tmpdir, `{"id":"17583c7dd0dae6244203b8029733bdb7d17fccbb2b5d93e2b24cf48b8bfd06e2","parent":"d1592a710ac323612bd786fa8ac20727c58d8a67847e5a65177c594f43919498","created":"2015-10-31T22:22:55.613815829Z","container":"349b014153779e30093d94f6df2a43c7a0a164e05aa207389917b540add39b51","container_config":{"Hostname":"23304fc829f9","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"ExposedPorts":null,"PublishService":"","Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":null,"Cmd":["/bin/sh","-c","#(nop) CMD [\"sh\"]"],"Image":"d1592a710ac323612bd786fa8ac20727c58d8a67847e5a65177c594f43919498","Volumes":null,"VolumeDriver":"","WorkingDir":"","Entrypoint":null,"NetworkDisabled":false,"MacAddress":"","OnBuild":null,"Labels":null},"docker_version":"1.8.2","config":{"Hostname":"23304fc829f9","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"ExposedPorts":null,"PublishService":"","Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":null,"Cmd":["sh"],"Image":"d1592a710ac323612bd786fa8ac20727c58d8a67847e5a65177c594f43919498","Volumes":null,"VolumeDriver":"","WorkingDir":"","Entrypoint":null,"NetworkDisabled":false,"MacAddress":"","OnBuild":null,"Labels":null},"architecture":"amd64","os":"linux","Size":0}`, "", "sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4")
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ _, err = addImage(tmpdir, `{"id":"d1592a710ac323612bd786fa8ac20727c58d8a67847e5a65177c594f43919498","created":"2015-10-31T22:22:54.690851953Z","container":"23304fc829f9b9349416f6eb1afec162907eba3a328f51d53a17f8986f865d65","container_config":{"Hostname":"23304fc829f9","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"ExposedPorts":null,"PublishService":"","Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":null,"Cmd":["/bin/sh","-c","#(nop) ADD file:a3bc1e842b69636f9df5256c49c5374fb4eef1e281fe3f282c65fb853ee171c5 in /"],"Image":"","Volumes":null,"VolumeDriver":"","WorkingDir":"","Entrypoint":null,"NetworkDisabled":false,"MacAddress":"","OnBuild":null,"Labels":null},"docker_version":"1.8.2","config":{"Hostname":"23304fc829f9","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"ExposedPorts":null,"PublishService":"","Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":null,"Cmd":null,"Image":"","Volumes":null,"VolumeDriver":"","WorkingDir":"","Entrypoint":null,"NetworkDisabled":false,"MacAddress":"","OnBuild":null,"Labels":null},"architecture":"amd64","os":"linux","Size":1108935}`, "", "sha256:55dc925c23d1ed82551fd018c27ac3ee731377b6bad3963a2a4e76e753d70e57")
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ err = migrateImages(tmpdir, ls, is, ms, mappings)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ expected["d1592a710ac323612bd786fa8ac20727c58d8a67847e5a65177c594f43919498"] = image.ID("sha256:c091bb33854e57e6902b74c08719856d30b5593c7db6143b2b48376b8a588395")
+ expected["17583c7dd0dae6244203b8029733bdb7d17fccbb2b5d93e2b24cf48b8bfd06e2"] = image.ID("sha256:d963020e755ff2715b936065949472c1f8a6300144b922992a1a421999e71f07")
+
+ if actual, expected := ls.count, 2; actual != expected {
+ t.Fatalf("invalid register count: expected %q, got %q", expected, actual)
+ }
+
+ v2MetadataService := metadata.NewV2MetadataService(ms)
+ receivedMetadata, err := v2MetadataService.GetMetadata(layer.EmptyLayer.DiffID())
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ expectedMetadata := []metadata.V2Metadata{
+ {Digest: digest.Digest("sha256:55dc925c23d1ed82551fd018c27ac3ee731377b6bad3963a2a4e76e753d70e57")},
+ {Digest: digest.Digest("sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4")},
+ }
+
+ if !reflect.DeepEqual(expectedMetadata, receivedMetadata) {
+ t.Fatalf("invalid metadata: expected %q, got %q", expectedMetadata, receivedMetadata)
+ }
+
+}
+
+func TestMigrateUnsupported(t *testing.T) {
+ tmpdir, err := ioutil.TempDir("", "migrate-empty")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer os.RemoveAll(tmpdir)
+
+ err = os.MkdirAll(filepath.Join(tmpdir, "graph"), 0700)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ err = Migrate(tmpdir, "generic", nil, nil, nil, nil)
+ if err != errUnsupported {
+ t.Fatalf("expected unsupported error, got %q", err)
+ }
+}
+
+func TestMigrateEmptyDir(t *testing.T) {
+ tmpdir, err := ioutil.TempDir("", "migrate-empty")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer os.RemoveAll(tmpdir)
+
+ err = Migrate(tmpdir, "generic", nil, nil, nil, nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+}
+
+func addImage(dest, jsonConfig, parent, checksum string) (string, error) {
+ var config struct{ ID string }
+ if err := json.Unmarshal([]byte(jsonConfig), &config); err != nil {
+ return "", err
+ }
+ if config.ID == "" {
+ b := make([]byte, 32)
+ rand.Read(b)
+ config.ID = hex.EncodeToString(b)
+ }
+ contDir := filepath.Join(dest, "graph", config.ID)
+ if err := os.MkdirAll(contDir, 0700); err != nil {
+ return "", err
+ }
+ if err := ioutil.WriteFile(filepath.Join(contDir, "json"), []byte(jsonConfig), 0600); err != nil {
+ return "", err
+ }
+ if checksum != "" {
+ if err := ioutil.WriteFile(filepath.Join(contDir, "checksum"), []byte(checksum), 0600); err != nil {
+ return "", err
+ }
+ }
+ if err := ioutil.WriteFile(filepath.Join(contDir, ".migration-diffid"), []byte(layer.EmptyLayer.DiffID()), 0600); err != nil {
+ return "", err
+ }
+ if err := ioutil.WriteFile(filepath.Join(contDir, ".migration-size"), []byte("0"), 0600); err != nil {
+ return "", err
+ }
+ if parent != "" {
+ if err := ioutil.WriteFile(filepath.Join(contDir, "parent"), []byte(parent), 0600); err != nil {
+ return "", err
+ }
+ }
+ if checksum != "" {
+ if err := ioutil.WriteFile(filepath.Join(contDir, "checksum"), []byte(checksum), 0600); err != nil {
+ return "", err
+ }
+ }
+ return config.ID, nil
+}
+
+func addContainer(dest, jsonConfig string) error {
+ var config struct{ ID string }
+ if err := json.Unmarshal([]byte(jsonConfig), &config); err != nil {
+ return err
+ }
+ contDir := filepath.Join(dest, "containers", config.ID)
+ if err := os.MkdirAll(contDir, 0700); err != nil {
+ return err
+ }
+ if err := ioutil.WriteFile(filepath.Join(contDir, "config.json"), []byte(jsonConfig), 0600); err != nil {
+ return err
+ }
+ return nil
+}
+
+type mockTagAdder struct {
+ refs map[string]string
+}
+
+func (t *mockTagAdder) AddTag(ref reference.Named, id digest.Digest, force bool) error {
+ if t.refs == nil {
+ t.refs = make(map[string]string)
+ }
+ t.refs[ref.String()] = id.String()
+ return nil
+}
+func (t *mockTagAdder) AddDigest(ref reference.Canonical, id digest.Digest, force bool) error {
+ return t.AddTag(ref, id, force)
+}
+
+type mockRegistrar struct {
+ layers map[layer.ChainID]*mockLayer
+ count int
+}
+
+func (r *mockRegistrar) RegisterByGraphID(graphID string, parent layer.ChainID, diffID layer.DiffID, tarDataFile string, size int64) (layer.Layer, error) {
+ r.count++
+ l := &mockLayer{}
+ if parent != "" {
+ p, exists := r.layers[parent]
+ if !exists {
+ return nil, fmt.Errorf("invalid parent %q", parent)
+ }
+ l.parent = p
+ l.diffIDs = append(l.diffIDs, p.diffIDs...)
+ }
+ l.diffIDs = append(l.diffIDs, diffID)
+ if r.layers == nil {
+ r.layers = make(map[layer.ChainID]*mockLayer)
+ }
+ r.layers[l.ChainID()] = l
+ return l, nil
+}
+func (r *mockRegistrar) Release(l layer.Layer) ([]layer.Metadata, error) {
+ return nil, nil
+}
+func (r *mockRegistrar) Get(layer.ChainID) (layer.Layer, error) {
+ return nil, nil
+}
+
+type mountInfo struct {
+ name, graphID, parent string
+}
+type mockMounter struct {
+ mounts []mountInfo
+ count int
+}
+
+func (r *mockMounter) CreateRWLayerByGraphID(name string, graphID string, parent layer.ChainID) error {
+ r.mounts = append(r.mounts, mountInfo{name, graphID, string(parent)})
+ return nil
+}
+func (r *mockMounter) Unmount(string) error {
+ r.count--
+ return nil
+}
+func (r *mockMounter) Get(layer.ChainID) (layer.Layer, error) {
+ return nil, nil
+}
+
+func (r *mockMounter) Release(layer.Layer) ([]layer.Metadata, error) {
+ return nil, nil
+}
+
+type mockLayer struct {
+ diffIDs []layer.DiffID
+ parent *mockLayer
+}
+
+func (l *mockLayer) TarStream() (io.ReadCloser, error) {
+ return nil, nil
+}
+
+func (ml *mockLayer) TarSeekStream() (ioutils.ReadSeekCloser, error) {
+ return nil, fmt.Errorf("not implemented")
+}
+
+func (l *mockLayer) TarStreamFrom(layer.ChainID) (io.ReadCloser, error) {
+ return nil, nil
+}
+
+func (l *mockLayer) ChainID() layer.ChainID {
+ return layer.CreateChainID(l.diffIDs)
+}
+
+func (l *mockLayer) DiffID() layer.DiffID {
+ return l.diffIDs[len(l.diffIDs)-1]
+}
+
+func (l *mockLayer) Parent() layer.Layer {
+ if l.parent == nil {
+ return nil
+ }
+ return l.parent
+}
+
+func (l *mockLayer) Size() (int64, error) {
+ return 0, nil
+}
+
+func (l *mockLayer) DiffSize() (int64, error) {
+ return 0, nil
+}
+
+func (l *mockLayer) Platform() layer.Platform {
+ return ""
+}
+
+func (l *mockLayer) Metadata() (map[string]string, error) {
+ return nil, nil
+}