phantomjs.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.domain),
  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, cancel := context.WithTimeout(ctx, opts.Timeout+time.Second*2)
  51. defer cancel()
  52. cmd := exec.CommandContext(commandCtx, binPath, cmdArgs...)
  53. cmd.Stderr = cmd.Stdout
  54. if opts.Timezone != "" {
  55. baseEnviron := os.Environ()
  56. cmd.Env = appendEnviron(baseEnviron, "TZ", isoTimeOffsetToPosixTz(opts.Timezone))
  57. }
  58. out, err := cmd.Output()
  59. // check for timeout first
  60. if commandCtx.Err() == context.DeadlineExceeded {
  61. rs.log.Info("Rendering timed out")
  62. return nil, ErrTimeout
  63. }
  64. if err != nil {
  65. rs.log.Error("Phantomjs exited with non zero exit code", "error", err)
  66. return nil, err
  67. }
  68. rs.log.Debug("Phantomjs output", "out", string(out))
  69. rs.log.Debug("Image rendered", "path", pngPath)
  70. return &RenderResult{FilePath: pngPath}, nil
  71. }
  72. func isoTimeOffsetToPosixTz(isoOffset string) string {
  73. // invert offset
  74. if strings.HasPrefix(isoOffset, "UTC+") {
  75. return strings.Replace(isoOffset, "UTC+", "UTC-", 1)
  76. }
  77. if strings.HasPrefix(isoOffset, "UTC-") {
  78. return strings.Replace(isoOffset, "UTC-", "UTC+", 1)
  79. }
  80. return isoOffset
  81. }
  82. func appendEnviron(baseEnviron []string, name string, value string) []string {
  83. results := make([]string, 0)
  84. prefix := fmt.Sprintf("%s=", name)
  85. for _, v := range baseEnviron {
  86. if !strings.HasPrefix(v, prefix) {
  87. results = append(results, v)
  88. }
  89. }
  90. return append(results, fmt.Sprintf("%s=%s", name, value))
  91. }