From da24db551095e03bdcae7a66405e51abc18702d6 Mon Sep 17 00:00:00 2001 From: philc Date: Thu, 7 Nov 2024 15:45:45 +0100 Subject: [PATCH] setup and apxtri.js for setup --- apxtri/apxtri.js | 211 ++++++++++++++++++++++++++---------------- apxtri/setup/setup.sh | 61 +++++++----- 2 files changed, 171 insertions(+), 101 deletions(-) diff --git a/apxtri/apxtri.js b/apxtri/apxtri.js index d5e1789..2a3454c 100755 --- a/apxtri/apxtri.js +++ b/apxtri/apxtri.js @@ -21,61 +21,107 @@ apxtri.main = async () => { ); process.exit(0); } - //console.log(path.resolve("../adminapi/objects/tribes/itm/adminapi.json")) + //Check prerequest data if ( fs.existsSync("../adminapi/objects/tribes/idx/tribes_dns.json") && fs.existsSync("../adminapi/objects/tribes/itm/adminapi.json") ) { - apxtri.runexpress( - fs.readJsonSync(`../adminapi/objects/tribes/idx/tribes_dns.json`), - fs.readJSONSync("../adminapi/objects/tribes/itm/adminapi.json") + // check all tribes are in tribes_dns.json + const conf = fs.readJSONSync( + "../adminapi/objects/tribes/itm/adminapi.json" ); + const tribesdns = fs.readJsonSync( + `../adminapi/objects/tribes/idx/tribes_dns.json` + ); + //check if new tribe was add + glob.sync("../*").forEach(async (t) => { + const tribe = path.basename(t); + console.log(tribe); + if (!tribesdns[tribe]) { + await apxtri.setuptribe(tribe, conf); + } + }); } else { - await apxtri.setup(); + const initconf = fs.readJsonSync( + "../adminapi/apxtri/setup/initadminapi.json" + ); + await apxtri.setuptribe("adminapi", initconf); } + // run api with update conf + apxtri.runexpress( + fs.readJsonSync(`../adminapi/objects/tribes/idx/tribes_dns.json`), + fs.readJSONSync("../adminapi/objects/tribes/itm/adminapi.json") + ); }; -apxtri.setup = async () => { +apxtri.setuptribe = async (tribe, conf) => { + /** const tribedata={ + tribeId: tribe, + dns: [`${tribe}.${conf.townId}`], + status: "unchain", + nationId: conf.nationId, + townId: conf.townId, + activelog: [] + } + // must generate nginx conf based on wwwws if no wwws then create a admin{tribe} + fs.outputJSONSync(`../adminapi/tribes/itm/${tribe}.json`,tribedata,{space:2}) + tribesdns[tribe]=tribedata + fs.outputJSONSync(`../adminapi/objects/tribes/idx/tribes_dns.json`,tribesdns,{space:2}) + //create a nginx conf to access tribe.townId */ const getip = async () => { /** * Return json with public IP and network interfaces with their local IP * {WANIP:"public IP","eth0":"localIPoneth0",...} */ - const network = {}; - const urlgetip = `https://api.ipify.org?format=json`; - const getdata = await fetch(urlgetip); - if (getdata.ok) { - const data = await getdata.json(); - network.WANIP = data.ip; - } - const os = await import("os"); - const interfaces = os.networkInterfaces(); - for (const name in interfaces) { - for (const iface of interfaces[name]) { - // Check for IPv4 and make sure it's not an internal (loopback) address - if (iface.family === "IPv4" && !iface.internal) { - network[name]=iface.address; - //localIP = iface.address; - //console.log(`Local network IP (${name}): ${localIP}`); + const network = {}; + const urlgetip = `https://api.ipify.org?format=json`; + const getdata = await fetch(urlgetip); + if (getdata.ok) { + const data = await getdata.json(); + network.WANIP = data.ip; + } + const os = await import("os"); + const interfaces = os.networkInterfaces(); + for (const name in interfaces) { + for (const iface of interfaces[name]) { + // Check for IPv4 and make sure it's not an internal (loopback) address + if (iface.family === "IPv4" && !iface.internal) { + network[name] = iface.address; + //localIP = iface.address; + //console.log(`Local network IP (${name}): ${localIP}`); + } } } - } - return network + return network; }; - console.log("Nice to meet you, this is a first install hope you'll enjoy, if any issues please request on discord https://discord.gg/jF7cAkZn"); - const initadminapi = fs.readJsonSync( - "../adminapi/apxtri/setup/initadminapi.json" - ); - try { - initadminapi.townpath = __dirname.replace("/adminapi/apxtri", ""); - const townnation = initadminapi.townpath.split("/").slice(-1)[0].split("-"); - initadminapi.townId = townnation[0]; - initadminapi.nationId = townnation[1]; - initadminapi.dns.push(`${initadminapi.townId}.${initadminapi.nationId}`); - } catch (err) { - console.log("Your town folder must be something townid-nation"); + let inittribe; + if (tribe == "adminapi") { + console.log( + "Nice to meet you, this is a first install hope you'll enjoy, if any issues please request on discord https://discord.gg/jF7cAkZn" + ); + try { + inittribe = conf; + inittribe.townpath = __dirname.replace("/adminapi/apxtri", ""); + const townnation = inittribe.townpath.split("/").slice(-1)[0].split("-") + inittribe.townId = townnation[0]; + inittribe.nationId = townnation[1]; + inittribe.dns.push(`adminapi.${inittribe.townId}.${inittribe.nationId}`); + } catch (err) { + console.log("Your town folder must be something townid-nation"); + } + inittribe.sudoUser = process.env.USER; + } else { + console.log(`a new tribe called ${tribe} was detected`); + inittribe = { + tribeId: tribe, + townpath : __dirname.replace("/adminapi/apxtri", ""), + dns: [`admin.${tribe}.${conf.townId}.${conf.nationId}`], + status: "unchain", + nationId: conf.nationId, + townId: conf.townId, + activelog: [], + }; } - initadminapi.sudoUser = process.env.USER; //check nation exist and town does not exist if ( !fs.existsSync("../adminapi/objects/nations/idx/lst_nations.json") || @@ -86,44 +132,48 @@ apxtri.setup = async () => { ); process.exit(0); } - fs.outputJSONSync( - "../adminapi/objects/tribes/itm/adminapi.json", - initadminapi, - { space: 2 } - ); - fs.outputJSONSync("../adminapi/objects/tribes/conf.json", { - name: "tribes", - schema: "adminapi/schema/tribes.json", - lastupdate: 0, - }); - fs.outputJSONSync("../adminapi/objects/tribes/idx/lst_tribeId.json", [ - "adminapi", - ]); - fs.outputJSONSync("../adminapi/objects/tribes/idx/tribes_dns.json", { - adminapi: initadminapi.dns, - }); - - const idxadminapi = { - adminapi: { - tribeId: "adminapi", - dns: initadminapi.dns, - status: initadminapi.status, - nationId: initadminapi.nationId, - townId: initadminapi.townId, - }, - }; - fs.outputJSONSync("../adminapi/objects/tribes/idx/tribes.json", idxadminapi, { + fs.outputJSONSync(`../adminapi/objects/tribes/itm/${tribe}.json`, inittribe, { space: 2, }); + if (!fs.existsSync("../adminapi/objects/tribes/conf.json")) { + fs.outputJSONSync("../adminapi/objects/tribes/conf.json", { + name: "tribes", + schema: "adminapi/schema/tribes.json", + lastupdate: 0, + }); + } + const lst_tribeIdpath = "../adminapi/objects/tribes/idx/lst_tribeId.json"; + let lsttribe = fs.existsSync(lst_tribeIdpath) + ? fs.readJsonSync(lst_tribeIdpath) + : []; + lsttribe.push(tribe); + fs.outputJSONSync(lst_tribeIdpath, lsttribe); + const tribes_dnspath = "../adminapi/objects/tribes/idx/tribes_dns.json"; + const tribedns = fs.existsSync(tribes_dnspath) + ? fs.readJsonSync(tribes_dnspath) + : {}; + tribedns[tribe] = inittribe.dns; + fs.outputJSONSync(tribes_dnspath, tribedns); + const tribespath = "../adminapi/objects/tribes/idx/tribes.json"; + const tribes = fs.existsSync(tribespath) ? fs.readJSONSync(tribespath) : {}; + tribes[tribe] = inittribe; + fs.outputJSONSync(tribespath, tribes, { space: 2 }); + // check nginx conf and eventually change if not starting by user "current user"; let etcnginx = fs.readFileSync("/etc/nginx/nginx.conf", "utf8"); - if (etcnginx.split("\n")[0] !== `user ${initadminapi.sudoUser};`) { - initadminapi.mainpath=initadminapi.townpath.replace(`/${initadminapi.townId}-${initadminapi.nationId}`,"") - const nginxmain = fs.readFileSync("../adminapi/apxtri/setup/nginx.maincf","utf8"); + if (etcnginx.split("\n")[0] !== `user ${inittribe.sudoUser};`) { + inittribe.mainpath = inittribe.townpath.replace( + `/${inittribe.townId}-${inittribe.nationId}`, + "" + ); + const nginxmain = fs.readFileSync( + "../adminapi/apxtri/setup/nginx.maincf", + "utf8" + ); console.log("Modify /etc/nginx/nginx.conf"); fs.outputFileSync( "/etc/nginx/nginx.conf", - Mustache.render(nginxmain, initadminapi), + Mustache.render(nginxmain, inittribe), { adAdmin: true } ); } @@ -132,31 +182,34 @@ apxtri.setup = async () => { "../adminapi/apxtri/setup/nginx.wwwscf", "utf8" ); - initadminapi.website="adminapx" + inittribe.website = "admin"; fs.outputFileSync( - `../adminapi/nginx/adminapx.adminapi.conf`, - Mustache.render(nginxapx, initadminapi) + `../adminapi/nginx/${inittribe.website}.${tribe}.${inittribe.townId}.${inittribe.nationId}.conf`, + Mustache.render(nginxapx, inittribe) ); - // add hosts entry for local access + fs.outputFileSync( + `../${tribe}/objects/wwws/admin/dist/index_en.html`, + `

