瀏覽代碼

Merge pull request #28996 from Microsoft/jjh/sqlite-remove-windows

Windows: Factor out sqlite
Justin Cormack 8 年之前
父節點
當前提交
a756c1ac65

+ 5 - 14
daemon/daemon.go

@@ -40,7 +40,6 @@ import (
 	"github.com/docker/docker/libcontainerd"
 	"github.com/docker/docker/migrate/v1"
 	"github.com/docker/docker/pkg/fileutils"
-	"github.com/docker/docker/pkg/graphdb"
 	"github.com/docker/docker/pkg/idtools"
 	"github.com/docker/docker/pkg/plugingetter"
 	"github.com/docker/docker/pkg/progress"
@@ -158,7 +157,6 @@ func (daemon *Daemon) restore() error {
 		}
 	}
 
-	var migrateLegacyLinks bool
 	removeContainers := make(map[string]*container.Container)
 	restartContainers := make(map[*container.Container]chan struct{})
 	activeSandboxes := make(map[string]interface{})
@@ -190,6 +188,8 @@ func (daemon *Daemon) restore() error {
 			}
 		}
 	}
+
+	var migrateLegacyLinks bool // Not relevant on Windows
 	var wg sync.WaitGroup
 	var mapLock sync.Mutex
 	for _, c := range containers {
@@ -265,24 +265,15 @@ func (daemon *Daemon) restore() error {
 		return fmt.Errorf("Error initializing network controller: %v", err)
 	}
 
-	// migrate any legacy links from sqlite
-	linkdbFile := filepath.Join(daemon.root, "linkgraph.db")
-	var legacyLinkDB *graphdb.Database
+	// Perform migration of legacy sqlite links (no-op on Windows)
 	if migrateLegacyLinks {
-		legacyLinkDB, err = graphdb.NewSqliteConn(linkdbFile)
-		if err != nil {
-			return fmt.Errorf("error connecting to legacy link graph DB %s, container links may be lost: %v", linkdbFile, err)
+		if err := daemon.sqliteMigration(containers); err != nil {
+			return err
 		}
-		defer legacyLinkDB.Close()
 	}
 
 	// Now that all the containers are registered, register the links
 	for _, c := range containers {
-		if migrateLegacyLinks {
-			if err := daemon.migrateLegacySqliteLinks(legacyLinkDB, c); err != nil {
-				return err
-			}
-		}
 		if err := daemon.registerLinks(c, c.HostConfig); err != nil {
 			logrus.Errorf("failed to register link for container %s: %v", c.ID, err)
 		}

+ 0 - 41
daemon/links.go

@@ -1,12 +1,9 @@
 package daemon
 
 import (
-	"strings"
 	"sync"
 
-	"github.com/Sirupsen/logrus"
 	"github.com/docker/docker/container"
-	"github.com/docker/docker/pkg/graphdb"
 )
 
 // linkIndex stores link relationships between containers, including their specified alias
@@ -88,41 +85,3 @@ func (l *linkIndex) delete(container *container.Container) {
 	delete(l.childIdx, container)
 	l.mu.Unlock()
 }
-
-// migrateLegacySqliteLinks migrates sqlite links to use links from HostConfig
-// when sqlite links were used, hostConfig.Links was set to nil
-func (daemon *Daemon) migrateLegacySqliteLinks(db *graphdb.Database, container *container.Container) error {
-	// if links is populated (or an empty slice), then this isn't using sqlite links and can be skipped
-	if container.HostConfig == nil || container.HostConfig.Links != nil {
-		return nil
-	}
-
-	logrus.Debugf("migrating legacy sqlite link info for container: %s", container.ID)
-
-	fullName := container.Name
-	if fullName[0] != '/' {
-		fullName = "/" + fullName
-	}
-
-	// don't use a nil slice, this ensures that the check above will skip once the migration has completed
-	links := []string{}
-	children, err := db.Children(fullName, 0)
-	if err != nil {
-		if !strings.Contains(err.Error(), "Cannot find child for") {
-			return err
-		}
-		// else continue... it's ok if we didn't find any children, it'll just be nil and we can continue the migration
-	}
-
-	for _, child := range children {
-		c, err := daemon.GetContainer(child.Entity.ID())
-		if err != nil {
-			return err
-		}
-
-		links = append(links, c.Name+":"+child.Edge.Name)
-	}
-
-	container.HostConfig.Links = links
-	return container.WriteHostConfig()
-}

+ 72 - 0
daemon/links_linux.go

@@ -0,0 +1,72 @@
+package daemon
+
+import (
+	"fmt"
+	"path/filepath"
+	"strings"
+
+	"github.com/Sirupsen/logrus"
+	"github.com/docker/docker/container"
+	"github.com/docker/docker/pkg/graphdb"
+)
+
+// migrateLegacySqliteLinks migrates sqlite links to use links from HostConfig
+// when sqlite links were used, hostConfig.Links was set to nil
+func (daemon *Daemon) migrateLegacySqliteLinks(db *graphdb.Database, container *container.Container) error {
+	// if links is populated (or an empty slice), then this isn't using sqlite links and can be skipped
+	if container.HostConfig == nil || container.HostConfig.Links != nil {
+		return nil
+	}
+
+	logrus.Debugf("migrating legacy sqlite link info for container: %s", container.ID)
+
+	fullName := container.Name
+	if fullName[0] != '/' {
+		fullName = "/" + fullName
+	}
+
+	// don't use a nil slice, this ensures that the check above will skip once the migration has completed
+	links := []string{}
+	children, err := db.Children(fullName, 0)
+	if err != nil {
+		if !strings.Contains(err.Error(), "Cannot find child for") {
+			return err
+		}
+		// else continue... it's ok if we didn't find any children, it'll just be nil and we can continue the migration
+	}
+
+	for _, child := range children {
+		c, err := daemon.GetContainer(child.Entity.ID())
+		if err != nil {
+			return err
+		}
+
+		links = append(links, c.Name+":"+child.Edge.Name)
+	}
+
+	container.HostConfig.Links = links
+	return container.WriteHostConfig()
+}
+
+// sqliteMigration performs the link graph DB migration.
+func (daemon *Daemon) sqliteMigration(containers map[string]*container.Container) error {
+	// migrate any legacy links from sqlite
+	linkdbFile := filepath.Join(daemon.root, "linkgraph.db")
+	var (
+		legacyLinkDB *graphdb.Database
+		err          error
+	)
+
+	legacyLinkDB, err = graphdb.NewSqliteConn(linkdbFile)
+	if err != nil {
+		return fmt.Errorf("error connecting to legacy link graph DB %s, container links may be lost: %v", linkdbFile, err)
+	}
+	defer legacyLinkDB.Close()
+
+	for _, c := range containers {
+		if err := daemon.migrateLegacySqliteLinks(legacyLinkDB, c); err != nil {
+			return err
+		}
+	}
+	return nil
+}

+ 0 - 0
daemon/links_test.go → daemon/links_linux_test.go


+ 10 - 0
daemon/links_notlinux.go

@@ -0,0 +1,10 @@
+// +build !linux
+
+package daemon
+
+import "github.com/docker/docker/container"
+
+// sqliteMigration performs the link graph DB migration. No-op on platforms other than Linux
+func (daemon *Daemon) sqliteMigration(_ map[string]*container.Container) error {
+	return nil
+}

+ 5 - 1
pkg/graphdb/conn_sqlite3.go → pkg/graphdb/conn_sqlite3_linux.go

@@ -2,7 +2,11 @@
 
 package graphdb
 
-import "database/sql"
+import (
+	"database/sql"
+
+	_ "github.com/mattn/go-sqlite3" // registers sqlite
+)
 
 // NewSqliteConn opens a connection to a sqlite
 // database.

+ 0 - 7
pkg/graphdb/conn_sqlite3_unix.go

@@ -1,7 +0,0 @@
-// +build cgo,!windows
-
-package graphdb
-
-import (
-	_ "github.com/mattn/go-sqlite3" // registers sqlite
-)

+ 0 - 7
pkg/graphdb/conn_sqlite3_windows.go

@@ -1,7 +0,0 @@
-// +build cgo,windows
-
-package graphdb
-
-import (
-	_ "github.com/mattn/go-sqlite3" // registers sqlite
-)

+ 0 - 8
pkg/graphdb/conn_unsupported.go

@@ -1,8 +0,0 @@
-// +build !cgo
-
-package graphdb
-
-// NewSqliteConn return a new sqlite connection.
-func NewSqliteConn(root string) (*Database, error) {
-	panic("Not implemented")
-}

+ 0 - 0
pkg/graphdb/graphdb.go → pkg/graphdb/graphdb_linux.go


+ 0 - 0
pkg/graphdb/graphdb_test.go → pkg/graphdb/graphdb_linux_test.go


+ 0 - 0
pkg/graphdb/sort.go → pkg/graphdb/sort_linux.go


+ 0 - 0
pkg/graphdb/sort_test.go → pkg/graphdb/sort_linux_test.go


+ 3 - 0
pkg/graphdb/unsupported.go

@@ -0,0 +1,3 @@
+// +build !cgo !linux
+
+package graphdb

+ 0 - 0
pkg/graphdb/utils.go → pkg/graphdb/utils_linux.go