-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
179 lines (148 loc) · 5.35 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
from fabric.api import *
from fabric.operations import *
from fabric.contrib import django
from fabric.contrib.project import rsync_project
from fabric.contrib.console import confirm
from fabric.contrib.files import exists
from fabric.colors import yellow, green, blue, red
from fabric.operations import _prefix_commands, _prefix_env_vars
from contextlib import contextmanager as _contextmanager
import sys, os
abspath = lambda filename: os.path.join(
os.path.abspath(os.path.dirname(__file__)),
filename
)
# --------------------------------------------
# Finwallet platform cofiguration
# --------------------------------------------
class FabricException(Exception):
pass
def dev():
print "Connecting to AWS Server"
env.setup = True
env.user = 'ubuntu' #server username
env.ubuntu_version = '16.04'
env.use_ssh_config = True
env.shell = "/bin/bash -l -i -c"
env.password = 'your machines password'
#env.key_filename = abspath('your aws instance credentials .pem')
env.abort_exception = FabricException
env.hosts = [
'ip_address_of_server'
]
env.graceful = True
env.is_grunt = True
env.home = '/home/%s' %(env.user)
env.project = 'your_app_name'
#local config file path
env.nginx_config = abspath('devops/myproject')
env.gcorn_config = abspath('devops/gunicorn.service')
env.nginx_path = '/etc/nginx/sites-available'
env.app_sock = '0.0.0.0:8000'
env.app_path = '%s/%s' %(env.home,env.project)
env.virtualenvpath = '%s/virtual-env/%s' %(env.home,env.project)
env.activate = '%s/bin/activate' %(env.virtualenvpath)
env.rsync_exclude = [
'/devops',
'fab*',
'*.pem',
'*.pyc',
'.gitignore',
]
return
def install():
update()
sync_code_base()
install_python_dependency()
install_pip()
install_virtualenv()
pip_requirements()
config_gunicorn()
install_nginx()
nginx_config()
return
def update():
print 'Start updating the system'
sudo('apt-get update')
return
def sync_code_base():
print 'Syncing Django Project code base'
rsync_project(env.app_path, abspath('') + '*', exclude=env.rsync_exclude, delete=True, default_opts='-rvz')
sudo('chown -R %s:%s %s' % (env.user, env.user, env.app_path))
def install_python_dependency():
"""
install python dependency packages
"""
print "Installing python dependency packages"
sudo(
'apt-get -y install '
'python-setuptools libmysqlclient-dev '
'python-mysqldb libmysqlclient-dev '
'python-dev make automake gcc '
'libxml2 libxml2-dev libxslt-dev '
'python-dev libtidy-dev python-lxml '
'htop iftop'
)
sudo(
'apt-get install -y '
'libjpeg62 zlib1g-dev libjpeg8-dev '
'libjpeg-dev libfreetype6-dev '
'libpng-dev libgif-dev'
)
sudo('apt-get -y install libcurl4-gnutls-dev librtmp-dev')
sudo('apt-get install -y libjpeg-turbo8-dev')
sudo('apt-get install -y libjpeg62-dev')
def install_pip():
update()
print "Installing python pip in the system"
sudo('apt-get install python-pip -y')
return
def install_virtualenv():
print "Installing python virtualenv"
sudo('pip install virtualenv')
run('mkdir -p %s/virtual-env' % env.home)
sudo('chown -R %s:%s /home/ubuntu/virtual-env' % (env.user, env.user))
print "Creating virtualenv for django"
sudo('virtualenv %s' % env.virtualenvpath)
sudo('chmod +x %s' % env.activate)
print "Virtualenv installed"
sudo('apt-get -y install build-essential libssl-dev libffi-dev python-dev')
return
def pip_requirements():
sudo('source %s/bin/activate; cd %s; pip install -r requirements.txt' % (env.virtualenvpath, env.app_path))
def config_gunicorn():
print 'Configuring gUnicorn'
default_config='/etc/systemd/system/gunicorn.service'
if exists(default_config):
sudo('rm /etc/systemd/system/gunicorn.service')
print 'Deleted gunicorn systemd file'
print 'Install gUnicorn config'
put('%s' % (env.gcorn_config), '/etc/systemd/system/', use_sudo=True)
print 'Restarting gUnicorn'
sudo("systemctl enable gunicorn")
sudo("systemctl start gunicorn")
def install_nginx():
print 'Installing NGINX'
sudo("apt-get install -y nginx")
sudo('systemctl enable nginx')
sudo('systemctl start nginx')
return
def nginx_config():
print 'Install NGINX config'
put('%s' % (env.nginx_config), '/etc/nginx/sites-available', use_sudo=True)
sudo('ln -s %s/myproject /etc/nginx/sites-enabled' % (env.nginx_path))
print 'Restarting NGINX and gunicorn'
sudo("systemctl restart nginx")
sudo("systemctl restart gunicorn.service")
return
def deploy():
with cd('%s' % env.app_path):
run('mkdir static')
run('source %s; ./manage.py collectstatic' % env.activate)
run('source %s; ./manage.py makemigrations' % env.activate)
run('source %s; ./manage.py migrate' % env.activate)
#run('source %s; gunicorn --bind %s %s.wsgi:application' % (env.activate, env.app_sock, env.project))
sudo("systemctl restart gunicorn.service")
########################################################################
#--------------------------End of fabric file--------------------------#
########################################################################