Browse Source

Added multiple levels of configs

production
guest 2 years ago
parent
commit
60221abd66
1 changed files with 101 additions and 22 deletions
  1. 101
    22
      index.js

+ 101
- 22
index.js View File

@@ -269,11 +269,20 @@ var getPullCmd = (repo, branch) => {
return pullCmd
}

var performPull = (repo, branch) => {
var performPull = (repo, branch, repoowner, errHandler) => {
// PB : TODO -- Handle no branch passed in case.
// 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 : '';
console.log('pulling ' + instanceroot + '/' + repo + branchprint )
return nodeShellExec.apply(null, getPullCmd(repo, branch)).then(() => {
@@ -285,23 +294,24 @@ var performPull = (repo, branch) => {
})
.catch((e) => {
e.repo = repo;
if(errHandler) return errHandler(e)
if (__isElevated) {
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 {
console.log('cloning ' + repo)
// 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,
env: process.env
, cwd : instanceroot
, runas: processedArgs.runas
}).catch((e) => {
throw e;
}).then(() => {

return nodeShellExec('git', ['config', '--replace-all', 'core.symlinks', true],
@@ -310,7 +320,7 @@ var performPull = (repo, branch) => {
env: process.env
, cwd: instanceroot + '/' + repo
, 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(() => {
if (__isElevated) {
@@ -328,10 +338,12 @@ var performPull = (repo, branch) => {
})
.catch(e => {
e.repo = repo;
if(errHandler) return errHandler(e)
if (__isElevated) {
fs.writeFileSync('run.log', ', ' + JSON.stringify(e), { 'flag': 'a+' })
}
else statuslog.statuslog(e);
throw e
})
}
}
@@ -2229,11 +2241,72 @@ var cacheWriteInstanceConfig = function(chessinstances){
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');
selectedinstance = require(manifestpath)( null, selectedinstance)
// Config from server always override merges into selection except for the current selection.
@@ -2250,12 +2323,11 @@ var acquireConfig = function (selected) {
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...
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())
@@ -2614,7 +2686,7 @@ var __interactive_prompts = function( target, choices, promptsfilter ){
, choices : choices['instanceName'], defaultchoice : 'chess'}
, instanceType : { label : `Enter Instance Type ( <= ${target.instanceType || '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'}
, username : { label : `Enter User Id for ${target.reposerver} ( <= ${target.username || 'chess'} ) : `
, choices : choices['username'], defaultchoice : 'chess'}
@@ -2988,7 +3060,8 @@ var promptkeys = {
}
// promptkeys.runchoice = promptkeys.cmd ? 'c' : undefined

function createChessInsance( cfg ){
function createChessInstance( cfg ){
return createInstance(cfg)
var inst = {};
var __new = Object.assign({}, __default, cfg)
inst[cfg.node_env] = __new; return inst;
@@ -3198,7 +3271,7 @@ const GIT = (function(){
env: process.env
, cwd: instanceroot + '/' + repo
, 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(' ')}`
})
}
)
@@ -3243,8 +3316,7 @@ function createInstance(selectedinstance) {
// return selectedinstance
}

acquireElevationState().then(() => {
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.

@@ -3278,7 +3350,7 @@ acquireElevationState().then(() => {
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'] } );
chessinstances['current_run'] = { instanceName: instanceName, node_env, reposerver, root }

@@ -3363,6 +3435,7 @@ acquireElevationState().then(() => {

}

var startElxr = function() {
const retaincount = 2
var min = runtimestamp;
var collect = []
@@ -3476,6 +3549,12 @@ acquireElevationState().then(() => {
mainTasks.push(maintask)
return any(mainTasks);
})
}

acquireElevationState().then(() => {

return startElxr()
})

// detect if alread installed -> Take no action.

Loading…
Cancel
Save