Skip to content

Commit

Permalink
Fix chromium path for puppeteer (opendistro-for-elasticsearch#232)
Browse files Browse the repository at this point in the history
* Fix chromium path

* Add more possible paths

* Add error message

* Use plugin id constant
  • Loading branch information
joshuali925 authored and zhongnansu committed Dec 7, 2020
1 parent 708ab6f commit 22faf45
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ describe('test create visual report', () => {
const { dataUrl, fileName } = await createVisualReport(
reportParams as ReportParamsSchemaType,
queryUrl,
mockLogger,
undefined,
'./.chromium/headless_shell'
mockLogger
);
expect(fileName).toContain(`${reportParams.report_name}`);
expect(fileName).toContain('.png');
Expand All @@ -91,9 +89,7 @@ describe('test create visual report', () => {
const { dataUrl, fileName } = await createVisualReport(
reportParams as ReportParamsSchemaType,
queryUrl,
mockLogger,
undefined,
'./.chromium/headless_shell'
mockLogger
);
expect(fileName).toContain(`${reportParams.report_name}`);
expect(fileName).toContain('.pdf');
Expand Down
11 changes: 9 additions & 2 deletions kibana-reports/server/routes/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* permissions and limitations under the License.
*/

import { PLUGIN_ID } from '../../../common';

export enum FORMAT {
pdf = 'pdf',
png = 'png',
Expand Down Expand Up @@ -78,5 +80,10 @@ export const DEFAULT_REPORT_HEADER = '<h1>Open Distro Kibana Reports</h1>';

export const SECURITY_AUTH_COOKIE_NAME = 'security_authentication';

export const CHROMIUM_PATH =
'./plugins/opendistroReportsKibana/.chromium/headless_shell';
export const CHROMIUM_PATHS = [
`./plugins/${PLUGIN_ID}/.chromium/headless_shell`,
'./plugins/kibana-reports/.chromium/headless_shell',
'./.chromium/headless_shell',
`../plugins/${PLUGIN_ID}/.chromium/headless_shell`,
'../plugins/kibana-reports/.chromium/headless_shell',
];
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
REPORT_TYPE,
FORMAT,
SELECTOR,
CHROMIUM_PATH,
CHROMIUM_PATHS,
} from '../constants';
import { getFileName } from '../helpers';
import { CreateReportResultType } from '../types';
Expand All @@ -34,8 +34,7 @@ export const createVisualReport = async (
reportParams: ReportParamsSchemaType,
queryUrl: string,
logger: Logger,
cookie?: SetCookie,
chromiumPath = CHROMIUM_PATH
cookie?: SetCookie
): Promise<CreateReportResultType> => {
const {
core_params,
Expand All @@ -51,6 +50,16 @@ export const createVisualReport = async (
report_format: reportFormat,
} = coreParams;

const getChromiumPath = () => {
const path = CHROMIUM_PATHS.find((path) => {
try {
return fs.existsSync(path);
} catch (error) {}
});
if (path) return path;
logger.error('cannot find headless chromium for puppeteer');
};

// TODO: polish default header, maybe add a logo, depends on UX design
const window = new JSDOM('').window;
const DOMPurify = createDOMPurify(window);
Expand All @@ -68,7 +77,7 @@ export const createVisualReport = async (
* https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#setting-up-chrome-linux-sandbox
*/
args: ['--no-sandbox', '--disable-setuid-sandbox'],
executablePath: chromiumPath,
executablePath: getChromiumPath(),
});
const page = await browser.newPage();
page.setDefaultNavigationTimeout(0);
Expand Down

0 comments on commit 22faf45

Please sign in to comment.