Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin: fix Chrome path #451

Merged
merged 8 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion scripts/download_chrome.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const { BrowserPlatform, Browser, install, resolveBuildId } = require('@puppeteer/browsers');
const childProcess = require('child_process');
const fs = require('fs')
const path = require('path');

const archArg = process.argv[2];
Expand All @@ -25,6 +27,9 @@ const outputPath = path.resolve(process.cwd(), 'dist', process.argv[3] || `plugi

const browserVersion = Browser.CHROME;

// Clean up download folder if exists
childProcess.execFileSync('rm', ['-rf', `${outputPath}/chrome`]);

async function download() {
AgnesToulet marked this conversation as resolved.
Show resolved Hide resolved
const buildId = await resolveBuildId(browserVersion, platform, 'latest');
console.log(`Installing ${browserVersion} into ${outputPath}`);
Expand All @@ -36,6 +41,9 @@ async function download() {
});
}

download().then(() => {
download().then(browser => {
console.log(`${browserVersion} downloaded into:`, outputPath);

const chromeInfo = { buildId: browser.buildId };
return fs.writeFileSync(path.resolve(outputPath, 'chrome-info.json'), JSON.stringify(chromeInfo));
});
43 changes: 25 additions & 18 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as path from 'path';
import * as puppeteer from 'puppeteer';
import * as _ from 'lodash';
import * as fs from 'fs';
import { Browser, computeExecutablePath } from '@puppeteer/browsers';
import { RenderGRPCPluginV2 } from './plugin/v2/grpc_plugin';
import { HttpServer } from './service/http-server';
import { ConsoleLogger, PluginLogger } from './logger';
Expand All @@ -9,8 +10,6 @@ import { defaultPluginConfig, defaultServiceConfig, readJSONFileSync, PluginConf
import { serve } from './node-plugin';
import { createSanitizer } from './sanitizer/Sanitizer';

const chromeFolderPrefix = 'chromium';

async function main() {
const argv = minimist(process.argv.slice(2));
const env = Object.assign({}, process.env);
Expand All @@ -21,20 +20,18 @@ async function main() {
const config: PluginConfig = defaultPluginConfig;
populatePluginConfigFromEnv(config, env);
if (!config.rendering.chromeBin && (process as any).pkg) {
//@ts-ignore
const executablePath = puppeteer.executablePath() as string;

if (executablePath.includes(chromeFolderPrefix)) {
const parts = executablePath.split(path.sep);
while (!parts[0].startsWith(chromeFolderPrefix)) {
parts.shift();
}

config.rendering.chromeBin = [path.dirname(process.execPath), ...parts].join(path.sep);
} else {
// local chrome installation in dev mode
config.rendering.chromeBin = env['PUPPETEER_EXECUTABLE_PATH'];
}
const execPath = path.dirname(process.execPath);
const chromeInfoFile = fs.readFileSync(path.resolve(execPath, 'chrome-info.json'), 'utf8');
const chromeInfo = JSON.parse(chromeInfoFile);
AgnesToulet marked this conversation as resolved.
Show resolved Hide resolved

const executablePath = computeExecutablePath({
cacheDir: path.dirname(process.execPath),
browser: Browser.CHROME,
buildId: chromeInfo.buildId,
});

config.rendering.chromeBin = executablePath;
AgnesToulet marked this conversation as resolved.
Show resolved Hide resolved
logger.debug(`Setting chromeBin to ${config.rendering.chromeBin}`);
}

await serve({
Expand Down Expand Up @@ -78,7 +75,13 @@ async function main() {
}

main().catch((err) => {
console.error(err);
const errorLog = {
'@level': 'error',
'@message': 'failed to start grafana-image-renderer',
'error': err.message,
'trace': err.stack,
}
console.error(JSON.stringify(errorLog));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error was actually logged as a "debug" log in plugin mode so that's why it was usually not displayed. This is fixing that and it should help us getting more info from user issues.

process.exit(1);
});

Expand All @@ -91,6 +94,10 @@ function populatePluginConfigFromEnv(config: PluginConfig, env: NodeJS.ProcessEn
if (env['GF_PLUGIN_GRPC_PORT']) {
config.plugin.grpc.port = parseInt(env['GF_PLUGIN_GRPC_PORT'] as string, 10);
}

if (env['GF_PLUGIN_RENDERING_CHROME_BIN']) {
config.rendering.chromeBin = env['GF_PLUGIN_RENDERING_CHROME_BIN'];
}
}

function populateServiceConfigFromEnv(config: ServiceConfig, env: NodeJS.ProcessEnv) {
Expand Down
4 changes: 0 additions & 4 deletions src/plugin/v2/grpc_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,6 @@ class PluginGRPCServer {
const populateConfigFromEnv = (config: PluginConfig) => {
const env = Object.assign({}, process.env);

if (env['GF_PLUGIN_RENDERING_CHROME_BIN']) {
config.rendering.chromeBin = env['GF_PLUGIN_RENDERING_CHROME_BIN'];
}

if (env['GF_PLUGIN_RENDERING_ARGS']) {
const args = env['GF_PLUGIN_RENDERING_ARGS'] as string;
if (args.length > 0) {
Expand Down