Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

lin_verse.js 5.4KB

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