sourceMeta.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Copyright 2021-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. package meta
  15. import (
  16. "fmt"
  17. "os"
  18. "path"
  19. "strings"
  20. "sync"
  21. "github.com/lf-edge/ekuiper/internal/conf"
  22. "github.com/lf-edge/ekuiper/internal/pkg/filex"
  23. "github.com/lf-edge/ekuiper/pkg/ast"
  24. )
  25. type (
  26. fileSource struct {
  27. About *fileAbout `json:"about"`
  28. Libs []string `json:"libs"`
  29. DataSource interface{} `json:"dataSource,omitempty"`
  30. ConfKeys map[string][]*fileField `json:"properties"`
  31. Node interface{} `json:"node"`
  32. }
  33. uiSource struct {
  34. About *about `json:"about"`
  35. Libs []string `json:"libs"`
  36. DataSource interface{} `json:"dataSource,omitempty"`
  37. ConfKeys map[string][]field `json:"properties"`
  38. Node interface{} `json:"node"`
  39. isScan bool
  40. isLookup bool
  41. }
  42. )
  43. func newUiSource(fi *fileSource, isScan bool, isLookup bool) (*uiSource, error) {
  44. if nil == fi {
  45. return nil, nil
  46. }
  47. var err error
  48. ui := new(uiSource)
  49. ui.Libs = fi.Libs
  50. ui.About = newAbout(fi.About)
  51. ui.Node = fi.Node
  52. if fi.DataSource != nil {
  53. ui.DataSource = fi.DataSource
  54. }
  55. ui.ConfKeys = make(map[string][]field)
  56. for k, fields := range fi.ConfKeys {
  57. if ui.ConfKeys[k], err = newField(fields); nil != err {
  58. return nil, err
  59. }
  60. }
  61. ui.isScan = isScan
  62. ui.isLookup = isLookup
  63. return ui, nil
  64. }
  65. var (
  66. gSourcemetaLock = sync.RWMutex{}
  67. gSourcemetadata = make(map[string]*uiSource)
  68. )
  69. func UninstallSource(name string) {
  70. gSourcemetaLock.Lock()
  71. defer gSourcemetaLock.Unlock()
  72. if v, ok := gSourcemetadata[name+".json"]; ok {
  73. if nil != v.About {
  74. v.About.Installed = false
  75. }
  76. delete(gSourcemetadata, name+".json")
  77. }
  78. delYamlConf(fmt.Sprintf(SourceCfgOperatorKeyTemplate, name))
  79. }
  80. func ReadSourceMetaFile(filePath string, isScan bool, isLookup bool) error {
  81. fileName := path.Base(filePath)
  82. if "mqtt_source.json" == fileName {
  83. fileName = "mqtt.json"
  84. }
  85. ptrMeta := new(fileSource)
  86. _ = filex.ReadJsonUnmarshal(filePath, ptrMeta)
  87. if nil == ptrMeta.About {
  88. return fmt.Errorf("not found about of %s", filePath)
  89. } else {
  90. // TODO currently, only show installed source in ui
  91. ptrMeta.About.Installed = true
  92. }
  93. meta, err := newUiSource(ptrMeta, isScan, isLookup)
  94. if nil != err {
  95. return err
  96. }
  97. gSourcemetaLock.Lock()
  98. gSourcemetadata[fileName] = meta
  99. gSourcemetaLock.Unlock()
  100. loadConfigOperatorForSource(strings.TrimSuffix(fileName, `.json`))
  101. loadConfigOperatorForConnection(strings.TrimSuffix(fileName, `.json`))
  102. return err
  103. }
  104. func ReadSourceMetaDir(scanChecker InstallChecker, lookupChecker InstallChecker) error {
  105. // load etc/sources meta data
  106. confDir, err := conf.GetConfLoc()
  107. if nil != err {
  108. return err
  109. }
  110. dir := path.Join(confDir, "sources")
  111. dirEntries, err := os.ReadDir(dir)
  112. if nil != err {
  113. return err
  114. }
  115. if err = ReadSourceMetaFile(path.Join(confDir, "mqtt_source.json"), true, false); nil != err {
  116. return err
  117. }
  118. conf.Log.Infof("Loading metadata file for source : %s", "mqtt_source.json")
  119. for _, entry := range dirEntries {
  120. fileName := entry.Name()
  121. if strings.HasSuffix(fileName, ".json") {
  122. name := strings.TrimSuffix(fileName, ".json")
  123. isScan := scanChecker(name)
  124. isLookup := lookupChecker(name)
  125. if isScan || isLookup {
  126. filePath := path.Join(dir, fileName)
  127. if err = ReadSourceMetaFile(filePath, isScan, isLookup); nil != err {
  128. return err
  129. }
  130. conf.Log.Infof("Loading metadata file for source : %s", fileName)
  131. } else {
  132. conf.Log.Warnf("Find source metadata file but not installed : %s", fileName)
  133. }
  134. }
  135. }
  136. // load data/sources meta data
  137. confDir, err = conf.GetDataLoc()
  138. if nil != err {
  139. return err
  140. }
  141. dir = path.Join(confDir, "sources")
  142. dirEntries, err = os.ReadDir(dir)
  143. if nil != err {
  144. return err
  145. }
  146. for _, entry := range dirEntries {
  147. fileName := entry.Name()
  148. if strings.HasSuffix(fileName, ".json") {
  149. name := strings.TrimSuffix(fileName, ".json")
  150. isScan := scanChecker(name)
  151. isLookup := lookupChecker(name)
  152. if isScan || isLookup {
  153. filePath := path.Join(dir, fileName)
  154. if err = ReadSourceMetaFile(filePath, isScan, isLookup); nil != err {
  155. return err
  156. }
  157. conf.Log.Infof("Loading metadata file for source : %s", fileName)
  158. } else {
  159. conf.Log.Warnf("Find source metadata file but not installed : %s", fileName)
  160. }
  161. }
  162. }
  163. return nil
  164. }
  165. func GetSourceMeta(sourceName, language string) (ptrSourceProperty *uiSource, err error) {
  166. gSourcemetaLock.RLock()
  167. defer gSourcemetaLock.RUnlock()
  168. v, found := gSourcemetadata[sourceName+`.json`]
  169. if !found {
  170. return nil, fmt.Errorf(`%s%s`, getMsg(language, source, "not_found_plugin"), sourceName)
  171. }
  172. ui := new(uiSource)
  173. *ui = *v
  174. return ui, nil
  175. }
  176. func GetSourcesPlugins(kind string) (sources []*pluginfo) {
  177. gSourcemetaLock.RLock()
  178. defer gSourcemetaLock.RUnlock()
  179. for fileName, v := range gSourcemetadata {
  180. if kind == ast.StreamKindLookup && !v.isLookup {
  181. continue
  182. } else if kind == ast.StreamKindScan && !v.isScan {
  183. continue
  184. }
  185. node := new(pluginfo)
  186. node.Name = strings.TrimSuffix(fileName, `.json`)
  187. if nil == v {
  188. continue
  189. }
  190. if nil == v.About {
  191. continue
  192. }
  193. node.About = v.About
  194. i := 0
  195. for ; i < len(sources); i++ {
  196. if node.Name <= sources[i].Name {
  197. sources = append(sources, node)
  198. copy(sources[i+1:], sources[i:])
  199. sources[i] = node
  200. break
  201. }
  202. }
  203. if len(sources) == i {
  204. sources = append(sources, node)
  205. }
  206. }
  207. return sources
  208. }