| return pullCmd | return pullCmd | ||||
| } | } | ||||
| var performPull = (repo, branch) => { | |||||
| var performPull = (repo, branch, repoowner, errHandler) => { | |||||
| // PB : TODO -- Handle no branch passed in case. | // PB : TODO -- Handle no branch passed in case. | ||||
| // if(!branch) { throw 'No branch specified' } | // if(!branch) { throw 'No branch specified' } | ||||
| try{ | |||||
| var exists = existsSync(instanceroot + '/' + repo) | |||||
| } | |||||
| catch(e){ | |||||
| exists = false | |||||
| console.log(e) | |||||
| } | |||||
| if (existsSync(instanceroot + '/' + repo)) { | |||||
| if (exists) { | |||||
| var branchprint = branch ? ' branch :' + branch : ''; | var branchprint = branch ? ' branch :' + branch : ''; | ||||
| console.log('pulling ' + instanceroot + '/' + repo + branchprint ) | console.log('pulling ' + instanceroot + '/' + repo + branchprint ) | ||||
| return nodeShellExec.apply(null, getPullCmd(repo, branch)).then(() => { | return nodeShellExec.apply(null, getPullCmd(repo, branch)).then(() => { | ||||
| }) | }) | ||||
| .catch((e) => { | .catch((e) => { | ||||
| e.repo = repo; | e.repo = repo; | ||||
| if(errHandler) return errHandler(e) | |||||
| if (__isElevated) { | if (__isElevated) { | ||||
| fs.writeFileSync('run.log', ', ' + JSON.stringify(e), { 'flag': 'a+' }) | fs.writeFileSync('run.log', ', ' + JSON.stringify(e), { 'flag': 'a+' }) | ||||
| } | } | ||||
| else statuslog.statuslog(e); console.error(e) | |||||
| else statuslog.statuslog(e); | |||||
| // console.error(e) | |||||
| throw e; | |||||
| }) | }) | ||||
| } | } | ||||
| else { | else { | ||||
| console.log('cloning ' + repo) | console.log('cloning ' + repo) | ||||
| // PB : TODO -- detect if a clonable repo exists in currentGitAuthUser | // PB : TODO -- detect if a clonable repo exists in currentGitAuthUser | ||||
| return nodeShellExec('git', ['clone', '-c', 'core.symlinks=true', selectedinstance.reposerver + `/${defaultRepoOwner}/` + repo + '.git'], | |||||
| return nodeShellExec('git', ['clone', '-c', 'core.symlinks=true', selectedinstance.reposerver + `/${repoowner || defaultRepoOwner}/` + repo + '.git'], | |||||
| { | { | ||||
| inherit: true, shell: true, | inherit: true, shell: true, | ||||
| env: process.env | env: process.env | ||||
| , cwd : instanceroot | , cwd : instanceroot | ||||
| , runas: processedArgs.runas | , runas: processedArgs.runas | ||||
| }).catch((e) => { | |||||
| throw e; | |||||
| }).then(() => { | }).then(() => { | ||||
| return nodeShellExec('git', ['config', '--replace-all', 'core.symlinks', true], | return nodeShellExec('git', ['config', '--replace-all', 'core.symlinks', true], | ||||
| env: process.env | env: process.env | ||||
| , cwd: instanceroot + '/' + repo | , cwd: instanceroot + '/' + repo | ||||
| , runas: processedArgs.runas | , runas: processedArgs.runas | ||||
| , title: `git core.symlinks --replace-all true for ${selectedinstance.reposerver + `/${defaultRepoOwner}/` + repo + '.git'}` | |||||
| , title: `'git', ${['config', '--replace-all', 'user.name', username].join(' ')}` | |||||
| }) | }) | ||||
| .then(() => { | .then(() => { | ||||
| if (__isElevated) { | if (__isElevated) { | ||||
| }) | }) | ||||
| .catch(e => { | .catch(e => { | ||||
| e.repo = repo; | e.repo = repo; | ||||
| if(errHandler) return errHandler(e) | |||||
| if (__isElevated) { | if (__isElevated) { | ||||
| fs.writeFileSync('run.log', ', ' + JSON.stringify(e), { 'flag': 'a+' }) | fs.writeFileSync('run.log', ', ' + JSON.stringify(e), { 'flag': 'a+' }) | ||||
| } | } | ||||
| else statuslog.statuslog(e); | else statuslog.statuslog(e); | ||||
| throw e | |||||
| }) | }) | ||||
| } | } | ||||
| } | } | ||||
| fs.writeFileSync(instanceroot + '/chessinstances.js', 'module.exports = ' + JSON.stringify(chessinstances, null, 2) + '', { 'flag': 'w' }) | fs.writeFileSync(instanceroot + '/chessinstances.js', 'module.exports = ' + JSON.stringify(chessinstances, null, 2) + '', { 'flag': 'w' }) | ||||
| } | } | ||||
| var acquireConfig = function (selected) { | |||||
| var configs = (function(){ | |||||
| return { | |||||
| clusterNodeInstance(selected) { var clusternodename = 'node01' | |||||
| return __acquireConfig(selected, selected.username, clusternodename | |||||
| , selected.instanceName + '-config-' + selected.node_env + `-${clusternodename}` | |||||
| , function(e){ console.info('Customized node level config not found. This is not an Error. Will attempt with owner level config.') } | |||||
| ) | |||||
| } | |||||
| , ownerInstnace(selected) { return __acquireConfig(selected, selected.username, null, null | |||||
| , function(e){ console.info('Customized user level config not found. This is not an Error. Will attempt global common instance config.') } | |||||
| ) | |||||
| } | |||||
| // 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 | |||||
| // , function(e){ console.info('This is an error prompt choices.') } | |||||
| ) } | |||||
| , genericChessInstance(selected) { return __acquireConfig(selected) } | |||||
| } | |||||
| })() | |||||
| var acquireConfig = function(slections){ | |||||
| var configPriority = [ 'clusterNodeInstance', 'ownerInstnace', 'commonInstance', 'genericChess' ] | |||||
| return any(configPriority.map(cfg => { return function() { return configs[cfg](slections) } } ), true, true) | |||||
| } | |||||
| var __acquireConfig = function (selected, owner, clusternodename, configrepo, errHandler) { | |||||
| var configrepo = selected.instanceName + '-config-' + selected.node_env; | |||||
| configrepo = configrepo || selected.instanceName + '-config-' + selected.node_env; | |||||
| var errorHandler = (e) => { | |||||
| if(e.messages.join(' ').match(new RegExp (`fatal: unable to access '${selectedinstance.reposerver}/${owner}/${configrepo}.git/': Failed to connect to .*? port .*? after .*? ms: Timed out`))){ | |||||
| // console.error('Could not connect to repo server. Timed Out') | |||||
| return cli.prompt( ['(y)es', '(n)o', '(r)etry'], 'Could not connect to repo server. Timed Out. Would you like to switch server ? (y/n) ', 'y' ).then(propValue => { | |||||
| if(propValue === 'y') { | |||||
| reconfirm = getReconfirmAll() | |||||
| return startElxr() | |||||
| } | |||||
| else if(propValue === 'r'){ | |||||
| return acquireConfig(selected) | |||||
| } | |||||
| else process.exit() | |||||
| }) | |||||
| console.error() | |||||
| } | |||||
| 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' | |||||
| , '(c) customize your personal instance for this node' | |||||
| , '(e) exit'] | |||||
| , 'Config for instance not found. Would you like to with this name ? (y/n) ', 'y' ).then(propValue => { | |||||
| if(propValue === 'y') { | |||||
| return createLocalChessInsance() | |||||
| } | |||||
| else process.exit() | |||||
| }) | |||||
| } | |||||
| console.warn(e) | |||||
| console.warn('Config acquisition failed.') | |||||
| } | |||||
| var successHandler = () => { | |||||
| return performPull(configrepo).then(() => { | |||||
| var manifestpath = path.normalize(selected.root + '/' + selected.instanceName + '-config-' + selected.node_env + '/repo-manifest'); | var manifestpath = path.normalize(selected.root + '/' + selected.instanceName + '-config-' + selected.node_env + '/repo-manifest'); | ||||
| selectedinstance = require(manifestpath)( null, selectedinstance) | selectedinstance = require(manifestpath)( null, selectedinstance) | ||||
| // Config from server always override merges into selection except for the current selection. | // Config from server always override merges into selection except for the current selection. | ||||
| chessinstances[selected.instanceName][selected.node_env].reposervers = Array.from(new Set(chessinstances[selected.instanceName][selected.node_env].reposervers)) | chessinstances[selected.instanceName][selected.node_env].reposervers = Array.from(new Set(chessinstances[selected.instanceName][selected.node_env].reposervers)) | ||||
| selectedinstance.reposerver = selectedinstance.reposerver || selectedinstance.reposervers[0] // PB : TODO -- Attempt first one that is available and online... | selectedinstance.reposerver = selectedinstance.reposerver || selectedinstance.reposervers[0] // PB : TODO -- Attempt first one that is available and online... | ||||
| cacheWriteInstanceConfig(chessinstances) | cacheWriteInstanceConfig(chessinstances) | ||||
| ENV.NODE_ENV = selectedinstance.node_env; | |||||
| }) | |||||
| .catch((e) => { | |||||
| console.error(e) | |||||
| console.error('Config acquisition failed.') | |||||
| }) | |||||
| ENV.NODE_ENV = selectedinstance.node_env; | |||||
| } | |||||
| return performPull(configrepo, null, owner, errHandler || ((e)=>{ throw e })).then( successHandler ) | |||||
| .catch( errorHandler ) | |||||
| } | } | ||||
| var launchpath = path.normalize(process.cwd()) | var launchpath = path.normalize(process.cwd()) | ||||
| , choices : choices['instanceName'], defaultchoice : 'chess'} | , choices : choices['instanceName'], defaultchoice : 'chess'} | ||||
| , instanceType : { label : `Enter Instance Type ( <= ${target.instanceType || 'development'} ) : ` | , instanceType : { label : `Enter Instance Type ( <= ${target.instanceType || 'development'} ) : ` | ||||
| , choices : choices['instanceType'], defaultchoice : 'development'} | , choices : choices['instanceType'], defaultchoice : 'development'} | ||||
| , reposerver : { label : `Enter Instance Name ( <= ${target.reposerver || 'https://git.bbh.org.in'} ) : ` | |||||
| , reposerver : { label : `Enter Repo Url ( <= ${target.reposerver || 'https://git.bbh.org.in'} ) : ` | |||||
| , choices : choices['reposerver'], defaultchoice : 'https://git.bbh.org.in'} | , choices : choices['reposerver'], defaultchoice : 'https://git.bbh.org.in'} | ||||
| , username : { label : `Enter User Id for ${target.reposerver} ( <= ${target.username || 'chess'} ) : ` | , username : { label : `Enter User Id for ${target.reposerver} ( <= ${target.username || 'chess'} ) : ` | ||||
| , choices : choices['username'], defaultchoice : 'chess'} | , choices : choices['username'], defaultchoice : 'chess'} | ||||
| } | } | ||||
| // promptkeys.runchoice = promptkeys.cmd ? 'c' : undefined | // promptkeys.runchoice = promptkeys.cmd ? 'c' : undefined | ||||
| function createChessInsance( cfg ){ | |||||
| function createChessInstance( cfg ){ | |||||
| return createInstance(cfg) | |||||
| var inst = {}; | var inst = {}; | ||||
| var __new = Object.assign({}, __default, cfg) | var __new = Object.assign({}, __default, cfg) | ||||
| inst[cfg.node_env] = __new; return inst; | inst[cfg.node_env] = __new; return inst; | ||||
| env: process.env | env: process.env | ||||
| , cwd: instanceroot + '/' + repo | , cwd: instanceroot + '/' + repo | ||||
| , runas: processedArgs.runas | , runas: processedArgs.runas | ||||
| , title: `git core.symlinks --replace-all true for ${selectedinstance.reposerver + `/${defaultRepoOwner}/` + repo + '.git'}` | |||||
| , title: `'git', ${['config', '--replace-all', 'user.name', username].join(' ')}` | |||||
| }) | }) | ||||
| } | } | ||||
| ) | ) | ||||
| // return selectedinstance | // return selectedinstance | ||||
| } | } | ||||
| acquireElevationState().then(() => { | |||||
| var skipprerequisites = false; | |||||
| var skipprerequisites = false; | |||||
| function initinstances(selected_overrides) { | 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. | var root = selected_overrides.root // We should always have this coz we are running at some place which is fixed by detectinstances. | ||||
| promptkeys['reposerver'] = reposerver = chessinstances.current_run.reposerver = promptkeys['reposerver'] || __default.reposervers[0]; | promptkeys['reposerver'] = reposerver = chessinstances.current_run.reposerver = promptkeys['reposerver'] || __default.reposervers[0]; | ||||
| } | } | ||||
| chessinstances[instanceName] = chessinstances[instanceName] || createChessInsance( { | |||||
| chessinstances[instanceName] = chessinstances[instanceName] || createChessInstance( { | |||||
| instanceName, node_env, root : selected_overrides.root, reposerver : promptkeys['reposerver'] } ); | instanceName, node_env, root : selected_overrides.root, reposerver : promptkeys['reposerver'] } ); | ||||
| chessinstances['current_run'] = { instanceName: instanceName, node_env, reposerver, root } | chessinstances['current_run'] = { instanceName: instanceName, node_env, reposerver, root } | ||||
| } | } | ||||
| var startElxr = function() { | |||||
| const retaincount = 2 | const retaincount = 2 | ||||
| var min = runtimestamp; | var min = runtimestamp; | ||||
| var collect = [] | var collect = [] | ||||
| mainTasks.push(maintask) | mainTasks.push(maintask) | ||||
| return any(mainTasks); | return any(mainTasks); | ||||
| }) | }) | ||||
| } | |||||
| acquireElevationState().then(() => { | |||||
| return startElxr() | |||||
| }) | }) | ||||
| // detect if alread installed -> Take no action. | // detect if alread installed -> Take no action. |