-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
177 lines (143 loc) · 5.46 KB
/
Copy pathtasks.py
File metadata and controls
177 lines (143 loc) · 5.46 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# scp -r -P 14076 dist /srv/kairly/kairly-client
#
# ./manage.py collectstatic --settings='kairly.settings_prod'
from invoke import task, Collection
# Production
PY_HOST = 'app@node-13.rosti.cz'
PY_PORT = 14076
CRON_HOST = 'app@node-14.rosti.cz'
CRON_PORT = 15218
JS_HOST = 'app@node-13.rosti.cz'
JS_PORT = 14930
# It would be better compile app on server, but current plab
# is not sufficient (no enough RAM)
@task
def compile_js(ctx):
print("Deleting previous nuxt build...")
ctx.run("cd client && rm -rf .nuxt")
print("Compiling messages...")
ctx.run("cd client && npm run compilemessages")
print("Running nuxt build...")
ctx.run("cd client && npm run build")
@task
def upload_js(ctx):
print("Uploading nuxt build...")
ctx.run("scp -r -P {} client/.nuxt {}:/tmp/.nuxt".format(JS_PORT, JS_HOST))
@task
def promote_js(ctx):
print("Promoting nuxt build...")
remote_commands = [
'export TERM=xterm',
'source /srv/.bashrc',
'cd /srv/kairly',
'git fetch --tags',
'git checkout production',
'cd /srv/app',
'rm -rf /srv/kairly/client/.nuxt',
'mv /tmp/.nuxt /srv/kairly/client/.nuxt',
'cd /srv/app',
'npm install',
'git reset --hard', # reset package-lock.json back, workaround to unsupported npm ci
"supervisorctl restart app",
]
ctx.run("ssh -T -p {} {} '{}'".format(JS_PORT, JS_HOST, ' && '.join(remote_commands)))
@task(compile_js, upload_js, promote_js)
def deploy_js(ctx):
pass
@task
def deploy_py(ctx):
remote_commands = [
'export TERM=xterm',
'source /srv/.bashrc',
'cd /srv/kairly',
'git fetch --tags',
'git checkout production',
'cd /srv/app',
"./manage.py migrate --settings='kairly.settings_prod'",
"./manage.py collectstatic --noinput --settings='kairly.settings_prod'",
'supervisorctl restart app',
]
ctx.run("ssh -T -p {} {} '{}'".format(PY_PORT, PY_HOST, ' && '.join(remote_commands)))
@task(compile_js, upload_js, deploy_py, promote_js)
def deploy_app(ctx):
pass
@task
def deploy_cron(ctx):
remote_commands = [
'export TERM=xterm',
'source /srv/.bashrc',
'cd /srv/kairly',
'git fetch --tags',
'git checkout production',
'cp /srv/kairly/server/cron/crontab /srv/conf',
'crontab /srv/conf/crontab'
]
ctx.run("ssh -T -p {} {} '{}'".format(CRON_PORT, CRON_HOST, ' && '.join(remote_commands)))
@task()
def dbdump_prod(ctx):
import os
import sys
server_root = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'server')
sys.path.append(server_root)
from kairly import settings_prod as settings
dbconf = settings.DATABASES['default']
tmp_file = '/tmp/kairly.sql.gz'
dump_cmd = 'mysqldump --user="{}" --password="{}" -h {} --default-character-set=utf8mb4 {} | gzip > {}'.format(
dbconf['USER'], dbconf['PASSWORD'], dbconf['HOST'], dbconf['NAME'], tmp_file
)
remote_commands = [
'export TERM=xterm',
'source /srv/.bashrc',
dump_cmd
]
ctx.run("ssh -T -p {} {} '{}'".format(PY_PORT, PY_HOST, ' && '.join(remote_commands)))
ctx.run("scp -r -P {} {}:{} .".format(PY_PORT, PY_HOST, tmp_file))
remote_commands = [
'export TERM=xterm',
'source /srv/.bashrc',
'rm ' + tmp_file
]
ctx.run("ssh -T -p {} {} '{}'".format(PY_PORT, PY_HOST, ' && '.join(remote_commands)))
# ctx.run("gunzip kairly.sql.gz")
# to import
# Drop schema and recreate database with utf8mb4
# CREATE SCHEMA `kairly` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci
@task()
def dbdump_import(ctx):
print("Removing kairly schema...")
ctx.run('mysql -h 127.0.0.1 -u root -e "DROP SCHEMA IF EXISTS kairly"')
print("Recreating kairly schema...")
ctx.run('mysql -h 127.0.0.1 -u root -e "CREATE SCHEMA kairly DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"')
print("Importing db dump...")
ctx.run('zcat kairly.sql.gz | mysql -h 127.0.0.1 -u root --default-character-set=utf8mb4 kairly')
@task()
def download_media(ctx):
ctx.run("rsync -chavzP -e 'ssh -p {}' --stats {}:/srv/app/media server".format(PY_PORT, PY_HOST))
deploy_ns = Collection('deploy')
deploy_ns.add_task(deploy_app, 'app', default=True)
deploy_ns.add_task(deploy_js, 'js')
deploy_ns.add_task(deploy_py, 'py')
deploy_ns.add_task(deploy_cron, 'cron')
dbdump_ns = Collection('dbdump')
dbdump_ns.add_task(dbdump_prod, 'prod')
dbdump_ns.add_task(dbdump_import, 'import')
ns = Collection()
ns.add_collection(deploy_ns)
ns.add_collection(dbdump_ns)
ns.add_task(download_media)
"""
TODO try to implement fast import, something like
(but nice to have also limit articles with date range, like article from last week )
mysqldump -u root -h 127.0.0.1 --default-character-set=utf8mb4 --no-data kairly > /tmp/d1.sql
mysqldump -u root -h 127.0.0.1 --default-character-set=utf8mb4 --no-create-db --no-create-info \
--ignore-table=kairly.articles_post \
--ignore-table=kairly.django_session \
--ignore-table=kairly.thumbnail_kvstore \
kairly > /tmp/d2.sql
mysqldump -u root -h 127.0.0.1 --default-character-set=utf8mb4 --no-create-db --no-create-info --single-transaction \
kairly \
--tables articles_post \
--where="id not in (SELECT post_id FROM kairly.articles_issuepost) and id not in (SELECT post_id FROM kairly.articles_backlog)" \
> /tmp/d3.sql
cat /tmp/d1.sql /tmp/d2.sql /tmp/d3.sql | gzip > /tmp/d.sql.gz
"""