sourceMeta.go 5.7 KB

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