phantomjs.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. cmd.Env = os.Environ()
  57. if opts.Timezone != "" {
  58. timezone = isoTimeOffsetToPosixTz(opts.Timezone)
  59. cmd.Env = appendEnviron(cmd.Env, "TZ", timezone)
  60. }
  61. // Added to disable usage of newer version of OPENSSL
  62. // that seem to be incompatible with PhantomJS (used in Debian Buster)
  63. if runtime.GOOS == "linux" {
  64. disableNewOpenssl := "/etc/ssl"
  65. cmd.Env = appendEnviron(cmd.Env, "OPENSSL_CONF", disableNewOpenssl)
  66. }
  67. rs.log.Debug("executing Phantomjs", "binPath", binPath, "cmdArgs", cmdArgs, "timezone", timezone)
  68. out, err := cmd.Output()
  69. if out != nil {
  70. rs.log.Debug("Phantomjs output", "out", string(out))
  71. }
  72. if err != nil {
  73. rs.log.Debug("Phantomjs error", "error", err)
  74. }
  75. // check for timeout first
  76. if commandCtx.Err() == context.DeadlineExceeded {
  77. rs.log.Info("Rendering timed out")
  78. return nil, ErrTimeout
  79. }
  80. if err != nil {
  81. rs.log.Error("Phantomjs exited with non zero exit code", "error", err)
  82. return nil, err
  83. }
  84. rs.log.Debug("Image rendered", "path", pngPath)
  85. return &RenderResult{FilePath: pngPath}, nil
  86. }
  87. func isoTimeOffsetToPosixTz(isoOffset string) string {
  88. // invert offset
  89. if strings.HasPrefix(isoOffset, "UTC+") {
  90. return strings.Replace(isoOffset, "UTC+", "UTC-", 1)
  91. }
  92. if strings.HasPrefix(isoOffset, "UTC-") {
  93. return strings.Replace(isoOffset, "UTC-", "UTC+", 1)
  94. }
  95. return isoOffset
  96. }
  97. func appendEnviron(baseEnviron []string, name string, value string) []string {
  98. results := make([]string, 0)
  99. prefix := fmt.Sprintf("%s=", name)
  100. for _, v := range baseEnviron {
  101. if !strings.HasPrefix(v, prefix) {
  102. results = append(results, v)
  103. }
  104. }
  105. return append(results, fmt.Sprintf("%s=%s", name, value))
  106. }