| 1 | // Mgmt |
| 2 | // Copyright (C) 2013-2024+ James Shubin and the project contributors |
| 3 | // Written by James Shubin <[email protected]> and the project contributors |
| 4 | // |
| 5 | // This program is free software: you can redistribute it and/or modify |
| 6 | // it under the terms of the GNU General Public License as published by |
| 7 | // the Free Software Foundation, either version 3 of the License, or |
| 8 | // (at your option) any later version. |
| 9 | // |
| 10 | // This program is distributed in the hope that it will be useful, |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | // GNU General Public License for more details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU General Public License |
| 16 | // along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | // |
| 18 | // Additional permission under GNU GPL version 3 section 7 |
| 19 | // |
| 20 | // If you modify this program, or any covered work, by linking or combining it |
| 21 | // with embedded mcl code and modules (and that the embedded mcl code and |
| 22 | // modules which link with this program, contain a copy of their source code in |
| 23 | // the authoritative form) containing parts covered by the terms of any other |
| 24 | // license, the licensors of this program grant you additional permission to |
| 25 | // convey the resulting work. Furthermore, the licensors of this program grant |
| 26 | // the original author, James Shubin, additional permission to update this |
| 27 | // additional permission if he deems it necessary to achieve the goals of this |
| 28 | // additional permission. |
| 29 | |
| 30 | package autogroupscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/engine/graph/autogroup`/ |
| 31 | |
| 32 | import ( |
| 33 | "fmtscip-go gomod github.com/golang/go/src go1.21 fmt/" |
| 34 | |
| 35 | "github.com/purpleidea/mgmt/enginescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/engine`/" |
| 36 | "github.com/purpleidea/mgmt/pgraphscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/pgraph`/" |
| 37 | "github.com/purpleidea/mgmt/util/errwrapscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/errwrap`/" |
| 38 | ) |
| 39 | |
| 40 | // AutoGroup is the mechanical auto group "runner" that runs the interface spec. |
| 41 | // TODO: this algorithm may not be correct in all cases. replace if needed! |
| 42 | func AutoGroupscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/engine/graph/autogroup`/AutoGroup().(ag enginescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/engine`/.AutoGrouperscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/engine`/AutoGrouper#, g *pgraphscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/pgraph`/.Graphscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/pgraph`/Graph#, debug bool, logf func(format string, v ...interface{})) error { |
| 43 | logf("algorithm: %s...", ag.Namescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/engine`/AutoGrouper#Name.()) |
| 44 | if err := ag.Initscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/engine`/AutoGrouper#Init.(g); err != nil { |
| 45 | return errwrapscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/errwrap`/.Wrapfscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/errwrap`/Wrapf().(err, "error running autoGroup(init)") |
| 46 | } |
| 47 | |
| 48 | for { |
| 49 | var v, w pgraphscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/pgraph`/.Vertexscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/pgraph`/Vertex# |
| 50 | v, w, err := ag.VertexNextscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/engine`/AutoGrouper#VertexNext.() // get pair to compare |
| 51 | if err != nil { |
| 52 | return errwrapscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/errwrap`/.Wrapfscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/errwrap`/Wrapf().(err, "error running autoGroup(vertexNext)") |
| 53 | } |
| 54 | merged := false |
| 55 | // save names since they change during the runs |
| 56 | vStr := fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.21 fmt/Sprintf().("%v", v) // valid even if it is nil |
| 57 | wStr := fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.21 fmt/Sprintf().("%v", w) |
| 58 | |
| 59 | if err := ag.VertexCmpscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/engine`/AutoGrouper#VertexCmp.(v, w); err != nil { // cmp ? |
| 60 | if debug { |
| 61 | logf("!GroupCmp for: %s into: %s", wStr, vStr) |
| 62 | } |
| 63 | |
| 64 | // remove grouped vertex and merge edges (res is safe) |
| 65 | } else if err := VertexMergescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/engine/graph/autogroup`/VertexMerge().(g, v, w, ag.VertexMergescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/engine`/AutoGrouper#VertexMerge., ag.EdgeMergescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/engine`/AutoGrouper#EdgeMerge.); err != nil { // merge... |
| 66 | logf("!VertexMerge for: %s into: %s", wStr, vStr) |
| 67 | |
| 68 | } else { // success! |
| 69 | logf("%s into %s", wStr, vStr) |
| 70 | merged = true // woo |
| 71 | } |
| 72 | |
| 73 | // did these get used? |
| 74 | if ok, err := ag.VertexTestscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/engine`/AutoGrouper#VertexTest.(merged); err != nil { |
| 75 | return errwrapscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/errwrap`/.Wrapfscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/errwrap`/Wrapf().(err, "error running autoGroup(vertexTest)") |
| 76 | } else if !ok { |
| 77 | break // done! |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // It would be great to ensure we didn't add any graph cycles here, but |
| 82 | // instead of checking now, we'll move the check into the main loop. |
| 83 | |
| 84 | return nil |
| 85 | } |