Browse Source

new build

production
guest 3 years ago
parent
commit
2d8af7a29e
1 changed files with 87 additions and 19 deletions
  1. 87
    19
      elxr.js

+ 87
- 19
elxr.js View File

var chalk = require('chalk'); var chalk = require('chalk');
const homedir = require('os').homedir(); const homedir = require('os').homedir();



var ENV = Object.assign({}, process.env); // Shallow clone it.

// singleton one time lazy // singleton one time lazy
var getCredentials = function(){ var getCredentials = function(){


var creds = fs.readFileSync(path.normalize(`${homedir}/.elxrcredentials`), { encoding: 'utf8' }); var creds = fs.readFileSync(path.normalize(`${homedir}/.elxrcredentials`), { encoding: 'utf8' });
var creds = creds.split('\n').map( c => c.trim() && new URL(c)); var creds = creds.split('\n').map( c => c.trim() && new URL(c));
console.log(creds); console.log(creds);
creds.forEach(cred => {console.log( cred.hostname ); console.log( cred.username );} );


// Subsequent calls // Subsequent calls
getCredentials = ()=>{ return creds }; getCredentials = ()=>{ return creds };


require('glob').sync; require('glob').sync;


var ENV = Object.assign({}, process.env); // Shallow clone it.

const { readdir } = require("fs").promises; const { readdir } = require("fs").promises;


// Directory shallow walk and do perform on each dir. // Directory shallow walk and do perform on each dir.


return any(tasks).then(() => { return any(tasks).then(() => {


gitRepos.push('chess-server-lib/server');
gitRepos.push({ repo : 'chess-server-lib/server'});
gitRepos = gitRepos.concat(elevatedRunasRepos); gitRepos = gitRepos.concat(elevatedRunasRepos);
// console.dir(gitRepos)
// throw ""


// gitRepos = [ // gitRepos = [
// // 'bbhverse', 'serververse', 'elixir-server', // // 'bbhverse', 'serververse', 'elixir-server',
} }
}); });


// NODE_ENV=development DEBUG=loopback:connector:mssql node --tls-min-v1.0 --inspect elixir/server.js
// PB : NOTE -- npm i for client does not complete when NODE_ENV=production // PB : NOTE -- npm i for client does not complete when NODE_ENV=production
// therefore bower doesn't get installed !!! which then fails installing the bower dependenciew !!! // therefore bower doesn't get installed !!! which then fails installing the bower dependenciew !!!
// We work around this by running npm i for client without any NODE_ENV which probably defualts to development. // We work around this by running npm i for client without any NODE_ENV which probably defualts to development.
// }) // })


