sourceMeta.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. "github.com/lf-edge/ekuiper/internal/conf"
  18. "github.com/lf-edge/ekuiper/internal/pkg/filex"
  19. "io/ioutil"
  20. "path"
  21. "strings"
  22. "sync"
  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. confDir, err := conf.GetConfLoc()
  97. if nil != err {
  98. return err
  99. }
  100. dir := path.Join(confDir, "sources")
  101. infos, err := ioutil.ReadDir(dir)
  102. if nil != err {
  103. return err
  104. }
  105. if err = ReadSourceMetaFile(path.Join(confDir, "mqtt_source.json"), true); nil != err {
  106. return err
  107. }
  108. conf.Log.Infof("Loading metadata file for source : %s", "mqtt_source.json")
  109. for _, info := range infos {
  110. fileName := info.Name()
  111. if strings.HasSuffix(fileName, ".json") {
  112. filePath := path.Join(dir, fileName)
  113. if err = ReadSourceMetaFile(filePath, checker(strings.TrimSuffix(fileName, ".json"))); nil != err {
  114. return err
  115. }
  116. conf.Log.Infof("Loading metadata file for source : %s", fileName)
  117. }
  118. }
  119. return nil
  120. }
  121. func GetSourceMeta(sourceName, language string) (ptrSourceProperty *uiSource, err error) {
  122. gSourcemetaLock.RLock()
  123. defer gSourcemetaLock.RUnlock()
  124. v, found := gSourcemetadata[sourceName+`.json`]
  125. if !found {
  126. return nil, fmt.Errorf(`%s%s`, getMsg(language, source, "not_found_plugin"), sourceName)
  127. }
  128. ui := new(uiSource)
  129. *ui = *v
  130. return ui, nil
  131. }
  132. func GetSourcesPlugins() (sources []*pluginfo) {
  133. gSourcemetaLock.RLock()
  134. defer gSourcemetaLock.RUnlock()
  135. for fileName, v := range gSourcemetadata {
  136. node := new(pluginfo)
  137. node.Name = strings.TrimSuffix(fileName, `.json`)
  138. if nil == v {
  139. continue
  140. }
  141. if nil == v.About {
  142. continue
  143. }
  144. node.About = v.About
  145. i := 0
  146. for ; i < len(sources); i++ {
  147. if node.Name <= sources[i].Name {
  148. sources = append(sources, node)
  149. copy(sources[i+1:], sources[i:])
  150. sources[i] = node
  151. break
  152. }
  153. }
  154. if len(sources) == i {
  155. sources = append(sources, node)
  156. }
  157. }
  158. return sources
  159. }