2023-04-17 12:03:56 +02:00
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
*
*
*
* /
2023-01-22 10:53:09 +01:00
const Setup = { } ;
2023-04-17 12:03:56 +02:00
//const nationsync = require('./Nations').updateChains();
2023-01-22 10:53:09 +01:00
2023-04-17 12:03:56 +02:00
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 ;
} ;
2023-04-13 07:46:35 +02:00
2023-04-17 12:03:56 +02:00
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" ;
2023-04-13 07:46:35 +02:00
2023-04-17 12:03:56 +02:00
console . log ( townconf ) ;
if (
! readlineSync . keyInYN (
` \x 1b[42mThis is the first install from ./nationchains/www/adminapx/townconf.json (check it if you want) \n this will change your nginx config in /etc/nginx and run nginx from sudoer user ${ townconf . sudoerUser } (Yes/no)? \n no if you want to change parameter and run yarn setup again \x 1b[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 (
` \x 1b[42m######################################################################### \x 1b[0m \n \x 1b[42mWellcome into apxtrib, you can now 'yarn dev' for dev or 'yarn startpm2' for prod or 'yarn unittest' for testing purpose. \x 1b[0m \n \x 1b[42m Access to your town here http:// ${ townconf . dns } to finist your town set up. \n Check README's project to learn more. \x 1b[0m \n \x 1b[42m######################################################################### \x 1b[0m `
) ;
}
} ) ;
} ;
2023-01-22 10:53:09 +01:00
2023-04-17 12:03:56 +02:00
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 += ` \n resolving $ ${ conf . townName } . ${ conf . nationName } . ${ conf . dns } will not responding valid IP, please setup domain redirection IP before runing this script ` ;
}
return rep ;
2023-01-22 10:53:09 +01:00
} ;
2023-03-27 07:52:21 +02:00
2023-04-17 12:03:56 +02:00
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 ) ;
2023-01-22 10:53:09 +01:00
} ;
2023-04-17 12:03:56 +02:00
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 ( ) ;
}
2023-01-22 10:53:09 +01:00
} ;
2023-04-17 12:03:56 +02:00
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 } `
) ;
} ) ;
/ * c o n s t c o n f c l i = J S O N . p a r s e ( M u s t a c h e . r e n d e r ( f s . r e a d F i l e S y n c ( ` $ { _ _ b a s e } / s e t u p / t r i b e s / a p x t r i b / c l i e n t c o n f . m u s t a c h e ` , ' u t f 8 ' ) , t o w n S e t u p ) ) ;
2023-03-27 07:52:21 +02:00
fs . outputJsonSync ( ` ${ config . tribes } / ${ townSetup . druidid } /clientconf.json ` , confcli ) ;
2023-01-22 10:53:09 +01:00
// Create a new tribeid + admin user for this tribeid
// with access to {druidid}:webapp as admin
2023-03-27 07:52:21 +02:00
* /
2023-04-17 12:03:56 +02:00
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 ) ;
}
} ;
2023-01-22 10:53:09 +01:00
2023-04-17 12:03:56 +02:00
if ( Setup . check ( ) ) Setup . init ( ) ;