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`, + `