555

lin_verse.js 7.0KB

2 yıl önce
2 yıl önce
1 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. batchToRun.forEach(element => {
  85. element.processedArgs = batchToRun[0].processedArgs
  86. });
  87. return any(batchToRun.map(shell_verse.runElevated))
  88. }
  89. , getNonElevatedTask : function( taskToRun ){ return ()=>{ return shell_verse.runNonElevated(taskToRun) } }
  90. , runNonElevated : ( taskToRun ) => {
  91. // Let shell_verse decide whether to Elevate Out of Proc or In Proc
  92. if(__isElevated) {
  93. return Promise.resolve( 'Skipping regular task in elevated shell.' ) // Regular tasks unless marked as common tasks should not run in elevated shell...
  94. }
  95. else {
  96. // taskToRun by default is the launched command and args.
  97. // taskToRun = taskToRun || (()=>{ return op[processedArgs.label || processedArgs._[0] || 'undefined'](processedArgs) })
  98. return taskToRun().then(r=>{
  99. taskToRun.statuslog.statuslog(null, taskToRun.info /*repo*/ )
  100. return r;
  101. }).catch((e) => {
  102. e.info = taskToRun.info;
  103. if(taskToRun.errHandler) throw taskToRun.errHandler(e)
  104. taskToRun.statuslog.statuslog(e);
  105. // console.error(e)
  106. throw e;
  107. }).finally(()=>{})
  108. }
  109. }
  110. , isElevated : ()=>{
  111. return acquireElevationState().then( ()=>{
  112. shell_verse.isElevated = () => {
  113. return Promise.resolve(__isElevated)
  114. }
  115. return shell_verse.isElevated()
  116. })
  117. }
  118. // , isElevationOutOfProc : ()=>{ return true }
  119. , acquireElevationState : () => {
  120. return Promise.resolve(false).then(( elevationstate ) => {
  121. __isElevated ? console.log('Elevated') : console.log('Not Elevated')
  122. __isElevated = elevationstate;
  123. shell_verse.acquireElevationState = ()=> Promise.resolve(__isElevated);
  124. shell_verse.isElevated = () => { return Promise.resolve(__isElevated)}
  125. return __isElevated
  126. }).catch(() => {
  127. __isElevated = false;
  128. shell_verse.acquireElevationState = ()=> Promise.resolve(__isElevated);
  129. shell_verse.isElevated = () => { return Promise.resolve(__isElevated)}
  130. console.log('Not Elevated');
  131. return __isElevated
  132. })
  133. // .finally(()=>{
  134. // shell_verse.acquireElevationState = ()=> Promise.resolve(__isElevated);
  135. // shell_verse.isElevated = () => { return Promise.resolve(__isElevated)}
  136. // return __isElevated; // Value returned from finally is not supported by node.
  137. // })
  138. }
  139. , getTaskCheckExists : cli.createTask('getTaskCheckExists', 'which')
  140. , getbash : ()=>{ return "sh" }
  141. , createJuntionOrLink : (dirOrFile, target, opts)=>{
  142. return nodeShellExec('ln', ['-s', target, dirOrFile], opts).catch((e) => { console.error(e) })
  143. }
  144. , removeJuncionOrLink : ( junctionOrLink )=>{
  145. return nodeShellExec('rm', [junctionOrLink], { inherit: true, shell: true, env: process.env })
  146. }
  147. , requestElevation(elevatedRunner, taskToRun) {
  148. // PB : TODO -- Most linking cmds in linux may not need elevation like in windows.
  149. // However returning true here is pseudo and will break cases where real elevation is required in linux. We need to refactor to reimplement
  150. // this with sudo and change all calles to use ony where required.
  151. return elevatedRunner(taskToRun);
  152. }
  153. , iswin(){ return false}
  154. , islin(){ return true}
  155. }
  156. module.exports = shell_verse