-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.js
498 lines (420 loc) · 13.7 KB
/
run.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
'use strict'
const shell = require('shelljs')
const path = require('path')
const cla = require('command-line-args')
const u = require('@elife/utils');
const shortid = require('shortid');
const ssbKeys = require('ssb-keys')
const secret_ = require('@elife/secret')
const client = require('./client.js')
/* understand/
* Main entry point for our program
*/
function main() {
if(process.argv[2] == 'do') return clientDo()
let args = getArgs()
if(args.help) showHelp()
else if(args['info']) showInfo(args)
else if(args['gui']) launchGUI()
else if(args['rm-node-modules']) removeNodeModules()
else if(args['rm-package-lock']) removePackageLock()
else if(args['gen-docs']) generateDocs()
else if(args['docker-port-param']) dockerPortCmd(args)
else {
setupAvatarComponents()
setupEnvironmentVariables(args)
setupHomeFolders()
migrateOldData()
checkCoteConnection()
showCotePartition()
setupUserConfig()
setupKeys(startAvatar)
}
}
function getArgs() {
const ops = [
{ name: 'help', alias: 'h', type: Boolean },
{ name: 'gui', alias: 'g', type: Boolean },
{ name: 'info', alias: 'i', type: Boolean },
{ name: 'rm-node-modules', type: Boolean },
{ name: 'rm-package-lock', type: Boolean },
{ name: 'gen-docs', type: Boolean },
{ name: 'node-num', alias: 'n' },
{ name: 'docker-port-param', type: Boolean },
{ name: 'do', alias: 'd', type: String, multiple: true },
]
return cla(ops)
}
function showHelp() {
shell.echo(`Start avatar node
--info,i : Show information about the installed avatar
--gui,g : Run the default GUI client (QWERT)
Also accepts the following options (usually for devs)
--rm-node-modules : Remove all node modules
--rm-package-lock : Remove all package locks
--node-num=x, -n x : Start as node number 'x'
--help, -h : show this help
`)
}
function showInfo(args) {
setupEnvironmentVariables(args)
const { version } = require('./package.json')
const SSB_PORT = process.env["SSB_PORT"]
const SSB_WS_PORT = process.env["SSB_WS_PORT"]
shell.echo(`Avatar node (version ${version})`)
shell.echo(`Installed in:`)
shell.echo(` ${shell.pwd()}`)
shell.echo(`Data stored in: (BACKUP THIS FOLDER)`)
shell.echo(` ${u.dataLoc()}`)
shell.echo(`Replication Ports: (Open in firewall)`)
shell.echo(` ${SSB_PORT}, ${SSB_WS_PORT}`)
}
function launchGUI() {
let r
r = shell.pushd('-q', 'qwert')
if(r.code) {
shell.echo('Failed to enter qwert/ directory')
shell.exit(1)
}
r = shell.exec(`npm start`)
if(r.code) {
shell.echo(`Failed to run 'npm start' in qwert/ directory`)
}
shell.popd('-q')
}
function removeNodeModules() {
let structure = avatarStructure()
for(let i = 0;i < structure.length;i++) {
let loc
if(structure[i].required) loc = structure[i].required
if(structure[i].optional) loc = structure[i].optional
if(loc) {
let nm = path.join(loc, 'node_modules')
if(shell.test("-d", nm)) {
shell.echo(`Removing ${nm}`)
let r = shell.rm("-rf", nm)
if(r.code) shell.echo(`Failed to remove ${nm}`)
}
}
}
shell.echo(`Remember to remove './node_modules' manually (needed for this script to run)`)
}
function removePackageLock() {
let structure = avatarStructure()
for(let i = 0;i < structure.length;i++) {
let loc
if(structure[i].required) loc = structure[i].required
if(structure[i].optional) loc = structure[i].optional
if(loc) {
let yl = path.join(loc, 'package-lock.json')
if(shell.test("-f", yl)) {
shell.echo(`Removing ${yl}`)
let r = shell.rm(yl)
if(r.code) shell.echo(`Failed to remove ${yl}`)
}
}
}
shell.rm('package-lock.json')
}
function generateDocs() {
shell.exec(`npm run docs`)
}
/* problem/
* we need the docker container to expose the SSB ports
* which could be dynamically set based on the node number
* way/
* we get the port numbers then create a docker `-p` parameter
* to expose the ports
*/
function dockerPortCmd(args) {
setupEnvironmentVariables(args)
const SSB_PORT = process.env["SSB_PORT"]
const SSB_WS_PORT = process.env["SSB_WS_PORT"]
shell.echo(`-p ${SSB_PORT}:${SSB_PORT} -p ${SSB_WS_PORT}:${SSB_WS_PORT}`)
}
/* outcome/
* The structure of the avatar node - required repos, additional
* directories, and optional repos.
*/
function avatarStructure() {
return [
{ dir: "services" },
{ required: "services/elife-ai" },
{ dir: "services/elife-ai/brains" },
{ required: "services/elife-ai/brains/ebrain-aiml" },
{ required: "services/elife-ai/brains/ebrain-aiml/aiml" },
{ required: "services/elife-level-db" },
{ required: "services/elife-stellar" },
{ required: "services/elife-sbot" },
{ required: "services/elife-communication-mgr" },
{ dir: "services/elife-communication-mgr/channels" },
{ required: "services/elife-communication-mgr/channels/elife-telegram" },
{ required: "services/elife-skill-mgr" },
{ dir: "services/elife-skill-mgr/skills" },
{ required: "services/elife-skill-mgr/skills/eskill-intro" },
{ required: "services/elife-skill-mgr/skills/eskill-about" },
{ required: "services/elife-skill-mgr/skills/eskill-follower" },
{ required: "services/elife-skill-mgr/skills/eskill-nw" },
{ optional: "services/elife-skill-mgr/skills/eskill-vanity-address" },
{ optional: "services/elife-skill-mgr/skills/eskill-kb-creator" },
{ optional: "services/elife-skill-mgr/skills/eskill-direct-message" },
{ optional: "services/elife-skill-mgr/skills/eskill-ai-artist" },
{ optional: "services/elife-skill-mgr/skills/eskill-coupon" },
{ optional: "services/elife-skill-mgr/skills/eskill-path-payment" },
]
}
/* outcome/
* Set up the various avatar components needed (required and optional)
* in the correct directory structures.
*/
function setupAvatarComponents() {
let structure = avatarStructure()
for(let i = 0;i < structure.length;i++) {
let s = structure[i]
if(s.required) if(!install(s)) return false
if(s.dir) if(!mkdir(s.dir)) return false
if(s.optional) install(s)
}
return true
}
/* outcome/
* Set up the environment variables so all the sub-components can access
* them (the home location and the node number and setup COTEJS
* partition environment variable
*/
function setupEnvironmentVariables(args) {
shell.env['ELIFE_INSTALL_FOLDER'] = shell.pwd()
let nn = "0"
if(shell.env['ELIFE_NODE_NUM']) nn = shell.env['ELIFE_NODE_NUM']
if(args['node-num']) nn = args['node-num']
if(isNaN(parseInt(nn))) {
shell.echo(`node-num ${nn} is not a valid integer`)
shell.exit(1)
}
shell.env["ELIFE_NODE_NUM"] = nn
shell.env['COTE_ENV'] = partitionParam()
shell.env['ELIFE_HOME'] = u.homeLoc()
setup_port_vars_1()
function setup_port_vars_1() {
process.env["SSB_PORT"] = u.adjustPort(8191)
process.env["SSB_WS_PORT"] = u.adjustPort(8192)
process.env["QWERT_PORT"] = u.adjustPort(8193)
process.env["EBRAIN_AIML_PORT"] = u.adjustPort(8194)
process.env["AIARTIST_PORT"] = u.adjustPort(8195)
}
}
/* understand/
* Cote.js has a automated discovery service that allows it to find
* matching microservices anywhere on the network that share the same
* 'environment' parameter.
*
* problem/
* We do not want nodes that are near each other (on the same machine or
* on the same network) to start responding to each other's microservice
* requests.
*
* way/
* We returns reasonably-unique identifier that we can use to partition
* cote.js microservice environments and prevent nodes from interfering
* with each other.
*/
function partitionParam() {
return shortid.generate()
}
/* outcome/
* Create the data and skill folders
*/
function setupHomeFolders() {
mkdir(u.dataLoc())
mkdir(u.ssbLoc())
mkdir(u.skillLoc())
mkdir(u.logsLoc())
}
/* problem/
* The current data folders are 'better' located but for those that have
* already installed the avatar's they have their data in different
* locations and should be able to continue to use them.
*
* way/
* If the new data folder is empty we look into the old data folder:
* ../elife.data
* OR
* /data
* and we move all the existing data (__ssb/, kb/, stellar/, level.db/,
* .luminate-pw, cfg.env) to the new folder.
*/
function migrateOldData() {
let dl = u.dataLoc()
let existing = shell.ls(dl)
if(shell.error()) {
shell.echo(`Failed checking data directory for migration: ${dl}`)
shell.exit(1)
}
if(existing.length > 0) return
let old = find_old_data_dir_1()
if(!old) return
shell.echo(`\n\nMigrating from: ${old} to: ${dl}`)
let datalist = [
'__ssb', 'kb', 'stellar', 'level.db', '.luminate-pw', 'cfg.env',
]
for(let i = 0;i < datalist.length;i++) {
let from = path.join(old, datalist[i])
if(shell.test("-e", from)) {
shell.echo(`Moving: ${from} to: ${dl}`)
shell.mv(from, dl)
}
}
shell.echo(`Migration of existing data done...\n\n`)
function find_old_data_dir_1() {
if(shell.test("-d",'/data')) return '/data'
let d = path.join(shell.pwd().toString(),'../elife.data')
if(shell.test("-d",d)) return d
}
}
function checkCoteConnection() {
shell.exec(`node ccc`)
}
/* outcome/
* Show the CoteJS partition parameter
*/
function showCotePartition() {
shell.echo(`
FYI: Microservice Partition Key (for development):
COTE_ENV=${shell.env.COTE_ENV}
`)
}
/* outcome/
* If the user configuration file does not exist, create a template that
* they can fill in.
*/
function setupUserConfig() {
let cfg = path.join(u.dataLoc(), 'cfg.env')
if(shell.test("-f", cfg)) return
shell.echo(`\n\nCreating configuration file...`)
shell.echo(`# understand/
# We use environment variables to configure various skills and services.
# In order to pass the information to the required components we need to
# set them in this file.
# For Telegram Channel
TELEGRAM_TOKEN=
# For what-wine skill
MASHAPE_KEY=
# for AI Artist Skill
AIARTIST_HOST=
AIARTIST_PORT=
`).to(cfg)
shell.echo(`Please edit this file: ${cfg}`)
shell.echo(`To add your own TELEGRAM_TOKEN, etc...\n\n`)
}
function mkdir(d) {
let r = shell.mkdir('-p', d)
if(r.code) {
shell.echo(`Failed to create directory: ${d}`)
return false
}
return true
}
function install(what) {
if(!what.required && !what.optional) return false
let repo = what.required ? what.required : what.optional
let type_ = what.required ? 'required' : 'optional'
shell.echo(`Checking ${type_}: ${repo}`)
if(!createRepo(repo)) return false
if(!setupRepo(repo, what.postInstall)) return false
return true
}
/* outcome/
* Checks if the repo exists otherwise goes to the directory path and
* downloads it
*/
function createRepo(rp) {
if(shell.test("-d", rp)) return true
shell.echo(`Creating: ${rp}`)
let dir = path.dirname(rp)
let repo = path.basename(rp)
let r
r = shell.pushd('-q', dir)
if(r.code) {
shell.echo(`Failed to change directory to: ${dir}`)
shell.popd('-q')
return false
}
r = shell.exec(`git clone https://github.com/everlifeai/${repo}.git`)
if(r.code) {
shell.echo(`Failed to download git repo: ${repo}`)
shell.popd('-q')
return false
}
r = shell.popd('-q')
if(r.code) {
shell.echo(`Failed to return to base directory from creating repo: ${repo}`)
return false
}
return true
}
/* outcome/
* Set up the node_modules and run any post install scripts
*/
function setupRepo(rp, postInstall) {
if(shell.test("-d", path.join(rp,'node_modules'))) return true
if(!shell.test("-f", path.join(rp,'package.json'))) return true
shell.echo(`Setting up: ${rp}`)
let r
r = shell.pushd('-q', rp)
if(r.code) {
shell.echo(`Failed to change directory to: ${rp}`)
return false
}
r = shell.exec(`npm install`)
if(r.code) {
shell.echo(`Failed to npm install in: ${rp}`)
shell.popd('-q')
return false
}
if(postInstall) {
shell.echo(`Running post-install: ${postInstall}`)
r = shell.exec(postInstall)
if(r.code) {
shell.echo(`Failed to run post-install: ${postInstall}`)
shell.popd('-q')
return false
}
}
r = shell.popd('-q')
if(r.code) {
shell.echo(`Failed to return to base directory after setting up: ${rp}`)
return false
}
return true
}
function setupKeys(cb) {
if (shell.test('-f', u.secretFile())) return cb()
shell.echo("Generating Keys");
secret_.create(err => {
if(err) {
shell.echo(err)
shell.echo('Failed to generate keys')
shell.exit(1)
}
cb()
})
}
function startAvatar() {
shell.exec(`npm start`)
}
function clientDo() {
let argv = process.argv.slice(3)
let nn = { "node-num": "0" }
let args = []
for(let i = 0;i < argv.length;i++) {
if(argv[i] == '-n' || argv[i] == '--node-num') {
i++
nn["node-num"] = argv[i]
continue
}
args.push(argv[i])
}
setupEnvironmentVariables(nn)
client.do_(args)
}
main();