-
Notifications
You must be signed in to change notification settings - Fork 3
/
fabfile.py
126 lines (92 loc) · 3.17 KB
/
fabfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# coding=utf-8
from fabric.colors import green
from fabric.context_managers import cd
from fabric.operations import get, run
from fabric.state import env
env.shell = "/bin/zsh -c"
APP_NAME = "app_name"
# APP_NAME is also used as user and
# password for the database for local systems - get_new_db task
env.path = f"/opt/www/{APP_NAME}"
env.hosts = ["servername"]
# # DEPLOYMENT TARGETS
# # ####################
# def stage():
# env.environment = "stage"
# env.hosts = ["servername"]
# env.gateway = "[email protected]" # if needed
# def live():
# env.environment = "live"
# env.hosts = ["servername"]
# T A S K S
# ###########
def deploy_only():
"""Pull all updates from the remote repository."""
with cd(env.path):
print(green("updating from repository .."))
run("git pull")
def clear_cache():
with cd(env.path):
print(green("\ndeleting cache .."))
manage("clear_cache")
# manage("thumbnail clear_delete_all")
def restart():
"""Restart nginx and the backend worker."""
print(green("restarting server .."))
run(f"pm2 restart {APP_NAME}")
clear_cache()
def deploy():
deploy_only()
update_static()
restart()
def migrate():
"""
Pull all updates from the remote repository.
Migrates the database and installs new lib versions from requirements.
Static files are also collected.
"""
deploy_only()
with cd(env.path):
print(green("updating packages .."))
# this might cause some trouble: https://github.com/python-poetry/poetry/issues/732
run("poetry run pip install --upgrade pip setuptools")
run("poetry install")
print(green("migrating database .."))
manage("migrate --noinput")
update_static()
restart()
def update_static():
with cd(env.path):
print(green("compressing files .."))
manage("compress -e pug,html --force")
# manage("compilescss")
print(green("collecting static files .."))
manage("collectstatic --noinput")
def manage(command):
run("poetry run ./manage.py " + command)
# # Postgres version
# def get_new_db():
# users = local('psql -c "\\du"', capture=True)
# if APP_NAME not in users:
# local(f"psql -c \"CREATE USER {APP_NAME} WITH CREATEDB PASSWORD '{APP_NAME}';\"")
#
# dbs = local('psql -c "\\l"', capture=True)
# if {APP_NAME} in dbs:
# local(f'psql -U {APP_NAME} postgres -c "DROP DATABASE {APP_NAME};"')
#
# local(f'psql -U {APP_NAME} postgres -c "CREATE DATABASE {APP_NAME};"')
#
# with cd(env.path):
# run(f"pg_dump -h localhost -U {APP_NAME} --disable-triggers {APP_NAME} >! dump")
# get("dump", "dump")
#
# local(f"psql -U {APP_NAME} {APP_NAME} -f dump")
# SQLite version
def get_new_db():
with cd(env.path):
# Create a new Checkpoint in TRUNCATE mode to ensure all data is written to the main file
run('sqlite3 db.sqlite3 "PRAGMA wal_checkpoint(TRUNCATE);"')
# Step 2: Copy the database file to the local machine
get("db.sqlite3", "db.sqlite3")
# Optional: Verify or log that the database has been successfully copied
green("Database copied!")