Skip to content

Commit

Permalink
Some deployment changes of the docker containers (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
k3nd0x authored Jun 20, 2024
2 parents 1b7fc4f + a0bc813 commit 8ac1de4
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 49 deletions.
12 changes: 9 additions & 3 deletions dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
46 changes: 31 additions & 15 deletions webapp/api/routes/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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''')
Expand All @@ -118,6 +132,8 @@ async def startup_event():
mysql.close()




### AUTHENTICATION ###
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="./admin/token")

Expand Down
5 changes: 1 addition & 4 deletions webapp/api/routes/functs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down
6 changes: 1 addition & 5 deletions webapp/api/routes/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'''
Expand All @@ -115,9 +114,6 @@ async def login_user(current_user = Depends(get_current_user)):

response = user_data


#response = mysql.get(query)

mysql.close()

try:
Expand Down Expand Up @@ -451,4 +447,4 @@ async def settings_patch(patchSettings: patchSettings, current_user = Depends(ge
mysql.close()


return return_list
return return_list
13 changes: 0 additions & 13 deletions webapp/config/dbschema/update/1-2.sql

This file was deleted.

26 changes: 26 additions & 0 deletions webapp/config/dbschema/update/1.2.sql
Original file line number Diff line number Diff line change
@@ -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);
1 change: 1 addition & 0 deletions webapp/config/dbschema/update/1.3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE `pig_meta` set `value` = '1.3' where `key` = 'version';
18 changes: 9 additions & 9 deletions webapp/config/supervisor/supervisord.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 8ac1de4

Please sign in to comment.