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,21 +21,53 @@ 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")
) {
// 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 {
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")
);
} else {
await apxtri.setup();
}
};
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
@ -60,22 +92,36 @@ apxtri.setup = async () => {
}
}
}
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"
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 {
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}`);
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");
}
initadminapi.sudoUser = process.env.USER;
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: [],
};
}
//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/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,
});
fs.outputJSONSync("../adminapi/objects/tribes/idx/lst_tribeId.json", [
"adminapi",
]);
fs.outputJSONSync("../adminapi/objects/tribes/idx/tribes_dns.json", {
adminapi: initadminapi.dns,
});
}
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 });
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,
});
// 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,10 +182,15 @@ 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)
);
fs.outputFileSync(
`../${tribe}/objects/wwws/admin/dist/index_en.html`,
`<h1>Wellcome in ${tribe}</h1>`,
"utF8"
);
// add hosts entry for local access
// this command is ran by the setup.sh
@ -147,16 +202,14 @@ apxtri.setup = async () => {
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) => {

View File

@ -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
function help {
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 " 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 [ "$url" == "newtribe"]; then
mkdir -p "${tribe}"
else
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 "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)"
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"
#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
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"
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