選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

lin_verse.js 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. const { any } = require('bbhverse');
  2. const fs = require('fs')
  3. var path = require('path');
  4. var cli = require('./cliverse')
  5. var nodeShellExec = cli.nodeShellExec;
  6. // PB : TODO -- linux prerequistes.
  7. // export GIT_SSL_NO_VERIFY=true
  8. // npm i -g nodemon
  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. init( o ){ Object.assign(this, o) }
  30. , downloadsdir : '../Downloads'
  31. , getCommonTask( taskToRun ){ return ()=>{ return shell_verse.runTask(taskToRun) }}
  32. , runTask : ( taskToRun ) => {
  33. if (__isElevated) return shell_verse.runElevated( taskToRun )
  34. else return shell_verse.runNonElevated( taskToRun )
  35. }
  36. , elevatedRunner( taskToRun, inBatch ){
  37. // 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.
  38. try {
  39. // PB : We do not need IPC in linux. Until a real sudo elevation is required eveything works in non elevated mode...
  40. var __runasresult = null;
  41. return taskToRun().then((r)=>{
  42. // PB : TODO -- Every elevation should have its own messaging file. Async writes from multiple processes are a problem here...
  43. // fs.writeFileSync('run.log', ', ' + JSON.stringify( { info : taskToRun.info, success: true }), { 'flag': 'a+' })
  44. // fs.writeFileSync('run.done', 'success') // PB : TODO -- This should be done conditionally if we are running inproc.
  45. return __runasresult = r;
  46. })
  47. .catch((e) => {
  48. // fs.writeFileSync('run.log', ', ' + JSON.stringify(e), { 'flag': 'a+' })
  49. // fs.writeFileSync('run.done', 'failure')
  50. console.error(e)
  51. })
  52. .finally(() => {
  53. // if(__runasresult && !__runasresult.skipped) fs.unlinkSync('run.done')
  54. })
  55. }
  56. catch (e) {
  57. console.error('Error Invalid command : ' + e)
  58. if(!inBatch) fs.writeFileSync('run.done', 'error')
  59. }
  60. finally {
  61. }
  62. }
  63. , getElevatedTask : function( taskToRun ){ return ()=>{ return shell_verse.runElevated(taskToRun) }}
  64. , getElevatedTaskInBatch : function( taskToRun ){ return ()=>{ return shell_verse.runElevatedInBatch(taskToRun) }}
  65. , runElevatedInBatch : ( taskToRun ) => {
  66. if (__isElevated) return shell_verse.elevatedRunner(taskToRun, true)
  67. else return shell_verse.requestElevation(shell_verse.elevatedRunner, taskToRun)
  68. }
  69. , runElevated : ( taskToRun ) => {
  70. // Let shell_verse decide whether to Elevate Out of Proc or In Proc
  71. // taskToRun by default is the launched command and args. Specially in windows out of proc.
  72. // taskToRun = taskToRun || (()=>{ return op[processedArgs.label || processedArgs._[0] || 'undefined'](processedArgs) })
  73. if(taskToRun.processedArgs.skipelevated) return Promise.resolve({ skipped : true });
  74. if (__isElevated) {
  75. return shell_verse.elevatedRunner(taskToRun)
  76. }
  77. else {
  78. console.log('Requesting Elevated Privileges');
  79. // requesteElevation is acutally request elevation and run. Both In Proc and Out of Proc.
  80. // Linux doesnt require elevation for most commands...
  81. return shell_verse.requestElevation(shell_verse.elevatedRunner, taskToRun)
  82. }
  83. }
  84. , runElevatedBatch( batchToRun ){
  85. // 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.
  86. // Irrespective of the batch we just call runElevated once.
  87. batchToRun.forEach(element => {
  88. element.processedArgs = batchToRun[0].processedArgs
  89. });
  90. return any(batchToRun.map(shell_verse.runElevated))
  91. }
  92. , getNonElevatedTask : function( taskToRun ){ return ()=>{ return shell_verse.runNonElevated(taskToRun) } }
  93. , runNonElevated : ( taskToRun ) => {
  94. // Let shell_verse decide whether to Elevate Out of Proc or In Proc
  95. if(__isElevated) {
  96. return Promise.resolve( 'Skipping regular task in elevated shell.' ) // Regular tasks unless marked as common tasks should not run in elevated shell...
  97. }
  98. else {
  99. // taskToRun by default is the launched command and args.
  100. // taskToRun = taskToRun || (()=>{ return op[processedArgs.label || processedArgs._[0] || 'undefined'](processedArgs) })
  101. return taskToRun().then(r=>{
  102. taskToRun.statuslog.statuslog(null, taskToRun.info /*repo*/ )
  103. return r;
  104. }).catch((e) => {
  105. e.info = taskToRun.info;
  106. if(taskToRun.errHandler) throw taskToRun.errHandler(e)
  107. taskToRun.statuslog.statuslog(e); //
  108. // console.error(e)
  109. throw e;
  110. }).finally(()=>{})
  111. }
  112. }
  113. , isElevated : ()=>{
  114. return acquireElevationState().then( ()=>{
  115. shell_verse.isElevated = () => {
  116. return Promise.resolve(__isElevated)
  117. }
  118. return shell_verse.isElevated()
  119. })
  120. }
  121. // , isElevationOutOfProc : ()=>{ return true }
  122. , acquireElevationState : () => {
  123. return Promise.resolve(false).then(( elevationstate ) => {
  124. __isElevated ? console.log('Elevated') : console.log('Not Elevated')
  125. __isElevated = elevationstate;
  126. shell_verse.acquireElevationState = ()=> Promise.resolve(__isElevated);
  127. shell_verse.isElevated = () => { return Promise.resolve(__isElevated)}
  128. return __isElevated
  129. }).catch(() => {
  130. __isElevated = false;
  131. shell_verse.acquireElevationState = ()=> Promise.resolve(__isElevated);
  132. shell_verse.isElevated = () => { return Promise.resolve(__isElevated)}
  133. console.log('Not Elevated');
  134. return __isElevated
  135. })
  136. // .finally(()=>{
  137. // shell_verse.acquireElevationState = ()=> Promise.resolve(__isElevated);
  138. // shell_verse.isElevated = () => { return Promise.resolve(__isElevated)}
  139. // return __isElevated; // Value returned from finally is not supported by node.
  140. // })
  141. }
  142. , getTaskCheckExists : cli.createTask('getTaskCheckExists', 'which')
  143. , getbash : ()=>{ return "sh" }
  144. , createJuntionOrLink : (dirOrFile, target, opts)=>{
  145. return nodeShellExec('ln', ['-s', target, dirOrFile], opts).catch((e) => { console.error(e) })
  146. }
  147. , removeJuncionOrLink : ( junctionOrLink )=>{
  148. return nodeShellExec('rm', [junctionOrLink], { inherit: true, shell: true, env: process.env })
  149. }
  150. , requestElevation(elevatedRunner, taskToRun) {
  151. // PB : TODO -- Most linking cmds in linux may not need elevation like in windows.
  152. // However returning true here is pseudo and will break cases where real elevation is required in linux. We need to refactor to reimplement
  153. // this with sudo and change all calles to use ony where required.
  154. return elevatedRunner(taskToRun);
  155. }
  156. , iswin(){ return false}
  157. , ensureDirectoryExistence(filePath) {
  158. var dirname = path.dirname(filePath);
  159. if (fs.existsSync(dirname)) {
  160. return filePath;
  161. }
  162. this.ensureDirectoryExistence(dirname);
  163. fs.mkdirSync(dirname);
  164. return filePath;
  165. }
  166. , generateDependencies(){
  167. }
  168. , iswin(){ return false}
  169. , islin(){ return true}
  170. }
  171. module.exports = shell_verse