This repository has been archived by the owner on Apr 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
183 lines (128 loc) · 6.1 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
# coding: utf-8
from datetime import date
from fabric.api import *
from fabric.contrib.files import exists, append, upload_template, sed
from fab_settings import *
env.directory = '/home/%s/projects/zeonsearch' % SSH_USER
env.manage_dir = env.directory
env.user = SSH_USER
env.activate = 'source %s/ENV/bin/activate' % env.directory
env.www_ssh_key = 'ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAlgcYVZYvzu1GX4Td+RLt9BIqUr33gkTz6MW2MHvWS/+9eKueA6+N7Bei2NqTBNg2HLUY0uOyG1NBmzoWZglht70iChcGLMVkvciQ1/QQfr5bvIbfgpPHuZMwn4ElFiiabhnZe9wALp+jjg0TnolWxbAfwJUmv2UDXXSiYDrfBes= glader hosting rsa-key-20150528'
env.forward_agent = True
if not env.hosts:
env.hosts = ['82.196.9.202']
def virtualenv(command):
with cd(env.directory):
run(env.activate + ' && ' + command)
def init():
with settings(user='root'):
append('/etc/apt/sources.list', 'deb-src http://archive.ubuntu.com/ubuntu precise main')
append('/etc/apt/sources.list', 'deb-src http://archive.ubuntu.com/ubuntu precise-updates main')
# run('apt-get update')
# run('apt-get upgrade -y')
# run('apt-get install -y mc nginx mysql-client python-setuptools python-dev python-pip rrdtool sendmail memcached fail2ban')
# run('apt-get build-dep -y python-mysqldb')
# run('pip install --upgrade virtualenv')
# run('apt-get install libtiff4-dev libjpeg8-dev zlib1g-dev libfreetype6-dev liblcms2-dev libwebp-dev') # Pillow dependencies
if not exists('/home/%s' % SSH_USER):
run('yes | adduser --disabled-password %s' % SSH_USER)
run('mkdir /home/%s/.ssh' % SSH_USER)
run('echo "%s" >> /home/%s/.ssh/authorized_keys' % (env.www_ssh_key, SSH_USER))
if not exists('/var/log/projects/zeonsearch'):
run('mkdir -p /var/log/projects/zeonsearch')
run('chmod 777 /var/log/projects/zeonsearch')
if exists('/etc/nginx/sites-enabled/default'):
run('rm /etc/nginx/sites-enabled/default')
if not exists('/etc/nginx/listen'):
put('etc/nginx/listen', '/etc/nginx/listen', use_sudo=True)
if not exists('/etc/nginx/sites-available/90-zeonsearch.conf'):
run('touch /etc/nginx/sites-available/90-zeonsearch.conf')
run('chown %s /etc/nginx/sites-available/90-zeonsearch.conf' % SSH_USER)
if not exists('/etc/nginx/sites-enabled/90-zeonsearch.conf'):
run('ln -s /etc/nginx/sites-available/90-zeonsearch.conf /etc/nginx/sites-enabled/90-zeonsearch.conf', shell=False)
if not exists('/etc/init/zeonsearch.conf'):
run('touch /etc/init/zeonsearch.conf')
run('chown %s /etc/init/zeonsearch.conf' % SSH_USER)
append('/etc/sudoers', '%s ALL=(ALL) NOPASSWD:/sbin/restart zeonsearch,/sbin/restart zeonsearch_celery' % SSH_USER)
run('mkdir -p /home/%s/projects/zeonsearch' % SSH_USER)
run('chown -R %(user)s:%(user)s /home/%(user)s' % {'user': SSH_USER})
def init_mysql():
with settings(host_string='82.196.15.15', user='root'):
run('apt-get update')
run('apt-get upgrade -y')
run('apt-get install -y fail2ban mc')
run('DEBIAN_FRONTEND=noninteractive apt-get -q -y install mysql-server')
run('mysqladmin -u root password mysecretpasswordgoeshere111')
sed('/etc/mysql/my.cnf', 'bind-address.+$', 'bind-address = ::')
run('/etc/init.d/mysql restart')
def production(mode=''):
upload()
environment()
local_settings()
nginx()
upstart()
migrate()
# collect_static()
restart()
def upload():
with settings(user=SSH_USER):
local('git archive HEAD | gzip > archive.tar.gz')
put('archive.tar.gz', env.directory + '/archive.tar.gz')
with cd(env.directory):
run('tar -zxf archive.tar.gz')
run('rm archive.tar.gz')
local('del archive.tar.gz')
def environment():
with cd(env.directory):
with settings(warn_only=True):
run('virtualenv ENV')
virtualenv('pip install -r etc/deps/requirements.txt')
def local_settings():
with settings(user=SSH_USER):
with cd(env.manage_dir):
upload_template(
'zeonsearch/local_settings.py.sample',
'zeonsearch/local_settings.py',
globals(),
backup=False
)
def nginx():
run('cp %(directory)s/etc/nginx/90-zeonsearch.conf /etc/nginx/sites-available/90-zeonsearch.conf' % env, shell=False)
#sudo('/etc/init.d/nginx reload', shell=False)
def upstart():
run('cp %(directory)s/etc/upstart/zeonsearch.conf /etc/init/zeonsearch.conf' % env, shell=False)
def manage_py(command):
virtualenv('cd %s && python manage.py %s' % (env.manage_dir, command))
def migrate():
with settings(user=SSH_USER):
manage_py('migrate')
def collect_static():
with settings(user=SSH_USER):
run('mkdir -p /home/www/projects/zeonsearch/static')
manage_py('collectstatic -c --noinput')
def restart():
run('sudo restart zeonsearch')
def remote(args=''):
manage_py(args)
# -----------------------------------------------------------------------
def run_local():
local('ENV\\Scripts\\python manage.py runserver 0.0.0.0:8000')
def local_env():
with settings(warn_only=True):
local('virtualenv.exe ENV --system-site-packages')
local('ENV\\Scripts\\pip install -r etc\\deps\\requirements_test.txt ')
def enter(args=''):
local('ENV\\Scripts\\python manage.py %s' % args)
def local_migrate():
with settings(warn_only=True):
local('ENV\\Scripts\\python manage.py makemigrations core')
local('ENV\\Scripts\\python manage.py migrate')
def update_local_db():
run('mysqldump -u %(DATABASE_USER)s -p%(DATABASE_PASSWORD)s -h %(DATABASE_HOST)s %(DATABASE_DB)s |gzip > zeonsearch.sql.gz' % globals())
get('zeonsearch.sql.gz', 'zeonsearch.sql.gz')
run('rm zeonsearch.sql.gz')
local('gzip -d zeonsearch.sql.gz')
local('mysql -uroot %(DATABASE_DB)s < zeonsearch.sql' % globals())
local('del zeonsearch.sql')
def local_static():
local('cd src && ..\\ENV\\scripts\\python manage.py collectstatic -c --noinput --verbosity=0')