@magda/docker-utils 1.1.0-alpha.0 → 1.1.0-alpha.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +73 -1
- package/bin/create-docker-context-for-node-component.js +26 -1
- package/bin/retag-and-push.js +48 -20
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -8,4 +8,76 @@ After Adding this package `@magda/docker-utils` as the [devDependencies](https:/
|
|
|
8
8
|
|
|
9
9
|
The two scripts takes both parameters from commandline as well `package.json` key `config.docker`.
|
|
10
10
|
|
|
11
|
-
You can
|
|
11
|
+
You can run the script with `--help` switch to print all available commandline options.
|
|
12
|
+
|
|
13
|
+
You can also find examples from [magda repo](https://github.com/magda-io/magda).
|
|
14
|
+
|
|
15
|
+
### `create-docker-context-for-node-component`
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
> yarn create-docker-context-for-node-component --help
|
|
19
|
+
Options:
|
|
20
|
+
--build Pipe the Docker context straight to Docker.
|
|
21
|
+
[boolean] [default: false]
|
|
22
|
+
--tag The tag to pass to "docker build". This parameter is only
|
|
23
|
+
used if --build is specified. If the value of this
|
|
24
|
+
parameter is `auto`, a tag name is automatically created
|
|
25
|
+
from NPM configuration. [string] [default: "auto"]
|
|
26
|
+
--repository The repository to use in auto tag generation. Will default
|
|
27
|
+
to '', i.e. dockerhub unless --local is set. Requires
|
|
28
|
+
--tag=auto [string]
|
|
29
|
+
--name The package name to use in auto tag generation. Will
|
|
30
|
+
default to ''. Used to override the docker nanme config in
|
|
31
|
+
package.json during the auto tagging. Requires --tag=auto
|
|
32
|
+
[string]
|
|
33
|
+
--output The output path and filename for the Docker context .tar
|
|
34
|
+
file. [string]
|
|
35
|
+
--local Build for a local Kubernetes container registry. This
|
|
36
|
+
parameter is only used if --build is specified.
|
|
37
|
+
[boolean] [default: false]
|
|
38
|
+
--push Push the build image to the docker registry. This
|
|
39
|
+
parameter is only used if --build is specified.
|
|
40
|
+
[boolean] [default: false]
|
|
41
|
+
--platform A list of platform that the docker image build should
|
|
42
|
+
target. Specify this value will enable multi-arch image
|
|
43
|
+
build. [string]
|
|
44
|
+
--noCache Disable the cache during the docker image build.
|
|
45
|
+
[boolean] [default: false]
|
|
46
|
+
--cacheFromVersion Version to cache from when building, using the
|
|
47
|
+
--cache-from field in docker. Will use the same repository
|
|
48
|
+
and name. Using this options causes the image to be pulled
|
|
49
|
+
before build. [string]
|
|
50
|
+
--help Show help [boolean]
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### `retag-and-push`
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
> yarn retag-and-push --help
|
|
57
|
+
Options:
|
|
58
|
+
--version Show version number [boolean]
|
|
59
|
+
--config Path to JSON config file
|
|
60
|
+
--help Show help [boolean]
|
|
61
|
+
--fromPrefix The prefix of the image that will be given a new tag
|
|
62
|
+
[string] [default: ""]
|
|
63
|
+
--fromName The package name that used to generate the fromTag. Used
|
|
64
|
+
to optionally override the docker nanme config in
|
|
65
|
+
package.json during the auto tagging.
|
|
66
|
+
[string] [default: ""]
|
|
67
|
+
--fromVersion The version of the existing image that will be given a new
|
|
68
|
+
tag [string] [default: "1.1.0-alpha.0"]
|
|
69
|
+
--toPrefix The prefix for the tag to push to [string] [default: ""]
|
|
70
|
+
--toName The package name that used to generate the toTag. Used to
|
|
71
|
+
optionally override the docker nanme config in
|
|
72
|
+
package.json during the auto tagging.
|
|
73
|
+
[string] [default: ""]
|
|
74
|
+
--copyFromRegistry When `copyFromRegistry`=true,
|
|
75
|
+
[regctl](https://github.com/regclient/regclient) will be
|
|
76
|
+
used.
|
|
77
|
+
The image will be copied directly from remote registry to
|
|
78
|
+
the destination registry rather than from a local image.
|
|
79
|
+
This allows copying multi-arch image from one registry to
|
|
80
|
+
another registry. [boolean] [default: false]
|
|
81
|
+
--toVersion The version for the tag to push to
|
|
82
|
+
[string] [default: "1.1.0-alpha.0"]
|
|
83
|
+
```
|
|
@@ -65,6 +65,16 @@ const argv = yargs
|
|
|
65
65
|
type: "boolean",
|
|
66
66
|
default: false
|
|
67
67
|
},
|
|
68
|
+
platform: {
|
|
69
|
+
description:
|
|
70
|
+
"A list of platform that the docker image build should target. Specify this value will enable multi-arch image build.",
|
|
71
|
+
type: "string"
|
|
72
|
+
},
|
|
73
|
+
noCache: {
|
|
74
|
+
description: "Disable the cache during the docker image build.",
|
|
75
|
+
type: "boolean",
|
|
76
|
+
default: false
|
|
77
|
+
},
|
|
68
78
|
cacheFromVersion: {
|
|
69
79
|
description:
|
|
70
80
|
"Version to cache from when building, using the --cache-from field in docker. Will use the same repository and name. Using this options causes the image to be pulled before build.",
|
|
@@ -81,6 +91,18 @@ if (!argv.build && !argv.output) {
|
|
|
81
91
|
process.exit(1);
|
|
82
92
|
}
|
|
83
93
|
|
|
94
|
+
if (argv.platform && !argv.push) {
|
|
95
|
+
console.log(
|
|
96
|
+
"When --platform is specified, --push must be specified as well as multi-arch image can only be pushed to remote registry."
|
|
97
|
+
);
|
|
98
|
+
process.exit(1);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (argv.noCache && argv.cacheFromVersion) {
|
|
102
|
+
console.log("When --noCache=true, --cacheFromVersion can't be specified.");
|
|
103
|
+
process.exit(1);
|
|
104
|
+
}
|
|
105
|
+
|
|
84
106
|
const componentSrcDir = path.resolve(process.cwd());
|
|
85
107
|
const dockerContextDir = fse.mkdtempSync(
|
|
86
108
|
path.resolve(__dirname, "..", "docker-context-")
|
|
@@ -157,9 +179,12 @@ if (argv.build) {
|
|
|
157
179
|
"docker",
|
|
158
180
|
[
|
|
159
181
|
...extraParameters,
|
|
182
|
+
...(argv.platform ? ["buildx"] : []),
|
|
160
183
|
"build",
|
|
161
184
|
...tagArgs,
|
|
162
185
|
...cacheFromArgs,
|
|
186
|
+
...(argv.noCache ? ["--no-cache"] : []),
|
|
187
|
+
...(argv.platform ? ["--platform", argv.platform, "--push"] : []),
|
|
163
188
|
"-f",
|
|
164
189
|
`./component/Dockerfile`,
|
|
165
190
|
"-"
|
|
@@ -175,7 +200,7 @@ if (argv.build) {
|
|
|
175
200
|
dockerProcess.on("close", (code) => {
|
|
176
201
|
fse.removeSync(dockerContextDir);
|
|
177
202
|
|
|
178
|
-
if (code === 0 && argv.push) {
|
|
203
|
+
if (code === 0 && argv.push && !argv.platform) {
|
|
179
204
|
if (tags.length === 0) {
|
|
180
205
|
console.error("Can not push an image without a tag.");
|
|
181
206
|
process.exit(1);
|
package/bin/retag-and-push.js
CHANGED
|
@@ -56,6 +56,13 @@ const argv = yargs
|
|
|
56
56
|
example: "data61/magda-ckan-connector",
|
|
57
57
|
default: ""
|
|
58
58
|
})
|
|
59
|
+
.option("copyFromRegistry", {
|
|
60
|
+
describe: `When \`copyFromRegistry\`=true, [regctl](https://github.com/regclient/regclient) will be used.
|
|
61
|
+
The image will be copied directly from remote registry to the destination registry rather than from a local image.
|
|
62
|
+
This allows copying multi-arch image from one registry to another registry.`,
|
|
63
|
+
type: "boolean",
|
|
64
|
+
default: false
|
|
65
|
+
})
|
|
59
66
|
.option("toVersion", {
|
|
60
67
|
describe: "The version for the tag to push to",
|
|
61
68
|
type: "string",
|
|
@@ -66,29 +73,50 @@ const argv = yargs
|
|
|
66
73
|
const fromTag =
|
|
67
74
|
argv.fromPrefix + getName(argv.fromName) + ":" + argv.fromVersion;
|
|
68
75
|
|
|
69
|
-
const
|
|
70
|
-
stdio: ["pipe", "inherit", "inherit"],
|
|
71
|
-
env: env
|
|
72
|
-
});
|
|
76
|
+
const toTag = argv.toPrefix + getName(argv.toName) + ":" + argv.toVersion;
|
|
73
77
|
|
|
74
|
-
if (
|
|
75
|
-
|
|
76
|
-
|
|
78
|
+
if (argv.copyFromRegistry) {
|
|
79
|
+
console.log(`Copying from \`${fromTag}\` to \`${toTag}\`...`);
|
|
80
|
+
const copyProcess = childProcess.spawn(
|
|
81
|
+
"regctl",
|
|
82
|
+
["image", "copy", fromTag, toTag],
|
|
83
|
+
{
|
|
84
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
85
|
+
env: env
|
|
86
|
+
}
|
|
87
|
+
);
|
|
88
|
+
copyProcess.stderr.pipe(process.stderr);
|
|
89
|
+
copyProcess.stdout.pipe(process.stdout);
|
|
90
|
+
copyProcess.on("close", (code) => {
|
|
91
|
+
process.exit(code);
|
|
92
|
+
});
|
|
93
|
+
} else {
|
|
94
|
+
const pullProcess = childProcess.spawnSync("docker", ["pull", fromTag], {
|
|
95
|
+
stdio: ["pipe", "inherit", "inherit"],
|
|
96
|
+
env: env
|
|
97
|
+
});
|
|
77
98
|
|
|
78
|
-
|
|
99
|
+
if (pullProcess.status !== 0) {
|
|
100
|
+
process.exit(pullProcess.status);
|
|
101
|
+
}
|
|
79
102
|
|
|
80
|
-
const tagProcess = childProcess.spawnSync(
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
103
|
+
const tagProcess = childProcess.spawnSync(
|
|
104
|
+
"docker",
|
|
105
|
+
["tag", fromTag, toTag],
|
|
106
|
+
{
|
|
107
|
+
stdio: ["pipe", "inherit", "inherit"],
|
|
108
|
+
env: env
|
|
109
|
+
}
|
|
110
|
+
);
|
|
84
111
|
|
|
85
|
-
if (tagProcess.status !== 0) {
|
|
86
|
-
|
|
87
|
-
}
|
|
112
|
+
if (tagProcess.status !== 0) {
|
|
113
|
+
process.exit(tagProcess.status);
|
|
114
|
+
}
|
|
88
115
|
|
|
89
|
-
const pushProcess = childProcess.spawnSync("docker", ["push", toTag], {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
});
|
|
116
|
+
const pushProcess = childProcess.spawnSync("docker", ["push", toTag], {
|
|
117
|
+
stdio: ["pipe", "inherit", "inherit"],
|
|
118
|
+
env: env
|
|
119
|
+
});
|
|
93
120
|
|
|
94
|
-
process.exit(pushProcess.status);
|
|
121
|
+
process.exit(pushProcess.status);
|
|
122
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@magda/docker-utils",
|
|
3
3
|
"description": "MAGDA Docker Utilities",
|
|
4
|
-
"version": "1.1.0-alpha.
|
|
4
|
+
"version": "1.1.0-alpha.4",
|
|
5
5
|
"bin": {
|
|
6
6
|
"create-docker-context-for-node-component": "./bin/create-docker-context-for-node-component.js",
|
|
7
7
|
"retag-and-push": "./bin/retag-and-push.js"
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"license": "Apache-2.0",
|
|
16
16
|
"main": "bin/create-secrets.js",
|
|
17
17
|
"devDependencies": {
|
|
18
|
-
"@magda/scripts": "^1.1.0-alpha.
|
|
18
|
+
"@magda/scripts": "^1.1.0-alpha.4"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"fs-extra": "^2.1.2",
|