phantomjs.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. if _, err := os.Stat(binPath); os.IsNotExist(err) {
  23. rs.log.Error("executable not found", "executable", binPath)
  24. return nil, ErrPhantomJSNotInstalled
  25. }
  26. scriptPath, _ := filepath.Abs(filepath.Join(rs.Cfg.PhantomDir, "render.js"))
  27. pngPath := rs.getFilePathForNewImage()
  28. renderKey := middleware.AddRenderAuthKey(opts.OrgId, opts.UserId, opts.OrgRole)
  29. defer middleware.RemoveRenderAuthKey(renderKey)
  30. phantomDebugArg := "--debug=false"
  31. if log.GetLogLevelFor("renderer") >= log.LvlDebug {
  32. phantomDebugArg = "--debug=true"
  33. }
  34. cmdArgs := []string{
  35. "--ignore-ssl-errors=true",
  36. "--web-security=false",
  37. phantomDebugArg,
  38. scriptPath,
  39. fmt.Sprintf("url=%v", url),
  40. fmt.Sprintf("width=%v", opts.Width),
  41. fmt.Sprintf("height=%v", opts.Height),
  42. fmt.Sprintf("png=%v", pngPath),
  43. fmt.Sprintf("domain=%v", rs.getLocalDomain()),
  44. fmt.Sprintf("timeout=%v", opts.Timeout.Seconds()),
  45. fmt.Sprintf("renderKey=%v", renderKey),
  46. }
  47. if opts.Encoding != "" {
  48. cmdArgs = append([]string{fmt.Sprintf("--output-encoding=%s", opts.Encoding)}, cmdArgs...)
  49. }
  50. commandCtx, _ := context.WithTimeout(ctx, opts.Timeout+time.Second*2)
  51. cmd := exec.CommandContext(commandCtx, binPath, cmdArgs...)
  52. cmd.Stderr = cmd.Stdout
  53. if opts.Timezone != "" {
  54. baseEnviron := os.Environ()
  55. cmd.Env = appendEnviron(baseEnviron, "TZ", isoTimeOffsetToPosixTz(opts.Timezone))
  56. }
  57. out, err := cmd.Output()
  58. // check for timeout first
  59. if commandCtx.Err() == context.DeadlineExceeded {
  60. rs.log.Info("Rendering timed out")
  61. return nil, ErrTimeout
  62. }
  63. if err != nil {
  64. rs.log.Error("Phantomjs exited with non zero exit code", "error", err)
  65. return nil, err
  66. }
  67. rs.log.Debug("Phantomjs output", "out", string(out))
  68. rs.log.Debug("Image rendered", "path", pngPath)
  69. return &RenderResult{FilePath: pngPath}, nil
  70. }
  71. func isoTimeOffsetToPosixTz(isoOffset string) string {
  72. // invert offset
  73. if strings.HasPrefix(isoOffset, "UTC+") {
  74. return strings.Replace(isoOffset, "UTC+", "UTC-", 1)
  75. }
  76. if strings.HasPrefix(isoOffset, "UTC-") {
  77. return strings.Replace(isoOffset, "UTC-", "UTC+", 1)
  78. }
  79. return isoOffset
  80. }
  81. func appendEnviron(baseEnviron []string, name string, value string) []string {
  82. results := make([]string, 0)
  83. prefix := fmt.Sprintf("%s=", name)
  84. for _, v := range baseEnviron {
  85. if !strings.HasPrefix(v, prefix) {
  86. results = append(results, v)
  87. }
  88. }
  89. return append(results, fmt.Sprintf("%s=%s", name, value))
  90. }