github.com/gravitational/moby@v1.13.1/daemon/links_linux.go (about)

     1  package daemon
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"strings"
     7  
     8  	"github.com/Sirupsen/logrus"
     9  	"github.com/docker/docker/container"
    10  	"github.com/docker/docker/pkg/graphdb"
    11  )
    12  
    13  // migrateLegacySqliteLinks migrates sqlite links to use links from HostConfig
    14  // when sqlite links were used, hostConfig.Links was set to nil
    15  func (daemon *Daemon) migrateLegacySqliteLinks(db *graphdb.Database, container *container.Container) error {
    16  	// if links is populated (or an empty slice), then this isn't using sqlite links and can be skipped
    17  	if container.HostConfig == nil || container.HostConfig.Links != nil {
    18  		return nil
    19  	}
    20  
    21  	logrus.Debugf("migrating legacy sqlite link info for container: %s", container.ID)
    22  
    23  	fullName := container.Name
    24  	if fullName[0] != '/' {
    25  		fullName = "/" + fullName
    26  	}
    27  
    28  	// don't use a nil slice, this ensures that the check above will skip once the migration has completed
    29  	links := []string{}
    30  	children, err := db.Children(fullName, 0)
    31  	if err != nil {
    32  		if !strings.Contains(err.Error(), "Cannot find child for") {
    33  			return err
    34  		}
    35  		// else continue... it's ok if we didn't find any children, it'll just be nil and we can continue the migration
    36  	}
    37  
    38  	for _, child := range children {
    39  		c, err := daemon.GetContainer(child.Entity.ID())
    40  		if err != nil {
    41  			return err
    42  		}
    43  
    44  		links = append(links, c.Name+":"+child.Edge.Name)
    45  	}
    46  
    47  	container.HostConfig.Links = links
    48  	return container.WriteHostConfig()
    49  }
    50  
    51  // sqliteMigration performs the link graph DB migration.
    52  func (daemon *Daemon) sqliteMigration(containers map[string]*container.Container) error {
    53  	// migrate any legacy links from sqlite
    54  	linkdbFile := filepath.Join(daemon.root, "linkgraph.db")
    55  	var (
    56  		legacyLinkDB *graphdb.Database
    57  		err          error
    58  	)
    59  
    60  	legacyLinkDB, err = graphdb.NewSqliteConn(linkdbFile)
    61  	if err != nil {
    62  		return fmt.Errorf("error connecting to legacy link graph DB %s, container links may be lost: %v", linkdbFile, err)
    63  	}
    64  	defer legacyLinkDB.Close()
    65  
    66  	for _, c := range containers {
    67  		if err := daemon.migrateLegacySqliteLinks(legacyLinkDB, c); err != nil {
    68  			return err
    69  		}
    70  	}
    71  	return nil
    72  }