services.go 3.8 KB

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