phantomjs.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package rendering
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "runtime"
  9. "strings"
  10. "time"
  11. "github.com/grafana/grafana/pkg/log"
  12. "github.com/grafana/grafana/pkg/middleware"
  13. )
  14. func (rs *RenderingService) renderViaPhantomJS(ctx context.Context, opts Opts) (*RenderResult, error) {
  15. rs.log.Info("Rendering", "path", opts.Path)
  16. var executable = "phantomjs"
  17. if runtime.GOOS == "windows" {
  18. executable = executable + ".exe"
  19. }
  20. url := rs.getURL(opts.Path)
  21. binPath, _ := filepath.Abs(filepath.Join(rs.Cfg.PhantomDir, executable))
  22. scriptPath, _ := filepath.Abs(filepath.Join(rs.Cfg.PhantomDir, "render.js"))
  23. pngPath := rs.getFilePathForNewImage()
  24. renderKey := middleware.AddRenderAuthKey(opts.OrgId, opts.UserId, opts.OrgRole)
  25. defer middleware.RemoveRenderAuthKey(renderKey)
  26. phantomDebugArg := "--debug=false"
  27. if log.GetLogLevelFor("renderer") >= log.LvlDebug {
  28. phantomDebugArg = "--debug=true"
  29. }
  30. cmdArgs := []string{
  31. "--ignore-ssl-errors=true",
  32. "--web-security=false",
  33. phantomDebugArg,
  34. scriptPath,
  35. fmt.Sprintf("url=%v", url),
  36. fmt.Sprintf("width=%v", opts.Width),
  37. fmt.Sprintf("height=%v", opts.Height),
  38. fmt.Sprintf("png=%v", pngPath),
  39. fmt.Sprintf("domain=%v", rs.getLocalDomain()),
  40. fmt.Sprintf("timeout=%v", opts.Timeout.Seconds()),
  41. fmt.Sprintf("renderKey=%v", renderKey),
  42. }
  43. if opts.Encoding != "" {
  44. cmdArgs = append([]string{fmt.Sprintf("--output-encoding=%s", opts.Encoding)}, cmdArgs...)
  45. }
  46. commandCtx, _ := context.WithTimeout(ctx, opts.Timeout+time.Second*2)
  47. cmd := exec.CommandContext(commandCtx, binPath, cmdArgs...)
  48. cmd.Stderr = cmd.Stdout
  49. if opts.Timezone != "" {
  50. baseEnviron := os.Environ()
  51. cmd.Env = appendEnviron(baseEnviron, "TZ", isoTimeOffsetToPosixTz(opts.Timezone))
  52. }
  53. out, err := cmd.Output()
  54. // check for timeout first
  55. if commandCtx.Err() == context.DeadlineExceeded {
  56. rs.log.Info("Rendering timed out")
  57. return nil, ErrTimeout
  58. }
  59. if err != nil {
  60. rs.log.Error("Phantomjs exited with non zero exit code", "error", err)
  61. return nil, err
  62. }
  63. rs.log.Debug("Phantomjs output", "out", string(out))
  64. rs.log.Debug("Image rendered", "path", pngPath)
  65. return &RenderResult{FilePath: pngPath}, nil
  66. }
  67. func isoTimeOffsetToPosixTz(isoOffset string) string {
  68. // invert offset
  69. if strings.HasPrefix(isoOffset, "UTC+") {
  70. return strings.Replace(isoOffset, "UTC+", "UTC-", 1)
  71. }
  72. if strings.HasPrefix(isoOffset, "UTC-") {
  73. return strings.Replace(isoOffset, "UTC-", "UTC+", 1)
  74. }
  75. return isoOffset
  76. }
  77. func appendEnviron(baseEnviron []string, name string, value string) []string {
  78. results := make([]string, 0)
  79. prefix := fmt.Sprintf("%s=", name)
  80. for _, v := range baseEnviron {
  81. if !strings.HasPrefix(v, prefix) {
  82. results = append(results, v)
  83. }
  84. }
  85. return append(results, fmt.Sprintf("%s=%s", name, value))
  86. }