Skip to content

Commit 364ee01

Browse files
Plugin: fix Chrome path (#451)
* Plugin: fix Chrome path * fix shell commands * fix chrome file extension * fix file extension at runtime * fix exec path issue for Mac + improve error log * Update src/app.ts Co-authored-by: Alex Khomenko <[email protected]> * clean up --------- Co-authored-by: Alex Khomenko <[email protected]>
1 parent 9dd50d1 commit 364ee01

File tree

3 files changed

+28
-23
lines changed

3 files changed

+28
-23
lines changed

scripts/download_chrome.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { BrowserPlatform, Browser, install, resolveBuildId } = require('@puppeteer/browsers');
2+
const fs = require('fs')
23
const path = require('path');
34

45
const archArg = process.argv[2];
@@ -36,6 +37,9 @@ async function download() {
3637
});
3738
}
3839

39-
download().then(() => {
40+
download().then(browser => {
4041
console.log(`${browserVersion} downloaded into:`, outputPath);
42+
43+
const chromeInfo = { buildId: browser.buildId };
44+
return fs.writeFileSync(path.resolve(outputPath, 'chrome-info.json'), JSON.stringify(chromeInfo));
4145
});

src/app.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as path from 'path';
2-
import * as puppeteer from 'puppeteer';
32
import * as _ from 'lodash';
3+
import * as fs from 'fs';
4+
import { Browser, computeExecutablePath } from '@puppeteer/browsers';
45
import { RenderGRPCPluginV2 } from './plugin/v2/grpc_plugin';
56
import { HttpServer } from './service/http-server';
67
import { ConsoleLogger, PluginLogger } from './logger';
@@ -9,8 +10,6 @@ import { defaultPluginConfig, defaultServiceConfig, readJSONFileSync, PluginConf
910
import { serve } from './node-plugin';
1011
import { createSanitizer } from './sanitizer/Sanitizer';
1112

12-
const chromeFolderPrefix = 'chromium';
13-
1413
async 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

8075
main().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

96101
function populateServiceConfigFromEnv(config: ServiceConfig, env: NodeJS.ProcessEnv) {

src/plugin/v2/grpc_plugin.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,6 @@ class PluginGRPCServer {
240240
const populateConfigFromEnv = (config: PluginConfig) => {
241241
const env = Object.assign({}, process.env);
242242

243-
if (env['GF_PLUGIN_RENDERING_CHROME_BIN']) {
244-
config.rendering.chromeBin = env['GF_PLUGIN_RENDERING_CHROME_BIN'];
245-
}
246-
247243
if (env['GF_PLUGIN_RENDERING_ARGS']) {
248244
const args = env['GF_PLUGIN_RENDERING_ARGS'] as string;
249245
if (args.length > 0) {

0 commit comments

Comments
 (0)