|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- const fs = require('fs')
-
- var cli = require('./cliverse')
- var nodeShellExec = cli.nodeShellExec;
-
- // PB : TODO -- linux prerequistes.
- // npm i -g nodemon
- // sudo apt install python2
- // sudo apt install build-essential
- // sudo apt install -y make
-
- // phantomjs": "^2.1.7
- // "sass": "^1.32.7",
- // "node-sass": "^5.0.0",
- // "ember-cli-sass": "^10.0.1",
- // "gulp-sass": "^4.1.0",
-
- // ember-searchable-select
- // ember-cli-sass
-
- // ember-cp-validatations
- // ember-cli-yuidoc
- // yuidoc-ember-theme
-
- // chess-server-lib/server
- // java
-
- // client
- // export NODE_OPTIONS=--openssl-legacy-provider
-
- // elixir-client npmi
- // elixir-client/chess-client-lib npmi
- // crossfilter2
-
- var __isElevated = null;
- var shell_verse = {
- // getCommonTask is agnostic of whether we are running in an elevated shell or not. It runs in either case.
- getCommonTask( taskToRun ){ return ()=>{ return shell_verse.runTask(taskToRun) }}
- , runTask : ( taskToRun ) => {
- if (__isElevated) return shell_verse.elevatedRunner()
- else return shell_verse.runNonElevated( taskToRun )
- }
-
- , elevatedRunner( taskToRun ){
- try {
- var __runasresult = null;
- return taskToRun().then((r)=>{
- // PB : TODO -- Every elevation should have its own messaging file. Async writes from multiple processes are a problem here...
- fs.writeFileSync('run.log', ', ' + JSON.stringify( { info : taskToRun.info, success: true }), { 'flag': 'a+' })
- fs.writeFileSync('run.done', 'success') // PB : TODO -- This should be done conditionally if we are running inproc.
- return __runasresult = r;
- })
- .catch((e) => {
- fs.writeFileSync('run.log', ', ' + JSON.stringify(e), { 'flag': 'a+' })
- fs.writeFileSync('run.done', 'failure')
- console.error(e)
- })
- .finally(() => {
- if(__runasresult && !__runasresult.skipped) fs.unlinkSync('run.done')
- })
- }
- catch (e) {
- console.error('Error Invalid command : ' + e)
- fs.writeFileSync('run.done', 'error')
- }
- finally {
- }
- }
- , getElevatedTask : function( taskToRun ){ return ()=>{ return shell_verse.runElevated(taskToRun) }}
- , runElevated : ( taskToRun ) => {
- // Let shell_verse decide whether to Elevate Out of Proc or In Proc
-
- // taskToRun by default is the launched command and args. Specially in windows out of proc.
- // taskToRun = taskToRun || (()=>{ return op[processedArgs.label || processedArgs._[0] || 'undefined'](processedArgs) })
-
- if(taskToRun.processedArgs.skipelevated) return Promise.resolve({ skipped : true });
-
-
- if (__isElevated) {
- return shell_verse.elevatedRunner(taskToRun)
- }
- else {
- console.log('Requesting Elevated Privileges');
- // requesteElevation is acutally request elevation and run. Both In Proc and Out of Proc.
- // Linux doesnt require elevation for most commands...
- return shell_verse.requestElevation(shell_verse.elevatedRunner, taskToRun)
- }
- }
-
- , getNonElevatedTask : function( taskToRun ){ return ()=>{ return shell_verse.runNonElevated(taskToRun) } }
- , runNonElevated : ( taskToRun ) => {
- // Let shell_verse decide whether to Elevate Out of Proc or In Proc
- if(__isElevated) {
- return Promise.resolve( new Error('Cannot Run Task in Elevated Mode.') )
- }
- else {
- // taskToRun by default is the launched command and args.
- // taskToRun = taskToRun || (()=>{ return op[processedArgs.label || processedArgs._[0] || 'undefined'](processedArgs) })
-
- return taskToRun().then(r=>{
- taskToRun.statuslog.statuslog(null, taskToRun.info /*repo*/ )
- return r;
- }).catch((e) => {
- e.info = taskToRun.info;
- if(taskToRun.errHandler) throw taskToRun.errHandler(e)
- taskToRun.statuslog.statuslog(e);
- // console.error(e)
- throw e;
- }).finally(()=>{})
- }
- }
-
- , isElevated : ()=>{
- return acquireElevationState().then( ()=>{
- shell_verse.isElevated = () => {
- return Promise.resolve(__isElevated)
- }
- return shell_verse.isElevated()
- })
- }
-
- // , isElevationOutOfProc : ()=>{ return true }
-
- , acquireElevationState : () => {
- return Promise.resolve(false).then(( elevationstate ) => {
- __isElevated ? console.log('Elevated') : console.log('Not Elevated')
- __isElevated = elevationstate;
- }).catch(() => {
- __isElevated = false;
- console.log('Not Elevated');
- }).finally(()=>{
- shell_verse.acquireElevationState = ()=> Promise.resolve(__isElevated);
- shell_verse.isElevated = () => { return Promise.resolve(__isElevated)}
- return __isElevated;
- })
- }
-
- , getTaskCheckExists : cli.createTask('getTaskCheckExists', 'which')
-
- , getbash : ()=>{ return "sh" }
-
- , createJuntionOrLink : (dirOrFile, target, opts)=>{
- return nodeShellExec('ln', ['-s', target, dirOrFile], opts).catch((e) => { console.error(e) })
- }
-
- , removeJuncionOrLink : ( junctionOrLink )=>{
- return nodeShellExec('rm', [junctionOrLink], { inherit: true, shell: true, env: process.env })
- }
-
- , requestElevation(elevatedRunner, taskToRun) {
- // PB : TODO -- Most linking cmds in linux may not need elevation like in windows.
- // However returning true here is pseudo and will break cases where real elevation is required in linux. We need to refactor to reimplement
- // this with sudo and change all calles to use ony where required.
- return elevatedRunner(taskToRun);
- }
- }
-
- module.exports = shell_verse
|