-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdockerContainerBuilder.js
120 lines (106 loc) · 4.06 KB
/
dockerContainerBuilder.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
/**
* Build the script that builds the `oc-admin` Docker Container
* *
* Changes log:
* Tony Massé - 2022-07-25 - Adapt `installerBuilder` to bild Docker Container
* Tony Massé - 2022-08-05 - Add `LATEST_TAG` to the Docker command
* Tony Massé - 2023-01-13 - Add Grype verification command
*/
// eslint-disable-next-line import/no-extraneous-dependencies
const yargs = require('yargs');
const fs = require('fs');
const path = require('path');
const parsedArguments = yargs
.option('distDirectory', {
alias: 'd',
description: '`dist` directory',
type: 'string'
})
.option('distSubDirectory', {
alias: 's',
description: '`dist` sub-directory, including the version number',
type: 'string'
})
.option('dockerFile', {
alias: 'f',
description: 'Full path of the `Dockerfile` file',
type: 'string'
})
.option('dockerBuildScriptTemplate', {
alias: 't',
description: 'Full path to the `_docker.build-oc-admin.sh` template file',
type: 'string'
})
.help()
.alias('help', 'h')
.argv;
// Grab the version from the Package file
const { version } = require('../package.json');
// Prep directories from parameters
const distDirectory = (
parsedArguments && parsedArguments.distDirectory && parsedArguments.distDirectory.length
? parsedArguments.distDirectory
: undefined
);
const distSubDirectory = (
parsedArguments && parsedArguments.distSubDirectory && parsedArguments.distSubDirectory.length
? parsedArguments.distSubDirectory
: undefined
);
const dockerFilePath = (
parsedArguments && parsedArguments.dockerFile && parsedArguments.dockerFile.length
? parsedArguments.dockerFile
: path.join('.', 'Dockerfile')
);
const dockerBuildScriptTemplatePath = (
parsedArguments
&& parsedArguments.dockerBuildScriptTemplate
&& parsedArguments.dockerBuildScriptTemplate.length
? parsedArguments.dockerBuildScriptTemplate
: path.join('.', '_docker.build-TEMPLATE.sh')
);
// Build the name of the new Installer
const versionTag = `v${version}`;
// Check all our ducks are in a row
if (
distDirectory
&& distDirectory.length
&& distSubDirectory
&& distSubDirectory.length
&& dockerFilePath
&& dockerFilePath.length
&& versionTag
&& versionTag.length > 1
&& dockerBuildScriptTemplatePath
&& dockerBuildScriptTemplatePath.length
) {
// Run Docker to build the Container
// eslint-disable-next-line no-console
console.log('👷 Build the Container building script...');
try {
const dockerCommand = `docker build --tag "tonymasse/oc-admin:${versionTag}" --tag "tonymasse/oc-admin:$LATEST_TAG" --file "${dockerFilePath}" "${distSubDirectory}"`;
const grypeCommand = `docker run --rm --volume /var/run/docker.sock:/var/run/docker.sock anchore/grype:latest "tonymasse/oc-admin:${versionTag}" --add-cpes-if-none`;
const dockerScriptPath = path.join(distDirectory, '_docker.build-oc-admin.sh');
// Read template from disk
let dockerScriptTemplate = fs.readFileSync(dockerBuildScriptTemplatePath, { encoding: 'utf-8' }) || '';
// Replace token
dockerScriptTemplate = String(dockerScriptTemplate).replaceAll('#_DOCKER_COMMAND_GOES_HERE', dockerCommand);
dockerScriptTemplate = String(dockerScriptTemplate).replaceAll('#_GRYPE_COMMAND_GOES_HERE', grypeCommand);
// Write to disk
fs.writeFileSync(dockerScriptPath, dockerScriptTemplate);
/* eslint-disable no-console */
console.log('🟢 Docker Container building script created.');
console.log('You now want to run the following command:');
// console.log('cd /var/lib/docker/volumes/oc-admin_dev/_data/ && chmod +x _docker.build-oc-admin.sh && _docker.build-oc-admin.sh');
console.log('cd dist/ && chmod +x _docker.build-oc-admin.sh && _docker.build-oc-admin.sh');
console.log('🏁');
console.log('');
/* eslint-enable no-console */
} catch (error) {
// eslint-disable-next-line no-console
console.log('🔴 Oopsy Daisy - ', error.message);
}
} else {
// eslint-disable-next-line no-console
console.log('Some of the parameters are missing. Run with `--help` for more information.');
}