11import * as path from 'path' ;
2- import * as puppeteer from 'puppeteer' ;
32import * as _ from 'lodash' ;
3+ import * as fs from 'fs' ;
4+ import { Browser , computeExecutablePath } from '@puppeteer/browsers' ;
45import { RenderGRPCPluginV2 } from './plugin/v2/grpc_plugin' ;
56import { HttpServer } from './service/http-server' ;
67import { ConsoleLogger , PluginLogger } from './logger' ;
@@ -9,8 +10,6 @@ import { defaultPluginConfig, defaultServiceConfig, readJSONFileSync, PluginConf
910import { serve } from './node-plugin' ;
1011import { createSanitizer } from './sanitizer/Sanitizer' ;
1112
12- const chromeFolderPrefix = 'chromium' ;
13-
1413async function main ( ) {
1514 const argv = minimist ( process . argv . slice ( 2 ) ) ;
1615 const env = Object . assign ( { } , process . env ) ;
@@ -21,20 +20,16 @@ async function main() {
2120 const config : PluginConfig = defaultPluginConfig ;
2221 populatePluginConfigFromEnv ( config , env ) ;
2322 if ( ! config . rendering . chromeBin && ( process as any ) . pkg ) {
24- //@ts -ignore
25- const executablePath = puppeteer . executablePath ( ) as string ;
26-
27- if ( executablePath . includes ( chromeFolderPrefix ) ) {
28- const parts = executablePath . split ( path . sep ) ;
29- while ( ! parts [ 0 ] . startsWith ( chromeFolderPrefix ) ) {
30- parts . shift ( ) ;
31- }
32-
33- config . rendering . chromeBin = [ path . dirname ( process . execPath ) , ...parts ] . join ( path . sep ) ;
34- } else {
35- // local chrome installation in dev mode
36- config . rendering . chromeBin = env [ 'PUPPETEER_EXECUTABLE_PATH' ] ;
37- }
23+ const execPath = path . dirname ( process . execPath ) ;
24+ const chromeInfoFile = fs . readFileSync ( path . resolve ( execPath , 'chrome-info.json' ) , 'utf8' ) ;
25+ const chromeInfo = JSON . parse ( chromeInfoFile ) ;
26+
27+ config . rendering . chromeBin = computeExecutablePath ( {
28+ cacheDir : path . dirname ( process . execPath ) ,
29+ browser : Browser . CHROME ,
30+ buildId : chromeInfo . buildId ,
31+ } ) ;
32+ logger . debug ( `Setting chromeBin to ${ config . rendering . chromeBin } ` ) ;
3833 }
3934
4035 await serve ( {
@@ -78,7 +73,13 @@ async function main() {
7873}
7974
8075main ( ) . catch ( ( err ) => {
81- console . error ( err ) ;
76+ const errorLog = {
77+ '@level' : 'error' ,
78+ '@message' : 'failed to start grafana-image-renderer' ,
79+ 'error' : err . message ,
80+ 'trace' : err . stack ,
81+ }
82+ console . error ( JSON . stringify ( errorLog ) ) ;
8283 process . exit ( 1 ) ;
8384} ) ;
8485
@@ -91,6 +92,10 @@ function populatePluginConfigFromEnv(config: PluginConfig, env: NodeJS.ProcessEn
9192 if ( env [ 'GF_PLUGIN_GRPC_PORT' ] ) {
9293 config . plugin . grpc . port = parseInt ( env [ 'GF_PLUGIN_GRPC_PORT' ] as string , 10 ) ;
9394 }
95+
96+ if ( env [ 'GF_PLUGIN_RENDERING_CHROME_BIN' ] ) {
97+ config . rendering . chromeBin = env [ 'GF_PLUGIN_RENDERING_CHROME_BIN' ] ;
98+ }
9499}
95100
96101function populateServiceConfigFromEnv ( config : ServiceConfig , env : NodeJS . ProcessEnv ) {
0 commit comments