services.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. )
  22. func Init(version string, skipTLSVerify bool) {
  23. grafanaVersion = version
  24. tr := &http.Transport{
  25. Proxy: http.ProxyFromEnvironment,
  26. DialContext: (&net.Dialer{
  27. Timeout: 30 * time.Second,
  28. KeepAlive: 30 * time.Second,
  29. DualStack: true,
  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. if err != nil {
  51. return m.PluginRepo{}, err
  52. }
  53. var data m.PluginRepo
  54. err = json.Unmarshal(body, &data)
  55. if err != nil {
  56. logger.Info("Failed to unmarshal plugin repo response error:", err)
  57. return m.PluginRepo{}, err
  58. }
  59. return data, nil
  60. }
  61. func ReadPlugin(pluginDir, pluginName string) (m.InstalledPlugin, error) {
  62. distPluginDataPath := path.Join(pluginDir, pluginName, "dist", "plugin.json")
  63. var data []byte
  64. var err error
  65. data, err = IoHelper.ReadFile(distPluginDataPath)
  66. if err != nil {
  67. pluginDataPath := path.Join(pluginDir, pluginName, "plugin.json")
  68. data, err = IoHelper.ReadFile(pluginDataPath)
  69. if err != nil {
  70. return m.InstalledPlugin{}, errors.New("Could not find dist/plugin.json or plugin.json on " + pluginName + " in " + pluginDir)
  71. }
  72. }
  73. res := m.InstalledPlugin{}
  74. json.Unmarshal(data, &res)
  75. if res.Info.Version == "" {
  76. res.Info.Version = "0.0.0"
  77. }
  78. if res.Id == "" {
  79. return m.InstalledPlugin{}, errors.New("could not find plugin " + pluginName + " in " + pluginDir)
  80. }
  81. return res, nil
  82. }
  83. func GetLocalPlugins(pluginDir string) []m.InstalledPlugin {
  84. result := make([]m.InstalledPlugin, 0)
  85. files, _ := IoHelper.ReadDir(pluginDir)
  86. for _, f := range files {
  87. res, err := ReadPlugin(pluginDir, f.Name())
  88. if err == nil {
  89. result = append(result, res)
  90. }
  91. }
  92. return result
  93. }
  94. func RemoveInstalledPlugin(pluginPath, pluginName string) error {
  95. logger.Infof("Removing plugin: %v\n", pluginName)
  96. pluginDir := path.Join(pluginPath, pluginName)
  97. _, err := IoHelper.Stat(pluginDir)
  98. if err != nil {
  99. return err
  100. }
  101. return IoHelper.RemoveAll(pluginDir)
  102. }
  103. func GetPlugin(pluginId, repoUrl string) (m.Plugin, error) {
  104. body, err := sendRequest(repoUrl, "repo", pluginId)
  105. if err != nil {
  106. logger.Info("Failed to send request", "error", err)
  107. return m.Plugin{}, fmt.Errorf("Failed to send request. error: %v", err)
  108. }
  109. if err != nil {
  110. return m.Plugin{}, err
  111. }
  112. var data m.Plugin
  113. err = json.Unmarshal(body, &data)
  114. if err != nil {
  115. logger.Info("Failed to unmarshal plugin repo response error:", err)
  116. return m.Plugin{}, err
  117. }
  118. return data, nil
  119. }
  120. func sendRequest(repoUrl string, subPaths ...string) ([]byte, error) {
  121. u, _ := url.Parse(repoUrl)
  122. for _, v := range subPaths {
  123. u.Path = path.Join(u.Path, v)
  124. }
  125. req, err := http.NewRequest(http.MethodGet, u.String(), nil)
  126. req.Header.Set("grafana-version", grafanaVersion)
  127. req.Header.Set("grafana-os", runtime.GOOS)
  128. req.Header.Set("grafana-arch", runtime.GOARCH)
  129. req.Header.Set("User-Agent", "grafana "+grafanaVersion)
  130. if err != nil {
  131. return []byte{}, err
  132. }
  133. res, err := HttpClient.Do(req)
  134. if err != nil {
  135. return []byte{}, err
  136. }
  137. if res.StatusCode/100 != 2 {
  138. return []byte{}, fmt.Errorf("Api returned invalid status: %s", res.Status)
  139. }
  140. body, err := ioutil.ReadAll(res.Body)
  141. defer res.Body.Close()
  142. return body, err
  143. }