Wellcome in ${tribe}

`, + "utF8" + ); + // add hosts entry for local access // this command is ran by the setup.sh // grep -q '^127.0.0.1 adminapx.adminapi' /etc/hosts || echo '127.0.0.1 adminapx.adminapi' | sudo tee -a /etc/hosts > /dev/null - const ips=await getip(); + const ips = await getip(); const { exec } = require("child_process"); exec(initadminapi.nginx.restart, (error, stdout, stderr) => { if (error) { console.log("\x1b[42m", error, stdout, stderr, "x1b[0m"); process.exit(0); } else { - const etchosts=Object.values(ips).map(ip => `${ip} ${initadminapi.website}`).join('\n ') + const etchosts = Object.values(ips) + .map((ip) => `${ip} ${initadminapi.website}`) + .join("\n "); console.log( `\x1b[42m###############################################################################################################\x1b[0m\n\x1b[42mWellcome into this fresh apxtri install, currently running as "$ yarn dev".\n To access and set up a public domain name, add lines in file /etc/hosts into the machine you want to use to access:\n ${etchosts} \n Then open in your local browser \x1b[0m\x1b[32m http://${initadminapi.website} \x1b[0m \x1b[42m \nFor local dev continue with 'yarn dev'\nTo run as a production run 'yarn startapx' or 'yarn restartapx' then pm2 monitor your production process.\n\x1b[0m\n\x1b[42m###############################################################################################################\x1b[0m` ); } }); - apxtri.runexpress( - fs.readJsonSync(`../adminapi/objects/tribes/idx/tribes_dns.json`), - fs.readJSONSync("../adminapi/objects/tribes/itm/adminapi.json") - ); }; apxtri.runexpress = async (tribesdns, conf) => { @@ -268,7 +321,7 @@ apxtri.runexpress = async (tribesdns, conf) => { }); if (log) { console.log(currentmod, logroute); - console.log(currentmod,conf.api); + console.log(currentmod, conf.api); } //Listen event file for each tribe // @TODO à ajouter ici diff --git a/apxtri/setup/setup.sh b/apxtri/setup/setup.sh index 6a39034..eed1ed1 100755 --- a/apxtri/setup/setup.sh +++ b/apxtri/setup/setup.sh @@ -24,34 +24,50 @@ # .setup.sh smatchit https://testwall-ants.ndda.fr https://gitea.ndda.fr/smatchit/smatchit tribe=$1 # name of the tribe to install -url=$2 # url to get the data from +url=$2 # url to get the data from OR newtribe gitrepo=$3 # url to get apxtri code from a git repo (empty if must come from a backup or the url) -# get -wget "${url}/${tribe}/setup.objects.tar.gz" -if [[ $? -eq 0 ]]; then - echo "Check the url:$url seems not answer" - urlko=1 -fi -if [[ "$urlok" -eq 1 || -z "$tribe" ]]; then +function help { echo "Need more params, cmd must be $ . setup.sh " echo "Example1: . setup.sh adminapi https://testwall-ants.ndda.fr https://gitea.ndda.fr/apxtri/adminapi" echo " install adminapi from the repo (with git accessright) and get data for adminapi from testwall-ants.nnda.fr" echo "Example2: setup.sh smatchit https://testwall-ants.ndda.fr" echo " install smatchit as a copy of testwall-ants.ndda.fr but not from the git (you don't need to have git acess but need to know a specific code)" + echo "Example3: setup.sh smatchit newtribe" + echo " create a new empty tribe" +} + +if [ -z "$tribe" ]; then + help else - if [ -z "$gitrepo" ]; then - wget "${url}/${tribe}/setup.apxtri.tar.gz" - tar -xzf setup.apxtri.tar.gz -C . -p && rm setup.apxtri.tar.gz - wget "${url}/${tribe}/setup.schema.tar.gz" - tar -xzf setup.schema.tar.gz -C . -p && rm setup.schema.tar.gz - wget "${url}/${tribe}/setup.nginx.tar.gz" - tar -xzf setup.nginx.tar.gz -C . -p && rm setup.nginx.tar.gz - echo "please change your dns accordingly with ${tribe}/nginx/ conf file" + if [ "$url" == "newtribe"]; then + mkdir -p "${tribe}" else - git clone "$gitrepo" + wget "${url}/${tribe}/setup.objects.tar.gz" + if [[ $? -eq 0 ]]; then + echo "Check the url:$url seems not answer" + urlko=1 + fi + if [[ "$urlok" -eq 1 || -z "$tribe" ]]; then + help + else + # add 127.0.0.1 town.nation in /ertc/hoss if not already exist to make localhost available + dns=$(basename "$PWD" | sed 's/-/./g') + grep -q "^127.0.0.1 $dns" /etc/hosts || echo "127.0.0.1 $dns" | sudo tee -a /etc/hosts > /dev/null + if [ -z "$gitrepo" ]; then + wget "${url}/${tribe}/setup.apxtri.tar.gz" + tar -xzf setup.apxtri.tar.gz -C . -p && rm setup.apxtri.tar.gz + wget "${url}/${tribe}/setup.schema.tar.gz" + tar -xzf setup.schema.tar.gz -C . -p && rm setup.schema.tar.gz + #wget "${url}/${tribe}/setup.nginx.tar.gz" + #tar -xzf setup.nginx.tar.gz -C . -p && rm setup.nginx.tar.gz + #echo "please change your dns accordingly with ${tribe}/nginx/ conf file" + else + git clone "$gitrepo" + fi + tar -xzf setup.objects.tar.gz -C . -p && rm setup.objects.tar.gz + fi fi - tar -xzf setup.objects.tar.gz -C . -p && rm setup.objects.tar.gz mkdir -p "${tribe}/objects/persons" mkdir -p "${tribe}/objects/persons/itm" mkdir -p "${tribe}/objects/persons/idx" @@ -59,11 +75,12 @@ else mkdir -p "${tribe}/logs" mkdir -p "${tribe}/logs/nginx" mkdir -p "${tribe}/backups" - # add 127.0.0.1 local host - dns=$(basename "$PWD" | sed 's/-/./g') - grep -q "^127.0.0.1 $dns" /etc/hosts || echo "127.0.0.1 $dns" | sudo tee -a /etc/hosts > /dev/null + # add specific dependancy for tribe if apxtri exist cd "$tribe" - yarn install + if [ -d "$PWD/apxtri" ]; then + yarn install + fi + # run as dev that will create missing file to make it works cd ../adminapi yarn dev fi