UNPKG

3.89 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const p_spawn_1 = require("p-spawn");
4const block_1 = require("./block");
5const realm_1 = require("./realm");
6const utils_1 = require("./utils");
7const hook_1 = require("./hook");
8const session_1 = require("./session");
9/**
10 * Push the local docker image for the service(s) to the remote (tag and push)
11 * Assumptions:
12 * - Assume the service names match the docker image convention as is on a local docker repo (localhost:5000/...)
13 * - Today assume remote is gcloud
14 * @param realm
15 * @param serviceNames
16 */
17async 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 //// Call prep hook the docker push
30 await hook_1.callHook(realm, "dpush_prep", names);
31 //// For each service, push the docker image
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 // we make sure the tag exist
37 await p_spawn_1.spawn('docker', ['tag', sourceImage, remoteImage]);
38 // This assume gcloud has been setup on the local machine
39 // Note: gcloud requires `gcloud auth configure-docker` and just use `docker push...
40 try {
41 await p_spawn_1.spawn('docker', ['push', remoteImage]);
42 }
43 catch (ex) {
44 // Note: Allow the hook to eventually recorver.
45 const passed = await hook_1.callHook(realm, 'dpush_image_ex', ex, remoteImage);
46 // If the dpush_image_ex return true, means it was recovered.
47 // Otherwise there might not have been any hook or could not recover. (note that the hook could throw ex as well)
48 if (passed !== true) {
49 throw ex;
50 }
51 }
52 console.log(`----- /DONE pushing ${serviceName} : ${remoteImage} ------\n`);
53 }
54}
55exports.push = push;
56async 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 // check if this block has a dbuildDependencies
62 const dbuildDeps = utils_1.asNames(block.dbuildDependencies);
63 for (let dep of dbuildDeps) {
64 await block_1.buildBlock(dep);
65 }
66 // make sure we build the service first
67 await block_1.buildBlock(block.name);
68 // build the image
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 // it can be use by future k8s after push
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}
91exports.buildDockerImage = buildDockerImage;