links_linux.go 2.0 KB

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