-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommon.js
162 lines (152 loc) · 5.27 KB
/
common.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* eslint no-console:0 */
//
// Use a Custom Resource Definition to extend the Kubernetes API and the client.
//
var exports = module.exports = {};
let fs = require('fs');
const path = require('path');
exports.delete_env = async function(client, env_name, nmspace) {
const del_env = await client.apis['fission.io'].v1.namespaces(nmspace).environments(env_name).delete();
console.log("Fission envirnment ", del_env['body']['details']['name'], " deleted");
}
exports.delete_fn = async function(client, fn_name, nmspace) {
const del_fn = await client.apis['fission.io'].v1.namespaces(nmspace).functions(fn_name).delete();
console.log("Fission function ", del_fn['body']['details']['name'], " deleted");
}
exports.fn_code = async function(client, fn_name, nmspace) {
const fn_data = await client.apis['fission.io'].v1.namespaces(nmspace).functions(fn_name).get();
const pkg_name = fn_data['body']['spec']['package']['packageref']['name'];
const pkg_namespace = fn_data['body']['spec']['package']['packageref']['namespace'];
const pkg_data = await client.apis['fission.io'].v1.namespaces(pkg_namespace).packages(pkg_name).get();
const pkg_code = pkg_data['body']['spec']['deployment']['literal'];
const pkg_decode = new Buffer(pkg_code, 'base64');
console.log("Code for", fn_name, "in", nmspace, ":\n");
console.log(pkg_decode.toString());
}
exports.fn_info = async function(client, fn_name, nmspace) {
const fn_info = await client.apis['fission.io'].v1.namespaces(nmspace).functions(fn_name).getStream();
console.log("Detailed logs and Information for", fn_name, "in ", nmspace, ":\n");
console.log(fn_info);
}
exports.create_env = async function(client, env_name, nmspace, img) {
const env = {
apiVersion: 'fission.io/v1',
kind: 'Environment',
metadata: {
name: env_name,
namespace: nmspace,
},
spec: {
TerminationGracePeriod: 360,
builder: {},
keeparchive: false,
poolsize: 1,
resources: {},
runtime: {
functionendpointport: 0,
image: img,
loadendpointpath: '',
loadendpointport: 0
},
version: 1
}
}
const envs = await client.apis['fission.io'].v1.namespaces(nmspace).environments.post({
body: env
});
console.log("Fission environment ", envs['body']['metadata']['name'], " created");
}
async function create_func(client, funcname, env_name, pkg_name, nmspace) {
const fn = {
apiVersion: 'fission.io/v1',
kind: 'Function',
metadata: {
clusterName: '',
name: funcname,
namespace: nmspace,
},
spec: {
InvokeStrategy: {
ExecutionStrategy: ['poolmgr'],
StrategyType: 'execution'
},
configmaps: null,
environment: {
name: env_name,
namespace: nmspace,
},
package: {
packageref: {
namespace: nmspace,
name: pkg_name['metadata']['name'],
resourceVersion: pkg_name['metadata']['resourceVersion'],
}
},
resources: {},
secrets: null
}
}
const fns = await client.apis['fission.io'].v1.namespaces(nmspace).functions.post({
body: fn
});
console.log("Fission function ", fns['body']['metadata']['name'], " created");
}
async function create_pkgs(client, archive, new_name, nmspace, env_name) {
const pkg = {
apiVersion: 'fission.io/v1',
kind: 'Package',
metadata: {
clusterName: '',
name: new_name,
namespace: nmspace
},
spec: {
deployment: archive,
environment: {
name: env_name,
namespace: nmspace
},
source: {
checksum: {}
}
}
}
const pkgs = await client.apis['fission.io'].v1.namespaces(nmspace).packages.post({
body: pkg
});
return pkgs;
}
function makeid() {
var text = "";
var possible = "abcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 5; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
function get_contents(filePath, cb) {
fs.readFile(filePath, {
encoding: 'utf-8'
}, function(err, data) {
if (err) {
console.log(err);
} else {
cb(data);
}
});
}
exports.create_func_pkg = async function(client, name, env_name, code, nmspace) {
const filename = code;
// if http or https download to temp dir
let parentDir = path.resolve(process.cwd(), '.');
const filePath = path.join(parentDir, filename);
get_contents(filePath, async function(data) {
const archive = {
type: 'literal',
literal: Buffer(data).toString('base64'),
checksum: {}
}
const new_name = name + "-" + "js" + makeid();
const pkg_name = await create_pkgs(client, archive, new_name, nmspace, env_name);
create_func(client, name, env_name, pkg_name['body'], nmspace);
});
}