sourceMeta.go 4.8 KB

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