cloudmason 0.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/.github/workflows/CODEOWNERS +1 -0
  2. package/.github/workflows/main.yml +27 -27
  3. package/README.md +373 -25
  4. package/build.js +20 -20
  5. package/commands/delete.js +67 -28
  6. package/commands/helpers/cf.js +181 -117
  7. package/commands/helpers/common.js +82 -0
  8. package/commands/helpers/ec2.js +154 -40
  9. package/commands/helpers/params.js +231 -178
  10. package/commands/helpers/s3.js +186 -67
  11. package/commands/helpers/stacks/asg.yaml +420 -224
  12. package/commands/helpers/stacks/infra.yaml +102 -106
  13. package/commands/helpers/stacks.js +25 -25
  14. package/commands/index.html +22 -0
  15. package/commands/init_org.js +54 -61
  16. package/commands/inspect.js +40 -0
  17. package/commands/launch_app.js +80 -57
  18. package/commands/list_apps.js +21 -21
  19. package/commands/new_app.js +44 -50
  20. package/commands/new_instance.js +133 -186
  21. package/commands/reset_stack.js +27 -27
  22. package/commands/starter.js +21 -0
  23. package/commands/starters/asg_node/index.js +62 -0
  24. package/commands/starters/asg_node/mason.txt +1 -0
  25. package/commands/starters/asg_node/modules/appConfig.js +131 -0
  26. package/commands/starters/asg_node/package-lock.json +5877 -0
  27. package/commands/starters/asg_node/package.json +23 -0
  28. package/commands/starters/asg_node/public/css/favicon-16x16.png +0 -0
  29. package/commands/starters/asg_node/public/css/fonts/Lato-Bold.ttf +0 -0
  30. package/commands/starters/asg_node/public/css/fonts/Lato-Regular.ttf +0 -0
  31. package/commands/starters/asg_node/public/css/fonts/Montserrat-Var.ttf +0 -0
  32. package/commands/starters/asg_node/public/css/fonts/OpenSans.ttf +0 -0
  33. package/commands/starters/asg_node/public/css/fonts/bpmn.woff2 +0 -0
  34. package/commands/starters/asg_node/public/css/fonts/fonts.css +17 -0
  35. package/commands/starters/asg_node/public/css/index.css +9 -0
  36. package/commands/starters/asg_node/public/index.html +15 -0
  37. package/commands/starters/asg_node/public/js/index.js +5 -0
  38. package/commands/starters/asg_node/start.sh +4 -0
  39. package/commands/update_app.js +235 -272
  40. package/commands/update_stack.js +27 -0
  41. package/commands/utils.js +32 -32
  42. package/main.js +262 -220
  43. package/package.json +1 -28
  44. package/test.bat +16 -9
  45. package/commands/delete_app.js +0 -28
  46. package/commands/helpers/stacks/asg_draft.json +0 -321
