-
Notifications
You must be signed in to change notification settings - Fork 0
/
deckape.js
106 lines (88 loc) · 4.04 KB
/
deckape.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env node
const { exec } = require('child_process');
const { Command } = require('commander');
const program = new Command();
const runCommand = (command) => {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error executing command: ${command}\n${stderr}`);
reject(error);
} else {
console.log(`Command executed successfully: ${command}\n${stdout}`);
resolve(stdout);
}
});
});
};
const deleteContainer = async (containerName, containerTag) => {
try {
console.log('Killing and deleting container...');
await runCommand(`docker ps -aq --filter ancestor=${containerName}:${containerTag} | xargs -r docker kill`);
await runCommand(`docker ps -aq --filter ancestor=${containerName}:${containerTag} | xargs -r docker rm`);
console.log('Container killed and deleted successfully.');
} catch (error) {
console.error('Failed to kill and delete container:', error);
}
};
const rebuildContainer = async (containerName, containerTag) => {
try {
console.log('Stopping container...');
await runCommand(`docker ps -aq --filter ancestor=${containerName}:${containerTag} | xargs -r docker stop`);
console.log('Building container...');
await runCommand(`docker build -t ${containerName}:${containerTag} .`);
console.log('Starting container...');
await runCommand(`docker run --platform linux/amd64 -p 9000:8080 ${containerName}:${containerTag}`);
console.log('Container rebuilt and started successfully.');
} catch (error) {
console.error('Failed to rebuild container:', error);
}
};
const publishToECR = async (containerName, containerTag) => {
const region = 'us-east-1';
const accountId = "123456789012";
const repositoryName = containerName;
const ecrUri = `${accountId}.dkr.ecr.${region}.amazonaws.com/${repositoryName}`;
try {
console.log('Logging in to AWS ECR...');
await runCommand(`aws ecr get-login-password --region ${region} | docker login --username AWS --password-stdin ${accountId}.dkr.ecr.${region}.amazonaws.com`);
console.log('Creating ECR repository if it does not exist...');
await runCommand(`aws ecr create-repository --repository-name ${repositoryName} --region ${region} --image-scanning-configuration scanOnPush=true --image-tag-mutability IMMUTABLE || true`);
console.log('Tagging Docker image...');
await runCommand(`docker tag ${containerName}:${containerTag} ${ecrUri}:latest`);
console.log('Pushing Docker image to ECR...');
await runCommand(`docker push ${ecrUri}:latest`);
console.log('Docker image pushed to ECR successfully.');
} catch (error) {
console.error('Failed to publish Docker image to ECR:', error);
}
};
program
.name('deckape')
.description('CLI tool for managing local container development lifecycle workflows for locally developing containerized serverless functions.')
.version('1.0.0');
program
.command('delete')
.description('Delete containers and images')
.argument('<containerName>', 'Name of the container')
.argument('<containerTag>', 'Tag of the container')
.action((containerName, containerTag) => {
deleteContainer(containerName, containerTag);
});
program
.command('rebuild')
.description('Rebuild and start the container')
.argument('<containerName>', 'Name of the container')
.argument('<containerTag>', 'Tag of the container')
.action((containerName, containerTag) => {
rebuildContainer(containerName, containerTag);
});
program
.command('publish')
.description('Publish the container to AWS ECR')
.argument('<containerName>', 'Name of the container')
.argument('<containerTag>', 'Tag of the container')
.action((containerName, containerTag) => {
publishToECR(containerName, containerTag);
});
program.parse(process.argv);