forked from tomimick/restpie3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
125 lines (92 loc) · 3.2 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# fabfile.py: automated tasks
# - deploy sources from local machine to test/production server
# - migrate local/server database
#
# Author: [email protected]
import sys
import os
import time
import io
from fabric.api import env, run, task, sudo, local, put
from fabric.contrib.console import confirm
from fabric.contrib.project import rsync_project
from fabric.operations import prompt
# write your own server info here:
TEST_SERVER = "testserver.mydomain.com"
PRODUCTION_SERVER = "www.mydomain.com"
SSH_USER = ""
SSH_PRIVATE_KEY = "~/.ssh/xxx_rsa"
# --------------------------------------------------------------------------
# fabric reads these
env.hosts = [TEST_SERVER]
env.use_ssh_config = True
env.user = SSH_USER
env.remotedir = "/app/"
env.port = 22
env.key_filename = SSH_PRIVATE_KEY
# --------------------------------------------------------------------------
# DATABASE TASKS
@task
def postgres_migrate_local():
"""Local database migrate"""
local("python scripts/dbmigrate.py")
@task
def postgres_migrate_remote():
"""Server database migrate"""
dir = env.remotedir
cmd = "cd {}; PYTHONPATH={}py PYSRV_CONFIG_PATH={} python3 scripts/dbmigrate.py".format(dir, dir, dir+"real-server-config.json")
run(cmd)
@task
def postgres_run_server():
local("postgres -D /usr/local/var/postgres")
@task
def postgres_list_tables():
sql = "SELECT * FROM pg_catalog.pg_tables WHERE schemaname = 'public'"
local("psql -d tmdb -c \"{}\"".format(sql))
@task
def postgres_list_users():
sql = "SELECT * FROM users"
local("psql -d tmdb -c \"{}\"".format(sql))
@task
def postgres_gen_models():
"""Generate peewee models from database: generated-models.py"""
cmd = "pwiz.py -e postgresql -u tm -P tmdb >generated-models.py"
local(cmd)
# --------------------------------------------------------------------------
# DEPLOY TASKS
@task
def production():
"""Set target host to production server"""
if confirm("DEPLOY PRODUCTION, YOU SURE ??????", default=False):
env.hosts = [PRODUCTION_SERVER]
print("Deploying soon... ", env.hosts[0].upper())
# wait a little so you can still stop...
time.sleep(5)
else:
print("Exiting")
sys.exit(1)
@task
def deploy():
"""Deploy current local sources to server + db migration"""
rsync_files()
postgres_migrate_remote()
# touch VERSION, uwsgi will then restart automatically
data = io.StringIO("%d" % time.time())
put(data, "/app/VERSION", use_sudo=False)
def rsync_files():
"""rsync source files to remote server"""
exclude_list = ['*.pyc', '.git', '.DS_Store', 'node_modules', '__pycache__',
'doc', 'trash']
rsync_project(env.remotedir, local_dir=".", delete=False,
default_opts='-hrvz', exclude=exclude_list,
extra_opts=' -O --no-perms --checksum')
@task
def deploy_mydaemon():
"""Update uwsgi master config conf/pydaemon.service, then restart"""
sudo("systemctl stop pydaemon", warn_only=True)
put("conf/pydaemon.service", "/etc/systemd/system/", use_sudo=True)
sudo("systemctl enable pydaemon")
sudo("systemctl daemon-reload")
sudo("systemctl start pydaemon")