1 | import { spawn } from 'p-spawn';
|
2 | import { Block, buildBlock, loadDockerBlocks } from './block';
|
3 | import { getLocalImageName, getRemoteImageName, Realm } from './realm';
|
4 | import { asNames, now, printLog } from './utils';
|
5 | import { callHook } from './hook';
|
6 | import { addSessionState, hasSessionState } from './session';
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | export async function push(realm: Realm, serviceNames?: string | string[]) {
|
18 | let names: string[];
|
19 | if (serviceNames) {
|
20 | names = asNames(serviceNames);
|
21 | } else {
|
22 | const dockerBlocks = await loadDockerBlocks();
|
23 | names = Object.keys(dockerBlocks)
|
24 | }
|
25 |
|
26 | if (realm.registry == null) {
|
27 | throw new Error(`Realm '${realm.name}' does not have registry, cannot push docker image.`);
|
28 | }
|
29 |
|
30 |
|
31 | await callHook(realm, "dpush_prep", names);
|
32 |
|
33 |
|
34 | for (let serviceName of names) {
|
35 | let sourceImage = getLocalImageName(realm, serviceName)
|
36 | let remoteImage = getRemoteImageName(realm, serviceName);
|
37 |
|
38 | console.log(`----- Pushing service ${serviceName} : ${remoteImage}`);
|
39 |
|
40 |
|
41 | await spawn('docker', ['tag', sourceImage, remoteImage]);
|
42 |
|
43 |
|
44 |
|
45 | try {
|
46 | await spawn('docker', ['push', remoteImage]);
|
47 | } catch (ex) {
|
48 |
|
49 | const passed = await callHook(realm, 'dpush_image_ex', ex, remoteImage);
|
50 |
|
51 |
|
52 | if (passed !== true) {
|
53 | throw ex;
|
54 | }
|
55 | }
|
56 | console.log(`----- /DONE pushing ${serviceName} : ${remoteImage} ------\n`);
|
57 | }
|
58 | }
|
59 |
|
60 |
|
61 | export async function buildDockerImage(realm: Realm, block: Block) {
|
62 | const dir = block.dir;
|
63 | const imageName = getLocalImageName(realm, block.name);
|
64 |
|
65 | const start = now();
|
66 |
|
67 | console.log(`============ BUILDING docker image: ${imageName}\n`);
|
68 |
|
69 |
|
70 | const dbuildDeps = asNames(block.dbuildDependencies);
|
71 | for (let dep of dbuildDeps) {
|
72 | await buildBlock(dep);
|
73 | }
|
74 |
|
75 |
|
76 | await buildBlock(block.name);
|
77 |
|
78 |
|
79 | console.log(`------ docker build ... ${imageName}`);
|
80 | await spawn('docker', ['build', '--rm', '-t', imageName, '.'], { cwd: dir });
|
81 | console.log(`------ DONE - docker build ... ${imageName}`);
|
82 |
|
83 | console.log();
|
84 |
|
85 |
|
86 | let status = 'DONE';
|
87 | console.log(`------ docker push ... ${imageName}`);
|
88 | try {
|
89 | if (!hasSessionState('NO_LOCAL_REGISTRY')) {
|
90 | await spawn('docker', ['push', imageName]);
|
91 | }
|
92 | } catch (ex) {
|
93 | addSessionState('NO_LOCAL_REGISTRY');
|
94 | }
|
95 | if (hasSessionState('NO_LOCAL_REGISTRY')) {
|
96 | console.log('INFO - No Local Registry (http://localhost:5000/) - Skipping push to local registry');
|
97 | status = "SKIPPING";
|
98 | }
|
99 |
|
100 | console.log(`------ ${status} - docker push ... ${imageName}`);
|
101 |
|
102 | printLog(`\n============ DONE - BUILDING docker image: ${imageName}`, null, start);
|
103 | } |
\ | No newline at end of file |