You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lin_verse.js 5.7KB

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