Browse Source

manifest format detection and upgrade

master
asasasasasas 3 years ago
parent
commit
5cb5bbd334
1 changed files with 57 additions and 43 deletions
  1. 57
    43
      index.js

+ 57
- 43
index.js View File

, { cwd: instanceroot + '/' + repo, title : 'pull all origins for ' + branch + ' ' + repo + ' ' + parameters.join(' ') }] , { cwd: instanceroot + '/' + repo, title : 'pull all origins for ' + branch + ' ' + repo + ' ' + parameters.join(' ') }]
} }
else { else {
console.warn('No branch was specified detecting from working client.')
// console.warn('No branch was specified detecting from working client.')
// First check if working client exits. // First check if working client exits.
// if (existsSync(instanceroot + '/' + repo)) { // if (existsSync(instanceroot + '/' + repo)) {
var pullCmd = gitops.getdiscoverbranchcmd(repo) var pullCmd = gitops.getdiscoverbranchcmd(repo)
} }




var repomanifest = null;
var currentGitAuthUser; // nodeShellExec('git', ['config', 'user.email']) ... PB : TODO-- get the current gittea username var currentGitAuthUser; // nodeShellExec('git', ['config', 'user.email']) ... PB : TODO-- get the current gittea username
var defaultRepoOwner = 'chess'; var defaultRepoOwner = 'chess';
var elevatedRunasRepos = null var elevatedRunasRepos = null
var gitbash = "C:\\Program Files\\Git\\bin\\sh.exe" var gitbash = "C:\\Program Files\\Git\\bin\\sh.exe"
// var gitbash = "G:\\Installed\\Git\\bin\\sh.exe" // var gitbash = "G:\\Installed\\Git\\bin\\sh.exe"
// Relevant git repos // Relevant git repos
// var repomanifest = require('../'+repomanifest.instanceName+'-config-'+ nodeenv +'/repo-manifest')()
var exludeMergeRepos = []; var exludeMergeRepos = [];
var useGitPull = processedArgs.useGitPull || false; var useGitPull = processedArgs.useGitPull || false;
var configPromise = null var configPromise = null
4) Iterates all repos and checkout to the ENV specified. 'git', ['checkout', checkoutMap[runconfig.NODE_ENV] || runconfig.NODE_ENV] 4) Iterates all repos and checkout to the ENV specified. 'git', ['checkout', checkoutMap[runconfig.NODE_ENV] || runconfig.NODE_ENV]
5) Iterates all repos and merge from source configured in mergesource. 'git', ['merge', mergesource], 5) Iterates all repos and merge from source configured in mergesource. 'git', ['merge', mergesource],
*/ */
var runconfig = { NODE_ENV: repomanifest.node_env }
var runconfig = { NODE_ENV: selectedinstance.node_env }
try { runconfig = Object.assign(runconfig, require(instanceroot + '/run.js')) } catch (e) { } try { runconfig = Object.assign(runconfig, require(instanceroot + '/run.js')) } catch (e) { }
// We no longer need to check ruans.. ??? if we were initiated from self invoked privileged shell ? // We no longer need to check ruans.. ??? if we were initiated from self invoked privileged shell ?
if (( /*processedArgs.runas && processedArgs.runas !== 'self' &&*/ !processedArgs.force) && if (( /*processedArgs.runas && processedArgs.runas !== 'self' &&*/ !processedArgs.force) &&
runconfig.NODE_ENV && runconfig.NODE_ENV === (repomanifest.node_env || runconfig.NODE_ENV) &&
repomanifest.instanceName && runconfig.use === repomanifest.instanceName) {
runconfig.NODE_ENV && runconfig.NODE_ENV === (selectedinstance.node_env || runconfig.NODE_ENV) &&
selectedinstance.instanceName && runconfig.use === selectedinstance.instanceName) {


console.log(`No change detected. Already using requested specs : ${runconfig.NODE_ENV} ${runconfig.use}`) console.log(`No change detected. Already using requested specs : ${runconfig.NODE_ENV} ${runconfig.use}`)
if (processedArgs.runas) { fs.writeFileSync('run.done', 'success') } if (processedArgs.runas) { fs.writeFileSync('run.done', 'success') }




def = def || { def = def || {
repos : repomanifest.repos,
elevated : repomanifest.elevated
repos : selectedinstance.repos,
elevated : selectedinstance.elevated
} }


var elevatedpulltasks = null; var elevatedpulltasks = null;


function preworkerconfig(){ function preworkerconfig(){
// Everything runs after this check is completed. Elevation occurs out of process when needed. // Everything runs after this check is completed. Elevation occurs out of process when needed.
gitRepos = repomanifest.repos
gitRepos = selectedinstance.repos
// gitRepos = ['chess-server-lib']; // gitRepos = ['chess-server-lib'];
// Repositiories that have symlinks that require elevated priviletes in windows to create symlinks // Repositiories that have symlinks that require elevated priviletes in windows to create symlinks
elevatedRunasRepos = repomanifest.elevated
elevatedRunasRepos = selectedinstance.elevated
// Repos that should excluded from merge for releases... // Repos that should excluded from merge for releases...
exludeMergeRepos = repomanifest.exludeMergeRepos
exludeMergeRepos = selectedinstance.exludeMergeRepos


// mysqldump --add-drop-table --no-data -u root -p db_name | grep 'DROP TABLE' ) > drop_all_tables.sql // mysqldump --add-drop-table --no-data -u root -p db_name | grep 'DROP TABLE' ) > drop_all_tables.sql
// mysql -u root -p db_name < drop_all_tables.sql // mysql -u root -p db_name < drop_all_tables.sql
else return __runcmd(processedArgs.label || processedArgs._[0] || 'h'); else return __runcmd(processedArgs.label || processedArgs._[0] || 'h');
} }


var getManifest = function () {
// Once choices are made we need to load config according to those choices.

// No trace of a previous run...
// Default Config...
return repomanifest = selectedinstance;
}

function acquireChoices(selectedinstance) { function acquireChoices(selectedinstance) {


var hasconfig = false; var hasconfig = false;
selectedinstance.instanceName = processedArgs._[1] = processedArgs._[1] || 'chess' selectedinstance.instanceName = processedArgs._[1] = processedArgs._[1] || 'chess'
selectedinstance.node_env = processedArgs.node_env = (process.env.NODE_ENV && process.env.NODE_ENV.trim()) || processedArgs.node_env || 'development' selectedinstance.node_env = processedArgs.node_env = (process.env.NODE_ENV && process.env.NODE_ENV.trim()) || processedArgs.node_env || 'development'
selectedinstance.reposerver = 'https://git.bbh.org.in' selectedinstance.reposerver = 'https://git.bbh.org.in'
getManifest() // PB : TODO -- acquire the manifest directly from http url instead of clone before cloning the config. Since
// This is because the manifest at any server location can redirect to the preferred server..
} }
else if (choice === 'h') { else if (choice === 'h') {
processedArgs._[0] = 'h' processedArgs._[0] = 'h'
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, chessinstances) {
var acquireConfig = function (selected) {


var configrepo = selected.instanceName + '-config-' + selected.node_env; var configrepo = selected.instanceName + '-config-' + selected.node_env;


return performPull(configrepo).then(() => { 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');
repomanifest = require(manifestpath)()
chessinstances = chessinstances || {};
chessinstances[selected.instanceName] = chessinstances[selected.instanceName] || {}
chessinstances[selected.instanceName][selected.node_env] = chessinstances[selected.instanceName][selected.node_env] || {}
chessinstances['current_run'] = { instanceName: selected.instanceName, node_env: selected.node_env }
selectedinstance = require(manifestpath)()
// Config from server always override merges into selection ecept for the current selection. // Config from server always override merges into selection ecept for the current selection.
// PB : TODO -- utils.assign Array merges are non-distinct... // PB : TODO -- utils.assign Array merges are non-distinct...
selectedinstance = utils.assign(chessinstances[selected.instanceName][selected.node_env], selected, repomanifest)
// chessinstances[selectedinstance.instanceName][selectedinstance.node_env] = selectedinstance;
cacheWriteInstanceConfig(chessinstances)
selectedinstance = chessinstances[selected.instanceName][selected.node_env]
// PB : TODO -- We should probably write the new server config also...
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 } })
}
chessinstances[selected.instanceName][selected.node_env] = selectedinstance = utils.assign(selected, selectedinstance)
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)
ENV.NODE_ENV = selectedinstance.node_env; ENV.NODE_ENV = selectedinstance.node_env;
}) })
.catch((e) => { .catch((e) => {
acquireElevationState().then(() => { acquireElevationState().then(() => {
var skipprerequisites = false; var skipprerequisites = false;
var clioverrides = { } var clioverrides = { }
function initinstances(chessinstances, selected) {
chessinstances.current_run.instanceName = processedArgs._[1] = processedArgs._[1] || chessinstances.current_run.instanceName || selected.instanceName;
chessinstances.current_run.node_env = processedArgs.node_env = processedArgs.node_env || chessinstances.current_run.node_env || selected.node_env;
chessinstances[chessinstances.current_run.instanceName] = chessinstances[chessinstances.current_run.instanceName] || {}
function initinstances(selected) {
// PB : TODO -- processedArgs should already be part of selected.
var instanceName = processedArgs._[1] = processedArgs._[1] || chessinstances.current_run.instanceName || selected.instanceName;
var node_env = processedArgs.node_env = processedArgs.node_env || chessinstances.current_run.node_env || selected.node_env;
chessinstances['current_run'] = chessinstances[instanceName][selected.node_env] ?
{ instanceName: instanceName, node_env: node_env } : chessinstances['current_run']
// chessinstances[chessinstances.current_run.instanceName] = chessinstances[chessinstances.current_run.instanceName] || {}


if(path.normalize(selected.root) !== path.normalize(chessinstances[instanceName][node_env].root)) {
throw "Mismatched chessinstances config found " + chessinstances[instanceName][node_env].root + ' does not match ' + selected.root
}
// Override sequence. // Override sequence.
// __default, chessinstances[current_run], instanceName-config-development, cliargs, interactve_promts // __default, chessinstances[current_run], instanceName-config-development, cliargs, interactve_promts
selectedinstance = Object.assign( selectedinstance = Object.assign(
selected selected
, chessinstances[chessinstances.current_run.instanceName][chessinstances.current_run.node_env]
, chessinstances[instanceName][node_env]
, clioverrides , clioverrides
// , __interactve_promts -- Cant just override. Also need selectedinstance to be ready... // , __interactve_promts -- Cant just override. Also need selectedinstance to be ready...
); );
repomanifest = selectedinstance

chessinstances[instanceName] = chessinstances[instanceName] || {}
chessinstances[instanceName][node_env] = chessinstances[instanceName][node_env] || {}
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 = utils.assign(selected, 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 return chessinstances
} }
function acquirelocalinstances(selected){ function acquirelocalinstances(selected){
// Default cmd to run ! // Default cmd to run !
processedArgs._[0] === processedArgs._[0] || 'pull'; processedArgs._[0] === processedArgs._[0] || 'pull';
// selectedinstance.reposerver = repomanifest.reposervers[0] // PB : TODO -- Attempt first one that is available and online from all that are available...
// selectedinstance.reposerver = selectedinstance.reposervers[0] // PB : TODO -- Attempt first one that is available and online from all that are available.
if(!noprerequisites[processedArgs._[0]] if(!noprerequisites[processedArgs._[0]]
&& !skipprereqs[processedArgs._[0]] && !skipprereqs[processedArgs._[0]]
) { ) {
return prerequisites.git.verifyAndInstall().then(()=>{ return prerequisites.git.verifyAndInstall().then(()=>{
var e = { message : 'verifyAndInstall', success : true} var e = { message : 'verifyAndInstall', success : true}
return acquireConfig(selectedinstance, chessinstances).catch((err) => {
return acquireConfig(selectedinstance).catch((err) => {
e = err; e = err;
console.error('Chosen cofiguraton failed or not found. Fix config and rerun or chose another.') console.error('Chosen cofiguraton failed or not found. Fix config and rerun or chose another.')
console.error(err) console.error(err)
} }
try { try {
chessinstances = acquirelocalinstances(selectedinstance); chessinstances = acquirelocalinstances(selectedinstance);
initinstances(chessinstances, selectedinstance)
initinstances(selectedinstance)
var instanceNameChoices = new Set(Object.keys( chessinstances) ) var instanceNameChoices = new Set(Object.keys( chessinstances) )
instanceNameChoices.delete('current_run') instanceNameChoices.delete('current_run')
catch (e) { catch (e) {
console.error(e) // Missing chessinstances is not an error... console.error(e) // Missing chessinstances is not an error...
initinstances(chessinstances, selectedinstance)
initinstances(selectedinstance)
var instanceNameChoices = new Set(Object.keys( chessinstances) ) var instanceNameChoices = new Set(Object.keys( chessinstances) )
instanceNameChoices.delete('current_run') instanceNameChoices.delete('current_run')
todo = todo.then(() => { todo = todo.then(() => {
try { try {
chessinstances = acquirelocalinstances(selectedinstance) chessinstances = acquirelocalinstances(selectedinstance)
initinstances(chessinstances, selectedinstance)
initinstances(selectedinstance)
} }
catch (e) { catch (e) {
console.error(e) console.error(e)

Loading…
Cancel
Save