@@ -1,68 +1,187 @@
1
- const { S3Client,PutObjectCommand,CopyObjectCommand,HeadObjectCommand } = require("@aws-sdk/client-s3");
2
- const fs = require('fs');
3
-
4
-
5
- exports.uploadInfraFile = async function(fileKey,localPath){
6
- const s3Path = fileKey;
7
- if (!fs.existsSync(localPath)){
8
- throw new Error('File does not exist:' + localPath)
9
- }
10
- const fileStream = fs.createReadStream(localPath);
11
- // Upload Stack
12
- const client = new S3Client({region: process.env.orgRegion});
13
- const input = {
14
- Body: fileStream,
15
- Bucket: process.env.orgBucket,
16
- Key: s3Path.toLowerCase()
17
- };
18
- const poCommand = new PutObjectCommand(input);
19
- const poResponse = await client.send(poCommand);
20
- return poResponse;
21
- }
22
-
23
- exports.uploadInfraText = async function(fileKey,fileText){
24
- // Upload Stack
25
- const client = new S3Client({region: process.env.orgRegion});
26
- const input = {
27
- Body: fileText,
28
- Bucket: process.env.orgBucket,
29
- Key: fileKey.toLowerCase()
30
- };
31
- const poCommand = new PutObjectCommand(input);
32
- const poResponse = await client.send(poCommand);
33
- return poResponse;
34
- }
35
-
36
- exports.infraFileExists = async function(fileKey){
37
- const client = new S3Client({region: process.env.orgRegion});
38
-
39
- const input = {
40
- Bucket: process.env.orgBucket,
41
- Key: fileKey.toLowerCase()
42
- };
43
-
44
- const command = new HeadObjectCommand(input)
45
- try{
46
- const response = await client.send(command);
47
- return true;
48
- } catch (e){
49
- if (/NotFound/.test(e)){
50
- return false;
51
- } else {
52
- console.log('Uncaught Error:',e)
53
- throw e;
54
- }
55
- }
56
- }
57
-
58
-
59
- exports.copyInfraFile = async function(srcKey,destKey){
60
- const client = new S3Client({ region: process.env.orgRegion });
61
- const params = {
62
- Bucket: process.env.orgBucket,
63
- CopySource: `${process.env.orgBucket}/${srcKey}`,
64
- Key: destKey.toLowerCase(),
65
- };
66
- await client.send(new CopyObjectCommand(params));
67
- return true;
1
+ const { S3Client,PutObjectCommand,CopyObjectCommand,HeadObjectCommand,DeleteObjectsCommand,ListObjectsV2Command,GetObjectCommand,ListObjectVersionsCommand,DeleteObjectCommand } = require("@aws-sdk/client-s3");
2
+ const fs = require('fs');
3
+
4
+
5
+
6
+ exports.uploadInfraFile = async function(fileKey,localPath){
7
+ const s3Path = fileKey;
8
+ if (!fs.existsSync(localPath)){
9
+ throw new Error('File does not exist:' + localPath)
10
+ }
11
+ const fileStream = fs.createReadStream(localPath);
12
+ // Upload Stack
13
+ const client = new S3Client({region: process.env.orgRegion});
14
+ const input = {
15
+ Body: fileStream,
16
+ Bucket: process.env.orgBucket,
17
+ Key: s3Path.toLowerCase()
18
+ };
19
+ const poCommand = new PutObjectCommand(input);
20
+ const poResponse = await client.send(poCommand);
21
+ return poResponse;
22
+ }
23
+
24
+ exports.uploadInfraText = async function(fileKey,fileText){
25
+ // Upload Stack
26
+ const client = new S3Client({region: process.env.orgRegion});
27
+ const input = {
28
+ Body: fileText,
29
+ Bucket: process.env.orgBucket,
30
+ Key: fileKey.toLowerCase()
31
+ };
32
+ const poCommand = new PutObjectCommand(input);
33
+ const poResponse = await client.send(poCommand);
34
+ return poResponse;
35
+ }
36
+
37
+ exports.infraFileExists = async function(fileKey){
38
+ const client = new S3Client({region: process.env.orgRegion});
39
+
40
+ const input = {
41
+ Bucket: process.env.orgBucket,
42
+ Key: fileKey.toLowerCase()
43
+ };
44
+
45
+ const command = new HeadObjectCommand(input)
46
+ try{
47
+ const response = await client.send(command);
48
+ return true;
49
+ } catch (e){
50
+ if (/NotFound/.test(e)){
51
+ return false;
52
+ } else {
53
+ console.log('Uncaught Error:',e)
54
+ throw e;
55
+ }
56
+ }
57
+ }
58
+
59
+
60
+ exports.copyInfraFile = async function(srcKey,destKey){
61
+ const client = new S3Client({ region: process.env.orgRegion });
62
+ const params = {
63
+ Bucket: process.env.orgBucket,
64
+ CopySource: `${process.env.orgBucket}/${srcKey}`,
65
+ Key: destKey.toLowerCase(),
66
+ };
67
+ await client.send(new CopyObjectCommand(params));
68
+ return true;
69
+ }
70
+
71
+ exports.emptyBucket = async function(bucketName,region){
72
+ await emptyS3Bucket(bucketName,region)
73
+ return true;
74
+ }
75
+
76
+ exports.deleteAppFolder = async function(appName){
77
+ await deleteFolder(
78
+ process.env.orgBucket,
79
+ `apps/${appName.toLowerCase()}/`,
80
+ process.env.orgRegion
81
+ )
82
+ return true;
83
+ }
84
+
85
+ exports.readFiles = async function(bucketName,prefix,region){
86
+ const s3Client= new S3Client({region});
87
+ const Content = [];
88
+ const listCommand = new ListObjectsV2Command({
89
+ Bucket: bucketName,
90
+ Prefix: prefix,
91
+ MaxKeys: 50
92
+ });
93
+ const listResponse = await s3Client.send(listCommand);
94
+
95
+ if (!listResponse.Contents) { return []}
96
+
97
+
98
+ const proms = listResponse.Contents.map(object=>{
99
+ return s3Client.send(new GetObjectCommand({
100
+ Bucket: bucketName,
101
+ Key: object.Key,
102
+ })).then(r=>{
103
+ return streamToString(r.Body).then(fileContent=>{
104
+ Content.push({
105
+ Key: object.Key,
106
+ FileName: object.Key.replace(prefix + '/',''),
107
+ Content: fileContent,
108
+ LastModified: object.LastModified
109
+ });
110
+ })
111
+ });
112
+ })
113
+ await Promise.all(proms);
114
+ return Content.sort((a,b)=>{ return new Date(a.LastModified) - new Date(b.LastModified) });
115
+ }
116
+
117
+
118
+ ///////////
119
+
120
+ async function deleteFolder(bucketName, prefix, region){
121
+ const client = new S3Client({ region });
122
+ const listParams = {
123
+ Bucket: bucketName,
124
+ Prefix: prefix
125
+ };
126
+ const listedObjects = await client.send(new ListObjectsV2Command(listParams));
127
+
128
+ if (!listedObjects.Contents || listedObjects.Contents.length === 0) return;
129
+
130
+ // Array to hold all keys to delete
131
+ const deleteKeys = listedObjects.Contents.map(({ Key }) => ({ Key }));
132
+
133
+ // Delete objects
134
+ const deleteParams = {
135
+ Bucket: bucketName,
136
+ Delete: { Objects: deleteKeys }
137
+ };
138
+ await client.send(new DeleteObjectsCommand(deleteParams));
139
+
140
+ // In case of pagination (more than 1000 objects), recursively handle the next batch
141
+ if (listedObjects.IsTruncated) await deleteFolder(bucketName, prefix, region);
142
+ }
143
+
144
+
145
+ async function emptyS3Bucket(bucketName, region) {
146
+ const s3Client= new S3Client({region});
147
+
148
+ // List all object versions
149
+ const listedObjectVersions = await s3Client.send(new ListObjectVersionsCommand({ Bucket: bucketName }));
150
+ if (listedObjectVersions.Versions || listedObjectVersions.DeleteMarkers) {
151
+ const objectsToDelete = [
152
+ ...listedObjectVersions.Versions.map(v => ({ Key: v.Key, VersionId: v.VersionId })),
153
+ ...listedObjectVersions.DeleteMarkers.map(dm => ({ Key: dm.Key, VersionId: dm.VersionId }))
154
+ ];
155
+
156
+ for (const object of objectsToDelete) {
157
+ await s3Client.send(new DeleteObjectCommand({
158
+ Bucket: bucketName,
159
+ Key: object.Key,
160
+ VersionId: object.VersionId
161
+ }));
162
+ }
163
+ }
164
+
165
+ // List all objects
166
+ const listedObjects = await s3Client.send(new ListObjectsV2Command({ Bucket: bucketName }));
167
+ if (!listedObjects.Contents || listedObjects.Contents.length === 0) return;
168
+
169
+ // Delete all objects
170
+ const deleteParams = {
171
+ Bucket: bucketName,
172
+ Delete: { Objects: listedObjects.Contents.map(({ Key }) => ({ Key })) }
173
+ };
174
+ await s3Client.send(new DeleteObjectsCommand(deleteParams));
175
+
176
+ // Recursively call the function if the bucket is not empty
177
+ if (listedObjects.IsTruncated) await emptyS3Bucket(bucketName);
178
+ }
179
+
180
+ async function streamToString(stream) {
181
+ return new Promise((resolve, reject) => {
182
+ const chunks = [];
183
+ stream.on('data', (chunk) => chunks.push(chunk));
184
+ stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
185
+ stream.on('error', reject);
186
+ });
68
187
  }