| } | } | ||||
| // PB : TODO -- Use the ORG level instance before falling back to common Instance coz common instance may not exist for certain orgs. | // PB : TODO -- Use the ORG level instance before falling back to common Instance coz common instance may not exist for certain orgs. | ||||
| , commonInstance(selected) { return __acquireConfig(selected, defaultRepoOwner | , commonInstance(selected) { return __acquireConfig(selected, defaultRepoOwner | ||||
| // , function(e){ console.info('This is an error prompt choices.') } | |||||
| // , function(e){ console.info('This is probably an error unless the user is asking to create a new instance with this name.') } | |||||
| ) } | ) } | ||||
| , genericChessInstance(selected) { return __acquireConfig(selected) } | , genericChessInstance(selected) { return __acquireConfig(selected) } | ||||
| } | } | ||||
| } | } | ||||
| if(e.messages.join(' ').match(new RegExp (`fatal: repository '${selectedinstance.reposerver}/${owner}/${configrepo}.git/' not found`))){ | if(e.messages.join(' ').match(new RegExp (`fatal: repository '${selectedinstance.reposerver}/${owner}/${configrepo}.git/' not found`))){ | ||||
| return cli.prompt( ['(i) install new instance with this name locally' | |||||
| , '(f) fork a new instance for yourself' | |||||
| , '(o) fork a new instance for your organization' // prompt organization name... | |||||
| , '(c) create a custom personal instance for this node' // prompt hostname as nodename | |||||
| , '(p) create a custom personal instance' | |||||
| , '(e) exit'] | |||||
| return cli.prompt( { i : 'install new instance with this name locally' | |||||
| , f : 'fork a new instance for yourself from another instance' | |||||
| , o : 'fork a new instance for your organization' // prompt organization name... | |||||
| , c : 'create a custom personal instance for this node' // prompt hostname as nodename | |||||
| , p : 'create a custom personal instance' | |||||
| , e : 'exit'} | |||||
| , 'Config for instance not found. Would you like to ', 'e' ).then(propValue => { | , 'Config for instance not found. Would you like to ', 'e' ).then(propValue => { | ||||
| if(propValue === 'i') return eNotImplemented() // return createLocalChessInsance(selectedinstance) | if(propValue === 'i') return eNotImplemented() // return createLocalChessInsance(selectedinstance) | ||||
| return { root, node_env : path.basename(root), instanceName : path.basename( path.dirname(root) ) } | return { root, node_env : path.basename(root), instanceName : path.basename( path.dirname(root) ) } | ||||
| } | } | ||||
| var detectinstances = function () { | |||||
| var detectLocalInstances = function () { | |||||
| console.log(`launchpath = ${launchpath}`) | console.log(`launchpath = ${launchpath}`) | ||||
| console.log(`thisscriptdir = ${thisscriptdir}`) | console.log(`thisscriptdir = ${thisscriptdir}`) | ||||
| } | } | ||||
| // promptkeys.runchoice = promptkeys.cmd ? 'c' : undefined | // promptkeys.runchoice = promptkeys.cmd ? 'c' : undefined | ||||
| function createChessInstance( cfg ){ | |||||
| return createInstance(cfg) | |||||
| // var inst = {}; | |||||
| // var __new = Object.assign({}, __default, cfg) | |||||
| // inst[cfg.node_env] = __new; return inst; | |||||
| function createLocalChessInstance( cfg ){ | |||||
| // return createInstance(cfg) | |||||
| var inst = {}; | |||||
| var __new = Object.assign({}, /*__default,*/ cfg) | |||||
| inst[cfg.node_env] = __new; return inst; | |||||
| } | } | ||||
| var choices = { | var choices = { | ||||
| } | } | ||||
| var detection_state = { | var detection_state = { | ||||
| didWeFindInstance : false | |||||
| localInstanceDetected : false | |||||
| } | } | ||||
| const https = require('https') | const https = require('https') | ||||
| } | } | ||||
| var skipprerequisites = false; | var skipprerequisites = false; | ||||
| function initinstances(selected_overrides) { | |||||
| var root = selected_overrides.root // We should always have this coz we are running at some place which is fixed by detectinstances. | |||||
| function initinstances(selected_overrides) { | |||||
| var root = selected_overrides.root // We should always have this coz we are running at some place which is fixed by detectLocalInstances. | |||||
| var instanceName = selected_overrides.instanceName | |||||
| || clioverrides.instanceName | |||||
| // || processedArgs._[1] | |||||
| || chessinstances.current_run.instanceName | |||||
| var node_env = selected_overrides.node_env | |||||
| || clioverrides.node_env | |||||
| // || processedArgs.node_env | |||||
| || chessinstances.current_run.node_env | |||||
| var reposerver = selected_overrides.reposerver | |||||
| || clioverrides.reposerver | |||||
| // || processedArgs.node_env | |||||
| || chessinstances.current_run.reposerver | |||||
| var instanceName = selected_overrides.instanceName | |||||
| || clioverrides.instanceName | |||||
| // || processedArgs._[1] | |||||
| || chessinstances.current_run.instanceName | |||||
| if(!instanceName) { | |||||
| promptkeys['instanceName'] = instanceName = chessinstances.current_run.instanceName = promptkeys['instanceName'] || __default.instanceName; | |||||
| promptkeys['node_env'] = node_env = chessinstances.current_run.node_env = promptkeys['node_env'] || __default.node_env; | |||||
| promptkeys['reposerver'] = reposerver = chessinstances.current_run.reposerver = promptkeys['reposerver'] || __default.reposervers[0]; | |||||
| } | |||||
| var node_env = selected_overrides.node_env | |||||
| || clioverrides.node_env | |||||
| // || processedArgs.node_env | |||||
| || chessinstances.current_run.node_env | |||||
| var reposerver = selected_overrides.reposerver | |||||
| || clioverrides.reposerver | |||||
| // || processedArgs.node_env | |||||
| || chessinstances.current_run.reposerver | |||||
| if(!node_env) { | |||||
| promptkeys['node_env'] = node_env = chessinstances.current_run.node_env = promptkeys['node_env'] || __default.node_env; | |||||
| promptkeys['reposerver'] = reposerver = chessinstances.current_run.reposerver = promptkeys['reposerver'] || __default.reposervers[0]; | |||||
| } | |||||
| if(!instanceName) { | |||||
| promptkeys['instanceName'] = instanceName = chessinstances.current_run.instanceName = promptkeys['instanceName'] || __default.instanceName; | |||||
| promptkeys['node_env'] = node_env = chessinstances.current_run.node_env = promptkeys['node_env'] || __default.node_env; | |||||
| promptkeys['reposerver'] = reposerver = chessinstances.current_run.reposerver = promptkeys['reposerver'] || __default.reposervers[0]; | |||||
| } | |||||
| if(!reposerver) { | |||||
| promptkeys['reposerver'] = reposerver = chessinstances.current_run.reposerver = promptkeys['reposerver'] || __default.reposervers[0]; | |||||
| } | |||||
| if(!node_env) { | |||||
| promptkeys['node_env'] = node_env = chessinstances.current_run.node_env = promptkeys['node_env'] || __default.node_env; | |||||
| promptkeys['reposerver'] = reposerver = chessinstances.current_run.reposerver = promptkeys['reposerver'] || __default.reposervers[0]; | |||||
| } | |||||
| chessinstances[instanceName] = chessinstances[instanceName] || createChessInstance( { | |||||
| instanceName, node_env, root : selected_overrides.root, reposerver : promptkeys['reposerver'] } ); | |||||
| chessinstances['current_run'] = { instanceName: instanceName, node_env, reposerver, root } | |||||
| if(!reposerver) { | |||||
| promptkeys['reposerver'] = reposerver = chessinstances.current_run.reposerver = promptkeys['reposerver'] || __default.reposervers[0]; | |||||
| } | |||||
| if(path.normalize(selected_overrides.root) !== path.normalize(chessinstances[instanceName][node_env].root)) { | |||||
| throw "instanceName and instanceType specified doesn't match whats already present do you want to continue " + chessinstances[instanceName][node_env].root + ' does not match ' + selected_overrides.root | |||||
| } | |||||
| // Override sequence. | |||||
| // __default, chessinstances[current_run], instanceName-config-development, cliargs, interactve_promts | |||||
| selectedinstance = utils.assign( | |||||
| chessinstances[instanceName][node_env] | |||||
| , clioverrides | |||||
| , selected_overrides | |||||
| // , __interactive_prompts -- Cant just override. Also need selectedinstance to be ready... | |||||
| ); | |||||
| chessinstances[instanceName] = chessinstances[instanceName] || createLocalChessInstance( { | |||||
| instanceName, node_env, root : selected_overrides.root, reposerver : selected_overrides.reposerver /* promptkeys['reposerver'] */ } ); | |||||
| chessinstances['current_run'] = { instanceName: instanceName, node_env, reposerver, root } | |||||
| // chessinstances[instanceName] = chessinstances[instanceName] || {} | |||||
| // chessinstances[instanceName][node_env] = chessinstances[instanceName][node_env] || {} | |||||
| // if(!selectedinstance.repos || selectedinstance.instanceName) { | |||||
| // // Brand New. | |||||
| // selectedinstance = Object.assign( __default, selectedinstance ) | |||||
| // } | |||||
| if(!selectedinstance.repos[0].repo) { | |||||
| console.warn('repo manifest has obsolete format. Attempting upgrade.') | |||||
| selectedinstance.repos = selectedinstance.repos.map(function(repo){ return { repo } }) | |||||
| } | |||||
| if(selectedinstance.elevated[0] && !selectedinstance.elevated[0].repo) { | |||||
| console.warn('elevated repo manifest has obsolete format. Attempting upgrade.') | |||||
| selectedinstance.elevated = selectedinstance.elevated.map(function(repo){ return { repo } }) | |||||
| } | |||||
| // Config from server always override merges into selection except for the current selection. | |||||
| // PB : TODO -- utils.assign Array merges are non-distinct... | |||||
| chessinstances[instanceName][node_env] = selectedinstance; | |||||
| // chessinstances[selectedinstance.instanceName][selectedinstance.node_env] = selectedinstance; | |||||
| cacheWriteInstanceConfig(chessinstances) | |||||
| // PB : TODO -- We should probably write the new server config also... | |||||
| selectedinstance.reposerver = selectedinstance.reposerver || selectedinstance.reposervers[0] // PB : TODO -- Attempt first one that is available and online... | |||||
| if(path.normalize(selected_overrides.root) !== path.normalize(chessinstances[instanceName][node_env].root)) { | |||||
| throw "instanceName and instanceType specified doesn't match whats already present do you want to continue " + chessinstances[instanceName][node_env].root + ' does not match ' + selected_overrides.root | |||||
| } | |||||
| // Override sequence. | |||||
| // __default, chessinstances[current_run], instanceName-config-development, cliargs, interactve_promts | |||||
| selectedinstance = utils.assign( | |||||
| chessinstances[instanceName][node_env] | |||||
| , clioverrides | |||||
| , selected_overrides | |||||
| // , __interactive_prompts -- Cant just override. Also need selectedinstance to be ready... | |||||
| ); | |||||
| return chessinstances | |||||
| // chessinstances[instanceName] = chessinstances[instanceName] || {} | |||||
| // chessinstances[instanceName][node_env] = chessinstances[instanceName][node_env] || {} | |||||
| // if(!selectedinstance.repos || selectedinstance.instanceName) { | |||||
| // // Brand New. | |||||
| // selectedinstance = Object.assign( __default, selectedinstance ) | |||||
| // } | |||||
| if(!selectedinstance.repos[0].repo) { | |||||
| console.warn('repo manifest has obsolete format. Attempting upgrade.') | |||||
| selectedinstance.repos = selectedinstance.repos.map(function(repo){ return { repo } }) | |||||
| } | |||||
| if(selectedinstance.elevated[0] && !selectedinstance.elevated[0].repo) { | |||||
| console.warn('elevated repo manifest has obsolete format. Attempting upgrade.') | |||||
| selectedinstance.elevated = selectedinstance.elevated.map(function(repo){ return { repo } }) | |||||
| } | } | ||||
| var skipprereqs = {} | |||||
| var maintask = () => { | |||||
| // Default cmd to run ! | |||||
| processedArgs._[0] === processedArgs._[0] || 'pull'; | |||||
| // selectedinstance.reposerver = selectedinstance.reposervers[0] // PB : TODO -- Attempt first one that is available and online from all that are available. | |||||
| if(!noprerequisites[processedArgs._[0]] | |||||
| && !skipprereqs[processedArgs._[0]] | |||||
| ) { | |||||
| return prerequisites.git.verifyAndInstall().then(()=>{ | |||||
| var e = { message : 'verifyAndInstall', success : true} | |||||
| var inittasks = [] | |||||
| if(!detection_state.didWeFindInstance) { | |||||
| inittasks.push(createInstance(selectedinstance).then(() => { preworkerconfig(); return elxrworker(true) })) | |||||
| } | |||||
| else { | |||||
| inittasks.push(acquireConfig(selectedinstance).catch((err) => { | |||||
| e = err; | |||||
| console.error('Chosen cofiguraton failed or not found. Fix config and rerun or chose another.') | |||||
| console.error(err) | |||||
| }).then(() => { preworkerconfig(); return elxrworker(true) }) | |||||
| // .finally(()=>{ | |||||
| // fs.writeFileSync('run.log', ', ' + JSON.stringify({ error: e.message }), { 'flag': 'a+' }) | |||||
| // if(!e.success) fs.writeFileSync('run.done', 'error'); | |||||
| // // return process.exit() | |||||
| // })) | |||||
| ) | |||||
| } | |||||
| return any(inittasks) | |||||
| }) | |||||
| } | |||||
| else { | |||||
| console.log('cmd has no preqs or has been configured to skip preqs') | |||||
| preworkerconfig() | |||||
| return elxrworker() | |||||
| } | |||||
| // Config from server always override merges into selection except for the current selection. | |||||
| // PB : TODO -- utils.assign Array merges are non-distinct... | |||||
| chessinstances[instanceName][node_env] = selectedinstance; | |||||
| // chessinstances[selectedinstance.instanceName][selectedinstance.node_env] = selectedinstance; | |||||
| cacheWriteInstanceConfig(chessinstances) | |||||
| // PB : TODO -- We should probably write the new server config also... | |||||
| selectedinstance.reposerver = selectedinstance.reposerver || selectedinstance.reposervers[0] // PB : TODO -- Attempt first one that is available and online... | |||||
| return chessinstances | |||||
| } | |||||
| var skipprereqs = {} | |||||
| var maintask = () => { | |||||
| // Default cmd to run ! | |||||
| processedArgs._[0] === processedArgs._[0] || 'pull'; | |||||
| // selectedinstance.reposerver = selectedinstance.reposervers[0] // PB : TODO -- Attempt first one that is available and online from all that are available. | |||||
| if(!noprerequisites[processedArgs._[0]] | |||||
| && !skipprereqs[processedArgs._[0]] | |||||
| ) { | |||||
| return prerequisites.git.verifyAndInstall().then(()=>{ | |||||
| var e = { message : 'verifyAndInstall', success : true} | |||||
| var inittasks = [] | |||||
| if(!detection_state.localInstanceDetected) { | |||||
| inittasks.push(createInstance(selectedinstance).then(() => { preworkerconfig(); return elxrworker(true) })) | |||||
| } | |||||
| else { | |||||
| inittasks.push(acquireConfig(selectedinstance).catch((err) => { | |||||
| e = err; | |||||
| console.error('Chosen cofiguraton failed or not found. Fix config and rerun or chose another.') | |||||
| console.error(err) | |||||
| }).then(() => { preworkerconfig(); return elxrworker(true) }) | |||||
| // .finally(()=>{ | |||||
| // fs.writeFileSync('run.log', ', ' + JSON.stringify({ error: e.message }), { 'flag': 'a+' }) | |||||
| // if(!e.success) fs.writeFileSync('run.done', 'error'); | |||||
| // // return process.exit() | |||||
| // })) | |||||
| ) | |||||
| } | |||||
| return any(inittasks) | |||||
| }) | |||||
| } | } | ||||
| else { | |||||
| console.log('cmd has no preqs or has been configured to skip preqs') | |||||
| preworkerconfig() | |||||
| return elxrworker() | |||||
| } | |||||
| } | |||||
| var startElxr = function() { | var startElxr = function() { | ||||
| const retaincount = 2 | const retaincount = 2 | ||||
| reconfirm = getReconfirmAll() | reconfirm = getReconfirmAll() | ||||
| } | } | ||||
| return detectinstances().then((detectedinstanceoptions)=>{ | |||||
| return detectLocalInstances().then((detectedinstanceoptions)=>{ | |||||
| detectedinstanceoptions.splice(0,0, __default) | detectedinstanceoptions.splice(0,0, __default) | ||||
| initinstances(selectedinstance) // use the local instances for defaults if at all possible. | initinstances(selectedinstance) // use the local instances for defaults if at all possible. | ||||
| var todo = any( getInteractionPoints(detectedinstanceoptions, promptkeys) ).then(()=>{ | var todo = any( getInteractionPoints(detectedinstanceoptions, promptkeys) ).then(()=>{ | ||||
| var inst = initinstances(selectedinstance) | var inst = initinstances(selectedinstance) | ||||
| detection_state.didWeFindInstance = true; | |||||
| detection_state.localInstanceDetected = true; | |||||
| return inst; | return inst; | ||||
| }) | }) | ||||
| } | } | ||||
| findlocalinstances(chessinstances, detectedinstanceoptions) | findlocalinstances(chessinstances, detectedinstanceoptions) | ||||
| detectedinstanceoptions.splice(0,0, __default) | detectedinstanceoptions.splice(0,0, __default) | ||||
| initinstances(selectedinstance) | initinstances(selectedinstance) | ||||
| detection_state.didWeFindInstance = true; | |||||
| detection_state.localInstanceDetected = true; | |||||
| } | } | ||||
| catch (e) { | catch (e) { | ||||
| // console.error(e) | // console.error(e) | ||||
| // // selectedinstance = Object.assign(detectedInstance, clioverrides); | // // selectedinstance = Object.assign(detectedInstance, clioverrides); | ||||
| // return selectedinstance = Object.assign(__default, selectedinstance); | // return selectedinstance = Object.assign(__default, selectedinstance); | ||||
| // }) | // }) | ||||
| detection_state.didWeFindInstance = false; | |||||
| detection_state.localInstanceDetected = false; | |||||
| return selectedinstance | return selectedinstance | ||||
| } | } | ||||
| }) | }) |