From da3ac551225e1881f0eb48a2059386c9d8851b79 Mon Sep 17 00:00:00 2001 From: Jokob-sk Date: Sat, 24 Dec 2022 13:12:23 +1100 Subject: [PATCH] PUSHPROD default db and conf file for new installs --- Dockerfile | 6 +++- back/pialert.py | 76 ++++++++++++++++++++++++++++++------------- docker-compose.yml | 3 +- dockerfiles/README.md | 5 ++- 4 files changed, 63 insertions(+), 27 deletions(-) diff --git a/Dockerfile b/Dockerfile index a56966fa..bd297e62 100755 --- a/Dockerfile +++ b/Dockerfile @@ -35,7 +35,11 @@ RUN rm /etc/nginx/sites-available/default \ && ln -s /home/pi/pialert/install/default /etc/nginx/sites-available/default \ && sed -ie 's/listen 80/listen '${PORT}'/g' /etc/nginx/sites-available/default \ # run the hardware vendors update - && /home/pi/pialert/back/update_vendors.sh + && /home/pi/pialert/back/update_vendors.sh \ + # Create a backup of the pialert.conf to be used if the user didn't supply a configuration file + && cp /home/pi/pialert/config/pialert.conf /home/pi/pialert/back/pialert.conf_bak \ + # Create a backup of the pialert.db to be used if the user didn't supply a database + && cp /home/pi/pialert/db/pialert.db /home/pi/pialert/back/pialert.db_bak ENTRYPOINT ["tini", "--"] diff --git a/back/pialert.py b/back/pialert.py index f7dce735..68dcd2a4 100755 --- a/back/pialert.py +++ b/back/pialert.py @@ -41,14 +41,16 @@ import threading # PATHS #=============================================================================== PIALERT_BACK_PATH = os.path.dirname(os.path.abspath(__file__)) -pialertPath = PIALERT_BACK_PATH + "/.." +pialertPath = PIALERT_BACK_PATH + "/.." #to fix - remove references and use pialertPath instead logPath = pialertPath + '/front/log' +pialertPath = '/home/pi/pialert' confPath = "/config/pialert.conf" dbPath = '/db/pialert.db' fullConfPath = pialertPath + confPath fullDbPath = pialertPath + dbPath STOPARPSCAN = pialertPath + "/db/setting_stoparpscan" + #------------------------------------------------------------------------------- def file_print(*args): @@ -61,11 +63,6 @@ def file_print(*args): file.write(result + '\n') file.close() - -#=============================================================================== -# USER CONFIG VARIABLES - START -#=============================================================================== - # check RW access of DB and config file file_print('\n Permissions check (All should be True)') file_print('------------------------------------------------') @@ -75,7 +72,55 @@ file_print( " " + dbPath + " | " + " READ | " + str(os.access(fullDbPath file_print( " " + dbPath + " | " + " WRITE | " + str(os.access(fullDbPath, os.W_OK))) file_print('------------------------------------------------') -pialertPath = '/home/pi/pialert' +#------------------------------------------------------------------------------- +def append_file_binary (pPath, input): + file = open (pPath, 'ab') + file.write (input) + file.close() + +#------------------------------------------------------------------------------- +def logResult (stdout, stderr): + if stderr != None: + append_file_binary (logPath + '/stderr.log', stderr) + if stdout != None: + append_file_binary (logPath + '/stdout.log', stdout) + +def initialiseFile(pathToCheck, defaultFile): + # if file not readable (missing?) try to copy over the backed-up (default) one + if str(os.access(pathToCheck, os.R_OK)) == "False": + file_print("[Setup] The "+ pathToCheck +" file is not readable (missing?). Trying to copy over the backed-up (default) one.") + try: + # try runnning a subprocess + p = subprocess.Popen(["cp", defaultFile , pathToCheck], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + stdout, stderr = p.communicate() + + if str(os.access(pathToCheck, os.R_OK)) == "False": + file_print("[Setup] Error copying the default file ("+defaultFile+") to it's destination ("+pathToCheck+"). Make sure the app has Read & Write access to the parent directory.") + else: + file_print("[Setup] Default file ("+defaultFile+") copied over successfully to ("+pathToCheck+").") + + # write stdout and stderr into .log files for debugging if needed + logResult (stdout, stderr) + + except subprocess.CalledProcessError as e: + # An error occured, handle it + file_print("[Setup] Error copying the default file ("+defaultFile+"). Make sure the app has Read & Write access to " + pathToCheck) + file_print(e.output) + +#=============================================================================== +# Basic checks and Setup +#=============================================================================== + +# check and initialize pialert.conf +initialiseFile(fullConfPath, "/home/pi/pialert/back/pialert.conf_bak" ) + +# check and initialize pialert.db +initialiseFile(fullDbPath, "/home/pi/pialert/back/pialert.db_bak") + +#=============================================================================== +# USER CONFIG VARIABLES - START +#=============================================================================== + vendorsDB = '/usr/share/arp-scan/ieee-oui.txt' piholeDB = '/etc/pihole/pihole-FTL.db' @@ -1824,12 +1869,6 @@ def write_file (pPath, pText): file.write (pText) file.close() -#------------------------------------------------------------------------------- -def append_file_binary (pPath, input): - file = open (pPath, 'ab') - file.write (input) - file.close() - #------------------------------------------------------------------------------- def append_line_to_file (pPath, pText): # append the line depending using the correct python version @@ -2341,7 +2380,8 @@ def openDB (): return # Log - print_log ('Opening DB...') + print_log ('Opening DB...') + # Open DB and Cursor sql_connection = sqlite3.connect (fullDbPath, isolation_level=None) @@ -2445,14 +2485,6 @@ def add_json_list (row, list): #------------------------------------------------------------------------------- -def logResult (stdout, stderr): - if stderr != None: - append_file_binary (logPath + '/stderr.log', stderr) - if stdout != None: - append_file_binary (logPath + '/stdout.log', stdout) - -#------------------------------------------------------------------------------- - def to_text(_json): payloadData = "" if len(_json['internet']) > 0 and 'internet' in INCLUDED_SECTIONS: diff --git a/docker-compose.yml b/docker-compose.yml index e44cd0ce..e3fcb4ea 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,7 +7,8 @@ services: restart: unless-stopped volumes: - ${APP_DATA_LOCATION}/pialert/config:/home/pi/pialert/config - - ${APP_DATA_LOCATION}/pialert/db/pialert.db:/home/pi/pialert/db/pialert.db + # - ${APP_DATA_LOCATION}/pialert/db/pialert.db:/home/pi/pialert/db/pialert.db + - ${APP_DATA_LOCATION}/pialert/db:/home/pi/pialert/db # (optional) map an empty file with the name 'setting_darkmode' if you want to force the dark mode on container rebuilt - ${APP_DATA_LOCATION}/pialert/db/setting_darkmode:/home/pi/pialert/db/setting_darkmode # (optional) useful for debugging if you have issues setting up the container diff --git a/dockerfiles/README.md b/dockerfiles/README.md index e25ddbc9..30a38272 100755 --- a/dockerfiles/README.md +++ b/dockerfiles/README.md @@ -34,15 +34,14 @@ | | Path | Description | | :------------- | :------------- |:-------------| -| **Required** | `:/home/pi/pialert/config` | Folder which needs to contain the `pialert.conf` file (see below for details) | -| **Required** | `:/home/pi/pialert/db/pialert.db` | Map an empty [pialert.db file which can be downloaded from here](https://github.com/jokob-sk/Pi.Alert/blob/main/db/pialert.db) | +| **Required** | `:/home/pi/pialert/config` | Folder which will contain the `pialert.conf` file (see below for details) | +| **Required** | `:/home/pi/pialert/db` | Folder which will contain the `pialert.db` file | |Optional| `:/home/pi/pialert/db/setting_darkmode` | Map an empty file with the name `setting_darkmode` if you want to force the dark mode on container rebuilt | |Optional| `:/home/pi/pialert/front/log` | Logs folder useful for debugging if you have issues setting up the container | |Optional| `:/etc/pihole/pihole-FTL.db` | PiHole's `pihole-FTL.db` database file. Required if you want to use PiHole | |Optional| `:/etc/pihole/dhcp.leases` | PiHole's `dhcp.leases` file. Required if you want to use PiHole | - ### Config (`pialert.conf`) - Download [pialert.conf from here](https://github.com/jokob-sk/Pi.Alert/tree/main/config).