|
| 1 | +package v2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "sort" |
| 5 | + |
| 6 | + "get.porter.sh/porter/pkg/cnab" |
| 7 | + depsv2ext "get.porter.sh/porter/pkg/cnab/extensions/dependencies/v2" |
| 8 | + "github.com/yourbasic/graph" |
| 9 | +) |
| 10 | + |
| 11 | +// BundleGraph is a directed acyclic graph of a bundle and its dependencies |
| 12 | +// (which may be other bundles, or installations) It is used to resolve the |
| 13 | +// dependency order in which the bundles must be executed. |
| 14 | +type BundleGraph struct { |
| 15 | + // nodeKeys is a map from the node key to its index in nodes |
| 16 | + nodeKeys map[string]int |
| 17 | + nodes []Node |
| 18 | +} |
| 19 | + |
| 20 | +func NewBundleGraph() *BundleGraph { |
| 21 | + return &BundleGraph{ |
| 22 | + nodeKeys: make(map[string]int), |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +// RegisterNode adds the specified node to the graph |
| 27 | +// returning true if the node is already present. |
| 28 | +func (g *BundleGraph) RegisterNode(node Node) bool { |
| 29 | + _, exists := g.nodeKeys[node.GetKey()] |
| 30 | + if !exists { |
| 31 | + nodeIndex := len(g.nodes) |
| 32 | + g.nodes = append(g.nodes, node) |
| 33 | + g.nodeKeys[node.GetKey()] = nodeIndex |
| 34 | + } |
| 35 | + return exists |
| 36 | +} |
| 37 | + |
| 38 | +func (g *BundleGraph) Sort() ([]Node, bool) { |
| 39 | + dag := graph.New(len(g.nodes)) |
| 40 | + for nodeIndex, node := range g.nodes { |
| 41 | + for _, depKey := range node.GetRequires() { |
| 42 | + depIndex, ok := g.nodeKeys[depKey] |
| 43 | + if !ok { |
| 44 | + panic("oops") |
| 45 | + } |
| 46 | + dag.Add(nodeIndex, depIndex) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + indices, ok := graph.TopSort(dag) |
| 51 | + if !ok { |
| 52 | + return nil, false |
| 53 | + } |
| 54 | + |
| 55 | + // Reverse the sort so that items with no dependencies are listed first |
| 56 | + count := len(indices) |
| 57 | + results := make([]Node, count) |
| 58 | + for i, nodeIndex := range indices { |
| 59 | + results[count-i-1] = g.nodes[nodeIndex] |
| 60 | + } |
| 61 | + return results, true |
| 62 | +} |
| 63 | + |
| 64 | +func (g *BundleGraph) GetNode(key string) (Node, bool) { |
| 65 | + if nodeIndex, ok := g.nodeKeys[key]; ok { |
| 66 | + return g.nodes[nodeIndex], true |
| 67 | + } |
| 68 | + return nil, false |
| 69 | +} |
| 70 | + |
| 71 | +// Node in a BundleGraph. |
| 72 | +type Node interface { |
| 73 | + GetRequires() []string |
| 74 | + GetKey() string |
| 75 | +} |
| 76 | + |
| 77 | +var _ Node = BundleNode{} |
| 78 | +var _ Node = InstallationNode{} |
| 79 | + |
| 80 | +// BundleNode is a Node in a BundleGraph that represents a dependency on a bundle |
| 81 | +// that has not yet been installed. |
| 82 | +type BundleNode struct { |
| 83 | + Key string |
| 84 | + ParentKey string |
| 85 | + Reference cnab.BundleReference |
| 86 | + Requires []string |
| 87 | + |
| 88 | + // TODO(PEP003): DO we need to store this? Can we do it somewhere else or hold a reference to the dep and add more to the Node interface? |
| 89 | + Credentials map[string]depsv2ext.DependencySource |
| 90 | + Parameters map[string]depsv2ext.DependencySource |
| 91 | +} |
| 92 | + |
| 93 | +func (d BundleNode) GetKey() string { |
| 94 | + return d.Key |
| 95 | +} |
| 96 | + |
| 97 | +func (d BundleNode) GetParentKey() string { |
| 98 | + return d.ParentKey |
| 99 | +} |
| 100 | + |
| 101 | +func (d BundleNode) GetRequires() []string { |
| 102 | + sort.Strings(d.Requires) |
| 103 | + return d.Requires |
| 104 | +} |
| 105 | + |
| 106 | +func (d BundleNode) IsRoot() bool { |
| 107 | + return d.Key == "root" |
| 108 | +} |
| 109 | + |
| 110 | +// InstallationNode is a Node in a BundleGraph that represents a dependency on an |
| 111 | +// installed bundle (installation). |
| 112 | +type InstallationNode struct { |
| 113 | + Key string |
| 114 | + ParentKey string |
| 115 | + Namespace string |
| 116 | + Name string |
| 117 | +} |
| 118 | + |
| 119 | +func (d InstallationNode) GetKey() string { |
| 120 | + return d.Key |
| 121 | +} |
| 122 | + |
| 123 | +func (d InstallationNode) GetParentKey() string { |
| 124 | + return d.ParentKey |
| 125 | +} |
| 126 | + |
| 127 | +func (d InstallationNode) GetRequires() []string { |
| 128 | + return nil |
| 129 | +} |
0 commit comments