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