UNPKG

3.37 kBPlain TextView Raw
1import { spawn } from 'p-spawn';
2import { loadBlocks } from './block';
3import { getRepositoryName, Realm } from './main';
4
5
6//#region ---------- Hook Functions ----------
7// finish initializing a realm object
8export async function realm_init(realm: Realm) {
9 // a aws realm must have a profile. Default is 'default'
10 if (!realm.profile) {
11 realm.profile = 'default';
12 }
13}
14
15export async function dpush_prep(realm: Realm, serviceNames: string[]) {
16 const blockByName = await loadBlocks();
17
18 const repoNames = serviceNames.map(sn => getRepositoryName(blockByName[sn]));
19
20 const existingRepositoryNames = await getAwsExistingRepositoryNames(realm);
21 const missingRepositoryNames = repoNames.filter(n => !existingRepositoryNames.includes(n));
22 await createRepositories(realm, missingRepositoryNames);
23}
24
25
26export async function dpush_image_ex(realm: Realm, ex: any, remoteImage: string) {
27 console.log(`dpush - recovering - first docker dpush to ${remoteImage} failed, trying to recover`);
28
29 // AWS CLI 2
30 try {
31 // get ecr password
32 const cmdGetPwd = await spawn('aws', ['ecr', 'get-login-password', '--profile', realm.profile], { capture: 'stdout' });
33 const pwd = cmdGetPwd.stdout!.trim();
34
35 // docker login (leave output to console)
36 const cmdDockerLogin = await spawn('docker', ['login', '--username', 'AWS', '--password-stdin', realm.registry], { input: pwd });
37 console.log('dpush - relogin success');
38 console.log(`dpush - trying to push again ${remoteImage}`);
39 await spawn('docker', ['push', remoteImage]);
40 console.log('dpush - fully recovered');
41 } catch (ex) {
42 console.log('dpush - error - relogin/repush failed', ex);
43 throw ex;
44 }
45
46 // AWS CLI 1.x
47 // aws ecr get-login --no-include-email
48 // const reloginCmd = await spawn('aws', ['ecr', 'get-login', '--no-include-email', '--profile', realm.profile], { capture: 'stdout' });
49 // const reloginArg = reloginCmd.stdout!.trim().split(' ');
50 // reloginArg.shift(); // remove the 'docker' first item.
51 // try {
52 // const relogin = await spawn('docker', reloginArg, { capture: 'stdout' });
53 // console.log('dpush - relogin success');
54 // console.log(`dpush - trying to push again ${remoteImage}`);
55 // await spawn('docker', ['push', remoteImage]);
56 // console.log('dpush - fully recovered');
57 // } catch (ex2) {
58 // console.log('dpush - error - relogin/repush failed', ex2);
59 // throw ex2;
60 // }
61 return true;
62}
63//#endregion ---------- /Hook Functions ----------
64
65
66//#region ---------- Utils ----------
67async function createRepositories(realm: Realm, repoNames: string[]) {
68 for (const repo of repoNames) {
69 // aws ecr create-repository --profile default --repository-name cstar-agent
70 console.log(`Info - aws ECR repository ${repo} does not exist, creating...`);
71 await spawn('aws', ['ecr', 'create-repository', '--profile', realm.profile, '--repository-name', repo]);
72 }
73}
74
75async function getAwsExistingRepositoryNames(realm: Realm) {
76
77 // NOTE: Somehow even when the login has expired, this call works (it's the push that does not work)
78 const dataStr = await spawn('aws', ['ecr', 'describe-repositories', '--profile', realm.profile], { capture: 'stdout' });
79 const repositories = JSON.parse(dataStr.stdout!.trim()).repositories;
80 if (repositories) {
81 return repositories.map((r: any) => r.repositoryName);
82 } else {
83 return [];
84 }
85}
86//#endregion ---------- /Utils ----------
\No newline at end of file