This repository has been archived by the owner on May 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
/
build-one-app-docker-setup.js
190 lines (163 loc) · 7.63 KB
/
build-one-app-docker-setup.js
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
#!/usr/bin/env node
/*
* Copyright 2019 American Express Travel Related Services Company, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
const { readFileSync, existsSync, promises: fs } = require('node:fs');
const path = require('node:path');
const { execSync } = require('node:child_process');
const { extract } = require('tar');
const {
nginxOriginStaticsRootDir,
promisifySpawn,
sanitizeEnvVars,
sampleProdDir,
} = require('./utils');
const nginxOriginStaticsAppDir = path.resolve(nginxOriginStaticsRootDir, 'app');
const userIntendsToSkipOneAppImageBuild = process.env.ONE_DANGEROUSLY_SKIP_ONE_APP_IMAGE_BUILD;
const userIntendsToSkipApiImagesBuild = process.env.ONE_DANGEROUSLY_SKIP_API_IMAGES_BUILD;
const nodeVersion = readFileSync('.nvmrc', { encoding: 'utf8' }).trim();
const sanitizedEnvVars = sanitizeEnvVars();
const buildApiImages = async () => {
console.time('Docker API Images Build');
console.log('🛠 Building fast-api, slow-api, and extra-slow-api Docker images');
try {
await promisifySpawn(`docker compose build --build-arg VERSION=${nodeVersion} --no-cache --parallel fast-api slow-api extra-slow-api`, { shell: true, cwd: sampleProdDir, env: { ...sanitizedEnvVars } });
} catch (error) {
console.log('🚨 Docker API Images could not be built!\n');
throw error;
}
console.log('✅ Docker API images built\n');
console.timeEnd('Docker API Images Build');
};
const buildOneAppImage = async () => {
console.time('Docker Build One App Image');
console.log('🛠 Building one-app Docker image');
try {
await promisifySpawn(`docker compose build --build-arg VERSION=${nodeVersion} --no-cache one-app`, { shell: true, cwd: sampleProdDir, env: { ...sanitizedEnvVars } });
} catch (error) {
console.log('🚨 Docker could not build the one-app image!\n');
throw error;
}
console.log('✅ Docker built One App Image \n');
console.timeEnd('Docker Build One App Image');
};
const generateCertsFor = async (container, commonName) => {
console.log(`🛠 Generating ${container} certs`);
try {
await promisifySpawn(`sh generate-certs.sh ${commonName} ${container}`, { shell: true, cwd: sampleProdDir, env: { ...sanitizedEnvVars } });
} catch (error) {
console.log(`🚨 ${container} certs could not be generated\n`);
throw error;
}
console.log(`✅ ${container} certs created! \n`);
};
const buildExtraCerts = async () => {
console.log('🛠 Building extra-certs.pem');
try {
const extraCerts = await Promise.all([
fs.readFile(path.join(sampleProdDir, 'nginx', 'nginx-cert.pem'), 'utf8'),
fs.readFile(path.join(sampleProdDir, 'api', 'api-cert.pem'), 'utf8'),
fs.readFile(path.join(sampleProdDir, 'one-app', 'one-app-cert.pem'), 'utf8'),
]);
await fs.writeFile(path.join(sampleProdDir, 'extra-certs.pem'), extraCerts.join('\n'));
} catch (error) {
console.log('🚨 extra-certs.pem could not be built\n');
throw error;
}
console.log('✅ extra-certs.pem built! \n');
};
const collectOneAppStaticFiles = async () => {
console.log('🛠 Collecting One App static files from Docker image');
try {
// clean up any existing static assets tar files
const filesInDirectory = await fs.readdir(process.cwd());
const oldStaticAssetsArchives = filesInDirectory.filter((fileName) => fileName.match(/^one-app_.+_static-assets\.tgz$/g));
oldStaticAssetsArchives.forEach(async (file) => fs.rm(file));
await promisifySpawn('node ./scripts/build-static-assets-artifact.js one-app:at-test', { shell: true, env: { ...sanitizedEnvVars } });
const filesInDirectoryAfterCleanup = await fs.readdir(process.cwd());
const staticAssetsArchiveFilename = filesInDirectoryAfterCleanup.find((fileName) => fileName.match(/^one-app_.+_static-assets\.tgz$/));
await extract({ cwd: path.resolve(nginxOriginStaticsRootDir, 'app'), file: staticAssetsArchiveFilename });
} catch (error) {
console.log('🚨 One App Statics could not be pulled from Docker image and moved to Nginx origin dir.\n');
throw error;
}
console.log('✅ One App Statics successfully pulled from Docker image and moved to Nginx origin dir! \n');
};
const handleCertGeneration = async ({ skipApiImagesBuild, skipOneAppImageBuild }) => {
try {
await fs.access(path.join(sampleProdDir, 'extra-certs.pem'));
await fs.unlink(path.join(sampleProdDir, 'extra-certs.pem'));
console.log('✅ Removed old extra-certs.pem');
} catch {
// swallow error
}
await generateCertsFor('nginx', 'sample-cdn.frank');
const oneCertExists = existsSync(path.join(sampleProdDir, 'one-app', 'one-app-cert.pem'));
if (!skipOneAppImageBuild || !oneCertExists) {
await generateCertsFor('one-app', 'localhost');
}
const apiCertExists = existsSync(path.join(sampleProdDir, 'api', 'api-cert.pem'));
if (!skipApiImagesBuild || !apiCertExists) { await generateCertsFor('api', '*.api.frank'); }
await buildExtraCerts();
};
const prodSampleApiImagesAlreadyBuilt = () => execSync('docker images prod-sample-fast-api', { encoding: 'utf8' }).includes('prod-sample-fast-api')
&& execSync('docker images prod-sample-slow-api', { encoding: 'utf8' }).includes('prod-sample-slow-api')
&& execSync('docker images prod-sample-extra-slow-api', { encoding: 'utf8' }).includes('prod-sample-extra-slow-api');
const doWork = async () => {
const oneAppImageAlreadyBuilt = execSync('docker images one-app:at-test', { encoding: 'utf8' }).includes('at-test');
const skipOneAppImageBuild = userIntendsToSkipOneAppImageBuild && oneAppImageAlreadyBuilt;
const skipApiImagesBuild = userIntendsToSkipApiImagesBuild && prodSampleApiImagesAlreadyBuilt();
// clear cdn of statics.
await fs.rm(nginxOriginStaticsAppDir, { recursive: true, force: true });
await fs.mkdir(nginxOriginStaticsAppDir, { recursive: true });
await Promise.all(['traces.jsonl', 'logs.jsonl'].map(async (file) => {
const filePath = path.resolve(sampleProdDir, 'otel-collector', 'tmp', file);
await fs.mkdir(path.dirname(filePath), { recursive: true });
await fs.writeFile(filePath, '');
await fs.chmod(filePath, 0o666);
}));
await handleCertGeneration({
skipApiImagesBuild,
skipOneAppImageBuild,
});
if (userIntendsToSkipOneAppImageBuild && !oneAppImageAlreadyBuilt) {
console.warn(
'⚠️ Building One App Docker image despite the "ONE_DANGEROUSLY_SKIP_ONE_APP_IMAGE_BUILD"'
+ 'environment variable being set since no pre-built One App Docker image was found.'
);
}
if (!skipOneAppImageBuild) {
await buildOneAppImage();
} else {
console.warn(
'⚠️ Skipping One App Docker image build since the "ONE_DANGEROUSLY_SKIP_ONE_APP_IMAGE_BUILD"'
+ 'environment variable is set.\n\nNote that your tests **may** be running against an out of date '
+ 'version of One App that does not reflect changes you have made to the source code.'
);
}
if (!skipApiImagesBuild) {
await buildApiImages();
} else {
console.warn(
'⚠️ Skipping API Docker images build since the "ONE_DANGEROUSLY_SKIP_API_IMAGES_BUILD"'
+ 'environment variable is set.'
);
}
await collectOneAppStaticFiles();
};
doWork().catch((e) => {
console.error(e);
process.exit(1);
});