diff --git a/dockerfile b/dockerfile index 8ba9602..6138538 100644 --- a/dockerfile +++ b/dockerfile @@ -12,16 +12,22 @@ RUN apk add --update \ mysql-client \ redis +RUN adduser -D piglet + ENV MYSQL_PASSWORD=9vaGPf8fXzdexm5DM2 MYSQL_USER=piglet MYSQL_DATABASE=piglet MYSQL_HOST=database PIP_USE_PEP517=1 DOMAIN=localhost MAIL_SERVER= MAIL_PORT= MAIL_USER= MAIL_PASSWORD= MAIL_ENCRYPTIONPROTOCOL= SECURE_COOKIE=False DB_ROOT_PASSWORD=xUDEZMKWew9D3hn27ZRKSdGw COPY webapp/config/python/requirements.txt /tmp/requirements.txt -RUN pip3 install --quiet -r /tmp/requirements.txt --break-system-packages - COPY webapp /webapp -RUN mkdir -p /webapp/log/api /webapp/log/app /webapp/log/scheduler + +RUN mkdir -p /webapp/log/api /webapp/log/app /webapp/log/scheduler /webapp/api/uploads && chown -R piglet:piglet /webapp + +USER piglet + WORKDIR /webapp +RUN python -m venv venv && . /webapp/venv/bin/activate && pip install --upgrade pip && pip install --quiet -r /tmp/requirements.txt + EXPOSE 8080 80 5566 ENTRYPOINT ["supervisord", "--nodaemon", "--configuration", "/webapp/config/supervisor/supervisord.conf"] diff --git a/webapp/api/routes/admin.py b/webapp/api/routes/admin.py index 3b67961..f856ed1 100644 --- a/webapp/api/routes/admin.py +++ b/webapp/api/routes/admin.py @@ -75,30 +75,44 @@ async def startup_event(): mysql.post(i) except: continue - - - try: version = mysql.get("""select value from pig_meta where `key` = 'version'""")[0]["value"] print(f"Piglet Schema Version: {version}",flush=True) + sql_files = [] + version = float(version) + + if version >= 1.2: + schema_directory = '/webapp/config/dbschema/update' + for file in os.listdir(schema_directory): + if file.endswith('.sql'): + sql_files.append(file) + + for new_version in sql_files: + new_version_float = float(new_version.split('.sql')[0]) + + if new_version_float > version: + with open(f'/webapp/config/dbschema/update/{new_version}','r') as file: + sql_commands = file.read() + commands = sql_commands.split(";") + for i in commands: + try: + mysql.post(i) + except Exception as e: + print(f'Error in db schema upgrade -> {new_version_float} - {e}') + continue except: - v1_2inserts = ["""RENAME table new_orders to pig_orders""", - """alter table pig_orders add column id int auto_increment primary key first""", - """CREATE TABLE `pig_meta` (`key` VARCHAR(255),`value` VARCHAR(255), PRIMARY KEY (`key`))""", - """INSERT IGNORE INTO `pig_meta` (`key`, `value`) VALUES ('version', '1.2')""", - """CREATE TABLE IF NOT EXISTS `pig_userbudgets` (`user_id` int(11) NOT NULL,`budget_id` int(11) NOT NULL,`joined` tinyint(4) DEFAULT NULL,PRIMARY KEY (`user_id`,`budget_id`),KEY `budget_id` (`budget_id`),CONSTRAINT `budget_id` FOREIGN KEY (`budget_id`) REFERENCES `pig_budgets` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT `user_id` FOREIGN KEY (`user_id`) REFERENCES `registered_user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)""", - """INSERT IGNORE INTO pig_notitype VALUES (3,"budget","Budget")""", - """INSERT IGNORE INTO pig_notiobj VALUES (3,'joined','joined'),(4,'shared','shared')""", - """ALTER TABLE pig_notisettings ADD CONSTRAINT unique_line_constraint UNIQUE (user_id, notiobj, notitype, mail, web)""" - ] - for i in v1_2inserts: + with open('/webapp/config/dbschema/update/1.2.sql','r') as file: + sql_commands = file.read() + commands = sql_commands.split(";") + + for i in commands: try: mysql.post(i) - except: + except Exception as e: + print(f'Error in db schema upgrade -> 1.2 {e}') continue - #### migrate pig_bidmapping to pig_userbudgets user_data = mysql.get('''select * from registered_user''') bidmapping_data = mysql.get('''select * from pig_bidmapping''') @@ -118,6 +132,8 @@ async def startup_event(): mysql.close() + + ### AUTHENTICATION ### oauth2_scheme = OAuth2PasswordBearer(tokenUrl="./admin/token") diff --git a/webapp/api/routes/functs.py b/webapp/api/routes/functs.py index c8918a6..a27778d 100644 --- a/webapp/api/routes/functs.py +++ b/webapp/api/routes/functs.py @@ -28,9 +28,6 @@ def hex_color(): return Color('#'+ ''.join([random.choice(L) for i in range(6)][:])) def check(mysql,budget_id,user_id): - - #query = '''select b0,b1,b2,b3 from pig_bidmapping where id="{}"'''.format(bidmapping) - query = f'''select pig_budgets.* from pig_budgets JOIN pig_userbudgets on pig_budgets.id = pig_userbudgets.budget_id where pig_userbudgets.user_id = {user_id} and {budget_id}''' response = mysql.get(query) @@ -43,7 +40,7 @@ def check(mysql,budget_id,user_id): def _get_uids(mysql,budget_id): - get_uids = '''select id from registered_user where bid_mapping in (select id from pig_bidmapping where b0={bid} or b1={bid} or b2={bid} or b3={bid})'''.format(bid=budget_id) + get_uids = f'''select user_id from pig_userbudgets where budget_id={budget_id}''' uid_list = mysql.get(get_uids) diff --git a/webapp/api/routes/user.py b/webapp/api/routes/user.py index 96f3ae0..bc19697 100644 --- a/webapp/api/routes/user.py +++ b/webapp/api/routes/user.py @@ -95,7 +95,6 @@ async def login_user(current_user = Depends(get_current_user)): userid = current_user["id"] query = '''select r.id,r.email,r.verified,r.name,r.surname,r.color,r.image,r.budget_id,r.bid_mapping,pig_bidmapping.b0,pig_bidmapping.b1,pig_bidmapping.b2,pig_bidmapping.b3 from registered_user as r join pig_bidmapping on pig_bidmapping.id = r.bid_mapping where r.email="{}"'''.format(email) - query1 = f'''select id,email,verified,name,surname,color,image from registered_user where id={userid}''' query2 = f'''select budget_id from pig_userbudgets where user_id={userid} and joined=1''' @@ -115,9 +114,6 @@ async def login_user(current_user = Depends(get_current_user)): response = user_data - - #response = mysql.get(query) - mysql.close() try: @@ -451,4 +447,4 @@ async def settings_patch(patchSettings: patchSettings, current_user = Depends(ge mysql.close() - return return_list \ No newline at end of file + return return_list diff --git a/webapp/config/dbschema/update/1-2.sql b/webapp/config/dbschema/update/1-2.sql deleted file mode 100644 index 918a47a..0000000 --- a/webapp/config/dbschema/update/1-2.sql +++ /dev/null @@ -1,13 +0,0 @@ -RENAME table new_orders to pig_orders; - -alter table pig_orders add column id int auto_increment primary key first; - -CREATE TABLE IF NOT EXISTS `pig_userbudgets` ( - `user_id` int(11) NOT NULL, - `budget_id` int(11) NOT NULL, - `joined` tinyint(4) DEFAULT NULL, - PRIMARY KEY (`user_id`,`budget_id`), - KEY `budget_id` (`budget_id`), - CONSTRAINT `budget_id` FOREIGN KEY (`budget_id`) REFERENCES `pig_budgets` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION, - CONSTRAINT `user_id` FOREIGN KEY (`user_id`) REFERENCES `registered_user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; \ No newline at end of file diff --git a/webapp/config/dbschema/update/1.2.sql b/webapp/config/dbschema/update/1.2.sql new file mode 100644 index 0000000..8718bd1 --- /dev/null +++ b/webapp/config/dbschema/update/1.2.sql @@ -0,0 +1,26 @@ +RENAME TABLE IF EXISTS new_orders TO pig_orders; +ALTER TABLE pig_orders ADD COLUMN IF NOT EXISTS id INT AUTO_INCREMENT PRIMARY KEY FIRST; + +CREATE TABLE IF NOT EXISTS `pig_meta` ( + `key` VARCHAR(255), + `value` VARCHAR(255), + PRIMARY KEY (`key`) +); + +INSERT IGNORE INTO `pig_meta` (`key`, `value`) VALUES ('version', '1.2'); + +CREATE TABLE IF NOT EXISTS `pig_userbudgets` ( + `user_id` INT(11) NOT NULL, + `budget_id` INT(11) NOT NULL, + `joined` TINYINT(4) DEFAULT NULL, + PRIMARY KEY (`user_id`, `budget_id`), + KEY `budget_id` (`budget_id`), + CONSTRAINT `budget_id` FOREIGN KEY (`budget_id`) REFERENCES `pig_budgets` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION, + CONSTRAINT `user_id` FOREIGN KEY (`user_id`) REFERENCES `registered_user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION +); + +INSERT IGNORE INTO pig_notitype VALUES (3, "budget", "Budget"); + +INSERT IGNORE INTO pig_notiobj VALUES (3, 'joined', 'joined'), (4, 'shared', 'shared'); + +ALTER TABLE pig_notisettings ADD CONSTRAINT IF NOT EXISTS unique_line_constraint UNIQUE (user_id, notiobj, notitype, mail, web); diff --git a/webapp/config/dbschema/update/1.3.sql b/webapp/config/dbschema/update/1.3.sql new file mode 100644 index 0000000..7efc547 --- /dev/null +++ b/webapp/config/dbschema/update/1.3.sql @@ -0,0 +1 @@ +UPDATE `pig_meta` set `value` = '1.3' where `key` = 'version'; diff --git a/webapp/config/supervisor/supervisord.conf b/webapp/config/supervisor/supervisord.conf index 85fb0f2..3734aa1 100644 --- a/webapp/config/supervisor/supervisord.conf +++ b/webapp/config/supervisor/supervisord.conf @@ -6,7 +6,7 @@ stdout_logfile=/dev/stdout stdout_logfile_maxbytes=0 stderr_logfile=/dev/stderr stderr_logfile_maxbytes=0 -user=root +user=piglet [program:import_db] command=/webapp/config/scripts/install_db.sh @@ -19,11 +19,11 @@ autorestart=false priority=1 exitcodes=0 startretries=1 -user=root +user=piglet [program:piglet-redis] command=/usr/bin/redis-server /webapp/config/redis/redis.conf -user=root +user=piglet autostart=true stopsignal=QUIT priority=1 @@ -35,8 +35,8 @@ stderr_logfile=/dev/null stderr_logfile_maxbytes=0 [program:taskworker] -command=/usr/bin/celery -A scheduler.celery_config worker --loglevel=info -user=root +command=/webapp/venv/bin/celery -A scheduler.celery_config worker --loglevel=info +user=piglet autostart=true stopsignal=QUIT priority=2 @@ -48,8 +48,8 @@ stderr_logfile=/dev/null stderr_logfile_maxbytes=0 [program:taskscheduler] -command=/usr/bin/celery -A scheduler.celery_config beat -user=root +command=/webapp/venv/bin/celery -A scheduler.celery_config beat +user=piglet autostart=true stopsignal=QUIT priority=2 @@ -65,7 +65,7 @@ priority=3 autorestart = true startsecs=20 startretries=10 -command=/usr/bin/python3 /usr/bin/uvicorn api.main:app --host 0.0.0.0 --port 8080 +command=/webapp/venv/bin/python3 /webapp/venv/bin/uvicorn api.main:app --host 0.0.0.0 --port 8080 stdout_logfile=/dev/stdout stdout_logfile_maxbytes=0 stderr_logfile=/dev/stderr @@ -76,7 +76,7 @@ priority=5 autorestart = true startsecs=20 startretries=10 -command=/usr/bin/python3 /usr/bin/gunicorn --bind 0.0.0.0:80 app.views:app +command=/webapp/venv/bin/python3 /webapp/venv/bin/gunicorn --bind 0.0.0.0:80 app.views:app stdout_logfile=/dev/stdout stdout_logfile_maxbytes=0 stderr_logfile=/dev/stderr