Verified Commit bdf82b78 authored by Romain De Laage De Bellefaye's avatar Romain De Laage De Bellefaye 🌳
Browse files

[CodiMD] Add a script to delete pads

parent c6d84cd0
......@@ -23,10 +23,11 @@ RUN addgroup --gid 5010 codimd && \
adduser -u 5010 -G codimd -D codimd && \
mkdir /home/$USER_NAME/.npm && \
echo "prefix=/home/codimd/.npm/" > /home/$USER_NAME/.npmrc && \
mkdir -p /home/codimd/app && \
mkdir -p /home/codimd/app/deletedPads && \
chown -R codimd:codimd /home/codimd && \
# Git needed so NPM can clone packages and Python for node-gyp
apk add --no-cache bash git python && \
apk add --no-cache bash git python3-dev py3-pip gcc postgresql-dev musl-dev && \
pip3 install psycopg2 && \
wget https://github.com/hackmdio/portchecker/releases/download/${PORTCHECKER_VERSION}/portchecker-linux-amd64.tar.gz && \
tar xvf portchecker-linux-amd64.tar.gz -C /usr/local/bin && \
mv /usr/local/bin/portchecker-linux-amd64 /usr/local/bin/pcheck && \
......@@ -38,6 +39,8 @@ WORKDIR /home/codimd/app
COPY --chown=5010:5010 --from=BUILD /opt/codimd .
COPY --chown=5010:5010 deleteOldPad.py .
RUN npm install --production && npm cache clean --force && rm -rf /tmp/{core-js-banners,phantomjs}
COPY entrypoint.sh .
......
#! /usr/bin/python3
import psycopg2
import sys
from os import environ
INTERVAL = environ['OLD_INTERVAL']
USER = environ['POSTGRES_USER']
DATABASE = environ['POSTGRES_DB']
PASSWORD = environ['POSTGRES_PASSWORD']
HOST = ['DB_HOST']
PORT = ['DB_PORT']
print("===== Begin of deleteOldPad job =====")
try:
db = psycopg2.connect("user = " + USER + " dbname = " +DATABASE + " password = " + PASSWORD + " host = " + HOST + " port = " + PORT)
except:
sys.exit("deleteOldPad : Unable to connect database")
with db :
with db.cursor() as cur:
#Make a backup of old pads in deletedPads
try:
cur.execute("SELECT \"Notes\".\"id\", \"Notes\".\"content\" FROM \"Notes\" WHERE \"Notes\".\"updatedAt\" < NOW() - interval %s", (INTERVAL,))
except:
sys.exit("deleteOldPad : unable to select old pads")
for oldPad in cur.fetchall():
with open("deletedPads/" + oldPad[0] + '.txt', "w") as f:
f.write(oldPad[1])
#Delete the old pads
try:
cur.execute("DELETE FROM \"Notes\" WHERE \"Notes\".\"updatedAt\" < NOW() - interval %s", (INTERVAL,))
except:
sys.exit("deleteOldPad: unable to delete old pads")
print("deleteOldPad : Deleted old pad")
db.close()
print("===== End of deleteOldPad job =====")
......@@ -46,6 +46,9 @@ services:
CMD_CSP_ENABLE: "false"
CMD_MATTERMOST_BASEURL: https://team.picasoft.net
CMD_DEFAULT_PERMISSION: freely
OLD_INTERVAL: "2 years"
DB_HOST: "codimd-db"
DB_PORT: 5432
networks:
- proxy
- codimd
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment