Advertisement
Guest User

Untitled

a guest
May 4th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. #!/usr/bin/env node
  2.  
  3. // Generate a graphviz (graphviz.org) .dot file for the package relationships
  4. // of the currently running N|Solid instances, using the N|Solid 1.3
  5. // package_info command.
  6.  
  7. 'use strict'
  8.  
  9. var fs = require('fs')
  10. var path = require('path')
  11. var child_process = require('child_process')
  12.  
  13. nsolidAgent('info', function (err, infos) {
  14. if (err) {
  15. console.log('error connecting to N|Solid agents:', err.message)
  16. return
  17. }
  18.  
  19. if (infos.length == 0) {
  20. console.log('There are no N|Solid agents running at this time')
  21. return
  22. }
  23.  
  24. console.log('To convert the generated graphviz .dot files to PDF, use the command:')
  25. console.log('')
  26. console.log(' dot -Tpdf -O <file name of .dot file>')
  27. console.log('')
  28.  
  29. infos.forEach(function (info) {
  30. var appId = {
  31. app: info.app,
  32. id: info.id
  33. }
  34.  
  35. getPackageInfo(appId)
  36. })
  37. })
  38.  
  39. function getPackageInfo (appId) {
  40. var app = appId.app
  41. var id = appId.id
  42.  
  43. nsolidAgent('package_info', app, id, function (err, packageInfo) {
  44. if (err) throw err
  45.  
  46. processPackageInfo(app, id, packageInfo)
  47. })
  48. }
  49.  
  50. function processPackageInfo (app, id, packageInfo) {
  51. if (!packageInfo.packages) throw new Error('expecting packages property')
  52.  
  53. // get the package directories
  54. var pkgDirs = packageInfo.packages.map(function (pkg) { return pkg.path })
  55.  
  56. // create a map of package dir : package, and name/version counts
  57. var pkgMap = {}
  58. var pkgCount = {}
  59. packageInfo.packages.forEach(function (pkg) {
  60. pkgMap[pkg.path] = pkg
  61.  
  62. const nameVersion = pkg.name + '@' + pkg.version
  63. if (pkgCount[nameVersion] == null) {
  64. pkgCount[nameVersion] = 1
  65. } else {
  66. pkgCount[nameVersion]++
  67. }
  68. })
  69.  
  70. // turn all the dependencies into refs to their packages
  71. packageInfo.packages.forEach(function (pkg) {
  72. pkg.dependencies = pkg.dependencies.map(function (dep) {
  73. var depPath = path.resolve(pkg.path, dep)
  74. return pkgMap[depPath]
  75. })
  76. })
  77.  
  78. // console.log(app + ' - ' + id)
  79. // console.log('')
  80.  
  81. var oName = app + '-' + id + '.dot'
  82. console.log('creating ' + oName)
  83.  
  84. var oStream = fs.createWriteStream(oName)
  85. oStream.write('digraph packages {\n')
  86. oStream.write('node [style=filled];\n')
  87.  
  88. pkgDirs.forEach(function (pkgDir) {
  89. var pkg = pkgMap[pkgDir]
  90. var pkgNode = pkg.name + '\\n' + pkg.version
  91. var count = pkgCount[pkg.name + '@' + pkg.version]
  92.  
  93. if (count > 1) {
  94. oStream.write('"' + getNodeName(pkg) + '" [color="#FFA0A0"];\n')
  95. }
  96.  
  97. pkg.dependencies.forEach(function (dep) {
  98. oStream.write('"' + getNodeName(pkg) + '" -> "' + getNodeName(dep) + '";\n')
  99. })
  100. })
  101.  
  102. oStream.write('}\n')
  103.  
  104. function getNodeName(pkg) {
  105. var name = pkg.name.replace(/-/g, '-\\n') + '\\n' + pkg.version
  106. var count = pkgCount[pkg.name + '@' + pkg.version]
  107.  
  108. if (count === 1) return name
  109. return name + '\\n' + count + ' copies'
  110. }
  111. }
  112.  
  113.  
  114. function uniquePackageVersions (pkgs) {
  115. var result = []
  116. var found = {}
  117.  
  118. pkgs.forEach(function (pkg) {
  119. var pkgVersion = pkg.name + '@' + pkg.version
  120. if (found[pkgVersion]) return
  121.  
  122. found[pkgVersion] = true
  123.  
  124. result.push({
  125. name: pkg.name,
  126. version: pkg.version
  127. })
  128. })
  129.  
  130. return result
  131. }
  132.  
  133. function nsolidAgent (command, app, id, cb) {
  134. if ((typeof app === 'function') && !id && !cb) {
  135. cb = app
  136. app = null
  137. id = null
  138. }
  139.  
  140. if ((typeof id === 'function') && !cb) {
  141. cb = id
  142. id = null
  143. }
  144.  
  145. var options = []
  146.  
  147. if (app) options.push('--app ' + app)
  148. if (id) options.push('--id ' + id)
  149.  
  150. options = options.join(' ')
  151. if (options !== '') options = ' ' + options + ' '
  152.  
  153. command = 'nsolid-cli' + options + ' ' + command
  154.  
  155. var execOpts = {
  156. maxBuffer: 20 * 1000 * 1000
  157. }
  158.  
  159. child_process.exec(command, execOpts, done)
  160.  
  161. debugLog('running: ' + command)
  162.  
  163. function done (err, stdout, stderr) {
  164. debugLog('ran: ' + command)
  165. if (stderr) debugLog('stderr: ' + stderr)
  166.  
  167. if (err) {
  168. debugLog('err: ' + err)
  169. return cb(err)
  170. }
  171.  
  172. var result = []
  173. var lines = stdout.split('\n')
  174. for (var i = 0; i < lines.length; i++) {
  175. var line = lines[i]
  176. if (line === '') continue
  177.  
  178. try {
  179. line = JSON.parse(line)
  180. } catch (err) {
  181. line = JSON.stringify({err: 'invalid JSON'})
  182. }
  183.  
  184. if (app && id) {
  185. result.push(line)
  186. } else {
  187. result.push(line.reply)
  188. }
  189. }
  190.  
  191. if (app && id) {
  192. cb(null, result[0])
  193. } else {
  194. cb(null, result)
  195. }
  196. }
  197. }
  198.  
  199. function debugLog (message) {
  200. // console.error(message)
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement