sourceMeta.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. 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 gSourcemetaLock = sync.RWMutex{}
  66. var gSourcemetadata = make(map[string]*uiSource)
  67. func UninstallSource(name string) {
  68. gSourcemetaLock.Lock()
  69. defer gSourcemetaLock.Unlock()
  70. if v, ok := gSourcemetadata[name+".json"]; ok {
  71. if nil != v.About {
  72. v.About.Installed = false
  73. }
  74. delete(gSourcemetadata, name+".json")
  75. }
  76. delYamlConf(fmt.Sprintf(SourceCfgOperatorKeyTemplate, name))
  77. }
  78. func ReadSourceMetaFile(filePath string, isScan bool, isLookup bool) error {
  79. fileName := path.Base(filePath)
  80. if "mqtt_source.json" == fileName {
  81. fileName = "mqtt.json"
  82. }
  83. ptrMeta := new(fileSource)
  84. _ = filex.ReadJsonUnmarshal(filePath, ptrMeta)
  85. if nil == ptrMeta.About {
  86. return fmt.Errorf("not found about of %s", filePath)
  87. } else {
  88. // TODO currently, only show installed source in ui
  89. ptrMeta.About.Installed = true
  90. }
  91. meta, err := newUiSource(ptrMeta, isScan, isLookup)
  92. if nil != err {
  93. return err
  94. }
  95. gSourcemetaLock.Lock()
  96. gSourcemetadata[fileName] = meta
  97. gSourcemetaLock.Unlock()
  98. loadConfigOperatorForSource(strings.TrimSuffix(fileName, `.json`))
  99. loadConfigOperatorForConnection(strings.TrimSuffix(fileName, `.json`))
  100. return err
  101. }
  102. func ReadSourceMetaDir(scanChecker InstallChecker, lookupChecker InstallChecker) error {
  103. //load etc/sources meta data
  104. confDir, err := conf.GetConfLoc()
  105. if nil != err {
  106. return err
  107. }
  108. dir := path.Join(confDir, "sources")
  109. dirEntries, err := os.ReadDir(dir)
  110. if nil != err {
  111. return err
  112. }
  113. if err = ReadSourceMetaFile(path.Join(confDir, "mqtt_source.json"), true, false); nil != err {
  114. return err
  115. }
  116. conf.Log.Infof("Loading metadata file for source : %s", "mqtt_source.json")
  117. for _, entry := range dirEntries {
  118. fileName := entry.Name()
  119. if strings.HasSuffix(fileName, ".json") {
  120. name := strings.TrimSuffix(fileName, ".json")
  121. isScan := scanChecker(name)
  122. isLookup := lookupChecker(name)
  123. if isScan || isLookup {
  124. filePath := path.Join(dir, fileName)
  125. if err = ReadSourceMetaFile(filePath, isScan, isLookup); nil != err {
  126. return err
  127. }
  128. conf.Log.Infof("Loading metadata file for source : %s", fileName)
  129. } else {
  130. conf.Log.Warnf("Find source metadata file but not installed : %s", fileName)
  131. }
  132. }
  133. }
  134. //load data/sources meta data
  135. confDir, err = conf.GetDataLoc()
  136. if nil != err {
  137. return err
  138. }
  139. dir = path.Join(confDir, "sources")
  140. dirEntries, err = os.ReadDir(dir)
  141. if nil != err {
  142. return err
  143. }
  144. for _, entry := range dirEntries {
  145. fileName := entry.Name()
  146. if strings.HasSuffix(fileName, ".json") {
  147. name := strings.TrimSuffix(fileName, ".json")
  148. isScan := scanChecker(name)
  149. isLookup := lookupChecker(name)
  150. if isScan || isLookup {
  151. filePath := path.Join(dir, fileName)
  152. if err = ReadSourceMetaFile(filePath, isScan, isLookup); nil != err {
  153. return err
  154. }
  155. conf.Log.Infof("Loading metadata file for source : %s", fileName)
  156. } else {
  157. conf.Log.Warnf("Find source metadata file but not installed : %s", fileName)
  158. }
  159. }
  160. }
  161. return nil
  162. }
  163. func GetSourceMeta(sourceName, language string) (ptrSourceProperty *uiSource, err error) {
  164. gSourcemetaLock.RLock()
  165. defer gSourcemetaLock.RUnlock()
  166. v, found := gSourcemetadata[sourceName+`.json`]
  167. if !found {
  168. return nil, fmt.Errorf(`%s%s`, getMsg(language, source, "not_found_plugin"), sourceName)
  169. }
  170. ui := new(uiSource)
  171. *ui = *v
  172. return ui, nil
  173. }
  174. func GetSourcesPlugins(kind string) (sources []*pluginfo) {
  175. gSourcemetaLock.RLock()
  176. defer gSourcemetaLock.RUnlock()
  177. for fileName, v := range gSourcemetadata {
  178. if kind == ast.StreamKindLookup && !v.isLookup {
  179. continue
  180. } else if kind == ast.StreamKindScan && !v.isScan {
  181. continue
  182. }
  183. node := new(pluginfo)
  184. node.Name = strings.TrimSuffix(fileName, `.json`)
  185. if nil == v {
  186. continue
  187. }
  188. if nil == v.About {
  189. continue
  190. }
  191. node.About = v.About
  192. i := 0
  193. for ; i < len(sources); i++ {
  194. if node.Name <= sources[i].Name {
  195. sources = append(sources, node)
  196. copy(sources[i+1:], sources[i:])
  197. sources[i] = node
  198. break
  199. }
  200. }
  201. if len(sources) == i {
  202. sources = append(sources, node)
  203. }
  204. }
  205. return sources
  206. }