Skip to content
This repository has been archived by the owner on Sep 10, 2024. It is now read-only.

Commit

Permalink
[bazel] Setup remote cache settings on bootstrap (elastic#121445)
Browse files Browse the repository at this point in the history
  • Loading branch information
brianseeders authored Jan 18, 2022
1 parent b69487b commit e39faea
Show file tree
Hide file tree
Showing 5 changed files with 945 additions and 738 deletions.
3 changes: 3 additions & 0 deletions .bazelrc.common
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ coverage --instrument_test_targets
# Metadata settings
build --workspace_status_command="node ./src/dev/bazel_workspace_status.js"

# Load remote cache settings, if they exist
try-import %workspace%/.bazelrc.cache

# Load any settings specific to the current user.
# .bazelrc.user should appear in .gitignore so that settings are not shared with team members
# This needs to be last statement in this
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ report.asciidoc
/bazel
/bazel-*
.bazelrc.user
.bazelrc.cache

elastic-agent-*
fleet-server-*
elastic-agent.yml
fleet-server.yml
fleet-server.yml
1,581 changes: 844 additions & 737 deletions packages/kbn-pm/dist/index.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions packages/kbn-pm/src/commands/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
runBazel,
yarnIntegrityFileExists,
} from '../utils/bazel';
import { setupRemoteCache } from '../utils/bazel/setup_remote_cache';

export const BootstrapCommand: ICommand = {
description: 'Install dependencies and crosslink projects',
Expand Down Expand Up @@ -55,6 +56,9 @@ export const BootstrapCommand: ICommand = {
// Install bazel machinery tools if needed
await installBazelTools(rootPath);

// Setup remote cache settings in .bazelrc.cache if needed
await setupRemoteCache(rootPath);

// Bootstrap process for Bazel packages
// Bazel is now managing dependencies so yarn install
// will happen as part of this
Expand Down
92 changes: 92 additions & 0 deletions packages/kbn-pm/src/utils/bazel/setup_remote_cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import dedent from 'dedent';
import { existsSync, writeFileSync } from 'fs';
import { resolve } from 'path';
import { spawn } from '../child_process';
import { log } from '../log';

async function isVaultAvailable() {
try {
await spawn('vault', ['--version'], { stdio: 'pipe' });

return true;
} catch {
return false;
}
}

async function isElasticCommitter() {
try {
const { stdout: email } = await spawn('git', ['config', 'user.email'], {
stdio: 'pipe',
});

return email.trim().endsWith('@elastic.co');
} catch {
return false;
}
}

export async function setupRemoteCache(repoRootPath: string) {
// The remote cache is only for Elastic employees working locally (CI cache settings are handled elsewhere)
if (process.env.CI || !(await isElasticCommitter())) {
return;
}

log.debug(`[bazel_tools] setting up remote cache settings if necessary`);

const settingsPath = resolve(repoRootPath, '.bazelrc.cache');

if (existsSync(settingsPath)) {
log.debug(`[bazel_tools] remote cache settings already exist, skipping`);
return;
}

if (!(await isVaultAvailable())) {
log.info('[bazel_tools] vault is not available, unable to setup remote cache settings.');
log.info('[bazel_tools] building packages will work, but will be slower in many cases.');
log.info('[bazel_tools] reach out to Operations if you need assistance with this.');
return;
}

let apiKey = '';

try {
const { stdout } = await spawn(
'vault',
['read', '-field=readonly-key', 'secret/kibana-issues/dev/bazel-remote-cache'],
{
stdio: 'pipe',
}
);
apiKey = stdout.trim();
} catch (ex: unknown) {
log.info(
'[bazel_tools] unable to read bazel remote cache key from vault, are you authenticated?'
);
log.info('[bazel_tools] building packages will work, but will be slower in many cases.');
log.info('[bazel_tools] reach out to Operations if you need assistance with this.');
log.info(`[bazel_tools] ${ex}`);

return;
}

const contents = dedent`
# V1 - This file is automatically generated by 'yarn kbn bootstrap'
# To regenerate this file, delete it and run 'yarn kbn bootstrap' again.
build --bes_results_url=https://app.buildbuddy.io/invocation/
build --bes_backend=grpcs://cloud.buildbuddy.io
build --remote_cache=grpcs://cloud.buildbuddy.io
build --remote_timeout=3600
build --remote_header=${apiKey}
`;

writeFileSync(settingsPath, contents);
log.info(`[bazel_tools] remote cache settings written to ${settingsPath}`);
}

0 comments on commit e39faea

Please sign in to comment.