services.go 3.8 KB

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