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.

преди 2 години
преди 2 години
преди 2 години
преди 2 години
преди 2 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. , getNonElevatedTask : function( taskToRun ){ return ()=>{ return shell_verse.runNonElevated(taskToRun) } }
  74. , runNonElevated : ( taskToRun ) => {
  75. // Let shell_verse decide whether to Elevate Out of Proc or In Proc
  76. if(__isElevated) {
  77. return Promise.resolve( new Error('Cannot Run Task in Elevated Mode.') )
  78. }
  79. else {
  80. // taskToRun by default is the launched command and args.
  81. // taskToRun = taskToRun || (()=>{ return op[processedArgs.label || processedArgs._[0] || 'undefined'](processedArgs) })
  82. return taskToRun().then(r=>{
  83. taskToRun.statuslog.statuslog(null, taskToRun.info /*repo*/ )
  84. return r;
  85. }).catch((e) => {
  86. e.info = taskToRun.info;
  87. if(taskToRun.errHandler) throw taskToRun.errHandler(e)
  88. taskToRun.statuslog.statuslog(e);
  89. // console.error(e)
  90. throw e;
  91. }).finally(()=>{})
  92. }
  93. }
  94. , isElevated : ()=>{
  95. return acquireElevationState().then( ()=>{
  96. shell_verse.isElevated = () => {
  97. return Promise.resolve(__isElevated)
  98. }
  99. return shell_verse.isElevated()
  100. })
  101. }
  102. // , isElevationOutOfProc : ()=>{ return true }
  103. , acquireElevationState : () => {
  104. return Promise.resolve(false).then(( elevationstate ) => {
  105. __isElevated ? console.log('Elevated') : console.log('Not Elevated')
  106. __isElevated = elevationstate;
  107. }).catch(() => {
  108. __isElevated = false;
  109. console.log('Not Elevated');
  110. }).finally(()=>{
  111. shell_verse.acquireElevationState = ()=> Promise.resolve(__isElevated);
  112. shell_verse.isElevated = () => { return Promise.resolve(__isElevated)}
  113. return __isElevated;
  114. })
  115. }
  116. , getTaskCheckExists : cli.createTask('getTaskCheckExists', 'which')
  117. , getbash : ()=>{ return "sh" }
  118. , createJuntionOrLink : (dirOrFile, target, opts)=>{
  119. return nodeShellExec('ln', ['-s', target, dirOrFile], opts).catch((e) => { console.error(e) })
  120. }
  121. , removeJuncionOrLink : ( junctionOrLink )=>{
  122. return nodeShellExec('rm', [junctionOrLink], { inherit: true, shell: true, env: process.env })
  123. }
  124. , requestElevation(elevatedRunner, taskToRun) {
  125. // PB : TODO -- Most linking cmds in linux may not need elevation like in windows.
  126. // However returning true here is pseudo and will break cases where real elevation is required in linux. We need to refactor to reimplement
  127. // this with sudo and change all calles to use ony where required.
  128. return elevatedRunner(taskToRun);
  129. }
  130. }
  131. module.exports = shell_verse