datasources.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package datasources
  2. import (
  3. "errors"
  4. "io/ioutil"
  5. "path/filepath"
  6. "strings"
  7. "github.com/grafana/grafana/pkg/bus"
  8. "github.com/grafana/grafana/pkg/log"
  9. "github.com/grafana/grafana/pkg/models"
  10. yaml "gopkg.in/yaml.v2"
  11. )
  12. var (
  13. ErrInvalidConfigToManyDefault = errors.New("datasource.yaml config is invalid. Only one datasource can be marked as default")
  14. )
  15. func Provision(configDirectory string) error {
  16. dc := newDatasourceProvisioner(log.New("provisioning.datasources"))
  17. return dc.applyChanges(configDirectory)
  18. }
  19. type DatasourceProvisioner struct {
  20. log log.Logger
  21. cfgProvider configReader
  22. }
  23. func newDatasourceProvisioner(log log.Logger) DatasourceProvisioner {
  24. return DatasourceProvisioner{
  25. log: log,
  26. cfgProvider: configReader{},
  27. }
  28. }
  29. func (dc *DatasourceProvisioner) apply(cfg *DatasourcesAsConfig) error {
  30. if err := dc.deleteDatasources(cfg.DeleteDatasources); err != nil {
  31. return err
  32. }
  33. for _, ds := range cfg.Datasources {
  34. cmd := &models.GetDataSourceByNameQuery{OrgId: ds.OrgId, Name: ds.Name}
  35. err := bus.Dispatch(cmd)
  36. if err != nil && err != models.ErrDataSourceNotFound {
  37. return err
  38. }
  39. if err == models.ErrDataSourceNotFound {
  40. dc.log.Info("inserting datasource from configuration ", "name", ds.Name)
  41. insertCmd := createInsertCommand(ds)
  42. if err := bus.Dispatch(insertCmd); err != nil {
  43. return err
  44. }
  45. } else {
  46. dc.log.Debug("updating datasource from configuration", "name", ds.Name)
  47. updateCmd := createUpdateCommand(ds, cmd.Result.Id)
  48. if err := bus.Dispatch(updateCmd); err != nil {
  49. return err
  50. }
  51. }
  52. }
  53. return nil
  54. }
  55. func (dc *DatasourceProvisioner) applyChanges(configPath string) error {
  56. configs, err := dc.cfgProvider.readConfig(configPath)
  57. if err != nil {
  58. return err
  59. }
  60. for _, cfg := range configs {
  61. if err := dc.apply(cfg); err != nil {
  62. return err
  63. }
  64. }
  65. return nil
  66. }
  67. func (dc *DatasourceProvisioner) deleteDatasources(dsToDelete []*DeleteDatasourceConfig) error {
  68. for _, ds := range dsToDelete {
  69. cmd := &models.DeleteDataSourceByNameCommand{OrgId: ds.OrgId, Name: ds.Name}
  70. if err := bus.Dispatch(cmd); err != nil {
  71. return err
  72. }
  73. if cmd.DeletedDatasourcesCount > 0 {
  74. dc.log.Info("deleted datasource based on configuration", "name", ds.Name)
  75. }
  76. }
  77. return nil
  78. }
  79. type configReader struct{}
  80. func (configReader) readConfig(path string) ([]*DatasourcesAsConfig, error) {
  81. files, err := ioutil.ReadDir(path)
  82. if err != nil {
  83. return nil, err
  84. }
  85. var datasources []*DatasourcesAsConfig
  86. for _, file := range files {
  87. if strings.HasSuffix(file.Name(), ".yaml") || strings.HasSuffix(file.Name(), ".yml") {
  88. filename, _ := filepath.Abs(filepath.Join(path, file.Name()))
  89. yamlFile, err := ioutil.ReadFile(filename)
  90. if err != nil {
  91. return nil, err
  92. }
  93. var datasource *DatasourcesAsConfig
  94. err = yaml.Unmarshal(yamlFile, &datasource)
  95. if err != nil {
  96. return nil, err
  97. }
  98. if datasource != nil {
  99. datasources = append(datasources, datasource)
  100. }
  101. }
  102. }
  103. defaultCount := 0
  104. for i := range datasources {
  105. if datasources[i].Datasources == nil {
  106. continue
  107. }
  108. for _, ds := range datasources[i].Datasources {
  109. if ds.OrgId == 0 {
  110. ds.OrgId = 1
  111. }
  112. if ds.IsDefault {
  113. defaultCount++
  114. if defaultCount > 1 {
  115. return nil, ErrInvalidConfigToManyDefault
  116. }
  117. }
  118. }
  119. for _, ds := range datasources[i].DeleteDatasources {
  120. if ds.OrgId == 0 {
  121. ds.OrgId = 1
  122. }
  123. }
  124. }
  125. return datasources, nil
  126. }