meta_plugin_init.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 (plugin || !core) && (ui || !core)
  15. // +build plugin !core
  16. // +build ui !core
  17. package server
  18. import (
  19. "fmt"
  20. "github.com/gorilla/mux"
  21. "github.com/lf-edge/ekuiper/internal/conf"
  22. "github.com/lf-edge/ekuiper/internal/plugin"
  23. "net/http"
  24. "runtime"
  25. "strings"
  26. )
  27. // This must be and will be run after meta_init.go init()
  28. func init() {
  29. metaEndpoints = append(metaEndpoints, func(r *mux.Router) {
  30. r.HandleFunc("/plugins/sources/prebuild", prebuildSourcePlugins).Methods(http.MethodGet)
  31. r.HandleFunc("/plugins/sinks/prebuild", prebuildSinkPlugins).Methods(http.MethodGet)
  32. r.HandleFunc("/plugins/functions/prebuild", prebuildFuncsPlugins).Methods(http.MethodGet)
  33. })
  34. }
  35. func prebuildSourcePlugins(w http.ResponseWriter, r *http.Request) {
  36. prebuildPluginsHandler(w, r, plugin.SOURCE)
  37. }
  38. func prebuildSinkPlugins(w http.ResponseWriter, r *http.Request) {
  39. prebuildPluginsHandler(w, r, plugin.SINK)
  40. }
  41. func prebuildFuncsPlugins(w http.ResponseWriter, r *http.Request) {
  42. prebuildPluginsHandler(w, r, plugin.FUNCTION)
  43. }
  44. func prebuildPluginsHandler(w http.ResponseWriter, _ *http.Request, t plugin.PluginType) {
  45. emsg := "It's strongly recommended to install plugins at linux. If you choose to proceed to install plugin, please make sure the plugin is already validated in your own build."
  46. if runtime.GOOS == "linux" {
  47. osrelease, err := Read()
  48. if err != nil {
  49. handleError(w, err, "", logger)
  50. return
  51. }
  52. prettyName := strings.ToUpper(osrelease["PRETTY_NAME"])
  53. var os = "debian"
  54. if strings.Contains(prettyName, "ALPINE") {
  55. os = "alpine"
  56. }
  57. hosts := conf.Config.Basic.PluginHosts
  58. if err, plugins := fetchPluginList(t, hosts, os, runtime.GOARCH); err != nil {
  59. handleError(w, err, "", logger)
  60. } else {
  61. jsonResponse(plugins, w, logger)
  62. }
  63. } else {
  64. handleError(w, fmt.Errorf(emsg), "", logger)
  65. }
  66. }
  67. var NativeSourcePlugin = []string{"random", "zmq", "sql"}
  68. var NativeSinkPlugin = []string{"file", "image", "influx", "redis", "tdengine", "zmq", "sql"}
  69. var NativeFunctionPlugin = []string{"accumulateWordCount", "countPlusOne", "echo", "geohash", "image", "labelImage"}
  70. func fetchPluginList(t plugin.PluginType, hosts, os, arch string) (err error, result map[string]string) {
  71. ptype := "sources"
  72. plugins := NativeSourcePlugin
  73. if t == plugin.SINK {
  74. ptype = "sinks"
  75. plugins = NativeSinkPlugin
  76. } else if t == plugin.FUNCTION {
  77. ptype = "functions"
  78. plugins = NativeFunctionPlugin
  79. }
  80. if hosts == "" || ptype == "" || os == "" {
  81. logger.Errorf("Invalid parameter value: hosts %s, ptype %s or os: %s should not be empty.", hosts, ptype, os)
  82. return fmt.Errorf("invalid configruation for plugin host in kuiper.yaml"), nil
  83. }
  84. result = make(map[string]string)
  85. hostsArr := strings.Split(hosts, ",")
  86. for _, host := range hostsArr {
  87. host := strings.Trim(host, " ")
  88. tmp := []string{host, "kuiper-plugins", version, os, ptype}
  89. //The url is similar to http://host:port/kuiper-plugins/0.9.1/debian/sinks/
  90. url := strings.Join(tmp, "/")
  91. for _, p := range plugins {
  92. result[p] = url + "/" + p + "_" + arch + ".zip"
  93. }
  94. }
  95. return
  96. }