Skip to content

Commit 1d4aac5

Browse files
authored
[feat] add monk-bridge entities (#104)
1 parent 772065d commit 1d4aac5

File tree

13 files changed

+1216
-1
lines changed

13 files changed

+1216
-1
lines changed

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ set -ex
77

88
# Default modules if none specified.
99
# No quotes around expansion to allow word splitting for the default value.
10-
modules=(${@:-monkec mongodb-atlas neon netlify vercel aws-dynamo-db aws-s3 aws-sqs aws-iam aws-lambda aws-rds digitalocean-spaces digitalocean-database digitalocean-domains digitalocean-agent digitalocean-container-registry cloudflare aws-api-gateway aws-ec2 aws-cloudfront digitalocean-monitoring})
10+
modules=(${@:-monkec monk-bridge mongodb-atlas neon netlify vercel aws-dynamo-db aws-s3 aws-sqs aws-iam aws-lambda aws-rds digitalocean-spaces digitalocean-database digitalocean-domains digitalocean-agent digitalocean-container-registry cloudflare aws-api-gateway aws-ec2 aws-cloudfront digitalocean-monitoring})
1111

1212
for module in "${modules[@]}"; do
1313
echo "Building $module..."

dist/monk-bridge/MANIFEST

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
REPO monk-bridge
2+
VERSION_HASH 280717edf59d
3+
LOAD cluster.yaml runnable.yaml monk-bridge-base.yaml
4+
RESOURCES cluster-sync.js runnable-sync.js monk-bridge-base.js

dist/monk-bridge/cluster-sync.js

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
2+
// Generated by MonkEC - targeting Goja runtime
3+
var __create = Object.create;
4+
var __defProp = Object.defineProperty;
5+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6+
var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
7+
var __typeError = (msg) => {
8+
throw TypeError(msg);
9+
};
10+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
12+
var __decoratorStart = (base) => [, , , __create(base?.[__knownSymbol("metadata")] ?? null)];
13+
var __decoratorStrings = ["class", "method", "getter", "setter", "accessor", "field", "value", "get", "set"];
14+
var __expectFn = (fn) => fn !== void 0 && typeof fn !== "function" ? __typeError("Function expected") : fn;
15+
var __decoratorContext = (kind, name, done, metadata, fns) => ({ kind: __decoratorStrings[kind], name, metadata, addInitializer: (fn) => done._ ? __typeError("Already initialized") : fns.push(__expectFn(fn || null)) });
16+
var __decoratorMetadata = (array, target) => __defNormalProp(target, __knownSymbol("metadata"), array[3]);
17+
var __runInitializers = (array, flags, self, value) => {
18+
for (var i = 0, fns = array[flags >> 1], n = fns && fns.length; i < n; i++) flags & 1 ? fns[i].call(self) : value = fns[i].call(self, value);
19+
return value;
20+
};
21+
var __decorateElement = (array, flags, name, decorators, target, extra) => {
22+
var fn, it, done, ctx, access, k = flags & 7, s = !!(flags & 8), p = !!(flags & 16);
23+
var j = k > 3 ? array.length + 1 : k ? s ? 1 : 2 : 0, key = __decoratorStrings[k + 5];
24+
var initializers = k > 3 && (array[j - 1] = []), extraInitializers = array[j] || (array[j] = []);
25+
var desc = k && (!p && !s && (target = target.prototype), k < 5 && (k > 3 || !p) && __getOwnPropDesc(k < 4 ? target : { get [name]() {
26+
return __privateGet(this, extra);
27+
}, set [name](x) {
28+
return __privateSet(this, extra, x);
29+
} }, name));
30+
k ? p && k < 4 && __name(extra, (k > 2 ? "set " : k > 1 ? "get " : "") + name) : __name(target, name);
31+
for (var i = decorators.length - 1; i >= 0; i--) {
32+
ctx = __decoratorContext(k, name, done = {}, array[3], extraInitializers);
33+
if (k) {
34+
ctx.static = s, ctx.private = p, access = ctx.access = { has: p ? (x) => __privateIn(target, x) : (x) => name in x };
35+
if (k ^ 3) access.get = p ? (x) => (k ^ 1 ? __privateGet : __privateMethod)(x, target, k ^ 4 ? extra : desc.get) : (x) => x[name];
36+
if (k > 2) access.set = p ? (x, y) => __privateSet(x, target, y, k ^ 4 ? extra : desc.set) : (x, y) => x[name] = y;
37+
}
38+
it = (0, decorators[i])(k ? k < 4 ? p ? extra : desc[key] : k > 4 ? void 0 : { get: desc.get, set: desc.set } : target, ctx), done._ = 1;
39+
if (k ^ 4 || it === void 0) __expectFn(it) && (k > 4 ? initializers.unshift(it) : k ? p ? extra = it : desc[key] = it : target = it);
40+
else if (typeof it !== "object" || it === null) __typeError("Object expected");
41+
else __expectFn(fn = it.get) && (desc.get = fn), __expectFn(fn = it.set) && (desc.set = fn), __expectFn(fn = it.init) && initializers.unshift(fn);
42+
}
43+
return k || __decoratorMetadata(array, target), desc && __defProp(target, name, desc), p ? k ^ 4 ? extra : desc : target;
44+
};
45+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
46+
var __privateIn = (member, obj) => Object(obj) !== obj ? __typeError('Cannot use the "in" operator on this value') : member.has(obj);
47+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
48+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
49+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
50+
51+
// input/monk-bridge/cluster.ts
52+
const base = require("monkec/base");
53+
const action = base.action;
54+
const cli = require("cli");
55+
const monkBridgeBase = require("monk-bridge/monk-bridge-base");
56+
const MonkBridgeBase = monkBridgeBase.MonkBridgeBase;
57+
var ALLOWED = [
58+
"cluster.Info",
59+
"cluster.Peers",
60+
"internal.GetVersion",
61+
"templates.GetState",
62+
"templates.Balancers"
63+
];
64+
var _refresh_dec, _a, _init;
65+
var _Cluster = class _Cluster extends (_a = MonkBridgeBase, _refresh_dec = [action("refresh")], _a) {
66+
constructor() {
67+
super(...arguments);
68+
__runInitializers(_init, 5, this);
69+
}
70+
getBaseAllowedMethods() {
71+
return ALLOWED;
72+
}
73+
create() {
74+
const info = this.safeCall("cluster.Info", null);
75+
const versionResp = this.safeCall("internal.GetVersion", null);
76+
const balancers = this.safeCall("templates.Balancers", null);
77+
let peersMin;
78+
try {
79+
const peers = this.safeCall("cluster.Peers", null);
80+
if (Array.isArray(peers)) {
81+
peersMin = peers.map((p) => ({
82+
id: p?.id || p?.ID || p?.Id || "",
83+
public_ip: p?.publicIP || p?.PublicIP || p?.public_ip,
84+
name: p?.name || p?.Name,
85+
region: p?.region || p?.Region,
86+
provider: p?.provider || p?.Provider,
87+
version: p?.version || p?.Version
88+
})).filter((p) => p.id);
89+
}
90+
} catch {
91+
}
92+
const balancersMap = this.normalizeBalancers(balancers);
93+
const services = this.definition.expose_balancers === false ? {} : this.servicesFromBalancers(balancersMap);
94+
this.state.balancers_list = Object.values(services);
95+
this.state.version = versionResp?.Version || versionResp?.version || "";
96+
this.state.last_synced_at = Date.now();
97+
this.state.existing = true;
98+
if (peersMin) this.state.peers = peersMin;
99+
this.state.cluster_summary_id = info?.ID || info?.Id || info?.id || this.state.cluster_summary_id;
100+
this.state.cluster_summary_name = info?.Name || info?.name || this.state.cluster_summary_name;
101+
this.state.cluster_summary_open_ports = info?.OpenPorts || info?.openPorts || this.state.cluster_summary_open_ports;
102+
this.refreshTemplatesData();
103+
}
104+
update() {
105+
this.create();
106+
}
107+
refresh() {
108+
this.update();
109+
try {
110+
const v = this.safeCall("internal.GetVersion", null);
111+
this.state.version = v?.Version || v?.version || this.state.version || "";
112+
} catch {
113+
}
114+
cli.output("Refreshed remote cluster data");
115+
}
116+
normalizeBalancers(input) {
117+
if (!input) return {};
118+
if (Array.isArray(input)) {
119+
const map = {};
120+
for (const it of input) {
121+
const key = it?.name || it?.aliasName || it?.path || `balancer-${Object.keys(map).length + 1}`;
122+
map[key] = it;
123+
}
124+
return map;
125+
}
126+
return input;
127+
}
128+
servicesFromBalancers(balancers) {
129+
const out = {};
130+
for (const [key, b] of Object.entries(balancers)) {
131+
if (!b || !b.Address) continue;
132+
const port = String(b.FrontendPort || b.Port || "");
133+
const safeName = `balancer-${(b.Name || b.AliasName || key).toLowerCase().replace(/[^a-z0-9-]/g, "-")}-${port}`.replace(/-+/g, "-");
134+
out[safeName] = { address: b.Address, port, protocol: "tcp", publish: true };
135+
}
136+
return out;
137+
}
138+
refreshTemplatesData() {
139+
const f = this.definition.filters || {};
140+
let info = void 0;
141+
if (this.isAllowed("templates.GetState")) {
142+
const payload = {
143+
PeerID: "",
144+
TagsFilter: Array.isArray(f.tags_filter) ? f.tags_filter : [],
145+
NameFilter: "",
146+
Local: Boolean(f.local),
147+
All: true,
148+
FindGroups: true,
149+
FindRunnables: true,
150+
FindEntities: true
151+
};
152+
const state = this.safeCall("templates.GetState", payload);
153+
if (state && Array.isArray(state.ListTree)) {
154+
info = state.ListTree;
155+
} else {
156+
info = state;
157+
}
158+
}
159+
if (info !== void 0) this.state.templates_info = info;
160+
}
161+
};
162+
_init = __decoratorStart(_a);
163+
__decorateElement(_init, 1, "refresh", _refresh_dec, _Cluster);
164+
__decoratorMetadata(_init, _Cluster);
165+
__name(_Cluster, "Cluster");
166+
var Cluster = _Cluster;
167+
168+
169+
170+
function main(def, state, ctx) {
171+
const entity = new Cluster(def, state, ctx);
172+
return entity.main(ctx);
173+
}

dist/monk-bridge/cluster.yaml

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
namespace: monk-bridge
2+
cluster:
3+
defines: entity
4+
metadata:
5+
name: Cluster
6+
version-hash: 280717edf59d
7+
schema:
8+
monkcode_secret_ref:
9+
type: string
10+
allowed_methods:
11+
type: array
12+
items:
13+
type: string
14+
expose_balancers:
15+
type: boolean
16+
filters:
17+
type: object
18+
properties:
19+
path_filter:
20+
type: string
21+
tags_filter:
22+
type: array
23+
items:
24+
type: string
25+
names_filter:
26+
type: array
27+
items:
28+
type: string
29+
local:
30+
type: boolean
31+
additionalProperties: false
32+
fetch_templates_info:
33+
type: boolean
34+
required:
35+
- monkcode_secret_ref
36+
state-schema:
37+
existing:
38+
type: boolean
39+
last_synced_at:
40+
type: number
41+
version:
42+
type: string
43+
balancers_list:
44+
type: array
45+
items:
46+
type: object
47+
properties:
48+
address:
49+
type: string
50+
port:
51+
type: string
52+
protocol:
53+
type: string
54+
publish:
55+
type: boolean
56+
required:
57+
- address
58+
- port
59+
- protocol
60+
- publish
61+
additionalProperties: false
62+
cluster_summary_id:
63+
type: string
64+
cluster_summary_name:
65+
type: string
66+
cluster_summary_open_ports:
67+
type:
68+
- string
69+
- number
70+
templates_info: {}
71+
peers:
72+
type: array
73+
items:
74+
type: object
75+
properties:
76+
id:
77+
type: string
78+
public_ip:
79+
type: string
80+
name:
81+
type: string
82+
region:
83+
type: string
84+
provider:
85+
type: string
86+
version:
87+
type: string
88+
required:
89+
- id
90+
additionalProperties: false
91+
lifecycle:
92+
sync: <<< cluster-sync.js
93+
refresh: ''
94+
requires:
95+
- monkec/base
96+
- cli
97+
- monk-bridge/monk-bridge-base

dist/monk-bridge/monk-bridge-base.js

Lines changed: 100 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace: monk-bridge
2+
3+
monk-bridge-base:
4+
defines: module
5+
metadata:
6+
version-hash: 280717edf59d
7+
source: <<< monk-bridge-base.js

0 commit comments

Comments
 (0)