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