From 0c74da3b20339cc0a76c535e9a07779bfafbd4c3 Mon Sep 17 00:00:00 2001 From: philc Date: Mon, 17 Apr 2023 12:03:56 +0200 Subject: [PATCH] maj test git apres changement fai --- .gitignore | 1 + app/models/Setup.js | 394 ++++++++++-------- app/models/Setupold.js | 174 ++++++++ apxtrib.js | 35 +- nationchains/logs/nginx/setup.town.access.log | 4 + .../www/{setup => adminapx}/index.html | 0 .../nginx/modelwebsite.conf.mustache | 6 - .../nginx/nginx.conf.mustache | 6 +- .../www/{setup => adminapx}/townconf.json | 5 +- .../www/{setup => adminapx}/tribeconf.json | 0 nationchains/www/nginx_setup.conf | 52 +++ package.json | 1 + 12 files changed, 472 insertions(+), 206 deletions(-) mode change 100755 => 100644 app/models/Setup.js create mode 100755 app/models/Setupold.js create mode 100644 nationchains/logs/nginx/setup.town.access.log rename nationchains/www/{setup => adminapx}/index.html (100%) rename nationchains/www/{setup => adminapx}/nginx/modelwebsite.conf.mustache (87%) rename nationchains/www/{setup => adminapx}/nginx/nginx.conf.mustache (90%) rename nationchains/www/{setup => adminapx}/townconf.json (79%) rename nationchains/www/{setup => adminapx}/tribeconf.json (100%) create mode 100644 nationchains/www/nginx_setup.conf diff --git a/.gitignore b/.gitignore index e015f5d..95dd87b 100755 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ /node_modules /tmp +/nginx /nationchains/blocks /nationchains/tribes /yarn* \ No newline at end of file diff --git a/app/models/Setup.js b/app/models/Setup.js old mode 100755 new mode 100644 index 97f7a8f..3e1c130 --- a/app/models/Setup.js +++ b/app/models/Setup.js @@ -1,174 +1,242 @@ -const fs = require( 'fs-extra' ); -const path = require( 'path' ); -const dnsSync = require( 'dns-sync' ); -const Mustache = require( 'mustache' ); -const Nations = require('./Nations.js') +const fs = require("fs-extra"); +const path = require("path"); +const dnsSync = require("dns-sync"); +const mustache = require("mustache"); +const readlineSync = require("readline-sync"); +/** + * This Setup is run at the first installation + * This is not an exportable module + * + * + * + */ + const Setup = {}; +//const nationsync = require('./Nations').updateChains(); -const nationsync = Nations.updateChains() - -if (nationsync.status!=200){ - console.log( '\x1b[31m Check your internet access, to setup this town we need to update the Nations. It seems we cannot do it' ); - process.exit(); -} -if( !fs.existsSync( '/etc/nginx/nginx.conf' ) ) { - console.log( '\x1b[31m Check documentation, nginx have to be installed on this server first, no /etc/nginx/nginx.conf available' ); - process.exit(); -} -if( !fs.existsSync( './nationchains/tribes/index/conf.json' ) ){ - console.log( `\x1b[42m####################################\nWellcome into apxtrib, this is a first install.\nWe need to make this server accessible from internet subdomain.domain to current IP. This setup will create your unique tribeid, with an admin login user to let you connect to the parameter interface.\nCheck README's project to learn more. more.\n#####################################\x1b[0m` ); - const townSetup = fs.readJsonSync( './app/setup/townSetup.json') ; - console.log( `Current setup conf from :./app/setup/townSetup.json\nChange with relevant setup data and rerun yarn setup` ) ; - console.log( townSetup ) - const readline = require( 'readline' ); - const rl = readline.createInterface( { - input: process.stdin, - output: process.stdout - } ); - rl.question( 'This is the data from ./app/setup/townSetup.json used, is it correct to use as first install (Yes/no)?', function ( rep1 ) { - let quest = `This is a production install, please check that ${townSetup.townName}.${townSetup.nationName}.${townSetup.dns} IP is well redirect to tour server`; - if( rep1 !== "Yes" ) process.exit( 0 ); - if( townSetup.dns == 'unchain' ) { - quest = `This is a development installation, please add in your /etc/hosts 127.0.0.1 ${townSetup.townName}.${townSetup.nationName}.${townSetup.dns} `; - } - rl.question( quest + '\nAre you sure to set this? (Yes/no)', function ( rep2 ) { - if( rep2 == "Yes" ) { - const Checkjson = Setup.check( townSetup ); - if( Checkjson == "" ) { - const townconf=fs.readJsonSync('./app/setup/townconf.json') - // create tribes folder with townconf.json - const towndata={...townSetup,...townconf}; - const Towns = require('./Towns'); - const Tribes = require('./Tribes'); - const Pagans = require('./Pagans'); - if (!towndata.mayorid ) Pagans.create - - Towns.create('./nationchains','./nationchains','towns',{...townSetup,...townconf}); - //Nationschains.create(townSetup); - Tribes.create(townSetup); - - // creer un lien symbolique vers /nationchains/ pour traiter les demandes via xworkon comme une tribe - //Setup.config( townSetup ); - } else { - console.log( check ); - } - } else { - console.log( 'Nothing done please, check setup/configsetup.json and answer twice Yes' ) - } - rl.close(); - } ); - } ); - rl.on( 'close', function () { - console.log( '\n Setup process ended' ); - process.exit( 0 ); - } ); -} else { - console.log( 'Carefull you have already a config.js that is running. If you want to change remove config.js file and run again yarn setup' ); -} - -Setup.Checkjson = conf => { - var rep = ""; - const nation_town=fs.readJsonSync('./nationchains/socialworld/objects/towns/searchindex/towns_nation_uuid.json'); - if (!ObjectKeys(nation_town).includes(conf.nationName)){ - rep+=`your nationName ${conf.nationName} does not exist you have to choose an existing one`; - } - if (nation_town[conf.nationName].includes(conf.townName)){ - rep+=`This conf.townName already exist you have to find a unique town name per nation`; - } - const getnation = Odmdb.get('./nationchains/socialworld/objects','towns',[conf.NationName],[nationId]) - //if getnation.data.notfound - conf.language.forEach( l => { - if( ![ "fr", "en", "it", "de", "sp" ].includes( l ) ) { - rep += l + " Only fr,en,it,de,sp are available \n"; - } - } ); - if( !fs.existsSync( `/home/${conf.sudoerUser}` ) ) { - rep += `/home/${conf.sudoerUser} does not exist, user has to be create with a /home on this server\n`; - } - try { - if ("true"== execSync("timeout 2 sudo id && sudo=\"true\" || sudo=\"false\";echo \"$sudo\"").toString().trim().split(/\r?\n/).slice(-1)) { - rep+=`${sudoerUser} is not sudoer please change this `; - } ; - }catch(err){ - console.log(err); - rep+=" Check your user it seems to not be a sudoer" - } - if( conf.jwtsecret.length < 32 ) { - rep += "Your jwtsecretkey must have at least 32 characters" - } - if( conf.dns != 'unchain' && !dnsSync.resolve( `${conf.townName}.${conf.nationName}.${conf.dns}` ) ) { - rep += `\nresolving $${conf.townName}.${conf.nationName}.${conf.dns} will not responding valid IP, please setup domain redirection IP before runing this script` - } - return rep +Setup.check = () => { + if ("testinternet" != "testinternet") { + console.log( + "\x1b[31m Check your internet access, to setup this town we need to update the Nations. It seems we cannot do it" + ); + process.exit(); + } + if (!fs.existsSync("/etc/nginx/nginx.conf")) { + console.log( + "\x1b[31m Check documentation, nginx have to be installed on this server first, no /etc/nginx/nginx.conf available" + ); + process.exit(); + } + if (fs.existsSync("./nationchains/tribes/index/conf.json")) { + console.log( + "\x1b[31m Be carefull you already have a town set in ./nationchains/tribes/index.conf.json, check and remove it if you want to setup this town." + ); + process.exit(); + } + return true; }; -Setup.config = ( townSetup ) => { - // Init this instance with a .config.js - Setup.configjs( townSetup ); - // Create tribeid space + a user admin + webspace withe apxtrib webapp install - Setup.druidid( townSetup ); +Setup.init = () => { + // Get standard conf and current data + const townconf = fs.readJsonSync("./nationchains/www/adminapx/townconf.json"); + townconf.sudoerUser = process.env.USER; + townconf.dirname = path.resolve(`${__dirname}/../../`); + // nginx allow to set a new website space + townconf.nginx.include.push(`${townconf.dirname}/nationchains/**/nginx_*.conf`); + townconf.nginx.logs = `${townconf.dirname}/nationchains/logs/nginx`; + townconf.nginx.website = "setup"; + townconf.nginx.fswww = "nationchains/"; //for a local tribe nationchains/tribes/tribeid + townconf.nginx.tribeid = "town"; + + console.log(townconf); + if ( + !readlineSync.keyInYN( + `\x1b[42mThis is the first install from ./nationchains/www/adminapx/townconf.json (check it if you want) \nthis will change your nginx config in /etc/nginx and run nginx from sudoer user ${townconf.sudoerUser} (Yes/no)?\nno if you want to change parameter and run yarn setup again\x1b[0m` + ) + ) + process.exit(); + // saved and change nginx conf + if (!fs.existsSync("/etc/nginx/nginxconf.saved")) { + fs.moveSync("/etc/nginx/nginx.conf", "/etc/nginx/nginxconf.saved"); + console.log("your previous /etc/nginx/nginx.conf was backup in /etc/nginx/nginxconf.saved"); + } + const tplnginxconf = fs.readFileSync( + "./nationchains/www/adminapx/nginx/nginx.conf.mustache", + "utf8" + ); + fs.outputFileSync( + "/etc/nginx/nginx.conf", + mustache.render(tplnginxconf, townconf), + "utf8" + ); + const tplnginxwww = fs.readFileSync( + "./nationchains/www/adminapx/nginx/modelwebsite.conf.mustache", + "utf8" + ); + fs.outputFileSync( + `./${townconf.nginx.fswww}www/nginx_${townconf.nginx.website}.conf`, + mustache.render(tplnginxwww, townconf), + "utf8" + ); + fs.outputJsonSync("./nationchains/tribes/index/conf.json", townconf, { + spaces: 2, + }); + //restart nginx + const { exec } = require("child_process"); + exec(townconf.nginx.restart, (error, stdout, stderr) => { + if (error) { + console.log("\x1b[42m", error, stdout, stderr, "x1b[0m"); + } else { + console.log( + `\x1b[42m#########################################################################\x1b[0m\n\x1b[42mWellcome into apxtrib, you can now 'yarn dev' for dev or 'yarn startpm2' for prod or 'yarn unittest' for testing purpose.\x1b[0m \n\x1b[42m Access to your town here http://${townconf.dns} to finist your town set up.\nCheck README's project to learn more.\x1b[0m\n\x1b[42m#########################################################################\x1b[0m` + ); + } + }); }; -Setup.configjs = ( townSetup ) => { - // Set /config.js - let confapxtrib = fs.readFileSync( './setup/config.mustache', 'utf-8' ); - fs.writeFileSync( './config.js', Mustache.render( confapxtrib, townSetup ), 'utf-8' ); - if( fs.existsSync( './config.js' ) ) { - console.log( 'config.js successfully created.' ); - } else { - console.log( "config.js not created, check what's wrong in tpl:", confapxtrib ); - console.log( "for data :", townSetup ); - process.exit(); - } + +Setup.Checkjson = (conf) => { + var rep = ""; + const nation_town = fs.readJsonSync( + "./nationchains/socialworld/objects/towns/searchindex/towns_nation_uuid.json" + ); + if (!ObjectKeys(nation_town).includes(conf.nationName)) { + rep += `your nationName ${conf.nationName} does not exist you have to choose an existing one`; + } + if (nation_town[conf.nationName].includes(conf.townName)) { + rep += `This conf.townName already exist you have to find a unique town name per nation`; + } + const getnation = Odmdb.get( + "./nationchains/socialworld/objects", + "towns", + [conf.NationName], + [nationId] + ); + //if getnation.data.notfound + conf.language.forEach((l) => { + if (!["fr", "en", "it", "de", "sp"].includes(l)) { + rep += l + " Only fr,en,it,de,sp are available \n"; + } + }); + if (!fs.existsSync(`/home/${conf.sudoerUser}`)) { + rep += `/home/${conf.sudoerUser} does not exist, user has to be create with a /home on this server\n`; + } + try { + if ( + "true" == + execSync('timeout 2 sudo id && sudo="true" || sudo="false";echo "$sudo"') + .toString() + .trim() + .split(/\r?\n/) + .slice(-1) + ) { + rep += `${sudoerUser} is not sudoer please change this `; + } + } catch (err) { + console.log(err); + rep += " Check your user it seems to not be a sudoer"; + } + if (conf.jwtsecret.length < 32) { + rep += "Your jwtsecretkey must have at least 32 characters"; + } + if ( + conf.dns != "unchain" && + !dnsSync.resolve(`${conf.townName}.${conf.nationName}.${conf.dns}`) + ) { + rep += `\nresolving $${conf.townName}.${conf.nationName}.${conf.dns} will not responding valid IP, please setup domain redirection IP before runing this script`; + } + return rep; }; -Setup.druidid = ( townSetup ) => { - // create a tribeid with a user that will admin this instance into /tribes/tribeid /users - const config = require( '../config.js' ); - // Need to do it on setup this is also done again in models/Tribes.js - console.log( `${config.tribes}/${townSetup.druidid}` ) - fs.ensureDirSync( `${config.tribes}/${townSetup.druidid}` ); - [ 'users', 'www', 'referentials', 'nationchains' ].forEach( r => { - fs.copySync( `${__base}/setup/tribes/apxtrib/${r}`, `${config.tribes}/${townSetup.druidid}/${r}` ); - } ) - /* const confcli = JSON.parse( Mustache.render( fs.readFileSync( `${__base}/setup/tribes/apxtrib/clientconf.mustache`, 'utf8' ), townSetup ) ); + +Setup.config = (townSetup) => { + // Init this instance with a .config.js + Setup.configjs(townSetup); + // Create tribeid space + a user admin + webspace withe apxtrib webapp install + Setup.druidid(townSetup); +}; +Setup.configjs = (townSetup) => { + // Set /config.js + let confapxtrib = fs.readFileSync("./setup/config.mustache", "utf-8"); + fs.writeFileSync( + "./config.js", + Mustache.render(confapxtrib, townSetup), + "utf-8" + ); + if (fs.existsSync("./config.js")) { + console.log("config.js successfully created."); + } else { + console.log( + "config.js not created, check what's wrong in tpl:", + confapxtrib + ); + console.log("for data :", townSetup); + process.exit(); + } +}; +Setup.druidid = (townSetup) => { + // create a tribeid with a user that will admin this instance into /tribes/tribeid /users + const config = require("../config.js"); + // Need to do it on setup this is also done again in models/Tribes.js + console.log(`${config.tribes}/${townSetup.druidid}`); + fs.ensureDirSync(`${config.tribes}/${townSetup.druidid}`); + ["users", "www", "referentials", "nationchains"].forEach((r) => { + fs.copySync( + `${__base}/setup/tribes/apxtrib/${r}`, + `${config.tribes}/${townSetup.druidid}/${r}` + ); + }); + /* const confcli = JSON.parse( Mustache.render( fs.readFileSync( `${__base}/setup/tribes/apxtrib/clientconf.mustache`, 'utf8' ), townSetup ) ); fs.outputJsonSync( `${config.tribes}/${townSetup.druidid}/clientconf.json`, confcli ); // Create a new tribeid + admin user for this tribeid // with access to {druidid}:webapp as admin */ - const Tribes = require( '../models/Tribes.js' ); - const access = { app: {}, data: {} } - access.app[ `${townSetup.druidid}:webapp` ] = "admin"; - access.data[ townSetup.druidid ] = { "users": "CRUDO", "referentials": "CRUDO", "www": "CRUDO" }; - const createclient = Tribes.create( { - tribeid: townSetup.druidid, - genericpsw: townSetup.genericpsw, - lanquageReferential: townSetup.language, - useradmin: { - LOGIN: townSetup.login, - xlang: townSetup.language[ 0 ], - ACCESSRIGHTS: access - } - } ); - if( createclient.status == 200 ) { - console.log( `Your tribeid domain was created with login : ${townSetup.login} and password: ${townSetup.genericpsw}, change it after the 1st login on https://${townSetup.subdomain}.${townSetup.domain}` ); - // Create nginx conf for a first install - const confnginx = fs.readFileSync( './setup/nginx/nginx.conf.mustache', 'utf8' ); - fs.outputFileSync( '/etc/nginx/nginx.conf', Mustache.render( confnginx, townSetup ), 'utf-8' ); - // Create a spacedev for webapp of apxtrib - // that will be accesible in prod from https://subdomain.domain/ and in dev http://webapp.local.fr - const addspaceweb = Tribes.addspaceweb( { - setup: true, - dnsname: [ `${townSetup.subdomain}.${townSetup.domain}` ], - mode: townSetup.mode, - tribeid: townSetup.druidid, - website: 'webapp', - pageindex: "app_index_fr.html" - } ); - if( addspaceweb.status == 200 ) { - console.log( `WELL DONE run yarn dev to test then yarn startpm2 ` ) - } - } else { - console.log( 'Issue ', createclient ) - } -} + const Tribes = require("./Tribes.js"); + const access = { app: {}, data: {} }; + access.app[`${townSetup.druidid}:webapp`] = "admin"; + access.data[townSetup.druidid] = { + users: "CRUDO", + referentials: "CRUDO", + www: "CRUDO", + }; + const createclient = Tribes.create({ + tribeid: townSetup.druidid, + genericpsw: townSetup.genericpsw, + lanquageReferential: townSetup.language, + useradmin: { + LOGIN: townSetup.login, + xlang: townSetup.language[0], + ACCESSRIGHTS: access, + }, + }); + if (createclient.status == 200) { + console.log( + `Your tribeid domain was created with login : ${townSetup.login} and password: ${townSetup.genericpsw}, change it after the 1st login on https://${townSetup.subdomain}.${townSetup.domain}` + ); + // Create nginx conf for a first install + const confnginx = fs.readFileSync( + "./setup/nginx/nginx.conf.mustache", + "utf8" + ); + fs.outputFileSync( + "/etc/nginx/nginx.conf", + Mustache.render(confnginx, townSetup), + "utf-8" + ); + // Create a spacedev for webapp of apxtrib + // that will be accesible in prod from https://subdomain.domain/ and in dev http://webapp.local.fr + const addspaceweb = Tribes.addspaceweb({ + setup: true, + dnsname: [`${townSetup.subdomain}.${townSetup.domain}`], + mode: townSetup.mode, + tribeid: townSetup.druidid, + website: "webapp", + pageindex: "app_index_fr.html", + }); + if (addspaceweb.status == 200) { + console.log(`WELL DONE run yarn dev to test then yarn startpm2 `); + } + } else { + console.log("Issue ", createclient); + } +}; -module.exports = Setup; +if (Setup.check()) Setup.init(); diff --git a/app/models/Setupold.js b/app/models/Setupold.js new file mode 100755 index 0000000..97f7a8f --- /dev/null +++ b/app/models/Setupold.js @@ -0,0 +1,174 @@ +const fs = require( 'fs-extra' ); +const path = require( 'path' ); +const dnsSync = require( 'dns-sync' ); +const Mustache = require( 'mustache' ); +const Nations = require('./Nations.js') +const Setup = {}; + +const nationsync = Nations.updateChains() + +if (nationsync.status!=200){ + console.log( '\x1b[31m Check your internet access, to setup this town we need to update the Nations. It seems we cannot do it' ); + process.exit(); +} +if( !fs.existsSync( '/etc/nginx/nginx.conf' ) ) { + console.log( '\x1b[31m Check documentation, nginx have to be installed on this server first, no /etc/nginx/nginx.conf available' ); + process.exit(); +} +if( !fs.existsSync( './nationchains/tribes/index/conf.json' ) ){ + console.log( `\x1b[42m####################################\nWellcome into apxtrib, this is a first install.\nWe need to make this server accessible from internet subdomain.domain to current IP. This setup will create your unique tribeid, with an admin login user to let you connect to the parameter interface.\nCheck README's project to learn more. more.\n#####################################\x1b[0m` ); + const townSetup = fs.readJsonSync( './app/setup/townSetup.json') ; + console.log( `Current setup conf from :./app/setup/townSetup.json\nChange with relevant setup data and rerun yarn setup` ) ; + console.log( townSetup ) + const readline = require( 'readline' ); + const rl = readline.createInterface( { + input: process.stdin, + output: process.stdout + } ); + rl.question( 'This is the data from ./app/setup/townSetup.json used, is it correct to use as first install (Yes/no)?', function ( rep1 ) { + let quest = `This is a production install, please check that ${townSetup.townName}.${townSetup.nationName}.${townSetup.dns} IP is well redirect to tour server`; + if( rep1 !== "Yes" ) process.exit( 0 ); + if( townSetup.dns == 'unchain' ) { + quest = `This is a development installation, please add in your /etc/hosts 127.0.0.1 ${townSetup.townName}.${townSetup.nationName}.${townSetup.dns} `; + } + rl.question( quest + '\nAre you sure to set this? (Yes/no)', function ( rep2 ) { + if( rep2 == "Yes" ) { + const Checkjson = Setup.check( townSetup ); + if( Checkjson == "" ) { + const townconf=fs.readJsonSync('./app/setup/townconf.json') + // create tribes folder with townconf.json + const towndata={...townSetup,...townconf}; + const Towns = require('./Towns'); + const Tribes = require('./Tribes'); + const Pagans = require('./Pagans'); + if (!towndata.mayorid ) Pagans.create + + Towns.create('./nationchains','./nationchains','towns',{...townSetup,...townconf}); + //Nationschains.create(townSetup); + Tribes.create(townSetup); + + // creer un lien symbolique vers /nationchains/ pour traiter les demandes via xworkon comme une tribe + //Setup.config( townSetup ); + } else { + console.log( check ); + } + } else { + console.log( 'Nothing done please, check setup/configsetup.json and answer twice Yes' ) + } + rl.close(); + } ); + } ); + rl.on( 'close', function () { + console.log( '\n Setup process ended' ); + process.exit( 0 ); + } ); +} else { + console.log( 'Carefull you have already a config.js that is running. If you want to change remove config.js file and run again yarn setup' ); +} + +Setup.Checkjson = conf => { + var rep = ""; + const nation_town=fs.readJsonSync('./nationchains/socialworld/objects/towns/searchindex/towns_nation_uuid.json'); + if (!ObjectKeys(nation_town).includes(conf.nationName)){ + rep+=`your nationName ${conf.nationName} does not exist you have to choose an existing one`; + } + if (nation_town[conf.nationName].includes(conf.townName)){ + rep+=`This conf.townName already exist you have to find a unique town name per nation`; + } + const getnation = Odmdb.get('./nationchains/socialworld/objects','towns',[conf.NationName],[nationId]) + //if getnation.data.notfound + conf.language.forEach( l => { + if( ![ "fr", "en", "it", "de", "sp" ].includes( l ) ) { + rep += l + " Only fr,en,it,de,sp are available \n"; + } + } ); + if( !fs.existsSync( `/home/${conf.sudoerUser}` ) ) { + rep += `/home/${conf.sudoerUser} does not exist, user has to be create with a /home on this server\n`; + } + try { + if ("true"== execSync("timeout 2 sudo id && sudo=\"true\" || sudo=\"false\";echo \"$sudo\"").toString().trim().split(/\r?\n/).slice(-1)) { + rep+=`${sudoerUser} is not sudoer please change this `; + } ; + }catch(err){ + console.log(err); + rep+=" Check your user it seems to not be a sudoer" + } + if( conf.jwtsecret.length < 32 ) { + rep += "Your jwtsecretkey must have at least 32 characters" + } + if( conf.dns != 'unchain' && !dnsSync.resolve( `${conf.townName}.${conf.nationName}.${conf.dns}` ) ) { + rep += `\nresolving $${conf.townName}.${conf.nationName}.${conf.dns} will not responding valid IP, please setup domain redirection IP before runing this script` + } + return rep +}; + +Setup.config = ( townSetup ) => { + // Init this instance with a .config.js + Setup.configjs( townSetup ); + // Create tribeid space + a user admin + webspace withe apxtrib webapp install + Setup.druidid( townSetup ); +}; +Setup.configjs = ( townSetup ) => { + // Set /config.js + let confapxtrib = fs.readFileSync( './setup/config.mustache', 'utf-8' ); + fs.writeFileSync( './config.js', Mustache.render( confapxtrib, townSetup ), 'utf-8' ); + if( fs.existsSync( './config.js' ) ) { + console.log( 'config.js successfully created.' ); + } else { + console.log( "config.js not created, check what's wrong in tpl:", confapxtrib ); + console.log( "for data :", townSetup ); + process.exit(); + } +}; +Setup.druidid = ( townSetup ) => { + // create a tribeid with a user that will admin this instance into /tribes/tribeid /users + const config = require( '../config.js' ); + // Need to do it on setup this is also done again in models/Tribes.js + console.log( `${config.tribes}/${townSetup.druidid}` ) + fs.ensureDirSync( `${config.tribes}/${townSetup.druidid}` ); + [ 'users', 'www', 'referentials', 'nationchains' ].forEach( r => { + fs.copySync( `${__base}/setup/tribes/apxtrib/${r}`, `${config.tribes}/${townSetup.druidid}/${r}` ); + } ) + /* const confcli = JSON.parse( Mustache.render( fs.readFileSync( `${__base}/setup/tribes/apxtrib/clientconf.mustache`, 'utf8' ), townSetup ) ); + fs.outputJsonSync( `${config.tribes}/${townSetup.druidid}/clientconf.json`, confcli ); + // Create a new tribeid + admin user for this tribeid + // with access to {druidid}:webapp as admin + */ + const Tribes = require( '../models/Tribes.js' ); + const access = { app: {}, data: {} } + access.app[ `${townSetup.druidid}:webapp` ] = "admin"; + access.data[ townSetup.druidid ] = { "users": "CRUDO", "referentials": "CRUDO", "www": "CRUDO" }; + const createclient = Tribes.create( { + tribeid: townSetup.druidid, + genericpsw: townSetup.genericpsw, + lanquageReferential: townSetup.language, + useradmin: { + LOGIN: townSetup.login, + xlang: townSetup.language[ 0 ], + ACCESSRIGHTS: access + } + } ); + if( createclient.status == 200 ) { + console.log( `Your tribeid domain was created with login : ${townSetup.login} and password: ${townSetup.genericpsw}, change it after the 1st login on https://${townSetup.subdomain}.${townSetup.domain}` ); + // Create nginx conf for a first install + const confnginx = fs.readFileSync( './setup/nginx/nginx.conf.mustache', 'utf8' ); + fs.outputFileSync( '/etc/nginx/nginx.conf', Mustache.render( confnginx, townSetup ), 'utf-8' ); + // Create a spacedev for webapp of apxtrib + // that will be accesible in prod from https://subdomain.domain/ and in dev http://webapp.local.fr + const addspaceweb = Tribes.addspaceweb( { + setup: true, + dnsname: [ `${townSetup.subdomain}.${townSetup.domain}` ], + mode: townSetup.mode, + tribeid: townSetup.druidid, + website: 'webapp', + pageindex: "app_index_fr.html" + } ); + if( addspaceweb.status == 200 ) { + console.log( `WELL DONE run yarn dev to test then yarn startpm2 ` ) + } + } else { + console.log( 'Issue ', createclient ) + } +} + +module.exports = Setup; diff --git a/apxtrib.js b/apxtrib.js index 2cadab4..f9d992f 100755 --- a/apxtrib.js +++ b/apxtrib.js @@ -17,38 +17,9 @@ if( !fs.existsSync( '/etc/nginx/nginx.conf' ) ) { process.exit(); } if( !fs.existsSync( './nationchains/tribes/index/conf.json' ) ) { - // this is a first installation of a dev or prod machine - const readline = require( 'readline' ); - const rl = readline.createInterface( { - input: process.stdin, - output: process.stdout - } ); - const townconf = fs.readJsonSync( './nationchains/www/setup/townconf.json') ; - townconf.sudoerUser=process.env.USER; - townconf.dirname=__dirname; - townconf.nginx.include.push(`${__dirname}/nationchains/**/nginx_*.conf`); - townconf.nginx.logs=`${__dirname}/nationchains/logs/nginx`; - townconf.nginx.website='setup'; - townconf.nginx.fswww='nationchains/';//for a local tribe nationchains/tribes/tribeid - townconf.nginx.tribeid="town" - console.log(townconf) - - rl.question( 'This is the first install from ./nationchains/www/setup/townconf.json used, this will change your nginx config (/etc/nginx.conf will be saved as /etc/nginxconf.saved) (Yes/no)?', function ( rep1 ) { - let quest = `This is a production install, please check that ${townconf.townName}.${townconf.nationName}.${townconf.dns} IP is well redirect to tour server`; - if( rep1 !== "Yes" ) process.exit( 0 ); - - //console.log(process.env) - // saved and change nginx conf - const mustache=require('Mustache'); - fs.moveSync("/etc/nginx/nginx.conf","/etc/nginx/nginxconf.saved"); - const tplnginxconf=fs.readFileSync("./nationchains/www/setup/nginx/nginx.conf.mustache","utf8"); - fs.outputFileSync("/etc/nginx/nginx.conftest",mustache.render(tplnginxconf, townconf),"utf8") - const tplnginxwww=fs.readFileSync("./nationchains/www/setup/nginx/modelwebsiteconf.mustache","utf8"); - fs.outputFileSync(`./${townconf.nginx.fswww}www/nginx_${townconf.nginx.website}.conf`,mustache.render(tplnginxwww, townconf),"utf8") - //restart nginx - //fs.outputJsonSync('./nationchains/tribes/index/conf.json',setupconf); - console.log( `\x1b[42m#########################################################################\x1b[0m\n\x1b[42mWellcome into apxtrib, init your town and first tribe by 'yarn setup'. \x1b[0m \n\x1b[42mThen 'yarn dev' or 'yarn startpm2' or 'yarn unittest'. Check README's project to learn more.\x1b[0m\n\x1b[42m#########################################################################\x1b[0m` ); - process.exit(); + // this town is not set + console.log( `\x1b[42m############################################################################################\x1b[0m\n\x1b[42mWellcome into apxtrib, you must first init your town and tribes by a 'yarn setup'. \x1b[0m \n\x1b[42mThen 'yarn dev' or 'yarn startpm2' or 'yarn unittest'. Check README's project to learn more.\x1b[0m\n\x1b[42m############################################################################################\x1b[0m` ); + process.exit(); } const config = require( './nationchains/tribes/index/conf.json' ); // Tribes allow to get local apxtrib instance context diff --git a/nationchains/logs/nginx/setup.town.access.log b/nationchains/logs/nginx/setup.town.access.log new file mode 100644 index 0000000..69495ed --- /dev/null +++ b/nationchains/logs/nginx/setup.town.access.log @@ -0,0 +1,4 @@ +[16/Apr/2023:14:28:49 +0200]##"-"##"GET / HTTP/1.1" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36" +[16/Apr/2023:19:46:43 +0200]##"-"##"GET / HTTP/1.1" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36" +[17/Apr/2023:11:00:16 +0200]##"-"##"GET / HTTP/1.1" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36" +[17/Apr/2023:11:00:16 +0200]##"-"##"GET /favicon.ico HTTP/1.1" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36" diff --git a/nationchains/www/setup/index.html b/nationchains/www/adminapx/index.html similarity index 100% rename from nationchains/www/setup/index.html rename to nationchains/www/adminapx/index.html diff --git a/nationchains/www/setup/nginx/modelwebsite.conf.mustache b/nationchains/www/adminapx/nginx/modelwebsite.conf.mustache similarity index 87% rename from nationchains/www/setup/nginx/modelwebsite.conf.mustache rename to nationchains/www/adminapx/nginx/modelwebsite.conf.mustache index f5c8b41..e57fac7 100755 --- a/nationchains/www/setup/nginx/modelwebsite.conf.mustache +++ b/nationchains/www/adminapx/nginx/modelwebsite.conf.mustache @@ -1,11 +1,5 @@ server { server_name {{#dns}} {{.}} {{/dns}}; - add_header X-Request-ID $request_id; # Return to client - add_header Access-Control-Allow-Origin *; - add_header Access-Control-Max-Age 3600; - add_header Access-Control-Expose-Headers Content-Length; - add_header Access-Control-Allow-Headers Range; - access_log {{{dirname}}}/{{{nginx.fswww}}}logs/nginx/{{{nginx.website}}}.{{{nginx.tribeid}}}.access.log main; # /plugins/pluginame/components/xxx?plugin=pluginname&pluginkey=key # acess if exist pluginkey diff --git a/nationchains/www/setup/nginx/nginx.conf.mustache b/nationchains/www/adminapx/nginx/nginx.conf.mustache similarity index 90% rename from nationchains/www/setup/nginx/nginx.conf.mustache rename to nationchains/www/adminapx/nginx/nginx.conf.mustache index 1e3823f..9f86395 100755 --- a/nationchains/www/setup/nginx/nginx.conf.mustache +++ b/nationchains/www/adminapx/nginx/nginx.conf.mustache @@ -1,6 +1,6 @@ -user {{sudoerUser}}; +user {{{sudoerUser}}}; worker_processes auto; -error_log /var/log/nginx/error.log notice; +error_log {{{ }}}/var/log/nginx/error.log notice; pid /var/run/nginx.pid; #include /etc/nginx/modules-enabled/*.conf; @@ -29,6 +29,6 @@ http { # Virtual Host Configs ## {{#nginx.include}} - include {{.}}; + include {{{.}}}; {{/nginx.include}} } diff --git a/nationchains/www/setup/townconf.json b/nationchains/www/adminapx/townconf.json similarity index 79% rename from nationchains/www/setup/townconf.json rename to nationchains/www/adminapx/townconf.json index 1b1dfb7..7478a22 100644 --- a/nationchains/www/setup/townconf.json +++ b/nationchains/www/adminapx/townconf.json @@ -2,7 +2,7 @@ "nationName":"ants", "townName":"setuptown", "tribeName":"setuptribe", - "dns":"mydnsapxtrib", + "dns":["adminapx"], "apiport":3018, "language":["en"], "jwtsecret":"longsentenceusedtoencryptionChangethisforproduction", @@ -18,7 +18,8 @@ "json": { "limit": "500mb" } }, "nginx":{ + "restart":"sudo systemctl restart nginx", "worker_connections":1024, - "include":["/etc/nginx/conf.d/*.conf", "include /etc/nginx/sites-enabled/*"] + "include":["/etc/nginx/conf.d/*.conf", "/etc/nginx/sites-enabled/*"] } } \ No newline at end of file diff --git a/nationchains/www/setup/tribeconf.json b/nationchains/www/adminapx/tribeconf.json similarity index 100% rename from nationchains/www/setup/tribeconf.json rename to nationchains/www/adminapx/tribeconf.json diff --git a/nationchains/www/nginx_setup.conf b/nationchains/www/nginx_setup.conf new file mode 100644 index 0000000..fe36869 --- /dev/null +++ b/nationchains/www/nginx_setup.conf @@ -0,0 +1,52 @@ +server { + server_name adminapx ; + access_log /media/phil/usbfarm/apxtrib/nationchains/logs/nginx/setup.town.access.log main; + # /plugins/pluginame/components/xxx?plugin=pluginname&pluginkey=key + # acess if exist pluginkey + location /plugins/ { + add_header X-debug "plugins local $arg_plugin/keys/$arg_pluginkey sent"; + root /media/phil/usbfarm/apxtrib/nationchains//plugins/; + if (-f /media/phil/usbfarm/apxtrib/nationchains//plugins/$arg_plugin/keys/$arg_pluginkey) { + rewrite /plugins/([^/]+)/components/([^\?]+) /$1/components/$2 break; + } + return 403 "No valid token access for plugin:$arg_plugin with token:$arg_pluginkey please ask your admin"; + } + + location /cdn/ { + rewrite /cdn/(.*$) /$1 break; + root /media/phil/usbfarm/apxtrib/nationchains//www/cdn/; + } + + location /spacedev/ { + rewrite /spacedev/(.*$) /$1 break; + root /media/phil/usbfarm/apxtrib/nationchains//spacedev//dist/; + } + + location /app/ { + rewrite /app/(.*$) /$1 break; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + client_max_body_size 10m; + client_body_buffer_size 128k; + proxy_connect_timeout 90; + proxy_send_timeout 90; + proxy_read_timeout 90; + proxy_buffers 32 4k; + proxy_set_header X-NginX-Proxy true; + proxy_pass http://localhost:3018; + proxy_redirect off; + } + + location / { + root /media/phil/usbfarm/apxtrib/nationchains//www/app/; + index index.html ; + } + error_page 404 /404.html; + # redirect server error pages to the static page /50x.html + # + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/local/nginx/html; + } +} diff --git a/package.json b/package.json index 1609f32..5c566ef 100755 --- a/package.json +++ b/package.json @@ -78,6 +78,7 @@ "nodemailer-smtp-transport": "^2.7.4", "pdf-creator-node": "^2.2.2", "pm2": "^5.1.2", + "readline-sync": "^1.4.10", "stripe": "^7.4.0", "uuid": "^9.0.0" },