var bowerRepos = [{ repo : 'client'}]; var bowerRepos = [{ repo : 'client'}];
var bowertasks = [];
bowerRepos.forEach(repodef => { bowerRepos.forEach(repodef => {
repotasks.push(() => {
bowertasks.push(() => {
console.log(instanceroot + '/' + repodef.repo + '/node_modules/bower/bin/bower'); console.log(instanceroot + '/' + repodef.repo + '/node_modules/bower/bin/bower');
// var p = nodeShellExec('node_modules/bower/bin/bower', ['install'], { // var p = nodeShellExec('node_modules/bower/bin/bower', ['install'], {
var p = nodeShellExec(`"${gitbash}"`, ['-c', '"node_modules/bower/bin/bower i"'], { var p = nodeShellExec(`"${gitbash}"`, ['-c', '"node_modules/bower/bin/bower i"'], {
}); });
}); });
// console.log('rmtasks.length : ' + rmtasks.length) // console.log('rmtasks.length : ' + rmtasks.length)
return Promise.all(rmtasks).then(() => any(repotasks));
return Promise.all(rmtasks).then(() => any(repotasks)).then(()=>any(bowertasks));
}).catch(e => { }).catch(e => {
console.error(e); console.error(e);
}).finally(statuslog.finally) }).finally(statuslog.finally)
var promise = new Promise((resolve, reject)=>{ var promise = new Promise((resolve, reject)=>{
existslink('config', function(err, data){ existslink('config', function(err, data){
if(data) { if(data) {
var p = nodeShellExec('rmdir', ['config'], { inherit: true, shell: true, env: process.env }
).catch((err) => { console.log('Ignoring benign error : ' + err); return true; });
return resolve(p);
nodeShellExec('rmdir', ['config'], { inherit: true, shell: true, env: process.env })
.then(()=>{resolve(true);})
.catch((err) => { console.log('Ignoring benign error : ' + err); return resolve(true); });
} }
else resolve(false); else resolve(false);
}); });
var promise = new Promise((resolve, reject)=>{ var promise = new Promise((resolve, reject)=>{
existslink('data', function(err, data){ existslink('data', function(err, data){
if(data) { if(data) {
var p = nodeShellExec('rmdir', ['data'], { inherit: true, shell: true, env: process.env }
).catch((err) => { console.log('Ignoring benign error : ' + err); return true; });
return resolve(p);
nodeShellExec('rmdir', ['data'], { inherit: true, shell: true, env: process.env })
.then(()=>{resolve(true);})
.catch((err) => { console.log('Ignoring benign error : ' + err); return resolve(true); });
} }
else resolve(false); else resolve(false);
}); });
tasks = tasks.concat( tasks = tasks.concat(
[ [
() => { () => {
var p = nodeShellExec('mklink', ['/J', 'data', runconfig.use + '-data'], {
var dataToLink = runconfig.use + '-data' + '-' + process.env.NODE_ENV;
if(!existsSync(dataToLink)) dataToLink = runconfig.use + '-data';
var p = nodeShellExec('mklink', ['/J', 'data', dataToLink], {
inherit: true, shell: true inherit: true, shell: true
, cwd : instanceroot , cwd : instanceroot
, env: process.env , env: process.env
return { return {
clusterNodeInstance(selected) { var clusternodename = 'node01'; clusterNodeInstance(selected) { var clusternodename = 'node01';
return __acquireData(selected, selected.username, clusternodename return __acquireData(selected, selected.username, clusternodename
, selected.instanceName + '-config-' + selected.node_env + `-${clusternodename}`
, selected.instanceName + '-data-' + 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.'); , function(e){ console.info('Customized node level config not found. This is not an Error. Will attempt with owner level config.');
return e; } return e; }
) )
} }
, ownerInstnace(selected) { return __acquireData(selected, selected.username, null, null
, ownerInstnace(selected) { return __acquireData(selected, selected.username, null
, selected.instanceName + '-data-' + selected.node_env
, function(e){ console.info('Customized user level config not found. This is not an Error. Will attempt global common instance config.'); , function(e){ console.info('Customized user level config not found. This is not an Error. Will attempt global common instance config.');
return e } return e }
) )
var successHandler = () => { var successHandler = () => {


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);
utils.assign_strict(selectedinstance, require(manifestpath)( null, selectedinstance));
console.dir(selectedinstance.repos);
// 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.
// PB : TODO -- utils.assign Array merges are non-distinct... // PB : TODO -- utils.assign Array merges are non-distinct...
if(!selectedinstance.repos[0].repo) { if(!selectedinstance.repos[0].repo) {


var __acquireData = function (selected, owner, clusternodename, datarepo, errHandler) { var __acquireData = function (selected, owner, clusternodename, datarepo, errHandler) {


datarepo = datarepo || selected.instanceName + '-config-' + selected.node_env;
datarepo = datarepo || selected.instanceName + '-data';


var errorHandler = (e) => { var errorHandler = (e) => {
if(e.messages.join(' ').match(new RegExp (`fatal: unable to access '${selectedinstance.reposerver}/${owner}/${datarepo}.git/': Failed to connect to .*? port .*? after .*? ms: Timed out`))){ if(e.messages.join(' ').match(new RegExp (`fatal: unable to access '${selectedinstance.reposerver}/${owner}/${datarepo}.git/': Failed to connect to .*? port .*? after .*? ms: Timed out`))){


if(e.messages.join(' ').match(new RegExp (`fatal: repository '${selectedinstance.reposerver}/${owner}/${datarepo}.git/' not found`))){ if(e.messages.join(' ').match(new RegExp (`fatal: repository '${selectedinstance.reposerver}/${owner}/${datarepo}.git/' not found`))){
var choices = { var choices = {
t : `install a new temporary local instance with this name ( will not persist ).
Use your own username for additional options. You can request for a username at chess@bbh.org.in )`
t : `install a temporary local data folder.
For more options. Request and use a personal username at chess@bbh.org.in )`
, e : 'exit' }; , e : 'exit' };


if(selectedinstance.username !== 'guest' && selectedinstance.username !== 'demo') { if(selectedinstance.username !== 'guest' && selectedinstance.username !== 'demo') {
return GIT return GIT
})(); })();


function createInstanceData(target, source) {

var sourceinstance = source || target;
console.dir(sourceinstance);
var args = {
remotebase : sourceinstance.reposerver + '/chess/'
, sourcerepo : sourceinstance.repo || 'chess-data'
, targetrepo : `${target.instanceName}-data-${target.instanceType}${target.nodeName ? ('-' + target.nodeName) : ''}`
};

if(sourceinstance.local) {

var options = {
inherit: true, shell: true,
env: process.env
, cwd : instanceroot
, runas: processedArgs.runas
};
var cmdseq = [
['git', ['clone', `${args.remotebase}${args.sourcerepo}`, `${args.targetrepo}`], options]
];
return any(cmdseq.map(getshelltask)).then(() => { return true })
}
else {
// http://try.gitea.io/api/v1/org/{org}/repos
if(source.reposerver !== target.reposerver && source.username !== target.username) {
throw 'createInstanceData is possible only within the same repository server.'
}

var server = new URL(target.reposerver);
return GITEA.repository.fork( { hostname : server.host, protocol : server.protocol
, username : target.username, password : target.password
}
// , { repo : `${selectedinstance.instanceName}-config-${selectedinstance.instanceType}`}
, { repo : `${args.sourcerepo}`, owner : `${target.username}` }, {}, function( repository ){
return GITEA.repository.updateattributes( {
hostname : server.host, protocol : server.protocol
, username : target.username, password : target.password
}
, { repo : `${args.sourcerepo}`, owner : `${target.username}` }
, { name : `${args.targetrepo}`}
)
}
)
}
// GITEA.repository.updateattributes( {
// hostname : server.host, protocol : server.protocol
// , username : selectedinstance.username, password : selectedinstance.password
// }
// , { repo : `chess-config`, owner : selectedinstance.username }
// , { name : `${selectedinstance.instanceName}-config-${selectedinstance.instanceType}${selectedinstance.nodeName ? '-' + selectedinstance.nodeName : ''}`}
// )
// return selectedinstance
}


function createInstance(target, source) { function createInstance(target, source) {


var sourceinstance = source || target; var sourceinstance = source || target;

Loading…
Cancel
Save