utils.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package project
  2. import (
  3. "strings"
  4. "github.com/docker/engine-api/types/container"
  5. )
  6. // DefaultDependentServices return the dependent services (as an array of ServiceRelationship)
  7. // for the specified project and service. It looks for : links, volumesFrom, net and ipc configuration.
  8. func DefaultDependentServices(p *Project, s Service) []ServiceRelationship {
  9. config := s.Config()
  10. if config == nil {
  11. return []ServiceRelationship{}
  12. }
  13. result := []ServiceRelationship{}
  14. for _, link := range config.Links {
  15. result = append(result, NewServiceRelationship(link, RelTypeLink))
  16. }
  17. for _, volumesFrom := range config.VolumesFrom {
  18. result = append(result, NewServiceRelationship(volumesFrom, RelTypeVolumesFrom))
  19. }
  20. for _, dependsOn := range config.DependsOn {
  21. result = append(result, NewServiceRelationship(dependsOn, RelTypeDependsOn))
  22. }
  23. result = appendNs(p, result, s.Config().NetworkMode, RelTypeNetNamespace)
  24. result = appendNs(p, result, s.Config().Ipc, RelTypeIpcNamespace)
  25. return result
  26. }
  27. func appendNs(p *Project, rels []ServiceRelationship, conf string, relType ServiceRelationshipType) []ServiceRelationship {
  28. service := GetContainerFromIpcLikeConfig(p, conf)
  29. if service != "" {
  30. rels = append(rels, NewServiceRelationship(service, relType))
  31. }
  32. return rels
  33. }
  34. // NameAlias returns the name and alias based on the specified string.
  35. // If the name contains a colon (like name:alias) it will split it, otherwise
  36. // it will return the specified name as name and alias.
  37. func NameAlias(name string) (string, string) {
  38. parts := strings.SplitN(name, ":", 2)
  39. if len(parts) == 2 {
  40. return parts[0], parts[1]
  41. }
  42. return parts[0], parts[0]
  43. }
  44. // GetContainerFromIpcLikeConfig returns name of the service that shares the IPC
  45. // namespace with the specified service.
  46. func GetContainerFromIpcLikeConfig(p *Project, conf string) string {
  47. ipc := container.IpcMode(conf)
  48. if !ipc.IsContainer() {
  49. return ""
  50. }
  51. name := ipc.Container()
  52. if name == "" {
  53. return ""
  54. }
  55. if p.ServiceConfigs.Has(name) {
  56. return name
  57. }
  58. return ""
  59. }