services.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. "runtime"
  13. "time"
  14. "github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
  15. m "github.com/grafana/grafana/pkg/cmd/grafana-cli/models"
  16. )
  17. var (
  18. IoHelper m.IoUtil = IoUtilImp{}
  19. HttpClient http.Client
  20. grafanaVersion string
  21. ErrNotFoundError = errors.New("404 not found error")
  22. )
  23. func Init(version string, skipTLSVerify bool) {
  24. grafanaVersion = version
  25. tr := &http.Transport{
  26. Proxy: http.ProxyFromEnvironment,
  27. DialContext: (&net.Dialer{
  28. Timeout: 30 * time.Second,
  29. KeepAlive: 30 * time.Second,
  30. }).DialContext,
  31. MaxIdleConns: 100,
  32. IdleConnTimeout: 90 * time.Second,
  33. TLSHandshakeTimeout: 10 * time.Second,
  34. ExpectContinueTimeout: 1 * time.Second,
  35. TLSClientConfig: &tls.Config{
  36. InsecureSkipVerify: skipTLSVerify,
  37. },
  38. }
  39. HttpClient = http.Client{
  40. Timeout: 10 * time.Second,
  41. Transport: tr,
  42. }
  43. }
  44. func ListAllPlugins(repoUrl string) (m.PluginRepo, error) {
  45. body, err := sendRequest(repoUrl, "repo")
  46. if err != nil {
  47. logger.Info("Failed to send request", "error", err)
  48. return m.PluginRepo{}, fmt.Errorf("Failed to send request. error: %v", err)
  49. }
  50. var data m.PluginRepo
  51. err = json.Unmarshal(body, &data)
  52. if err != nil {
  53. logger.Info("Failed to unmarshal plugin repo response error:", 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. logger.Debugf("getting plugin metadata from: %v pluginId: %v \n", repoUrl, pluginId)
  102. body, err := sendRequest(repoUrl, "repo", pluginId)
  103. if err != nil {
  104. logger.Info("Failed to send request: ", err)
  105. if err == ErrNotFoundError {
  106. return m.Plugin{}, fmt.Errorf("Failed to find requested plugin, check if the plugin_id is correct. error: %v", err)
  107. }
  108. return m.Plugin{}, fmt.Errorf("Failed to send request. error: %v", err)
  109. }
  110. var data m.Plugin
  111. err = json.Unmarshal(body, &data)
  112. if err != nil {
  113. logger.Info("Failed to unmarshal plugin repo response error:", err)
  114. return m.Plugin{}, err
  115. }
  116. return data, nil
  117. }
  118. func sendRequest(repoUrl string, subPaths ...string) ([]byte, error) {
  119. u, _ := url.Parse(repoUrl)
  120. for _, v := range subPaths {
  121. u.Path = path.Join(u.Path, v)
  122. }
  123. req, err := http.NewRequest(http.MethodGet, u.String(), nil)
  124. req.Header.Set("grafana-version", grafanaVersion)
  125. req.Header.Set("grafana-os", runtime.GOOS)
  126. req.Header.Set("grafana-arch", runtime.GOARCH)
  127. req.Header.Set("User-Agent", "grafana "+grafanaVersion)
  128. if err != nil {
  129. return []byte{}, err
  130. }
  131. res, err := HttpClient.Do(req)
  132. if err != nil {
  133. return []byte{}, err
  134. }
  135. if res.StatusCode == 404 {
  136. return []byte{}, ErrNotFoundError
  137. }
  138. if res.StatusCode/100 != 2 {
  139. return []byte{}, fmt.Errorf("Api returned invalid status: %s", res.Status)
  140. }
  141. body, err := ioutil.ReadAll(res.Body)
  142. defer res.Body.Close()
  143. return body, err
  144. }