rpc_plugin.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2022 EMQ Technologies Co., Ltd.
  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. //go:build (rpc || !core) && (plugin || portable || !core)
  15. package server
  16. import (
  17. "encoding/json"
  18. "fmt"
  19. "github.com/lf-edge/ekuiper/internal/pkg/model"
  20. "github.com/lf-edge/ekuiper/internal/plugin"
  21. )
  22. func (t *Server) CreatePlugin(arg *model.PluginDesc, reply *string) error {
  23. pt := plugin.PluginType(arg.Type)
  24. p, err := getPluginByJson(arg, pt)
  25. if err != nil {
  26. return fmt.Errorf("Create plugin error: %s", err)
  27. }
  28. if p.GetFile() == "" {
  29. return fmt.Errorf("Create plugin error: Missing plugin file url.")
  30. }
  31. // define according to the build tag
  32. err = t.doRegister(pt, p)
  33. if err != nil {
  34. return err
  35. }
  36. if err != nil {
  37. return fmt.Errorf("Create plugin error: %s", err)
  38. } else {
  39. *reply = fmt.Sprintf("Plugin %s is created.", p.GetName())
  40. }
  41. return nil
  42. }
  43. func (t *Server) DropPlugin(arg *model.PluginDesc, reply *string) error {
  44. pt := plugin.PluginType(arg.Type)
  45. p, err := getPluginByJson(arg, pt)
  46. if err != nil {
  47. return fmt.Errorf("Drop plugin error: %s", err)
  48. }
  49. err = t.doDelete(pt, p.GetName(), arg.Stop)
  50. if err != nil {
  51. return fmt.Errorf("Drop plugin error: %s", err)
  52. } else {
  53. if pt == plugin.PORTABLE {
  54. *reply = fmt.Sprintf("Plugin %s is dropped .", p.GetName())
  55. } else {
  56. if arg.Stop {
  57. *reply = fmt.Sprintf("Plugin %s is dropped and Kuiper will be stopped.", p.GetName())
  58. } else {
  59. *reply = fmt.Sprintf("Plugin %s is dropped and Kuiper must restart for the change to take effect.", p.GetName())
  60. }
  61. }
  62. }
  63. return nil
  64. }
  65. func (t *Server) DescPlugin(arg *model.PluginDesc, reply *string) error {
  66. pt := plugin.PluginType(arg.Type)
  67. p, err := getPluginByJson(arg, pt)
  68. if err != nil {
  69. return fmt.Errorf("Describe plugin error: %s", err)
  70. }
  71. m, err := t.doDesc(pt, p.GetName())
  72. if err != nil {
  73. return fmt.Errorf("Describe plugin error: %s", err)
  74. } else {
  75. r, err := marshalDesc(m)
  76. if err != nil {
  77. return fmt.Errorf("Describe plugin error: %v", err)
  78. }
  79. *reply = r
  80. }
  81. return nil
  82. }
  83. func (t *Server) ShowPlugins(arg int, reply *string) error {
  84. pt := plugin.PluginType(arg)
  85. l, err := t.doShow(pt)
  86. if err != nil {
  87. return fmt.Errorf("Show plugin error: %s", err)
  88. }
  89. *reply = l
  90. return nil
  91. }
  92. func getPluginByJson(arg *model.PluginDesc, pt plugin.PluginType) (plugin.Plugin, error) {
  93. p := plugin.NewPluginByType(pt)
  94. if arg.Json != "" {
  95. if err := json.Unmarshal([]byte(arg.Json), p); err != nil {
  96. return nil, fmt.Errorf("Parse plugin %s error : %s.", arg.Json, err)
  97. }
  98. }
  99. p.SetName(arg.Name)
  100. return p, nil
  101. }