sourceMeta.go 5.0 KB

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