setup and apxtri.js for setup
This commit is contained in:
		
							
								
								
									
										153
									
								
								apxtri/apxtri.js
									
									
									
									
									
								
							
							
						
						
									
										153
									
								
								apxtri/apxtri.js
									
									
									
									
									
								
							| @@ -21,21 +21,53 @@ 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") | ||||||
|   ) { |   ) { | ||||||
|  |     // 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( |   apxtri.runexpress( | ||||||
|     fs.readJsonSync(`../adminapi/objects/tribes/idx/tribes_dns.json`), |     fs.readJsonSync(`../adminapi/objects/tribes/idx/tribes_dns.json`), | ||||||
|     fs.readJSONSync("../adminapi/objects/tribes/itm/adminapi.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 () => { |   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 | ||||||
| @@ -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"); |   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 { |     try { | ||||||
|     initadminapi.townpath = __dirname.replace("/adminapi/apxtri", ""); |       inittribe = conf; | ||||||
|     const townnation = initadminapi.townpath.split("/").slice(-1)[0].split("-"); |       inittribe.townpath = __dirname.replace("/adminapi/apxtri", ""); | ||||||
|     initadminapi.townId = townnation[0]; |       const townnation = inittribe.townpath.split("/").slice(-1)[0].split("-") | ||||||
|     initadminapi.nationId = townnation[1]; |       inittribe.townId = townnation[0]; | ||||||
|     initadminapi.dns.push(`${initadminapi.townId}.${initadminapi.nationId}`); |       inittribe.nationId = townnation[1]; | ||||||
|  |       inittribe.dns.push(`adminapi.${inittribe.townId}.${inittribe.nationId}`); | ||||||
|     } catch (err) { |     } catch (err) { | ||||||
|       console.log("Your town folder must be something townid-nation"); |       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 |   //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", |     space: 2, | ||||||
|     initadminapi, |   }); | ||||||
|     { space: 2 } |   if (!fs.existsSync("../adminapi/objects/tribes/conf.json")) { | ||||||
|   ); |  | ||||||
|     fs.outputJSONSync("../adminapi/objects/tribes/conf.json", { |     fs.outputJSONSync("../adminapi/objects/tribes/conf.json", { | ||||||
|       name: "tribes", |       name: "tribes", | ||||||
|       schema: "adminapi/schema/tribes.json", |       schema: "adminapi/schema/tribes.json", | ||||||
|       lastupdate: 0, |       lastupdate: 0, | ||||||
|     }); |     }); | ||||||
|   fs.outputJSONSync("../adminapi/objects/tribes/idx/lst_tribeId.json", [ |   } | ||||||
|     "adminapi", |   const lst_tribeIdpath = "../adminapi/objects/tribes/idx/lst_tribeId.json"; | ||||||
|   ]); |   let lsttribe = fs.existsSync(lst_tribeIdpath) | ||||||
|   fs.outputJSONSync("../adminapi/objects/tribes/idx/tribes_dns.json", { |     ? fs.readJsonSync(lst_tribeIdpath) | ||||||
|     adminapi: initadminapi.dns, |     : []; | ||||||
|   }); |   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"; |   // 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,10 +182,15 @@ 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 | ||||||
| @@ -147,16 +202,14 @@ apxtri.setup = async () => { | |||||||
|       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) => { | ||||||
|   | |||||||
| @@ -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 { | ||||||
|  |    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" |       wget "${url}/${tribe}/setup.objects.tar.gz" | ||||||
|       if [[ $? -eq 0 ]]; then |       if [[ $? -eq 0 ]]; then | ||||||
|          echo "Check the url:$url seems not answer" |          echo "Check the url:$url seems not answer" | ||||||
|          urlko=1 |          urlko=1 | ||||||
|       fi |       fi | ||||||
|       if [[ "$urlok" -eq 1 || -z "$tribe" ]]; then |       if [[ "$urlok" -eq 1 || -z "$tribe" ]]; then | ||||||
|    echo "Need more params, cmd must be $ . setup.sh <tribename> <url to get data> <gitrepourl>" |          help       | ||||||
|    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)" |  | ||||||
|       else |       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 |          if [ -z "$gitrepo" ]; then | ||||||
|             wget "${url}/${tribe}/setup.apxtri.tar.gz" |             wget "${url}/${tribe}/setup.apxtri.tar.gz" | ||||||
|             tar -xzf setup.apxtri.tar.gz -C . -p && rm setup.apxtri.tar.gz |             tar -xzf setup.apxtri.tar.gz -C . -p && rm setup.apxtri.tar.gz | ||||||
|             wget "${url}/${tribe}/setup.schema.tar.gz" |             wget "${url}/${tribe}/setup.schema.tar.gz" | ||||||
|             tar -xzf setup.schema.tar.gz -C . -p && rm setup.schema.tar.gz |             tar -xzf setup.schema.tar.gz -C . -p && rm setup.schema.tar.gz | ||||||
|       wget "${url}/${tribe}/setup.nginx.tar.gz" |             #wget "${url}/${tribe}/setup.nginx.tar.gz" | ||||||
|       tar -xzf setup.nginx.tar.gz -C . -p && rm 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" |             #echo "please change your dns accordingly with ${tribe}/nginx/ conf file" | ||||||
|          else |          else | ||||||
|             git clone "$gitrepo" |             git clone "$gitrepo" | ||||||
|          fi |          fi | ||||||
|          tar -xzf setup.objects.tar.gz -C . -p && rm setup.objects.tar.gz |          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" | ||||||
|    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" | ||||||
|  |    if [ -d "$PWD/apxtri" ]; then | ||||||
|       yarn install |       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 | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user