ovn_nbctl.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package ovnutil
  15. import (
  16. "context"
  17. "fmt"
  18. "os/exec"
  19. "sort"
  20. "strings"
  21. "time"
  22. "yunion.io/x/log"
  23. "yunion.io/x/ovsdb/schema/ovn_nb"
  24. "yunion.io/x/ovsdb/types"
  25. "yunion.io/x/pkg/errors"
  26. )
  27. const ovnNbCtlTimeout = 20 * time.Second
  28. const ovnBatchCmd = 75
  29. type CmdResult struct {
  30. Output string
  31. Err error
  32. }
  33. func (res *CmdResult) Error() string {
  34. return fmt.Sprintf("err: %v, output: %s", res.Err, res.Output)
  35. }
  36. type OvnNbCtl struct {
  37. db string
  38. }
  39. func NewOvnNbCtl(db string) *OvnNbCtl {
  40. cli := &OvnNbCtl{
  41. db: db,
  42. }
  43. return cli
  44. }
  45. func (cli *OvnNbCtl) prepArgs(args []string) []string {
  46. var r []string
  47. if cli.db != "" {
  48. r = make([]string, len(args)+1)
  49. r[0] = "--db=" + cli.db
  50. copy(r[1:], args)
  51. } else {
  52. r = args
  53. }
  54. return r
  55. }
  56. func (cli *OvnNbCtl) run(ctx context.Context, args []string) *CmdResult {
  57. ctx, cancel := context.WithTimeout(ctx, ovnNbCtlTimeout)
  58. defer cancel()
  59. args = cli.prepArgs(args)
  60. cmd := exec.CommandContext(ctx, "ovn-nbctl", args...)
  61. combined, err := cmd.CombinedOutput()
  62. res := &CmdResult{
  63. Output: string(combined),
  64. Err: err,
  65. }
  66. return res
  67. }
  68. func (cli *OvnNbCtl) splitArgs(args []string) [][]string {
  69. idx, ret, part := 0, [][]string{}, []string{}
  70. for _, arg := range args {
  71. if arg == "--" {
  72. if idx > 0 && idx%ovnBatchCmd == 0 {
  73. ret = append(ret, part)
  74. part = []string{}
  75. }
  76. idx++
  77. }
  78. part = append(part, arg)
  79. }
  80. if len(part) > 0 {
  81. ret = append(ret, part)
  82. }
  83. return ret
  84. }
  85. func (cli *OvnNbCtl) Must(ctx context.Context, msg string, args []string) *CmdResult {
  86. var res *CmdResult
  87. for _, _args := range cli.splitArgs(args) {
  88. res = cli.must(ctx, msg, _args)
  89. if res.Err != nil {
  90. return res
  91. }
  92. }
  93. return res
  94. }
  95. func (cli *OvnNbCtl) must(ctx context.Context, msg string, args []string) *CmdResult {
  96. res := cli.run(ctx, args)
  97. if res.Err != nil {
  98. panic(cli.errWrap(res, msg, args))
  99. }
  100. if cli.argsHasWrite(args) {
  101. log.Infof("%s:\n%s", msg, ovnNbctlArgsString(args))
  102. }
  103. return res
  104. }
  105. func (cli *OvnNbCtl) errWrap(err error, msg string, args []string) error {
  106. s := cli.argsString(args)
  107. return errors.Wrapf(err, "%s:\n%s\n", msg, s)
  108. }
  109. func (cli *OvnNbCtl) argsString(args []string) string {
  110. args = cli.prepArgs(args)
  111. s := ovnNbctlArgsString(args)
  112. return s
  113. }
  114. func (cli *OvnNbCtl) argsHasWrite(args []string) bool {
  115. for _, arg := range args {
  116. switch arg {
  117. case "create", "set", "add", "remove", "destroy", "clear":
  118. return true
  119. case "list", "find", "get":
  120. case "lsp-del", "lrp-del":
  121. return true
  122. default:
  123. }
  124. }
  125. return false
  126. }
  127. func ovnNbctlArgsString(args []string) string {
  128. var (
  129. s = ""
  130. indent = ""
  131. indent1 = "\t"
  132. indent2 = "\t\t"
  133. )
  134. s += "ovn-nbctl"
  135. for _, arg := range args {
  136. if arg == "--" {
  137. indent = indent1
  138. s += ` \` + "\n"
  139. s += indent
  140. s += arg
  141. } else if !strings.HasPrefix(arg, "--") && strings.ContainsRune(arg, '=') {
  142. if indent == indent1 {
  143. indent = indent2
  144. }
  145. s += ` \` + "\n"
  146. s += indent
  147. s += fmt.Sprintf("%q", arg)
  148. } else {
  149. s += fmt.Sprintf(" %q", arg)
  150. }
  151. }
  152. return s
  153. }
  154. func OvnNbctlArgsDestroy(irows []types.IRow) []string {
  155. sort.Slice(irows, func(i, j int) bool {
  156. ri := irows[i]
  157. rj := irows[j]
  158. iri := ri.OvsdbIsRoot()
  159. irj := rj.OvsdbIsRoot()
  160. if !iri && irj {
  161. return true
  162. }
  163. return false
  164. })
  165. var args []string
  166. for _, irow := range irows {
  167. var newArgs []string
  168. switch irow.(type) {
  169. case *ovn_nb.LogicalSwitchPort:
  170. newArgs = []string{"--", "--if-exists", "lsp-del", irow.OvsdbUuid()}
  171. case *ovn_nb.LogicalRouterPort:
  172. newArgs = []string{"--", "--if-exists", "lrp-del", irow.OvsdbUuid()}
  173. case *ovn_nb.LogicalRouterStaticRoute:
  174. case *ovn_nb.ACL:
  175. case *ovn_nb.QoS:
  176. default:
  177. if !irow.OvsdbIsRoot() {
  178. panic(irow.OvsdbTableName())
  179. }
  180. newArgs = []string{"--", "--if-exists", "destroy", irow.OvsdbTableName(), irow.OvsdbUuid()}
  181. }
  182. if len(newArgs) > 0 {
  183. if irow.OvsdbIsRoot() {
  184. args = append(args, newArgs...)
  185. } else {
  186. args = append(newArgs, args...)
  187. }
  188. }
  189. }
  190. return args
  191. }