UNPKG

4.72 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.getRepositoryName = exports._getImageName = exports.getLocalImageName = exports.buildDockerImage = exports.push = void 0;
4const p_spawn_1 = require("p-spawn");
5const block_1 = require("./block");
6const hook_1 = require("./hook");
7const realm_1 = require("./realm");
8const session_1 = require("./session");
9const utils_1 = require("./utils");
10/**
11 * Push the local docker image for the service(s) to the remote (tag and push)
12 * Assumptions:
13 * - Assume the service names match the docker image convention as is on a local docker repo (localhost:5000/...)
14 * - Today assume remote is gcloud
15 * @param realm
16 * @param serviceNames
17 */
18async 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 //// Call prep hook the docker push
32 await hook_1.callHook(realm, "dpush_prep", names);
33 //// For each service, push the docker image
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 // we make sure the tag exist
43 await p_spawn_1.spawn('docker', ['tag', sourceImage, remoteImage]);
44 // This assume gcloud has been setup on the local machine
45 // Note: gcloud requires `gcloud auth configure-docker` and just use `docker push...
46 try {
47 await p_spawn_1.spawn('docker', ['push', remoteImage]);
48 }
49 catch (ex) {
50 // Note: Allow the hook to eventually recorver.
51 const passed = await hook_1.callHook(realm, 'dpush_image_ex', ex, remoteImage);
52 // If the dpush_image_ex return true, means it was recovered.
53 // Otherwise there might not have been any hook or could not recover. (note that the hook could throw ex as well)
54 if (passed !== true) {
55 throw ex;
56 }
57 }
58 console.log(`----- /DONE pushing ${serviceName} : ${remoteImage} ------\n`);
59 }
60}
61exports.push = push;
62async 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 // check if this block has a dbuildDependencies
68 const dbuildDeps = utils_1.asNames(block.dbuildDependencies);
69 for (let dep of dbuildDeps) {
70 await block_1.buildBlock(dep);
71 }
72 // make sure we build the service first
73 await block_1.buildBlock(block.name);
74 // build the image
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 // it can be use by future k8s after push
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}
97exports.buildDockerImage = buildDockerImage;
98//#region ---------- docker naming ----------
99function getLocalImageName(block) {
100 return _getImageName(block, 'localhost:5000/');
101}
102exports.getLocalImageName = getLocalImageName;
103function _getImageName(block, basePath) {
104 const repoName = getRepositoryName(block);
105 return `${basePath}${repoName}:${block.imageTag}`;
106}
107exports._getImageName = _getImageName;
108function getRepositoryName(block) {
109 return `${block.system}-${block.name}`;
110}
111exports.getRepositoryName = getRepositoryName;
112//#endregion ---------- /docker naming ----------