@kumologica/sdk 3.2.0-beta2 → 3.2.0-beta21

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.
@@ -0,0 +1,242 @@
1
+ const fs = require('fs-extra');
2
+ const path = require('path');
3
+ const AWS = require('aws-sdk');
4
+ const { exp } = require('@kumologica/builder');
5
+ const AWSProfile = require('../aws/aws-profile');
6
+ const { openFileOnEditor } = require('../utils/editor');
7
+
8
+ /**
9
+ * sample events:
10
+ * https://docs.aws.amazon.com/lambda/latest/dg/lambda-services.html
11
+ */
12
+ class GithubDeployer {
13
+ constructor(terminal) {
14
+ this.term = terminal;
15
+ this.awsProfile = new AWSProfile();
16
+
17
+ AWS.config.apiVersions = {
18
+ cloudformation: '2010-05-15',
19
+ };
20
+ }
21
+
22
+ async initAWS(profile) {
23
+ AWS.config.credentials = new AWS.SharedIniFileCredentials({profile: profile});
24
+
25
+ const r = await this.awsProfile.getRegion(profile);
26
+ let s3 = new AWS.S3();
27
+ let bucketName = "CHANGE_IT_TO_REAL_BUCKET_NAME";
28
+
29
+ // find bucket
30
+ const buckets = await s3.listBuckets({}).promise();
31
+
32
+ if (buckets && buckets.Buckets) {
33
+ const bucket = buckets.Buckets.find(b => b.Name.startsWith('kumologica-designer-deploy'));
34
+ if (bucket) {
35
+ bucketName = bucket.Name;
36
+ }
37
+ }
38
+
39
+ return {region: r, bucket: bucketName};
40
+ }
41
+
42
+ chalk(c, t) {
43
+ if (c == 'whiteBright') {
44
+ c = '#FFFFFF';
45
+ } else if (c == 'redBright') {
46
+ c = '#FF0000';
47
+ } else if (c == 'yellowBright') {
48
+ c = '#FFFF00';
49
+ } else if (c == 'greenBright') {
50
+ c = '#00FF00';
51
+ }
52
+
53
+ return `<span style="color: ${c}">${t}</span>`;
54
+ }
55
+
56
+ log(text, calog = true) {
57
+ var t = text;
58
+
59
+ if (calog && this.chalk) {
60
+ t = `Kumologica: ${this.chalk('#F5DEB3', text)}`;
61
+ }
62
+
63
+ if (this.term) {
64
+ this.term.emit('terminal-output', t);
65
+ }
66
+ }
67
+
68
+ logCloud(text) {
69
+ if (this.term) {
70
+ this.term.emit('terminal-cloud', `${text}`);
71
+ }
72
+ }
73
+
74
+ prepare(projectDir, flowFileName, originaLambdaName) {
75
+ const deployDir = projectDir + '/deploy';
76
+
77
+ const flowName = flowFileName.replace('.json', '');
78
+
79
+ // setup deployment directory
80
+ fs.ensureDirSync(deployDir);
81
+ fs.emptyDirSync(deployDir);
82
+ fs.ensureDirSync(deployDir + '/node_modules');
83
+
84
+ this.processPackageJson(projectDir, deployDir);
85
+
86
+ const zipFileName = `${flowName}/lambda${Date.now()}.zip`;
87
+ const lambdaName = this.sanitizeLambdaName(originaLambdaName);
88
+
89
+ const functionName = `kumologica-${lambdaName}-lambda`;
90
+ }
91
+
92
+ async generateScript(projectInfo, params, profile) {
93
+
94
+ /*
95
+
96
+ projectInfo: {"projectName":"sockets",
97
+ "projectDir":"/Users/wojtek/Development/projects/sockets",
98
+ "projectFlowName":"sockets-flow.json"}
99
+ profile: kl
100
+ */
101
+ try {
102
+ this.log('Generating github action workflow');
103
+
104
+ //console.log(`projectInfo: ${JSON.stringify(projectInfo)}`);
105
+ //console.log(`profile: ${profile}`);
106
+
107
+ const awsSettings = await this.initAWS(profile);
108
+
109
+ const args = this.mapParams(params);
110
+ args.region = awsSettings.region;
111
+ args["bucket-name"] = awsSettings.bucket;
112
+
113
+ let wf = exp("github", "aws", args, console.log);
114
+
115
+ const wfDir = path.join(projectInfo.projectDir, '.github', 'workspaces');
116
+
117
+ this.createFile (
118
+ wfDir, 'workflow.yaml',
119
+ wf
120
+ );
121
+
122
+ this.log(`Github action workflow created: ${path.join(wfDir, 'workflow.yaml')}`);
123
+
124
+ openFileOnEditor(path.join(wfDir, 'workflow.yaml'));
125
+
126
+ return;
127
+
128
+
129
+ this.log(` ${this.chalk('#F5DEB3', 'AWS region:')} ${this.chalk('whiteBright', AWS.config.region)}`, false);
130
+
131
+
132
+ } catch (Error) {
133
+ this.log(`${this.chalk('redBright', 'Github action workflow creation failed.')}`);
134
+ this.log(` ${this.chalk('redBright', Error)}`);
135
+ throw Error;
136
+ }
137
+ }
138
+
139
+ mapParams(params) {
140
+ let vpcConf;
141
+ if (params.vpcConfig && params.vpcConfig.sn && params.vpcConfig.sn.length > 0
142
+ && params.vpcConfig.sg && params.vpcConfig.sg.length > 0) {
143
+ let sn = params.vpcConfig.sn.split(",").map(s => s.trim());
144
+ let sg = params.vpcConfig.sg.split(",").map(s => s.trim());
145
+
146
+ vpcConf = JSON.stringify({
147
+ SubnetIds: sn,
148
+ SecurityGroupIds: sg
149
+ });
150
+ }
151
+
152
+ let tags;
153
+ if ( params.tags && params.tags.length > 0) {
154
+ tags = {};
155
+ params.tags.forEach(t => tags[t.key] = t.value)
156
+ tags = JSON.stringify(tags);
157
+ }
158
+
159
+ let env;
160
+ if ( params.environment && params.environment.length > 0) {
161
+ env = {
162
+ Variables: {}
163
+ };
164
+ params.environment.forEach(e => env.Variables[e.key] = e.value)
165
+ env = JSON.stringify(env);
166
+ }
167
+
168
+ let tracing;
169
+ if (params.xRay) {
170
+ tracing = '{"Mode":"Active"}';
171
+ }
172
+ // KL_TRIGGERS: '[{"api": {"apiId": "CHANGE_IT_TO_API_GATEWAY_ID", "parentId": "CHANGE_IT_TO_PARENT_ID", "stage": "test", "resource": "accounts"}}, {"event": {"expression": "cron(0 1 * * ? *)", "reference": "1am", "name": "CliBuildDemoEvent1am"}}, {"event": {"expression": "rate(1 minute)", "reference": "5min", "name": "CliBuildDemoEvent5min"}}]'
173
+
174
+ // "events":[{"source":"api","api":"qyhtzl0n39","stage":"test","parentId":"xfdg32","resource":"sample","authorizerId":"fdr3"}]
175
+ // "events":[{"source":"api","api":"qyhtzl0n39","stage":"test","parentId":"xfdg32","resource":"sample","authorizerId":"fdr3"},
176
+ //{"source":"dynamodb","stream":"arn:aws:dynamodb:ap-southeast-2:174842903734:table/contact_us/stream/2021-04-25T15:02:15.028","batchSize":"23","batchWindow":"3","startingPosition":"LATEST"},
177
+ //{"source":"sqs","stream":"https://sqs.ap-southeast-2.amazonaws.com/174842903734/devq2","batchSize":"32"},
178
+ //{"source":"kinesis","stream":"arn:aws:dynamodb:ap-southeast-2:174842903734:table/contact_us/stream/2021-04-25T15:02:15.028","batchSize":"34","batchWindow":"33","startingPosition":"LATEST"},
179
+ // {"source":"cwevents","rule":"arn:aws:events:ap-southeast-2:174842903734:rule/Test"},
180
+ //{"source":"sns","topic":"arn:aws:sns:ap-southeast-2:174842903734:t1"},
181
+ //{"source":"s3","bucket":"kumoexcelstore","eventType":"s3:ObjectCreated:*","prefix":"cc","suffix":"fd"}],"vpcConfig":{"sg":"","sn":""}}
182
+ ///Users/wojtek/Development/kumologica/sdk/kumologica/packages/sdk/src/app/lib/github/index.js:114
183
+ let triggers;
184
+ if (params.events) {
185
+ triggers = [];
186
+ params.events.forEach(e => {
187
+ switch(e.source) {
188
+ case "api": triggers.push({"api": {"apiId": e.api, "stage": e.stage, "parentId": e.parentId, "resource": e.resource, "authorizerId": e.authorizerId}});
189
+ break;
190
+ case "sns": triggers.push({"sns": {"topicArn": e.topic}});
191
+ break;
192
+ case "sqs": triggers.push({"sqs": {"queueArn": e.queueArn, "batchSize": e.batchSize}});
193
+ break;
194
+ case "dynamodb": triggers.push({"dynamodb": {"streamArn": e.stream, "startingPosition": e.startingPosition, "batchSize": e.batchSize, "batchWindow": e.batchWindow}});
195
+ break;
196
+ case "kinesis": triggers.push({"kinesis": {"streamArn": e.stream, "startingPosition": e.startingPosition, "batchSize": e.batchSize, "batchWindow": e.batchWindow}});
197
+ break;
198
+ case "s3": triggers.push({"s3": {"bucket": e.bucket, "eventType": e.eventType, "prefix": e.prefix, "suffix": e.suffix}});
199
+ break;
200
+ case "cwevents": triggers.push({"event": {"expression": e.expression, "reference": e.reference, "name": e.name}});
201
+ break;
202
+ }
203
+ })
204
+ triggers = JSON.stringify(triggers);
205
+ };
206
+
207
+ let args = {
208
+ "zip-file-name": params.zipFileName,
209
+ "bucket-name": params.bucketName,
210
+ "lambda-name": params.functionName,
211
+ "memory-size": params.memory,
212
+ "description": params.description,
213
+ "environment": env,
214
+ "tags": tags,
215
+ "timeout": params.timeout,
216
+ "triggers": triggers,
217
+ "vpc-config": vpcConf,
218
+ "role-arn": params.role,
219
+ "tracing-config": tracing,
220
+ "reserved-concurrency": params.reservedConcurrency
221
+ // not present
222
+ // "role-name": params.roleName,
223
+ // "layers": params.layers,
224
+ // "policy": params.policy,
225
+ // "strict-mode": params.strictMode,
226
+ // "log-retention-days": params.logRetentionDays,
227
+ // "file-system-configs": params.fileSystemConfigs,
228
+ // "dead-letter-config": params.deadLetterConfig,
229
+ // "runtime": params.runtime,
230
+ // "architectures": params.architectures,
231
+ // "kms-key-arn": params.kmsKeyArn,
232
+ }
233
+ return args;
234
+ }
235
+
236
+ createFile(baseDir, fileName, content) {
237
+ fs.outputFileSync(path.join(baseDir, fileName), content, 'utf-8');
238
+ }
239
+
240
+ }
241
+
242
+ module.exports = GithubDeployer;
@@ -37,6 +37,7 @@ const { NetworkConfigStore } = require('./lib/stores/settings-network-store');
37
37
  const AWSProfile = require('./lib/aws/aws-profile');
38
38
  const AWSDeployer = require('./lib/aws');
39
39
  const ServerlessDeployer = require('./lib/serverless');
40
+ const GithubDeployer = require('./lib/github');
40
41
 
41
42
  const Kumohub = require('./lib/kumohub');
42
43
  const deployCli = require('@kumologica/builder');
@@ -48,6 +49,7 @@ const terminalEmitter = new events.EventEmitter();
48
49
  const awsDeployer = new AWSDeployer(terminalEmitter);
49
50
  const awsProfile = new AWSProfile();
50
51
  const serverlessDeployer = new ServerlessDeployer(terminalEmitter);
52
+ const githubDeployer = new GithubDeployer(terminalEmitter);
51
53
 
52
54
  const kumohub = new Kumohub(terminalEmitter);
53
55
 
@@ -133,12 +135,18 @@ async function generateScript(provider, projectInfo, params) {
133
135
  params,
134
136
  alias.profile
135
137
  );
136
- } else if (provider ==window.__kumologica.cloud.provider.serverless) {
138
+ } else if (provider == window.__kumologica.cloud.provider.serverless) {
137
139
  await serverlessDeployer.generateScript(
138
140
  projectInfo,
139
141
  params,
140
142
  alias.profile
141
143
  );
144
+ } else if (provider == window.__kumologica.cloud.provider.github) {
145
+ await githubDeployer.generateScript(
146
+ projectInfo,
147
+ params,
148
+ alias.profile
149
+ );
142
150
  }
143
151
  }
144
152
 
@@ -586,6 +594,8 @@ window.__kumologica.runtime.port = port;
586
594
  window.__kumologica.cloud = {provider: {}};
587
595
  window.__kumologica.cloud.provider.aws = "AWS";
588
596
  window.__kumologica.cloud.provider.serverless = "SERVERLESS";
597
+ window.__kumologica.cloud.provider.github = "GITHUB";
598
+
589
599
  window.__kumologica.cloud.profile = undefined;
590
600
  window.__kumologica.cloud.deploy = deploy;
591
601
  window.__kumologica.cloud.generateScript = generateScript;