@cloudnux/cli 0.1.0 → 0.11.0

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.
@@ -1,67 +0,0 @@
1
- terraform {
2
- backend "s3" {
3
- key = "<%= component %>/terraform.state"
4
- workspace_key_prefix = "state/<%= component %>"
5
- }
6
-
7
- required_providers {
8
- aws = {
9
- source = "hashicorp/aws"
10
- version = ">= 4.0"
11
- }
12
- }
13
-
14
- required_version = ">= 0.13.1"
15
- }
16
-
17
- data "aws_lambda_function" "this" {
18
- function_name = "<%= component %>"
19
- }
20
-
21
- data "aws_apigatewayv2_apis" "this" {
22
- tags = {
23
- application = "anubis"
24
- }
25
- }
26
-
27
- <% const httpEntries = Object.entries(entries).filter(([k,e]) => e.trigger.type.toLowerCase() === "http") %>
28
- <% if(httpEntries.length > 0) { %>
29
- module "http_trigger_for_<%= component %>" {
30
- source = "../../.deploy/aws/http"
31
- api_gateway_id = sort(data.aws_apigatewayv2_apis.this.ids)[0]
32
- lambda_arn = data.aws_lambda_function.this.arn
33
- lambda_name = "<%= component %>"
34
- entrypoints = {
35
- <% for(const [key,entry] of httpEntries){ %>
36
- "<%= key %>" = {
37
- description = "<% entry.trigger.options.description %>"
38
- method = "<%= entry.trigger.options.method %>"
39
- path = "<%= entry.trigger.options.route %>"
40
- }
41
- <% } %>
42
- }
43
- }
44
-
45
- <% } %>
46
-
47
- <% const scheduleEntries = Object.entries(entries).filter(([k,e]) => e.trigger.type.toLowerCase() === "schedule") %>
48
- <% if(scheduleEntries.length > 0) { %>
49
- module "schedule_trigger_for_<%= component %>" {
50
- source = "../../.deploy/aws/scheduler"
51
- component = "<%= component %>"
52
- target_arn = data.aws_lambda_function.this.arn
53
- schedules = {
54
- <% for(const [key,entry] of scheduleEntries) { %>
55
- "<%= key %>" = {
56
- name = "<%= key %>"
57
- pattern = "<%= entry.trigger.options.pattern %>"
58
- timezone = "UTC"
59
- state = "DISABLED"
60
- retry_policy = {
61
- maximum_retry_attempts = 3
62
- }
63
- }
64
- <% } %>
65
- }
66
- }
67
- <% } %>
@@ -1,63 +0,0 @@
1
- import fs from "fs";
2
- import path from "path";
3
- import Fastify, { FastifyInstance } from "fastify";
4
- import { initialize } from "@anubis/datastore";
5
- import { logger } from "@anubis/utils";
6
-
7
- <% for(const component of components){ %>
8
- import <%=component%>Entries from "./<%=component%>/entrypoint.ts" <% }
9
- %>
10
-
11
- async function docs(app: FastifyInstance) {
12
- const docsBuildPath = path.resolve("../../../.dist/docs");
13
-
14
- app.get("/index.js", function (request, reply) {
15
- const stream = fs.createReadStream(path.resolve(docsBuildPath, "index.js"));
16
- reply.type("text/javascript").send(stream);
17
- });
18
-
19
- app.get("/index.js.map", function (request, reply) {
20
- const stream = fs.createReadStream(path.resolve(docsBuildPath, "index.js.map"));
21
- reply.type("text/javascript").send(stream);
22
- });
23
-
24
- app.get("/index.css", function (request, reply) {
25
- const stream = fs.createReadStream(path.resolve(docsBuildPath, "index.css"));
26
- reply.type("text/javascript").send(stream);
27
- });
28
-
29
- app.get("*", function (request, reply) {
30
- const filePath = path.resolve(docsBuildPath, "index.html");
31
- const stream = fs.createReadStream(filePath);
32
- reply.type("text/html").send(stream);
33
- });
34
- }
35
-
36
- const fastify = Fastify({
37
- maxParamLength : 1000,
38
- logger: {
39
- transport: {
40
- target: 'pino-pretty',
41
- options: {
42
- colorize: true,
43
- translateTime: 'HH:MM:ss Z',
44
- ignore: 'pid,hostname',
45
- },
46
- }
47
- }
48
- });
49
-
50
- // Register the fastify-raw-body plugin
51
- fastify.register(require('fastify-raw-body'), {
52
- field: 'rawBody',
53
- encoding: 'utf8',
54
- });
55
-
56
- fastify.register(docs, { prefix: "docs" });
57
- <% for(const component of components){ %>
58
- fastify.register(<%=component%>Entries, { prefix: "api" });
59
- <%}%>
60
- fastify.listen({ port: 3000, host : "::" }).then((address) => {
61
- logger.setLogger(fastify.log as any);
62
- initialize();
63
- });
@@ -1,50 +0,0 @@
1
- import { Context, S3Event,
2
- APIGatewayProxyEventV2WithJWTAuthorizer,
3
- ScheduledEvent
4
- } from "aws-lambda";
5
-
6
- import { createRouter, httpMatcher, scheduleMatcher } from "@anubis/cloud-provider";
7
- import { functions } from "@anubis/aws-cloud-provider";
8
- import { initialize } from "@anubis/datastore";
9
- import { logger } from "@anubis/utils";
10
-
11
- import * as src from "../../packages/components/<%=component%>/src";
12
-
13
- type AWSEventType = S3Event | APIGatewayProxyEventV2WithJWTAuthorizer | ScheduledEvent;
14
-
15
- const router = createRouter();
16
- <%_ for(var [key,entry] of Object.entries(entries)) { _%>
17
- <%_ if(entry.trigger.type.toLowerCase() === "http") { _%>
18
- router.add(httpMatcher("<%=entry.trigger.options.method%>", "<%=entry.trigger.options.route%>"), src["<%= entry.handler %>"])
19
- <%_ } _%>
20
- <%_ if(entry.trigger.type.toLowerCase() === "schedule") { _%>
21
- router.add(scheduleMatcher("<%= key %>"), src["<%= entry.handler %>"])
22
- <%_ } _%>
23
- <%_ } _%>
24
-
25
- export async function handler(event: AWSEventType, context: Context) {
26
- logger.initialize("locations", context.awsRequestId);
27
- logger.info("request started");
28
- logger.debug({ event }, "Received event");
29
-
30
- try {
31
- await initialize();
32
- let output = null;
33
- if ("rawPath" in event) {
34
- output = await router.run("Http", functions.httpAdapter, event, context);
35
- }
36
- else if ("source" in event) {
37
- output = await router.run("Schedule", functions.scheduleAdapter, event, context);
38
- }
39
- else {
40
- throw new Error("invalid handler");
41
- }
42
- logger.debug({ output }, "Response Sent");
43
- logger.info("request completed");
44
- return output;
45
- }
46
- catch (error) {
47
- logger.error({ error }, "Error occurred");
48
- throw error;
49
- }
50
- }
@@ -1,120 +0,0 @@
1
- import { FastifyInstance } from "fastify";
2
- import chalk from "chalk";
3
- import consola from "consola";
4
- import logSymbols from "log-symbols";
5
- import { AddressInfo } from "net";
6
- const querystring = require('querystring');
7
-
8
- import {
9
- HTTPAuth,
10
- HttpMethod,
11
- createHttpContext,
12
- createScheduleContext,
13
- } from "@anubis/cloud-provider";
14
- import { env } from "@anubis/utils";
15
-
16
- import * as src from "../../packages/components/<%=component%>/src";
17
-
18
- <%_
19
- const convertRouteParamstoFastifyRouteTemplate = (route) => {
20
- return route.replace(/{(.*?)}/g, (sub) => ":" + sub.slice(1, -1))
21
- }
22
- _%>
23
- function logEntryURL(method: string, routePath: string){
24
- const bgcolors = {
25
- PUT : chalk.bold.bgYellow.white,
26
- POST : chalk.bold.bgGreen.white,
27
- DELETE : chalk.bold.bgRed.white,
28
- GET : chalk.bold.bgBlue.white,
29
- PATCH : chalk.bold.bgGreenBright.white
30
- };
31
- const colors = {
32
- PUT : chalk.bold.yellow,
33
- POST : chalk.bold.green,
34
- DELETE : chalk.bold.red,
35
- GET : chalk.bold.blue,
36
- PATCH : chalk.bold.greenBright
37
- };
38
-
39
- const prefix = new Array(Math.floor((6 - method.length) / 2)).fill(" ").join("");
40
- const postfix = new Array(6 - method.length - prefix.length).fill(" ").join("");
41
-
42
- consola.log(
43
- logSymbols.info + bgcolors[method](`[${prefix}${method}${postfix}]`) + colors[method](` http://localhost:3000/api${routePath}`)
44
- );
45
- }
46
-
47
- async function entries(app: FastifyInstance) {
48
- consola.log(chalk.underline(`\n\rUrls you have in`, chalk.bold(`<%=component%>`), `component:`));
49
- let routePath;
50
- <%_ for (const [name, entry] of Object.entries(entries)) { _%>
51
- <%_ if (entry.trigger.type === "http") { _%>
52
-
53
- routePath = `/http<%= convertRouteParamstoFastifyRouteTemplate(entry.trigger.options.route) %>`;
54
- logEntryURL('<%= entry.trigger.options.method %>',routePath);
55
- app.route({
56
- method: "<%= entry.trigger.options.method %>" as any,
57
- url: routePath,
58
- handler: async function (request, reply) {
59
-
60
- const rawBody = request.rawBody as string;
61
- const baseAddress = app.server.address() as AddressInfo;
62
- const isAuth = env("DEV_IDENTITY")
63
- const ctx = createHttpContext({
64
- body: rawBody,
65
- headers: request.headers,
66
- method: request.method as HttpMethod,
67
- url: `http://localhost:${baseAddress!.port}/api${routePath}`,
68
- matchingKey: "<%= entry.trigger.options.route %>",
69
- params: request.params as Record<string, string>,
70
- rawQueryString : querystring.stringify(request.query),
71
- host: request.hostname
72
- }, isAuth ? {
73
- appId: env("DEV_APP_ID"),
74
- memberId: env("DEV_MEMBER_ID"),
75
- customerId: env("DEV_CUSTOMER_ID"),
76
- identity: env("DEV_IDENTITY") as HTTPAuth["identity"],
77
- token: "",
78
- claims: {
79
- app_id: env("DEV_APP_ID"),
80
- member_id: env("DEV_MEMBER_ID"),
81
- customer_id: env("DEV_CUSTOMER_ID"),
82
- identity: env("DEV_IDENTITY")
83
- }
84
- } : undefined);
85
- try {
86
- await src["<%= entry.handler %>"](ctx);
87
- reply
88
- .headers(ctx.response.headers || {})
89
- .status(ctx.response.status)
90
- .send(ctx.response.body);
91
- }
92
- catch(e){
93
- reply.send(e);
94
- }
95
- }
96
- });
97
-
98
- <%_ } _%>
99
- <%_ if (entry.trigger.type === "schedule") { _%>
100
- logEntryURL('GET','/schedule/<%= name %>');
101
- app.route({
102
- method: "GET",
103
- url: `/schedule/<%= name %>`,
104
- handler: async function (request, reply) {
105
- const ctx = createScheduleContext("<%= name %>");
106
- try {
107
- await src["<%= entry.handler %>"](ctx);
108
- reply.status(200).send("success");
109
- }
110
- catch (e) {
111
- reply.send(e);
112
- }
113
- }
114
- });
115
-
116
- <%_ } _%>
117
- <%_ } _%>
118
- }
119
-
120
- export default entries;
@@ -1,67 +0,0 @@
1
- terraform {
2
- backend "s3" {
3
- key = "<%= component %>/terraform.state"
4
- workspace_key_prefix = "state/<%= component %>"
5
- }
6
-
7
- required_providers {
8
- aws = {
9
- source = "hashicorp/aws"
10
- version = ">= 4.0"
11
- }
12
- }
13
-
14
- required_version = ">= 0.13.1"
15
- }
16
-
17
- data "aws_lambda_function" "this" {
18
- function_name = "<%= component %>"
19
- }
20
-
21
- data "aws_apigatewayv2_apis" "this" {
22
- tags = {
23
- application = "anubis"
24
- }
25
- }
26
-
27
- <% const httpEntries = Object.entries(entries).filter(([k,e]) => e.trigger.type.toLowerCase() === "http") %>
28
- <% if(httpEntries.length > 0) { %>
29
- module "http_trigger_for_<%= component %>" {
30
- source = "../../.deploy/aws/http"
31
- api_gateway_id = sort(data.aws_apigatewayv2_apis.this.ids)[0]
32
- lambda_arn = data.aws_lambda_function.this.arn
33
- lambda_name = "<%= component %>"
34
- entrypoints = {
35
- <% for(const [key,entry] of httpEntries){ %>
36
- "<%= key %>" = {
37
- description = "<% entry.trigger.options.description %>"
38
- method = "<%= entry.trigger.options.method %>"
39
- path = "<%= entry.trigger.options.route %>"
40
- }
41
- <% } %>
42
- }
43
- }
44
-
45
- <% } %>
46
-
47
- <% const scheduleEntries = Object.entries(entries).filter(([k,e]) => e.trigger.type.toLowerCase() === "schedule") %>
48
- <% if(scheduleEntries.length > 0) { %>
49
- module "schedule_trigger_for_<%= component %>" {
50
- source = "../../.deploy/aws/scheduler"
51
- component = "<%= component %>"
52
- target_arn = data.aws_lambda_function.this.arn
53
- schedules = {
54
- <% for(const [key,entry] of scheduleEntries) { %>
55
- "<%= key %>" = {
56
- name = "<%= key %>"
57
- pattern = "<%= entry.trigger.options.pattern %>"
58
- timezone = "UTC"
59
- state = "DISABLED"
60
- retry_policy = {
61
- maximum_retry_attempts = 3
62
- }
63
- }
64
- <% } %>
65
- }
66
- }
67
- <% } %>