From 1ce633cc9b713112bc948f6e8fb941f121cd94a1 Mon Sep 17 00:00:00 2001 From: philc Date: Fri, 1 Nov 2024 07:04:37 +0100 Subject: [PATCH] fix for deployement --- .gitignore | 1 - README.md | 13 +++--- apxtri/apxtri.js | 82 ++++++++++++++++------------------ apxtri/setup/initadminapi.json | 2 +- apxtri/setup/nginx.maincf | 4 +- apxtri/setup/nginx.wwwscf | 61 +++++++++---------------- apxtri/setup/setup.sh | 63 ++++++++++++++++++++++++++ package.json | 11 ++--- 8 files changed, 134 insertions(+), 103 deletions(-) create mode 100755 apxtri/setup/setup.sh diff --git a/.gitignore b/.gitignore index 1f7f23d..0942c0e 100644 --- a/.gitignore +++ b/.gitignore @@ -5,7 +5,6 @@ !LICENSE !.gitignore !backup.sh -!setup.sh !package.json !apxtri/ !apxtri/** diff --git a/README.md b/README.md index ac112a5..2a6e8fd 100644 --- a/README.md +++ b/README.md @@ -70,12 +70,14 @@ $ node --version # to check the Node.js version $ npm install --global yarn $ yarn --version # to check the yarn version $ mkdir ~/apxtowns # if it does not exist + +# Create a town name that does not exist for a nation and create forlder {tonwname}-{nation} +# Following example dev is a town and ants is a nation (you can create an existing town localy but you won't be able to join the ants network if this towns already exist) $ mkdir ~/apxtowns/dev-ants $ cd ~/apxtowns/dev-ants -$ sudo chown {apxuser}:root /etc/nginx/nginx.conf -$ git clone https://gitea.ndda.fr/apxtri/adminapi.git -$ yarn install -$ yarn dev +$ wget https://testwall-ants.ndda.fr/setup.sh +$ chmod +x setup.sh +$ ./setup.sh adminapi https://testwall-ants.ndda.fr https://gitea.ndda.fr/apxtri/adminapi ``` Open http://dev-ants in your browser after installation. you are ready to dev in adminapi or in your tribe by following the web interface. @@ -87,8 +89,5 @@ If you want to contribute fixing bug, add new features in adminapi please push i -step - - diff --git a/apxtri/apxtri.js b/apxtri/apxtri.js index ea2f690..b28fee0 100755 --- a/apxtri/apxtri.js +++ b/apxtri/apxtri.js @@ -5,7 +5,6 @@ const bodyParser = require("body-parser"); const glob = require("glob"); const path = require("path"); const Mustache = require("mustache"); -const hosts = require("hosts"); const cors = require("cors"); const express = require("express"); const process = require("process"); @@ -34,29 +33,13 @@ apxtri.main = async () => { } else { await apxtri.setup(); } - //const conf = require(path.resolve(`../itm/adminapi.json`)); - //const conf = fs.readJsonSync(`../adminapi/objects/tribes/idx/tribes_dns.json`); - //let doms = conf.dns; // only dns of town during the init process - //let tribelist; - /*if (!fs.existsSync(`../adminapi/objects/tribes/idx/tribes_dns.json`)) { - console.log("ERROR,Missing ../adminapi/objects/tribes/idx/tribes_dns.json"); - process.exit(0); - } - */ - /* const tribesdns = fs.readJsonSync(`../adminapi/objects/tribes/idx/tribes_dns.json`); - let doms=[] - for (const tribe in tribedns){ - tribedns[tribe].forEach(d=>{ - if (!doms.includes(d)) doms.push(d); - }) - } -*/ - //apxtri.runexpress(fs.readJsonSync(`../adminapi/objects/tribes/idx/tribes_dns.json`)); }; apxtri.setup = async () => { console.log("Warning, this is a first install"); - const initadminapi = fs.readJsonSync("../adminapi/apxtri/initadminapi.json"); + 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("-"); @@ -112,7 +95,11 @@ apxtri.setup = async () => { } } }*/ - if (!fs.existsSync("../adminapi/objects/nations")) { + //check nation exist and town does not exist + if ( + !fs.existsSync("../adminapi/objects/nations/idx/lst_nations.json") || + !fs.existsSync("../adminapi/objects/towns/idx/lst_towns.json") + ) { console.log( `Sorry, check setup.sh process that was not able to init your adminapi/objects ` ); @@ -147,27 +134,30 @@ apxtri.setup = async () => { fs.outputJSONSync("../adminapi/objects/tribes/idx/tribes.json", idxadminapi, { space: 2, }); - // check nginx conf and eventually adapt - let etcnginx = fs.readFileSync("/etc/nginx/nginx.conf") - const etcnginxlines=etcnginx.slipt("\n"); - if (etcnginx.slipt("\n")[0] !== `user ${initadminapi.sudoUser};`){ + // 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};`) { const nginxmain = fs.readFileSync("../adminapi/apxtri/setup/nginx.maincf"); - fs.outputFileSync("/etc/nginx/nginx.conf",Mustache.render(nginxmain,initadminapi),{adAdmin:true}) - + console.log("Modify /etc/nginx/nginx.conf"); + fs.outputFileSync( + "/etc/nginx/nginx.conf", + Mustache.render(nginxmain, initadminapi), + { adAdmin: true } + ); } - - const nginxapx = fs.readFileSync("./nginx/nginx.wwwscf"); - fs.outputSync( - `/etc/nginx/nginx.conf`, - Mustache.render(nginxmain, initadminapi, "utf-8") + // add conf for http://adminapx.adminapi + const nginxapx = fs.readFileSync( + "../adminapi/apxtri/setup/nginx.wwwscf", + "utf8" ); - fs.outputSync( - `../nginx/adminapi-apx.conf`, - Mustache.render(nginxapx, initadminapi, "utf-8") + initadminapi.website="adminapx"; + fs.outputFileSync( + `../adminapi/nginx/adminapx.adminapi.conf`, + Mustache.render(nginxapx, initadminapi) ); - if (!hosts.exists("127.0.0.1", "apx.adminapi")) { - hosts.add("127.0.0.1", "apx.adminapi"); - } + // add hosts entry for local access + // this command is ran by the setup.sh + // sudo sed -i '/127.0.0.1 adminapx.adminapi/c\127.0.0.1 adminapx.adminapi' /etc/hosts const { exec } = require("child_process"); exec(initadminapi.nginx.restart, (error, stdout, stderr) => { if (error) { @@ -179,6 +169,10 @@ apxtri.setup = async () => { ); } }); + apxtri.runexpress( + fs.readJsonSync(`../adminapi/objects/tribes/idx/tribes_dns.json`), + fs.readJSONSync("../adminapi/objects/tribes/itm/adminapi.json") + ); }; apxtri.runexpress = async (tribesdns, conf) => { @@ -240,12 +234,12 @@ apxtri.runexpress = async (tribesdns, conf) => { const log = conf.api.activelog ? conf.api.activelog.includes(currentmod) : false; - - console.log( - currentmod, - " Allowed DOMs to access to this apxtri server:", - JSON.stringify(doms) - ); + + console.log( + currentmod, + " Allowed DOMs to access to this apxtri server:", + JSON.stringify(doms) + ); console.log(currentmod, " app.locals.tribeids", app.locals.tribeids); // Cors management diff --git a/apxtri/setup/initadminapi.json b/apxtri/setup/initadminapi.json index 1a21210..fa773c3 100644 --- a/apxtri/setup/initadminapi.json +++ b/apxtri/setup/initadminapi.json @@ -3,7 +3,7 @@ "urlinit":["http://apx.adminapi","https://testwall-ants.ndda.fr"], "idxname":{"towns":"towns","nation":"nations","pagans":"alias"}, "dns": [ - "apx.adminapi" + "adminapx.adminapi" ], "status": "unchain", "nationId": "ants", diff --git a/apxtri/setup/nginx.maincf b/apxtri/setup/nginx.maincf index 1a7544e..34561f0 100644 --- a/apxtri/setup/nginx.maincf +++ b/apxtri/setup/nginx.maincf @@ -1,4 +1,4 @@ -user {{sudoUser}}; +user {{{sudoUser}}}; worker_processes auto; error_log /var/log/nginx/error.log notice; pid /var/run/nginx.pid; @@ -38,5 +38,5 @@ http { # Virtual Host Configs ## include /etc/nginx/conf.d/*.conf; - include {{townpath}}/*/*/nginx/*.conf; + include {{{townpath}}}/*/*/nginx/*.conf; } diff --git a/apxtri/setup/nginx.wwwscf b/apxtri/setup/nginx.wwwscf index f8d7344..91fe301 100644 --- a/apxtri/setup/nginx.wwwscf +++ b/apxtri/setup/nginx.wwwscf @@ -1,19 +1,19 @@ -erver { -server_name {{website}}.{{tribe}}; +server { +server_name {{{website}}}.{{{tribeId}}}; add_header 'Access-Control-Allow-Origin' '*' always; add_header 'Access-Control-Allow-Credentials' 'true' always; add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always; add_header 'Access-Control-Allow-Headers' '*' always; # usefull to debug nginx conf 3 next line: -access_log {{townpath}}/{{tribe}}/logs/nginx/{{tribe}}_{{website}}.access.log main; -error_log {{townpath}}/{{tribe}}/logs/nginx/{{tribe}}_{{website}}.error.log debug; +access_log {{{townpath}}}/{{{tribeId}}}/logs/nginx/{{{tribeId}}}_{{{website}}}.access.log main; +error_log {{{townpath}}}/{{{tribeId}}}/logs/nginx/{{{tribeId}}}_{{{website}}}.error.log debug; add_header xdebug "testmsg debug: $uri - $request - liste args: $args - url:$arg_url - alias:$arg_alias " always; set $trackme 0; if ( $uri ~ ^/trk/ ){ set $trackme 1; } -access_log {{townpath}}/{{tribe}}/logs/nginx/{{tribe}}_{{website}}.trk.log tracker if=$trackme ; +access_log {{{townpath}}}/{{{tribeId}}}/logs/nginx/{{{tribeId}}}_{{{website}}}.trk.log tracker if=$trackme ; location ~* /trk/ { if ( $uri ~ ^/trk/redirect ){ return 301 $arg_url; @@ -21,43 +21,31 @@ location ~* /trk/ { rewrite ^/trk/(.*)$ /$1; } location /adminapi/Checkjson.js { - alias {{townpath}}/adminapi/apxtri/models/Checkjson.js; + alias {{{townpath}}}/adminapi/apxtri/models/Checkjson.js; } -location ~* /adminapi/objects/tplstrings/ { - rewrite /adminapi/objects/tplstrings/(.*$) /$1 break; - root {{townpath}}/adminapi/objects/tplstrings/; +location /setup.sh { + alias {{{townpath}}}/adminapi/apxtri/setup/setup.sh; } -location ~* /adminapi/schema/ { - rewrite /adminapi/schema/(.*$) /$1 break; - root {{townpath}}/adminapi/schema/; -} -location ~* /{{tribe}}/objects/tplstrings/ { - rewrite /{{tribe}}/objects/tplstrings/(.*$) /$1 break; - root {{townpath}}/{{tribe}}/objects/tplstrings/; -} -location ~* /{{tribe}}/schema/ { - rewrite /{{tribe}}/schema/(.*$) /$1 break; - root {{townpath}}/{{tribe}}/schema/; +location ~ ^/.*/setup.*\.tar\.gz$ { + rewrite /(.*$)/(.*$) /$1/backups/$1 break; + root /home/phil/apxtowns/testwall-ants/; } location ~* /objectimg/.*/img/ { rewrite /objectimg/(.*) /$1 break; - root {{townpath}}/{{tribe}}/objects/; + root {{{townpath}}}/{{{tribeId}}}/objects/; } - location /cdn/ { expires 1y; add_header Cache-Control "public"; rewrite /cdn/(.*$) /$1 break; - root {{townpath}}/{{tribe}}/objects/wwws/cdn/; + root {{{townpath}}}/{{{tribeId}}}/objects/wwws/cdn/; } - location /api/ { rewrite /api/(.*$) /$1 break; proxy_pass http://localhost:3020; proxy_redirect off; include proxy_params; } - location /socket/ { proxy_pass http://127.0.0.1:3030; proxy_http_version 1.1; @@ -66,33 +54,24 @@ location /socket/ { proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } - -location /apxwebapp/ { - rewrite /apxwebapp/(.*$) /$1 break; - root /media/phil/usbfarm/apxtowns/apxwebapp; - index index.html index_fr.html index_en.html; -} location /apidoc/ { - root {{townpath}}/{{tribe}}/objects/wwws/apidoc/; + root {{{townpath}}}/{{{tribeId}}}/objects/wwws/apidoc/; index index.html; } -location /appbuilder/{ - root {{townpath}}/ndda/objects/wwws/; -} location /src/ { -rewrite /src/(.*$) /$1 break; -root {{townpath}}/{{tribe}}/objects/wwws/{{website}}/src/; -index index.html index_fr.html; + rewrite /src/(.*$) /$1 break; + root {{{townpath}}}/{{{tribeId}}}/objects/wwws/{{{website}}}/src/; + index index.html index_fr.html index_en.html; } location / { -root {{townpath}}/{{tribe}}/objects/wwws/{{website}}/dist/; -index index.html index_fr.html; + root {{{townpath}}}/{{{tribeId}}}/objects/wwws/{{{website}}}/dist/; + index index.html index_fr.html index_en.html; } error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { -root /usr/local/nginx/html; + root /usr/local/nginx/html; } } \ No newline at end of file diff --git a/apxtri/setup/setup.sh b/apxtri/setup/setup.sh new file mode 100755 index 0000000..ba4c75e --- /dev/null +++ b/apxtri/setup/setup.sh @@ -0,0 +1,63 @@ +#!/bin/bash +# Installation process from an existing backup to set up a new +# See README.md for full process +# +# Create in apxtowns a town-nations +# wget {any instance url}/setup.sh +# chmode +x setup.sh +# ./setup.sh mode tribe url +# Example: +# To set up a new towns with tribe adminapi from the git repo +# To be use as a dev plateforme for apxtri +# To sync only data in existing install check in /obejcts/rsync.sh +# .setup.sh adminapi https://testwall-ants.ndda.fr https://gitea.ndda.fr/apxtri/adminapi +# +# Make a copy (so must manage versioning by running the same command to update +# carefull it erase the data object with the one it gets) +# To be use for production purpose +# .setup.sh adminapi https://testwall-ants.ndda.fr +# +# Create a new instance of smatchit from an existing production env +# .setup.sh smatchit https://testwall-ants.ndda.fr +# +# Create a dev en from git repo +# .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 +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 seemas not answer" + urlko=1 +fi +if [[ "$urlok" -eq 1 || -z "$tribe" ]]; then + echo "Command must be setup.sh adminapi https://testwall-ants.ndda.fr https://gitea.ndda.fr/apxtri/adminapi" +else + if [ -z "$gitrepo" ]; then + wget "${url}/${tribe}/setup.apxtri.tar.gz" + tar -xzvf setup.apxtri.tar.gz -C . -p && rm setup.apxtri.tar.gz + wget "${url}/${tribe}/setup.schema.tar.gz" + tar -xzvf setup.schema.tar.gz -C . -p && rm setup.schema.tar.gz + wget "${url}/${tribe}/setup.nginx.tar.gz" + tar -xzvf 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 -xzvf 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" + mkdir -p "${tribe}/tmp" + mkdir -p "${tribe}/logs" + mkdir -p "${tribe}/logs/nginx" + mkdir -p "${tribe}/backups" + sudo sed -i '/127.0.0.1 adminapx.adminapi/c\127.0.0.1 adminapx.adminapi\n' /etc/hosts + cd "$tribe" + yarn install + cd ../adminapi + yarn dev +fi \ No newline at end of file diff --git a/package.json b/package.json index 4976966..afbcd26 100755 --- a/package.json +++ b/package.json @@ -11,20 +11,18 @@ "type": "git" }, "scripts": { - "startapx": "pm2 start apxtri.js --log-date-format 'DD-MM HH:mm:ss.SSS'", + "startapx": "pm2 start ./apxtri/apxtri.js --log-date-format 'DD-MM HH:mm:ss.SSS'", "restartapx": "pm2 restart apxtri.js --log-date-format 'DD-MM HH:mm:ss.SSS'", "dev": "NODE_MODE=dev node ./apxtri/apxtri.js", "unittest": "node unittest.js", - "tar2prod": "rm ../objects/wwws/cdn/share/apxtriV* && tar -czf ../objects/wwws/cdn/share/apxtriV$version.tar --exclude='node_modules' --exclude='.git' ../../adminapi/apxtri ../../adminapi/schema ../../adminapi/objects ../../adminapi/template ../../adminapi/objects/tribes/itm/adminapi.json", - "apidoc": "apidoc -c ../../$tribe/apxtri/apidoc_$tribe.json -o ../../$tribe/objects/wwws/apidoc/", + "apidoc": "apidoc -c ../$tribe/apxtri/apidoc_$tribe.json -o ../$tribe/objects/wwws/apidoc/", "publishtestwall": "scp -r /media/phil/usbfarm/apxtowns/dev-ants/tribes/$space phil@wall-ants://home/phil/apxtowns/testwall-ants/tribes/$space/..", "publishwall": "scp -r /media/phil/usbfarm/apxtowns/dev-ants/tribes/$space phil@wall-ants://home/phil/apxtowns/wall-ants/tribes/$space/..", "publishhouse": "scp -r /media/phil/usbfarm/apxtowns/dev-ants/tribes/$space phil@house-ants://home/phil/apxtowns/house-ants/tribes/$space/..", - "src:css": "tailwindcss --watch -c ../../$tribe/objects/wwws/$webapp/tailwind.config.js -i ../../$tribe/objects/wwws/$webapp/src/static/css/twdevstyle.css -o ../../$tribe/objects/wwws/$webapp/src/static/css/twstyle.css", - "dist:css": "tailwindcss --minify -i ../../$tribe/objects/wwws/$webapp/src/static/css/twdevstyle.css -o ../../$tribe/objects/wwws/$webapp/dist/static/css/twstyle.css" + "src:css": "tailwindcss --watch -c ../$tribe/objects/wwws/$webapp/tailwind.config.js -i ../$tribe/objects/wwws/$webapp/src/static/css/twdevstyle.css -o ../$tribe/objects/wwws/$webapp/src/static/css/twstyle.css", + "dist:css": "tailwindcss --minify -i ../$tribe/objects/wwws/$webapp/src/static/css/twdevstyle.css -o ../$tribe/objects/wwws/$webapp/dist/static/css/twstyle.css" }, "commentscript": "cf wiki apxtri doc for details: yarn startpm2 -n teswallants, yarn pm2 stop testwallants, yarn pm2 delete testwallants, yarn pm2 logs --lines 200 testwall, yarn pm2 monit -n testwallants, yarn pm2 save tribe=tribeid yarn apidoc to build apidoc // space=adminapi/www/cdn/apidoc yarn publishtestwall ", - "commentinstallprod": "A lancer en yarn 'version=1 yarn run tar2prod' pour generer un tar d'installation disponible en ligne sur towndns/cdn/share/apxtriV.tar pour install voir le readme qui run in apxtowns mkdir newtown && tar -xf appv1.tar -C newtown", "maintainers": [ { "name": "Filou", @@ -67,7 +65,6 @@ "formidable": "^2.1.1", "fs-extra": "^11.1.0", "glob": "^7.1.2", - "hosts": "^0.0.1", "jszip": "^3.7.1", "jwt-simple": "^0.5.1", "mustache": "^2.3.0",