您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

lin_verse.js 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. const { any } = require('bbhverse');
  2. const fs = require('fs')
  3. var cli = require('./cliverse')
  4. var nodeShellExec = cli.nodeShellExec;
  5. // PB : TODO -- linux prerequistes.
  6. // export GIT_SSL_NO_VERIFY=true
  7. // npm i -g nodemon
  8. // phantomjs": "^2.1.7
  9. // "sass": "^1.32.7",
  10. // "node-sass": "^5.0.0",
  11. // "ember-cli-sass": "^10.0.1",
  12. // "gulp-sass": "^4.1.0",
  13. // ember-searchable-select
  14. // ember-cli-sass
  15. // ember-cp-validatations
  16. // ember-cli-yuidoc
  17. // yuidoc-ember-theme
  18. // chess-server-lib/server
  19. // java
  20. // client
  21. // export NODE_OPTIONS=--openssl-legacy-provider
  22. // elixir-client npmi
  23. // elixir-client/chess-client-lib npmi
  24. // crossfilter2
  25. var __isElevated = null;
  26. var shell_verse = {
  27. // getCommonTask is agnostic of whether we are running in an elevated shell or not. It runs in either case.
  28. getCommonTask( taskToRun ){ return ()=>{ return shell_verse.runTask(taskToRun) }}
  29. , runTask : ( taskToRun ) => {
  30. if (__isElevated) return shell_verse.runElevated( taskToRun )
  31. else return shell_verse.runNonElevated( taskToRun )
  32. }
  33. , elevatedRunner( taskToRun ){
  34. // PB : TODO -- Should be called only when we are in an elevated shell that was already requested from an unelevated shell with a batch of tasks.
  35. try {
  36. // PB : We do not need IPC in linux. Until a real sudo elevation is required eveything works in non elevated mode...
  37. var __runasresult = null;
  38. return taskToRun().then((r)=>{
  39. // PB : TODO -- Every elevation should have its own messaging file. Async writes from multiple processes are a problem here...
  40. // fs.writeFileSync('run.log', ', ' + JSON.stringify( { info : taskToRun.info, success: true }), { 'flag': 'a+' })
  41. // fs.writeFileSync('run.done', 'success') // PB : TODO -- This should be done conditionally if we are running inproc.
  42. return __runasresult = r;
  43. })
  44. .catch((e) => {
  45. // fs.writeFileSync('run.log', ', ' + JSON.stringify(e), { 'flag': 'a+' })
  46. // fs.writeFileSync('run.done', 'failure')
  47. console.error(e)
  48. })
  49. .finally(() => {
  50. // if(__runasresult && !__runasresult.skipped) fs.unlinkSync('run.done')
  51. })
  52. }
  53. catch (e) {
  54. console.error('Error Invalid command : ' + e)
  55. // fs.writeFileSync('run.done', 'error')
  56. }
  57. finally {
  58. }
  59. }
  60. , getElevatedTask : function( taskToRun ){ return ()=>{ return shell_verse.runElevated(taskToRun) }}
  61. , getElevatedTaskInBatch : function( taskToRun ){ return ()=>{ return shell_verse.runElevatedInBatch(taskToRun) }}
  62. , runElevatedInBatch : ( taskToRun ) => {
  63. if (__isElevated) return shell_verse.elevatedRunner(taskToRun, true)
  64. else return shell_verse.requestElevation(shell_verse.elevatedRunner, taskToRun)
  65. }
  66. , runElevated : ( taskToRun ) => {
  67. // Let shell_verse decide whether to Elevate Out of Proc or In Proc
  68. // taskToRun by default is the launched command and args. Specially in windows out of proc.
  69. // taskToRun = taskToRun || (()=>{ return op[processedArgs.label || processedArgs._[0] || 'undefined'](processedArgs) })
  70. if(taskToRun.processedArgs.skipelevated) return Promise.resolve({ skipped : true });
  71. if(__isElevated) {
  72. return shell_verse.elevatedRunner(taskToRun)
  73. }
  74. else {
  75. console.log('Requesting Elevated Privileges');
  76. // requesteElevation is acutally request elevation and run. Both In Proc and Out of Proc.
  77. // Linux doesnt require elevation for most commands...
  78. return shell_verse.requestElevation(shell_verse.elevatedRunner, taskToRun)
  79. }
  80. }
  81. , runElevatedBatch( batchToRun ){
  82. // In windows we don't need to run each task. We hand over to another shell which in elevated state rebuilds the whole batch and runs.
  83. // Irrespective of the batch we just call runElevated once.
  84. return any(batchToRun.map(shell_verse.runElevated))
  85. }
  86. , getNonElevatedTask : function( taskToRun ){ return ()=>{ return shell_verse.runNonElevated(taskToRun) } }
  87. , runNonElevated : ( taskToRun ) => {
  88. // Let shell_verse decide whether to Elevate Out of Proc or In Proc
  89. if(__isElevated) {
  90. return Promise.resolve( 'Skipping regular task in elevated shell.' ) // Regular tasks unless marked as common tasks should not run in elevated shell...
  91. }
  92. else {
  93. // taskToRun by default is the launched command and args.
  94. // taskToRun = taskToRun || (()=>{ return op[processedArgs.label || processedArgs._[0] || 'undefined'](processedArgs) })
  95. return taskToRun().then(r=>{
  96. taskToRun.statuslog.statuslog(null, taskToRun.info /*repo*/ )
  97. return r;
  98. }).catch((e) => {
  99. e.info = taskToRun.info;
  100. if(taskToRun.errHandler) throw taskToRun.errHandler(e)
  101. taskToRun.statuslog.statuslog(e);
  102. // console.error(e)
  103. throw e;
  104. }).finally(()=>{})
  105. }
  106. }
  107. , isElevated : ()=>{
  108. return acquireElevationState().then( ()=>{
  109. shell_verse.isElevated = () => {
  110. return Promise.resolve(__isElevated)
  111. }
  112. return shell_verse.isElevated()
  113. })
  114. }
  115. // , isElevationOutOfProc : ()=>{ return true }
  116. , acquireElevationState : () => {
  117. return Promise.resolve(false).then(( elevationstate ) => {
  118. __isElevated ? console.log('Elevated') : console.log('Not Elevated')
  119. __isElevated = elevationstate;
  120. shell_verse.acquireElevationState = ()=> Promise.resolve(__isElevated);
  121. shell_verse.isElevated = () => { return Promise.resolve(__isElevated)}
  122. return __isElevated
  123. }).catch(() => {
  124. __isElevated = false;
  125. shell_verse.acquireElevationState = ()=> Promise.resolve(__isElevated);
  126. shell_verse.isElevated = () => { return Promise.resolve(__isElevated)}
  127. console.log('Not Elevated');
  128. return __isElevated
  129. })
  130. // .finally(()=>{
  131. // shell_verse.acquireElevationState = ()=> Promise.resolve(__isElevated);
  132. // shell_verse.isElevated = () => { return Promise.resolve(__isElevated)}
  133. // return __isElevated; // Value returned from finally is not supported by node.
  134. // })
  135. }
  136. , getTaskCheckExists : cli.createTask('getTaskCheckExists', 'which')
  137. , getbash : ()=>{ return "sh" }
  138. , createJuntionOrLink : (dirOrFile, target, opts)=>{
  139. return nodeShellExec('ln', ['-s', target, dirOrFile], opts).catch((e) => { console.error(e) })
  140. }
  141. , removeJuncionOrLink : ( junctionOrLink )=>{
  142. return nodeShellExec('rm', [junctionOrLink], { inherit: true, shell: true, env: process.env })
  143. }
  144. , requestElevation(elevatedRunner, taskToRun) {
  145. // PB : TODO -- Most linking cmds in linux may not need elevation like in windows.
  146. // However returning true here is pseudo and will break cases where real elevation is required in linux. We need to refactor to reimplement
  147. // this with sudo and change all calles to use ony where required.
  148. return elevatedRunner(taskToRun);
  149. }
  150. , iswin(){ return false}
  151. , islin(){ return true}
  152. }
  153. module.exports = shell_verse