phantomjs.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/infra/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("rendering") >= log.LvlDebug {
  32. phantomDebugArg = "--debug=true"
  33. }
  34. cmdArgs := []string{
  35. "--ignore-ssl-errors=true",
  36. "--web-security=true",
  37. "--local-url-access=false",
  38. phantomDebugArg,
  39. scriptPath,
  40. fmt.Sprintf("url=%v", url),
  41. fmt.Sprintf("width=%v", opts.Width),
  42. fmt.Sprintf("height=%v", opts.Height),
  43. fmt.Sprintf("png=%v", pngPath),
  44. fmt.Sprintf("domain=%v", rs.domain),
  45. fmt.Sprintf("timeout=%v", opts.Timeout.Seconds()),
  46. fmt.Sprintf("renderKey=%v", renderKey),
  47. }
  48. if opts.Encoding != "" {
  49. cmdArgs = append([]string{fmt.Sprintf("--output-encoding=%s", opts.Encoding)}, cmdArgs...)
  50. }
  51. commandCtx, cancel := context.WithTimeout(ctx, opts.Timeout+time.Second*2)
  52. defer cancel()
  53. cmd := exec.CommandContext(commandCtx, binPath, cmdArgs...)
  54. cmd.Stderr = cmd.Stdout
  55. timezone := ""
  56. if opts.Timezone != "" {
  57. timezone = isoTimeOffsetToPosixTz(opts.Timezone)
  58. baseEnviron := os.Environ()
  59. cmd.Env = appendEnviron(baseEnviron, "TZ", timezone)
  60. }
  61. rs.log.Debug("executing Phantomjs", "binPath", binPath, "cmdArgs", cmdArgs, "timezone", timezone)
  62. out, err := cmd.Output()
  63. if out != nil {
  64. rs.log.Debug("Phantomjs output", "out", string(out))
  65. }
  66. if err != nil {
  67. rs.log.Debug("Phantomjs error", "error", err)
  68. }
  69. // check for timeout first
  70. if commandCtx.Err() == context.DeadlineExceeded {
  71. rs.log.Info("Rendering timed out")
  72. return nil, ErrTimeout
  73. }
  74. if err != nil {
  75. rs.log.Error("Phantomjs exited with non zero exit code", "error", err)
  76. return nil, err
  77. }
  78. rs.log.Debug("Image rendered", "path", pngPath)
  79. return &RenderResult{FilePath: pngPath}, nil
  80. }
  81. func isoTimeOffsetToPosixTz(isoOffset string) string {
  82. // invert offset
  83. if strings.HasPrefix(isoOffset, "UTC+") {
  84. return strings.Replace(isoOffset, "UTC+", "UTC-", 1)
  85. }
  86. if strings.HasPrefix(isoOffset, "UTC-") {
  87. return strings.Replace(isoOffset, "UTC-", "UTC+", 1)
  88. }
  89. return isoOffset
  90. }
  91. func appendEnviron(baseEnviron []string, name string, value string) []string {
  92. results := make([]string, 0)
  93. prefix := fmt.Sprintf("%s=", name)
  94. for _, v := range baseEnviron {
  95. if !strings.HasPrefix(v, prefix) {
  96. results = append(results, v)
  97. }
  98. }
  99. return append(results, fmt.Sprintf("%s=%s", name, value))
  100. }