setup and apxtri.js for setup

This commit is contained in:
philc 2024-11-07 15:45:45 +01:00
parent 0ccb4b2c7f
commit da24db5510
2 changed files with 171 additions and 101 deletions

View File

@ -21,61 +21,107 @@ apxtri.main = async () => {
); );
process.exit(0); process.exit(0);
} }
//console.log(path.resolve("../adminapi/objects/tribes/itm/adminapi.json")) //Check prerequest data
if ( if (
fs.existsSync("../adminapi/objects/tribes/idx/tribes_dns.json") && fs.existsSync("../adminapi/objects/tribes/idx/tribes_dns.json") &&
fs.existsSync("../adminapi/objects/tribes/itm/adminapi.json") fs.existsSync("../adminapi/objects/tribes/itm/adminapi.json")
) { ) {
apxtri.runexpress( // check all tribes are in tribes_dns.json
fs.readJsonSync(`../adminapi/objects/tribes/idx/tribes_dns.json`), const conf = fs.readJSONSync(
fs.readJSONSync("../adminapi/objects/tribes/itm/adminapi.json") "../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 { } 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 () => { const getip = async () => {
/** /**
* Return json with public IP and network interfaces with their local IP * Return json with public IP and network interfaces with their local IP
* {WANIP:"public IP","eth0":"localIPoneth0",...} * {WANIP:"public IP","eth0":"localIPoneth0",...}
*/ */
const network = {}; const network = {};
const urlgetip = `https://api.ipify.org?format=json`; const urlgetip = `https://api.ipify.org?format=json`;
const getdata = await fetch(urlgetip); const getdata = await fetch(urlgetip);
if (getdata.ok) { if (getdata.ok) {
const data = await getdata.json(); const data = await getdata.json();
network.WANIP = data.ip; network.WANIP = data.ip;
} }
const os = await import("os"); const os = await import("os");
const interfaces = os.networkInterfaces(); const interfaces = os.networkInterfaces();
for (const name in interfaces) { for (const name in interfaces) {
for (const iface of interfaces[name]) { for (const iface of interfaces[name]) {
// Check for IPv4 and make sure it's not an internal (loopback) address // Check for IPv4 and make sure it's not an internal (loopback) address
if (iface.family === "IPv4" && !iface.internal) { if (iface.family === "IPv4" && !iface.internal) {
network[name]=iface.address; network[name] = iface.address;
//localIP = iface.address; //localIP = iface.address;
//console.log(`Local network IP (${name}): ${localIP}`); //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"); let inittribe;
const initadminapi = fs.readJsonSync( if (tribe == "adminapi") {
"../adminapi/apxtri/setup/initadminapi.json" 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 { );
initadminapi.townpath = __dirname.replace("/adminapi/apxtri", ""); try {
const townnation = initadminapi.townpath.split("/").slice(-1)[0].split("-"); inittribe = conf;
initadminapi.townId = townnation[0]; inittribe.townpath = __dirname.replace("/adminapi/apxtri", "");
initadminapi.nationId = townnation[1]; const townnation = inittribe.townpath.split("/").slice(-1)[0].split("-")
initadminapi.dns.push(`${initadminapi.townId}.${initadminapi.nationId}`); inittribe.townId = townnation[0];
} catch (err) { inittribe.nationId = townnation[1];
console.log("Your town folder must be something townid-nation"); 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 //check nation exist and town does not exist
if ( if (
!fs.existsSync("../adminapi/objects/nations/idx/lst_nations.json") || !fs.existsSync("../adminapi/objects/nations/idx/lst_nations.json") ||
@ -86,44 +132,48 @@ apxtri.setup = async () => {
); );
process.exit(0); process.exit(0);
} }
fs.outputJSONSync( fs.outputJSONSync(`../adminapi/objects/tribes/itm/${tribe}.json`, inittribe, {
"../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, {
space: 2, 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"; // check nginx conf and eventually change if not starting by user "current user";
let etcnginx = fs.readFileSync("/etc/nginx/nginx.conf", "utf8"); let etcnginx = fs.readFileSync("/etc/nginx/nginx.conf", "utf8");
if (etcnginx.split("\n")[0] !== `user ${initadminapi.sudoUser};`) { if (etcnginx.split("\n")[0] !== `user ${inittribe.sudoUser};`) {
initadminapi.mainpath=initadminapi.townpath.replace(`/${initadminapi.townId}-${initadminapi.nationId}`,"") inittribe.mainpath = inittribe.townpath.replace(
const nginxmain = fs.readFileSync("../adminapi/apxtri/setup/nginx.maincf","utf8"); `/${inittribe.townId}-${inittribe.nationId}`,
""
);
const nginxmain = fs.readFileSync(
"../adminapi/apxtri/setup/nginx.maincf",
"utf8"
);
console.log("Modify /etc/nginx/nginx.conf"); console.log("Modify /etc/nginx/nginx.conf");
fs.outputFileSync( fs.outputFileSync(
"/etc/nginx/nginx.conf", "/etc/nginx/nginx.conf",
Mustache.render(nginxmain, initadminapi), Mustache.render(nginxmain, inittribe),
{ adAdmin: true } { adAdmin: true }
); );
} }
@ -132,31 +182,34 @@ apxtri.setup = async () => {
"../adminapi/apxtri/setup/nginx.wwwscf", "../adminapi/apxtri/setup/nginx.wwwscf",
"utf8" "utf8"
); );
initadminapi.website="adminapx" inittribe.website = "admin";
fs.outputFileSync( fs.outputFileSync(
`../adminapi/nginx/adminapx.adminapi.conf`, `../adminapi/nginx/${inittribe.website}.${tribe}.${inittribe.townId}.${inittribe.nationId}.conf`,
Mustache.render(nginxapx, initadminapi) Mustache.render(nginxapx, inittribe)
);
fs.outputFileSync(
`../${tribe}/objects/wwws/admin/dist/index_en.html`,
`<h1>Wellcome in ${tribe}</h1>`,
"utF8"
); );
// add hosts entry for local access // add hosts entry for local access
// this command is ran by the setup.sh // 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 // 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"); const { exec } = require("child_process");
exec(initadminapi.nginx.restart, (error, stdout, stderr) => { exec(initadminapi.nginx.restart, (error, stdout, stderr) => {
if (error) { if (error) {
console.log("\x1b[42m", error, stdout, stderr, "x1b[0m"); console.log("\x1b[42m", error, stdout, stderr, "x1b[0m");
process.exit(0); process.exit(0);
} else { } 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( 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` `\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) => { apxtri.runexpress = async (tribesdns, conf) => {
@ -268,7 +321,7 @@ apxtri.runexpress = async (tribesdns, conf) => {
}); });
if (log) { if (log) {
console.log(currentmod, logroute); console.log(currentmod, logroute);
console.log(currentmod,conf.api); console.log(currentmod, conf.api);
} }
//Listen event file for each tribe //Listen event file for each tribe
// @TODO à ajouter ici // @TODO à ajouter ici

View File

@ -24,34 +24,50 @@
# .setup.sh smatchit https://testwall-ants.ndda.fr https://gitea.ndda.fr/smatchit/smatchit # .setup.sh smatchit https://testwall-ants.ndda.fr https://gitea.ndda.fr/smatchit/smatchit
tribe=$1 # name of the tribe to install 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) gitrepo=$3 # url to get apxtri code from a git repo (empty if must come from a backup or the url)
# get function help {
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
echo "Need more params, cmd must be $ . setup.sh <tribename> <url to get data> <gitrepourl>" echo "Need more params, cmd must be $ . setup.sh <tribename> <url to get data> <gitrepourl>"
echo "Example1: . setup.sh adminapi https://testwall-ants.ndda.fr https://gitea.ndda.fr/apxtri/adminapi" 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 " 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 "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 " 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 else
if [ -z "$gitrepo" ]; then if [ "$url" == "newtribe"]; then
wget "${url}/${tribe}/setup.apxtri.tar.gz" mkdir -p "${tribe}"
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 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 fi
tar -xzf setup.objects.tar.gz -C . -p && rm setup.objects.tar.gz
mkdir -p "${tribe}/objects/persons" mkdir -p "${tribe}/objects/persons"
mkdir -p "${tribe}/objects/persons/itm" mkdir -p "${tribe}/objects/persons/itm"
mkdir -p "${tribe}/objects/persons/idx" mkdir -p "${tribe}/objects/persons/idx"
@ -59,11 +75,12 @@ else
mkdir -p "${tribe}/logs" mkdir -p "${tribe}/logs"
mkdir -p "${tribe}/logs/nginx" mkdir -p "${tribe}/logs/nginx"
mkdir -p "${tribe}/backups" mkdir -p "${tribe}/backups"
# add 127.0.0.1 local host # add specific dependancy for tribe if apxtri exist
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
cd "$tribe" 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 cd ../adminapi
yarn dev yarn dev
fi fi