UNPKG

3.43 kBPlain TextView Raw
1import { spawn } from 'p-spawn';
2import { Block, buildBlock, loadDockerBlocks } from './block';
3import { getLocalImageName, getRemoteImageName, Realm } from './realm';
4import { asNames, now, printLog } from './utils';
5import { callHook } from './hook';
6import { addSessionState, hasSessionState } from './session';
7
8
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 */
17export 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 //// Call prep hook the docker push
31 await callHook(realm, "dpush_prep", names);
32
33 //// For each service, push the docker image
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 // we make sure the tag exist
41 await spawn('docker', ['tag', sourceImage, remoteImage]);
42
43 // This assume gcloud has been setup on the local machine
44 // Note: gcloud requires `gcloud auth configure-docker` and just use `docker push...
45 try {
46 await spawn('docker', ['push', remoteImage]);
47 } catch (ex) {
48 // Note: Allow the hook to eventually recorver.
49 const passed = await callHook(realm, 'dpush_image_ex', ex, remoteImage);
50 // If the dpush_image_ex return true, means it was recovered.
51 // Otherwise there might not have been any hook or could not recover. (note that the hook could throw ex as well)
52 if (passed !== true) {
53 throw ex;
54 }
55 }
56 console.log(`----- /DONE pushing ${serviceName} : ${remoteImage} ------\n`);
57 }
58}
59
60
61export 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 // check if this block has a dbuildDependencies
70 const dbuildDeps = asNames(block.dbuildDependencies);
71 for (let dep of dbuildDeps) {
72 await buildBlock(dep);
73 }
74
75 // make sure we build the service first
76 await buildBlock(block.name);
77
78 // build the image
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 // it can be use by future k8s after push
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