1 | import { spawn } from 'p-spawn';
|
2 | import { loadBlocks } from './block';
|
3 | import { getRepositoryName, Realm } from './main';
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | export async function realm_init(realm: Realm) {
|
9 |
|
10 | if (!realm.profile) {
|
11 | realm.profile = 'default';
|
12 | }
|
13 | }
|
14 |
|
15 | export 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 |
|
26 | export 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 |
|
30 | try {
|
31 |
|
32 | const cmdGetPwd = await spawn('aws', ['ecr', 'get-login-password', '--profile', realm.profile], { capture: 'stdout' });
|
33 | const pwd = cmdGetPwd.stdout!.trim();
|
34 |
|
35 |
|
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 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | return true;
|
62 | }
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | async function createRepositories(realm: Realm, repoNames: string[]) {
|
68 | for (const repo of repoNames) {
|
69 |
|
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 |
|
75 | async function getAwsExistingRepositoryNames(realm: Realm) {
|
76 |
|
77 |
|
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 |
|
\ | No newline at end of file |