services.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package services
  2. import (
  3. "crypto/tls"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "net/url"
  10. "path"
  11. "time"
  12. "github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
  13. m "github.com/grafana/grafana/pkg/cmd/grafana-cli/models"
  14. )
  15. var (
  16. IoHelper m.IoUtil = IoUtilImp{}
  17. HttpClient http.Client
  18. grafanaVersion string
  19. )
  20. func Init(version string) {
  21. grafanaVersion = version
  22. tr := &http.Transport{
  23. TLSClientConfig: &tls.Config{InsecureSkipVerify: false},
  24. }
  25. HttpClient = http.Client{
  26. Timeout: time.Duration(10 * time.Second),
  27. Transport: tr,
  28. }
  29. }
  30. func ListAllPlugins(repoUrl string) (m.PluginRepo, error) {
  31. body, err := sendRequest(repoUrl, "repo")
  32. if err != nil {
  33. logger.Info("Failed to send request", "error", err)
  34. return m.PluginRepo{}, fmt.Errorf("Failed to send request. error: %v", err)
  35. }
  36. if err != nil {
  37. return m.PluginRepo{}, err
  38. }
  39. var data m.PluginRepo
  40. err = json.Unmarshal(body, &data)
  41. if err != nil {
  42. logger.Info("Failed to unmarshal graphite response error: %v", err)
  43. return m.PluginRepo{}, err
  44. }
  45. return data, nil
  46. }
  47. func ReadPlugin(pluginDir, pluginName string) (m.InstalledPlugin, error) {
  48. distPluginDataPath := path.Join(pluginDir, pluginName, "dist", "plugin.json")
  49. var data []byte
  50. var err error
  51. data, err = IoHelper.ReadFile(distPluginDataPath)
  52. if err != nil {
  53. pluginDataPath := path.Join(pluginDir, pluginName, "plugin.json")
  54. data, err = IoHelper.ReadFile(pluginDataPath)
  55. if err != nil {
  56. return m.InstalledPlugin{}, errors.New("Could not find dist/plugin.json or plugin.json on " + pluginName + " in " + pluginDir)
  57. }
  58. }
  59. res := m.InstalledPlugin{}
  60. json.Unmarshal(data, &res)
  61. if res.Info.Version == "" {
  62. res.Info.Version = "0.0.0"
  63. }
  64. if res.Id == "" {
  65. return m.InstalledPlugin{}, errors.New("could not find plugin " + pluginName + " in " + pluginDir)
  66. }
  67. return res, nil
  68. }
  69. func GetLocalPlugins(pluginDir string) []m.InstalledPlugin {
  70. result := make([]m.InstalledPlugin, 0)
  71. files, _ := IoHelper.ReadDir(pluginDir)
  72. for _, f := range files {
  73. res, err := ReadPlugin(pluginDir, f.Name())
  74. if err == nil {
  75. result = append(result, res)
  76. }
  77. }
  78. return result
  79. }
  80. func RemoveInstalledPlugin(pluginPath, pluginName string) error {
  81. logger.Infof("Removing plugin: %v\n", pluginName)
  82. pluginDir := path.Join(pluginPath, pluginName)
  83. _, err := IoHelper.Stat(pluginDir)
  84. if err != nil {
  85. return err
  86. }
  87. return IoHelper.RemoveAll(pluginDir)
  88. }
  89. func GetPlugin(pluginId, repoUrl string) (m.Plugin, error) {
  90. body, err := sendRequest(repoUrl, "repo", pluginId)
  91. if err != nil {
  92. logger.Info("Failed to send request", "error", err)
  93. return m.Plugin{}, fmt.Errorf("Failed to send request. error: %v", err)
  94. }
  95. if err != nil {
  96. return m.Plugin{}, err
  97. }
  98. var data m.Plugin
  99. err = json.Unmarshal(body, &data)
  100. if err != nil {
  101. logger.Info("Failed to unmarshal graphite response error: %v", err)
  102. return m.Plugin{}, err
  103. }
  104. return data, nil
  105. }
  106. func sendRequest(repoUrl string, subPaths ...string) ([]byte, error) {
  107. u, _ := url.Parse(repoUrl)
  108. for _, v := range subPaths {
  109. u.Path = path.Join(u.Path, v)
  110. }
  111. req, err := http.NewRequest(http.MethodGet, u.String(), nil)
  112. req.Header.Set("grafana-version", grafanaVersion)
  113. req.Header.Set("User-Agent", "grafana "+grafanaVersion)
  114. if err != nil {
  115. return []byte{}, err
  116. }
  117. res, err := HttpClient.Do(req)
  118. if err != nil {
  119. return []byte{}, err
  120. }
  121. if res.StatusCode/100 != 2 {
  122. return []byte{}, fmt.Errorf("Api returned invalid status: %s", res.Status)
  123. }
  124. body, err := ioutil.ReadAll(res.Body)
  125. defer res.Body.Close()
  126. return body, err
  127. }