forked from forrestfinch/apipanda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
executable file
·225 lines (180 loc) · 5.19 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
"""
Summary.
Attributes:
local_dir (str): Description
PRODUCTION (str): Description
remote_dir (str): Description
REMOTE_USER (str): Description
STAGING (str): Description
"""
from fabric.api import (local, cd, run, task, env, sudo,
open_shell, put, get)
STAGING = '162.243.53.17'
PRODUCTION = '162.243.113.95'
REMOTE_USER = 'root'
local_dir = './'
remote_dir = '/home/django/django_project'
env.hosts = [
# STAGING,
PRODUCTION
]
env.colorize_errors = True
env.user = REMOTE_USER
@task
def dev(port=8000):
"""Run local webserver.
Args:
port (int, optional): Description
"""
local("./manage.py runserver %s:%s" % ('127.0.0.1', port))
@task
def install(version=""):
"""Install project artifacts.
Args:
version (str, optional): Description
"""
local("pip install -r requirements.txt")
local("python%s manage.py makemigrations" % version)
local("python%s manage.py syncdb" % version)
local("python%s manage.py schemamigration --initial app" % version)
local("python%s manage.py migrate" % version)
local("python%s manage.py createsuperuser" % version)
@task
def clean():
"""Remove all the .pyc files."""
local("find . -name '*.pyc' -print0|xargs -0 rm", capture=False)
@task
def push(msg):
"""Push to github.
Args:
msg (str, optional): Description
"""
local("git add . && git commit -m '{}'".format(msg))
local("git push")
@task
def deploy(msg="deploy to server"):
"""Deploy the app to the live server on Digital Ocean.
Deleted Parameters:
msg (str, optional): Description
"""
clean()
push(msg)
server = run("cd {}".format(remote_dir))
if server.succeeded:
with cd(remote_dir):
remote_pull = run("git pull")
if remote_pull.succeeded:
deps_install = run("pip install -r requirements.txt")
if deps_install.succeeded:
print("Deployment was successful")
if server.failed:
print("Something bad happened!!", server)
@task
def launch():
"""
Summary.
Returns:
TYPE: Description
"""
server = run("cd {}".format(remote_dir))
if server.succeeded:
with cd(remote_dir):
sudo("service gunicorn restart")
sudo("service nginx restart")
print("New VERSION was launched successfully")
@task
def debug():
"""
Summary.
Returns:
TYPE: Description
"""
server = run("cd {}".format(remote_dir))
if server.succeeded:
with cd(remote_dir):
open_shell()
@task
def ssl():
"""
Summary.
Returns:
TYPE: Description
"""
key = put('./key', '/key')
if key.succeeded:
sudo('cat /key >> ~/.ssh/authorized_keys ')
# sudo('cat /key | ssh root@162.243.113.95 "cat >> ~/.ssh/authorized_keys" ')
@task
def migrate():
with cd(remote_dir):
upload = put('./payment/migrations', 'payment')
if upload.succeeded:
debug()
@task
def g(path='/tmp/tables.sql'):
contents = get(remote_path=path, local_path='./')
if contents.succeeded:
print('yes!')
@task
def p(path=remote_dir):
put('api/views.py', '%s/api/views.py' % path)
# put('api/serializer.py', '%s/api/serializer.py' % path)
# put('c.sql', '%s/c.sql' % path)
# put('manage.py', '%s/manage.py' % path)
# with cd('%s/lrc_api' % path):
# put('luc_api/settings.py', 'settings.py')
# put('property/models.py', '%s/property/models.py' % path)
# put('requirements.txt', '%s/requirements.txt' % path)
@task
def config():
"""
Summary.
Returns:
TYPE: Description
"""
put('.env', '~/.env')
env = run('cat ~/.env > ~/.bashrc')
if env.succeeded:
# sudo("mv /etc/init/gunicorn.conf /etc/init/gunicorn.conf.bak")
# Refresh Bash
# run('exec bash')
gunicorn = put('gunicorn.conf', '/etc/init/gunicorn.conf')
if gunicorn.succeeded:
# sudo(
# 'mv /etc/nginx/sites-available/django\
# /etc/nginx/sites-available/django.bak')
nginx = put('django', '/etc/nginx/sites-available/django')
if nginx.succeeded:
print('Deployed config.')
launch()
@task
def fixture():
"""
Summary.
Returns:
TYPE: Description
"""
dp = put('merge.sql', '/home/django/django_project/merge.sql')
if dp.succeeded:
open_shell()
@task
def provision(name="Bernard Ojengwa", email="bernard@initsng.com"):
"""
Summary.
Args:
name (str, optional): Description
email (str, optional): Description
Returns:
TYPE: Description
"""
server = run("cd {}".format(remote_dir))
if server.succeeded:
deps = sudo("apt-get update")
if deps.succeeded:
git = sudo(
'apt-get install git mysql-server libmysqlclient-dev -y')
if git.succeeded:
run("git config --global user.email %s" % email)
run("git config --global user.name %s" % name)
sudo('mysql_install_db')
sudo('/usr/bin/mysql_secure_installation')