-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunits.js
More file actions
305 lines (287 loc) · 9.47 KB
/
units.js
File metadata and controls
305 lines (287 loc) · 9.47 KB
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
const fs = require('fs')
const path = require('path')
const beautify = require('json-beautify')
const log = require('loglevel')
const request = require('request-promise-native')
const tree = require('tree-tree')
const FileHound = require('filehound')
const moment = require('moment')
const _ = require('underscore')
const { getApi, createDirIfNotExist, findLocalUnit, urlConstructor, authUser } = require('./util')
module.exports.syncUnits = async (user, watched) => {
const saveUnits = (units, dir, watched) => units.forEach(unit => saveUnit(unit, dir, watched))
const getUnitsAndDeploy = async user => await getApi(user, 'users', user.login, 'sync')
const getAvailableUnits = async user => await getApi(user, 'units')
const appendToArray = (array, unit) => { array.push(unit.id); return array }
let ids
try {
createDirIfNotExist(user.path)
} catch (error) {
throw error
}
try {
const available = (await getAvailableUnits(user)).items
ids = available.reduce(appendToArray, [])
} catch (err) {
throw new Error('Error while getting units. ' + err.message)
}
try {
const units = (await getUnitsAndDeploy(user)).filter(unit => ids.indexOf(unit.id) > -1)
saveUnits(units, user.path, watched)
} catch (err) {
throw new Error('Error while saving units. ' + err.message)
}
}
function saveUnit (unit, dir, watched) {
try {
const unitPath = path.join(dir, unit.name)
const codeFile = unitPath + '/index.js'
const readmeFile = unitPath + '/readme.md'
const configFile = unitPath + '/config.json'
if (watched.indexOf(codeFile) === -1) {
watched.push(codeFile, readmeFile, configFile)
}
log.debug(unitPath, codeFile)
createDirIfNotExist(unitPath)
fs.writeFileSync(codeFile, unit.code)
fs.writeFileSync(readmeFile, unit.readme)
saveParameters(configFile, unit.deploys && unit.deploys[0] ? unit.deploys[0].parameters : [])
} catch (err) {
log.debug(err.message.error)
throw err
}
}
function saveParameters (path, parameters) {
try {
let config = { secret: {}, public: {} }
const addToParams = param => { config[param.type][param.name] = param.value }
parameters.filter(param => param && param.name).forEach(addToParams)
fs.writeFileSync(path, beautify(config, null, 2, 10)) // magic numbers
} catch (err) {
throw err
}
}
module.exports.createNewUnit = async (user, watched, name, description = '', isPublic = true) => {
// const parseErrorFromServer = (err) =>
if (!name) throw new Error('No name to create unit')
let unit = {
name: name,
language: 'javascript',
code: '',
readme: '',
parameters: []
}
try {
const result = await request
.post({
url: user.server + '/api/units',
headers: {
Authorization: 'UCKEY ' + user.key
},
form: {
name: name,
description: description,
public: isPublic // true by default
}
})
unit = JSON.parse(result)
saveUnit(unit, user.path, watched)
log.debug(typeof result)
return user.path + '/' + unit.name // return path to unit
} catch (error) {
log.debug(error)
if (error.name === 'StatusCodeError') {
const messageObj = JSON.parse(JSON.parse(error.message.replace(/.*?"/, '"'))) // dont ask me why
throw new Error(messageObj.message)
}
throw new Error('Cannot create unit. ' + error.message)
}
// log.info('Unit created at'.debug, colors.strikethrough(user.path + '/' + name), 'you can edit it now'.debug)
// @TODO: continue improve module creating
}
module.exports.printUnits = (user, level = 0) => {
const toObject = (name, children = []) => ({ name: name, children: children })
const getSubFiles = (path) => FileHound.create().path(path).findSync()
const getLocalUnitsList = (user) => FileHound.create().paths(user.path).directory().findSync()
if (!user) throw new Error('No user')
const units = getLocalUnitsList(user)
const list = units.reduce((list, source) => {
const name = path.parse(source).name
const children = level ? getSubFiles(source).map(file => toObject(path.parse(file).base)) : []
list.push(toObject(name, children))
return list
}, [])
try {
log.info(tree(toObject(`[${user.login}] ${user.path}`.cyan, list)))
} catch (err) {
log.error(err)
}
}
module.exports.updateUnit = async (unitName, user, newContent) => {
try {
const deploy = await getDeploy(unitName, user) || await createNewDeploy(unitName, user, false)
log.debug('[CONTENT]'.debug, newContent)
log.debug('[DEPLOY]'.debug, JSON.stringify(deploy))
if (!deploy.unit) { // deploy from create deploy does not contan unit, @BUG
deploy.unit = {
code: deploy.code,
parameters: deploy.parameters,
readme: ''
}
}
let options = {
method: 'PATCH',
url: user.server + '/' + path.join('api', 'units', user.login, unitName),
headers: {
Authorization: 'UCKEY ' + user.key
},
form: {
code: newContent.code ? newContent.code : deploy.unit.code,
readme: newContent.readme ? newContent.readme : deploy.unit.readme,
parameters: newContent.parameters ? newContent.parameters : deploy.parameters,
deployment_id: deploy.id,
full_name: user.login + '/' + unitName
}
}
options.form.parameters = options.form.parameters.map(param => _.pick(param, 'name', 'type', 'value')) // clean params
log.debug('[OPTIONS]'.debug, options)
return await request(options)
} catch (err) {
if (err.message === 'No deploy for unit') throw err
log.debug(err, err.stack)
throw new Error('Error while updating unit. ' + err.message)
}
}
async function getDeploy (unitName, user = []) {
let deploys = await getApi(user, 'units', user.login, unitName, 'deployed')
if (deploys.stats > 0) {
return deploys.units[0]
} else {
return null
}
}
module.exports.deleteUnit = (user, argsArray) => {
const deleteLocalModule = (source) => {
try {
fs.readdirSync(source).forEach(file => fs.unlinkSync(path.join(source, file)))
fs.rmdirSync(source)
} catch (err) {
throw new Error('Error while deleting unit localy. ' + err.message)
}
}
const requestDelete = (user, name) => {
try {
const options = {
url: `${user.server}/api/units/${user.login}/${name}`,
headers: {
Authorization: 'UCKEY ' + user.key
},
method: 'DELETE'
}
request(options)
return
} catch (error) {
throw new Error(`Error while deleting unit "${name}" from server`)
}
}
const result = argsArray
.map(arg => typeof arg === 'string' ? arg : JSON.stringify(arg))
.map(arg => findLocalUnit(user, arg))
.filter(arg => !!arg)
result.forEach(source => requestDelete(user, path.parse(source).name))
result.forEach(source => deleteLocalModule(source))
if (!result.some(path => !!path)) throw new Error('No units found'.yellow)
else return argsArray.join(' ')
}
module.exports.runUnit = async (user, unitName) => {
// get deploy name
// run deploy
// get logs from deploy
if (!unitName || !findLocalUnit(user, unitName)) throw new Error('No such unit')
let deploy = await getDeploy(unitName, user)
if (!deploy) {
// create new deploy
deploy = await createNewDeploy(unitName, user, false) // public: false
}
printUnitLogs(deploy.name, user)
runDeploy(deploy.name, user)
}
async function createNewDeploy (unitName, user, isPublic) {
let options = {
url: `${user.server}/api/units/${user.login}/${unitName}/deploy`,
headers: authUser(user),
method: 'POST',
form: {
name: unitName,
public: isPublic,
full_name: user.login + '/' + unitName
}
}
log.debug('GET'.red, options)
let result
try {
result = await request(options)
} catch (error) {
log.error(error)
}
log.trace(result)
return JSON.parse(result)
}
async function runDeploy (name, user) {
if (!name) return
log.debug('[RUN UNIT]'.debug, name)
let result
try {
const options = {
url: urlConstructor('run', user, name),
method: 'GET',
headers: authUser(user)
}
log.debug('GET'.red, options.url)
try {
result = await request(options)
} catch (requestError) {
const error = JSON.parse(requestError.error)
log.error('[Module %s error] '.error + error.error, name)
if (error.position.line) { log.error('at line', error.position.line) } else { log.error('%s'.error, error.position) }
}
} catch (error) {
log.error(error)
}
if (!result) return
log.info('[ %s-result ]'.green, name, result)
return result
}
async function printUnitLogs (unitName, user) {
const url = urlConstructor('logs', user, unitName)
log.debug('[LOG]'.red, url)
const logStream = request(url)
let lastTs = null
logStream.on('data', (chunk) => {
// Parse logs
log.debug(chunk.toString())
if (chunk.toString().substring(0, 5) === 'data:') {
let message
try {
message = JSON.parse(chunk.toString().substring(5))
} catch (error) {
log.debug('[Chunk with error]'.red, chunk.toString())
return
}
if (message.log) {
if (lastTs !== message.ts) {
log.info('[ %s ]'.green, message.ts)
lastTs = message.ts
}
log.info('[ %s ]'.green, message.slot, message.log)
}
if (message.memory || message.cpu) {
// do something with stats
}
}
})
logStream.on('end', () => {
log.debug('[ %s-%s ]'.green, unitName, moment().format())
log.debug('End of logs')
})
}