conf_util.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/go-yaml/yaml"
  5. "io/ioutil"
  6. "os"
  7. "strings"
  8. )
  9. var khome = os.Getenv("KUIPER_HOME")
  10. var fileMap = map[string]string{
  11. "edgex": khome + "/etc/sources/edgex.yaml",
  12. "random": khome + "/etc/sources/random.yaml",
  13. "zmq": khome + "/etc/sources/zmq.yaml",
  14. "mqtt_source": khome + "/etc/mqtt_source.yaml",
  15. "kuiper": khome + "/etc/kuiper.yaml",
  16. "client": khome + "/etc/client.yaml",
  17. }
  18. var file_keys_map = map[string]map[string]string{
  19. "edgex": {
  20. "CLIENTID": "ClientId",
  21. "USERNAME": "Username",
  22. "PASSWORD": "Password",
  23. "QOS": "Qos",
  24. "KEEPALIVE": "KeepAlive",
  25. "RETAINED": "Retained",
  26. "CONNECTIONPAYLOAD": "ConnectionPayload",
  27. "CERTFILE": "CertFile",
  28. "KEYFILE": "KeyFile",
  29. "CERTPEMBLOCK": "CertPEMBlock",
  30. "KEYPEMBLOCK": "KeyPEMBlock",
  31. "SKIPCERTVERIFY": "SkipCertVerify",
  32. },
  33. "mqtt_source": {
  34. "SHAREDSUBSCRIPTION": "sharedSubscription",
  35. "CERTIFICATIONPATH": "certificationPath",
  36. "PRIVATEKEYPATH": "privateKeyPath",
  37. },
  38. "kuiper": {
  39. "CONSOLELOG": "consoleLog",
  40. "FILELOG": "fileLog",
  41. "RESTPORT": "restPort",
  42. "PROMETHEUSPORT": "prometheusPort",
  43. },
  44. }
  45. func fileExists(filename string) bool {
  46. info, err := os.Stat(filename)
  47. if os.IsNotExist(err) {
  48. return false
  49. }
  50. return !info.IsDir()
  51. }
  52. func deleteFile(path string) {
  53. // delete file
  54. var err = os.Remove(path)
  55. if err != nil {
  56. return
  57. }
  58. fmt.Println("File Deleted")
  59. }
  60. func main() {
  61. fmt.Println(fileMap["edgex"])
  62. files := make(map[string]map[interface{}]interface{})
  63. ProcessEnv(files, os.Environ())
  64. for f, v := range files {
  65. if bs, err := yaml.Marshal(v); err != nil {
  66. fmt.Println(err)
  67. } else {
  68. message := fmt.Sprintf("-------------------\nConf file %s: \n %s", f, string(bs))
  69. fmt.Println(message)
  70. if fname, ok := fileMap[f]; ok {
  71. if fileExists(fname) {
  72. deleteFile(fname)
  73. }
  74. if e := ioutil.WriteFile(fname, bs, 0644); e != nil {
  75. fmt.Println(e)
  76. }
  77. }
  78. }
  79. }
  80. }
  81. func ProcessEnv(files map[string]map[interface{}]interface{}, vars []string) {
  82. for _, e := range vars {
  83. pair := strings.SplitN(e, "=", 2)
  84. if len(pair) != 2 {
  85. fmt.Printf("invalid env %s, skip it.\n", e)
  86. continue
  87. }
  88. valid := false
  89. for k, _ := range fileMap {
  90. if strings.HasPrefix(pair[0], strings.ToUpper(k)) {
  91. valid = true
  92. break
  93. }
  94. }
  95. if !valid {
  96. continue
  97. } else {
  98. fmt.Printf("Find env: %s, start to handle it.\n", e)
  99. }
  100. env_v := strings.ReplaceAll(pair[0], "__", ".")
  101. keys := strings.Split(env_v, ".")
  102. for i, v := range keys {
  103. keys[i] = v
  104. }
  105. if len(keys) < 2 {
  106. fmt.Printf("not concerned env %s, skip it.\n", e)
  107. continue
  108. } else {
  109. k := strings.ToLower(keys[0])
  110. if v, ok := files[k]; !ok {
  111. if data, err := ioutil.ReadFile(fileMap[k]); err != nil {
  112. fmt.Printf("%s\n", err)
  113. } else {
  114. m := make(map[interface{}]interface{})
  115. err = yaml.Unmarshal([]byte(data), &m)
  116. if err != nil {
  117. fmt.Println(err)
  118. }
  119. files[k] = m
  120. Handle(k, m, keys[1:], pair[1])
  121. }
  122. } else {
  123. Handle(k, v, keys[1:], pair[1])
  124. }
  125. }
  126. }
  127. }
  128. func Handle(file string, conf map[interface{}]interface{}, skeys []string, val string) {
  129. key := getKey(file, skeys[0])
  130. if len(skeys) == 1 {
  131. conf[key] = val
  132. } else if len(skeys) >= 2 {
  133. if v, ok := conf[key]; ok {
  134. if v1, ok1 := v.(map[interface{}]interface{}); ok1 {
  135. Handle(file, v1, skeys[1:], val)
  136. } else {
  137. fmt.Printf("Not expected map: %v\n", v)
  138. }
  139. } else {
  140. v1 := make(map[interface{}]interface{})
  141. conf[key] = v1
  142. Handle(file, v1, skeys[1:], val)
  143. }
  144. }
  145. }
  146. func getKey(file string, key string) string{
  147. if m, ok := file_keys_map[file][key]; ok {
  148. return m
  149. } else {
  150. return strings.ToLower(key)
  151. }
  152. }