-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Expand file tree
/
Copy pathrun.ts
More file actions
223 lines (203 loc) · 7.69 KB
/
run.ts
File metadata and controls
223 lines (203 loc) · 7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import { access, link, unlink, chmod } from 'fs';
import { resolve, basename } from 'path';
import { promisify } from 'util';
import { ToolingLog } from '@kbn/tooling-log';
import { kibanaPackageJson } from '@kbn/repo-info';
import { write, copyAll, mkdirp, exec, Config, Build } from '../../../lib';
import * as dockerTemplates from './templates';
import { TemplateContext } from './template_context';
import { bundleDockerFiles } from './bundle_dockerfiles';
const accessAsync = promisify(access);
const linkAsync = promisify(link);
const unlinkAsync = promisify(unlink);
const chmodAsync = promisify(chmod);
export async function runDockerGenerator(
config: Config,
log: ToolingLog,
build: Build,
flags: {
architecture?: string;
baseImage: 'none' | 'wolfi' | 'ubi';
context: boolean;
image: boolean;
ironbank?: boolean;
cloud?: boolean;
serverless?: boolean;
dockerBuildDate?: string;
fips?: boolean;
}
) {
let baseImageName = '';
if (flags.baseImage === 'ubi') baseImageName = 'redhat/ubi9-minimal:latest';
/**
* Renovate config contains a regex manager to automatically update both Chainguard references
*
* If this logic moves to another file or under another name, then the Renovate regex manager
* for automatic Chainguard updates will break.
*/
if (flags.baseImage === 'wolfi')
baseImageName =
'docker.elastic.co/wolfi/chainguard-base:latest@sha256:792313d45d114da11e169252586f76a79ff20248c7af4283f11b94bff7f1aed7';
let imageFlavor = '';
if (flags.baseImage === 'wolfi' && !flags.serverless && !flags.cloud) imageFlavor += `-wolfi`;
if (flags.ironbank) imageFlavor += '-ironbank';
if (flags.cloud) imageFlavor += '-cloud';
if (flags.serverless) imageFlavor += '-serverless';
if (flags.fips) {
imageFlavor += '-fips';
baseImageName =
'docker.elastic.co/wolfi/chainguard-base-fips:latest@sha256:cbdc968f357706186e2b83ea1fea92382f5d51842e867a662541b67c28f4e809';
}
// General docker var config
const license = 'Elastic License';
const configuredNamespace = config.getDockerNamespace();
const imageNamespace = configuredNamespace
? configuredNamespace
: flags.cloud || flags.serverless
? 'kibana-ci'
: 'kibana';
const imageTag = `docker.elastic.co/${imageNamespace}/kibana`;
const version = config.getBuildVersion();
const artifactArchitecture = flags.architecture === 'aarch64' ? 'aarch64' : 'x86_64';
let artifactVariant = '';
if (flags.serverless) artifactVariant = '-serverless';
const artifactPrefix = `kibana${artifactVariant}-${version}-linux`;
const artifactTarball = `${artifactPrefix}-${artifactArchitecture}.tar.gz`;
const beatsArchitecture = flags.architecture === 'aarch64' ? 'arm64' : 'x86_64';
const metricbeatTarball = `metricbeat${
flags.fips ? '-fips' : ''
}-${version}-linux-${beatsArchitecture}.tar.gz`;
const filebeatTarball = `filebeat${
flags.fips ? '-fips' : ''
}-${version}-linux-${beatsArchitecture}.tar.gz`;
const artifactsDir = config.resolveFromTarget('.');
const beatsDir = config.resolveFromRepo('.beats');
const dockerBuildDate = flags.dockerBuildDate || new Date().toISOString();
const dockerBuildDir = config.resolveFromRepo('build', 'kibana-docker', `default${imageFlavor}`);
const imageArchitecture = flags.architecture === 'aarch64' ? '-arm64' : '-amd64';
const dockerTargetFilename = config.resolveFromTarget(
`kibana${imageFlavor}-${version}-docker-image${imageArchitecture}.tar.gz`
);
const dependencies = [
resolve(artifactsDir, artifactTarball),
...(flags.cloud
? [resolve(beatsDir, metricbeatTarball), resolve(beatsDir, filebeatTarball)]
: []),
];
const dockerPush = config.getDockerPush();
const dockerTag = config.getDockerTag();
const dockerTagQualifier = config.getDockerTagQualfiier();
const dockerCrossCompile = config.getDockerCrossCompile();
const publicArtifactSubdomain = config.isRelease ? 'artifacts' : 'snapshots-no-kpi';
const scope: TemplateContext = {
artifactPrefix,
artifactTarball,
imageFlavor,
version,
branch: kibanaPackageJson.branch,
license,
artifactsDir,
imageTag,
dockerBuildDir,
dockerTargetFilename,
dockerPush,
dockerTag,
dockerTagQualifier,
dockerCrossCompile,
baseImageName,
dockerBuildDate,
baseImage: flags.baseImage,
cloud: flags.cloud,
serverless: flags.serverless,
metricbeatTarball,
filebeatTarball,
ironbank: flags.ironbank,
architecture: flags.architecture,
revision: config.getBuildSha(),
publicArtifactSubdomain,
fips: flags.fips,
};
type HostArchitectureToDocker = Record<string, string>;
const hostTarget: HostArchitectureToDocker = {
x64: 'x64',
arm64: 'aarch64',
};
const buildArchitectureSupported = hostTarget[process.arch] === flags.architecture;
if (flags.architecture && !buildArchitectureSupported && !dockerCrossCompile) {
return;
}
// Create the docker build target folder
await mkdirp(dockerBuildDir);
// Write all the needed docker config files
// into kibana-docker folder
for (const [, dockerTemplate] of Object.entries(dockerTemplates)) {
let filename: string;
if (!dockerTemplate.name.includes('kibana.yml')) {
filename = `${dockerTemplate.name}.${artifactArchitecture}`;
} else {
filename = dockerTemplate.name;
}
await write(resolve(dockerBuildDir, filename), dockerTemplate.generator(scope));
}
// Copy serverless-only configuration files
if (flags.serverless) {
await mkdirp(resolve(dockerBuildDir, 'config'));
await copyAll(config.resolveFromRepo('config'), resolve(dockerBuildDir, 'config'), {
select: ['serverless*.yml'],
});
}
// Copy all the needed resources into kibana-docker folder
// in order to build the docker image accordingly the dockerfile defined
// under templates/kibana_yml.template/js
await copyAll(
config.resolveFromRepo('src/dev/build/tasks/os_packages/docker_generator/resources/base'),
dockerBuildDir
);
// Copy fips related resources
if (flags.fips) {
await copyAll(
config.resolveFromRepo('src/dev/build/tasks/os_packages/docker_generator/resources/fips'),
dockerBuildDir
);
}
// Build docker image into the target folder
// In order to do this we just call the file we
// created from the templates/build_docker_sh.template.js
// and we just run that bash script
const dockerBuildScript = `build_docker.sh.${artifactArchitecture}`;
await chmodAsync(`${resolve(dockerBuildDir, dockerBuildScript)}`, '755');
// Only build images on native targets
if (flags.image) {
// Link dependencies
for (const src of dependencies) {
const file = basename(src);
const dest = resolve(dockerBuildDir, file);
try {
await accessAsync(src);
await unlinkAsync(dest);
} catch (e) {
if (e && e.code === 'ENOENT' && e.syscall === 'access') {
throw new Error(`${src} is needed in order to build the docker image.`);
}
}
await linkAsync(src, dest);
}
await exec(log, `./${dockerBuildScript}`, [], {
cwd: dockerBuildDir,
level: 'info',
build,
});
}
// Pack Dockerfiles and create a target for them
if (flags.context) {
await bundleDockerFiles(config, log, scope);
}
}