rpc_plugin.go 3.1 KB

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