services.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package services
  2. import (
  3. "crypto/tls"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "net"
  8. "net/http"
  9. "path"
  10. "time"
  11. "github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
  12. m "github.com/grafana/grafana/pkg/cmd/grafana-cli/models"
  13. )
  14. var (
  15. IoHelper m.IoUtil = IoUtilImp{}
  16. HttpClient http.Client
  17. HttpClientNoTimeout http.Client
  18. grafanaVersion string
  19. ErrNotFoundError = errors.New("404 not found error")
  20. )
  21. type BadRequestError struct {
  22. Message string
  23. Status string
  24. }
  25. func (e *BadRequestError) Error() string {
  26. if len(e.Message) > 0 {
  27. return fmt.Sprintf("%s: %s", e.Status, e.Message)
  28. }
  29. return e.Status
  30. }
  31. func Init(version string, skipTLSVerify bool) {
  32. grafanaVersion = version
  33. HttpClient = makeHttpClient(skipTLSVerify, 10*time.Second)
  34. HttpClientNoTimeout = makeHttpClient(skipTLSVerify, 0)
  35. }
  36. func makeHttpClient(skipTLSVerify bool, timeout time.Duration) http.Client {
  37. tr := &http.Transport{
  38. Proxy: http.ProxyFromEnvironment,
  39. DialContext: (&net.Dialer{
  40. Timeout: 30 * time.Second,
  41. KeepAlive: 30 * time.Second,
  42. }).DialContext,
  43. MaxIdleConns: 100,
  44. IdleConnTimeout: 90 * time.Second,
  45. TLSHandshakeTimeout: 10 * time.Second,
  46. ExpectContinueTimeout: 1 * time.Second,
  47. TLSClientConfig: &tls.Config{
  48. InsecureSkipVerify: skipTLSVerify,
  49. },
  50. }
  51. return http.Client{
  52. Timeout: timeout,
  53. Transport: tr,
  54. }
  55. }
  56. func ReadPlugin(pluginDir, pluginName string) (m.InstalledPlugin, error) {
  57. distPluginDataPath := path.Join(pluginDir, pluginName, "dist", "plugin.json")
  58. var data []byte
  59. var err error
  60. data, err = IoHelper.ReadFile(distPluginDataPath)
  61. if err != nil {
  62. pluginDataPath := path.Join(pluginDir, pluginName, "plugin.json")
  63. data, err = IoHelper.ReadFile(pluginDataPath)
  64. if err != nil {
  65. return m.InstalledPlugin{}, errors.New("Could not find dist/plugin.json or plugin.json on " + pluginName + " in " + pluginDir)
  66. }
  67. }
  68. res := m.InstalledPlugin{}
  69. json.Unmarshal(data, &res)
  70. if res.Info.Version == "" {
  71. res.Info.Version = "0.0.0"
  72. }
  73. if res.Id == "" {
  74. return m.InstalledPlugin{}, errors.New("could not find plugin " + pluginName + " in " + pluginDir)
  75. }
  76. return res, nil
  77. }
  78. func GetLocalPlugins(pluginDir string) []m.InstalledPlugin {
  79. result := make([]m.InstalledPlugin, 0)
  80. files, _ := IoHelper.ReadDir(pluginDir)
  81. for _, f := range files {
  82. res, err := ReadPlugin(pluginDir, f.Name())
  83. if err == nil {
  84. result = append(result, res)
  85. }
  86. }
  87. return result
  88. }
  89. func RemoveInstalledPlugin(pluginPath, pluginName string) error {
  90. logger.Infof("Removing plugin: %v\n", pluginName)
  91. pluginDir := path.Join(pluginPath, pluginName)
  92. _, err := IoHelper.Stat(pluginDir)
  93. if err != nil {
  94. return err
  95. }
  96. return IoHelper.RemoveAll(pluginDir)
  97. }