This repository has been archived by the owner on Feb 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fabfile.py
138 lines (97 loc) · 2.86 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
127
128
129
130
131
132
133
134
135
136
137
138
from contextlib import contextmanager
from pathlib import Path
from fabric.context_managers import cd, path, shell_env
from fabric.contrib import django
from fabric.decorators import task
from fabric.operations import run, local, sudo
from fabric.state import env
django.project('noripyt')
from django.conf import settings
env.hosts = ['root@noripyt.com']
HOME = Path('/root/')
PROJECT_PATH = HOME / 'noripyt.com'
VIRTUALENV_PATH = HOME / '.virtualenvs/noripyt.com/'
DB_SETTINGS = settings.DATABASES['default']
DB_NAME = DB_SETTINGS['NAME']
DB_USER = DB_SETTINGS['USER']
REMOTE_BACKUP = HOME / 'backups/noripyt.backup'
LOCAL_BACKUP = './backups/noripyt.backup'
REMOTE_MEDIA = PROJECT_PATH / 'media/'
LOCAL_MEDIA = './media/'
@contextmanager
def workon(settings_module='noripyt.settings.production'):
with cd(str(PROJECT_PATH)):
with path(str(VIRTUALENV_PATH / 'bin'), behavior='prepend'):
with shell_env(DJANGO_SETTINGS_MODULE=settings_module):
yield
@task
def setup_firewall():
run('ufw disable')
run('ufw default deny')
run('ufw allow 80')
run('ufw allow 22')
run('ufw allow 443')
run('ufw enable')
@task
def create_ssl_certificate():
run('apt install letsencrypt')
run('systemctl stop nginx')
try:
run('letsencrypt certonly --standalone -d noripyt.com')
finally:
run('systemctl start nginx')
@task
def renew_ssl_certificate():
run('letsencrypt renew')
def upgrade_ubuntu():
sudo('apt-get update')
sudo('apt-get upgrade')
def pip_install():
with workon():
run('pip install -r requirements.txt')
def update_submodules():
with workon():
run('git submodule init')
run('git submodule update')
def migrate_db():
with workon():
run('./manage.py migrate')
def collectstatic():
with workon():
run('./manage.py collectstatic --noinput')
@task
def restart():
sudo('supervisorctl restart django')
@task
def update():
upgrade_ubuntu()
with workon():
run('git pull')
update_submodules()
run('find . -name "*.pyc" -delete')
pip_install()
migrate_db()
collectstatic()
restart()
@task
def save_remote_db():
run('pg_dump -U %s -Fc -b -v -f "%s" %s'
% (DB_USER, REMOTE_BACKUP, DB_NAME))
local('rsync "%s":"%s" "%s"' % (env.hosts[0], REMOTE_BACKUP, LOCAL_BACKUP))
@task
def restore_saved_db():
local('sudo -u postgres dropdb %s' % DB_NAME)
local('sudo -u postgres createdb %s' % DB_NAME)
local('pg_restore -U root -e -d %s -j 5 "%s"' % (DB_NAME, LOCAL_BACKUP))
@task
def clone_remote_db():
save_remote_db()
restore_saved_db()
@task
def clone_remote_media():
local('rsync -azv "%s":"%s" "%s"' % (env.hosts[0],
REMOTE_MEDIA, LOCAL_MEDIA))
@task
def clone_remote():
clone_remote_db()
clone_remote_media()