forked from CitoEngine/cito_plugin_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
208 lines (157 loc) · 6.52 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
"""Copyright 2014 Cyrus Dasadia
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from fabric.api import run, local, env, sudo, put, warn_only
from fabric.context_managers import cd, prefix
from fabric.colors import yellow, green
from fabric.contrib.files import append
from fabric.operations import prompt
from getpass import getpass
import inspect
import random
env.app_dir = '/opt/cito_plugin_server'
env.venv_dir = '/opt/virtualenvs/citopluginvenv'
env.pip_file = 'requirements.txt'
# Setting defaults in case e() is not called
env.django_settings_module = 'cito_plugin_server.settings.production'
env.django_settings_file = 'cito_plugin_server/settings/production.py'
def install_virtualenv():
print(yellow('Starting >> %s()' % _fn()))
sudo('pip -q install virtualenv')
def setup_virtualenv():
print(yellow('Starting >> %s()' % _fn()))
run('virtualenv --no-site-packages %(venv_dir)s' % env, pty=True)
def update_requirements():
print(yellow('Starting >> %s()' % _fn()))
with cd(env.app_dir):
ve_run('pip install -q --upgrade setuptools')
ve_run('pip install -r %s' % env.pip_file)
def mkdirs():
print(yellow('Starting >> %s()' % _fn()))
sudo('mkdir -p %(app_dir)s && chown %(user)s %(app_dir)s' % env)
sudo('mkdir -p %(venv_dir)s && chown %(user)s %(venv_dir)s' % env)
def upload_reqs():
print(yellow('Starting >> %s()' % _fn()))
put('requirements.txt', env.app_dir)
def install_build_deps():
print(yellow('Starting >> %s()' % _fn()))
_detect_pkg_manager()
with warn_only():
sudo('%(pkg_manager)s -y install build-essential python-dev python-mysqldb python-pip libmysqlclient-dev' % env)
def install_mysql():
print(yellow('Starting >> %s()' % _fn()))
_detect_pkg_manager()
with warn_only():
sudo('%(pkg_manager)s -y install mysql-server mysql-client' % env)
def bootstrap():
install_build_deps()
install_virtualenv()
mkdirs()
upload_reqs()
setup_virtualenv()
update_requirements()
install_mysql()
def deploy():
dj_copy_vagrant()
dj_create_secret()
dj_dbconfig()
dj_syncdb()
dj_create_superuser()
#################
# Django specific methods
#################
# noinspection PyBroadException
def dj_dbconfig(createdb=True):
"""
Setup mysql root password, create the db and update the config
"""
env.mysql_user = prompt(green("Enter MySQL username: "))
env.mysql_password = getpass(green("Enter MySQL password (will not display on screen): "))
env.mysql_dbname = prompt(green("Enter MySQL DB Name (default=cito_plugin_server): "), default='cito_plugin_server')
if createdb:
run('mysqladmin -u %(mysql_user)s -p%(mysql_password)s create %(mysql_dbname)s' % env)
with cd(env.app_dir):
sudo('chown %(user)s %(app_dir)s/%(django_settings_file)s' % env)
append('%(app_dir)s/%(django_settings_file)s' % env, "\nDATABASES['default']['USER'] = '%(mysql_user)s'" % env)
append('%(app_dir)s/%(django_settings_file)s' % env, "DATABASES['default']['PASSWORD'] = '%(mysql_password)s'" % env)
append('%(app_dir)s/%(django_settings_file)s' % env, "DATABASES['default']['NAME'] = '%(mysql_dbname)s'" % env)
def dj_create_superuser():
"""
Creates django superuser
"""
print(yellow('Starting >> %s()' % _fn()))
with cd(env.app_dir):
ve_run('python manage.py createsuperuser --settings=%s' % env.django_settings_module)
def dj_copy_vagrant(from_dir='/vagrant/'):
"""
Copies the files from /vagrant onto env.app_dir. Can be used as an alternate to git clone <repo>
"""
print(yellow('Starting >> %s(from:%s to:%s)' % (_fn(), from_dir, env.app_dir)))
with cd(env.app_dir):
sudo('cp -Rpf %s/* ./ ' % from_dir)
sudo('chown -R %(user)s %(app_dir)s/logs' % env)
def dj_syncdb():
"""
Initial db sync
"""
print(yellow('Starting >> %s()' % _fn()))
with cd(env.app_dir):
ve_run('python manage.py syncdb --noinput --migrate --settings=%s' % env.django_settings_module)
def dj_create_secret():
"""
Creates new SECRET_KEY everytime its run
"""
print(yellow('Starting >> %s()' % _fn()))
with cd(env.app_dir):
sudo('chown %(user)s %(app_dir)s/cito_plugin_server/settings/secret_key.py' % env)
secret_sauce = "".join([random.SystemRandom().choice("abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)") for i in range(50)])
append('%(app_dir)s/cito_plugin_server/settings/secret_key.py' % env, "SECRET_KEY = '%s'" % secret_sauce)
#################
# Helper methods
#################
def _fn():
return inspect.stack()[1][3]
def e(environment='production'):
env.environment = environment
env.django_settings_module = 'cito_plugin_server.settings.%s' % environment
env.django_settings_file = 'cito_plugin_server/settings/%s.py' % environment
print(green('django settings set to %(django_settings_module)s' % env))
return
def _get_vagrant_ssh_config():
result = local('vagrant ssh-config', capture=True)
conf = {}
for line in iter(result.splitlines()):
parts = line.split()
conf[parts[0]] = ' '.join(parts[1:])
return conf
def ve_run(command, func=run, base_dir=env.app_dir, *args, **kwargs):
with cd(base_dir):
with prefix('source %(venv_dir)s/bin/activate' % env):
return func(command, *args, **kwargs)
def _detect_pkg_manager():
print(yellow('Starting >> %s()' % _fn()))
managers = ['apt-get', 'yum', 'zypper']
with warn_only():
for v in managers:
env.pkg_manager = run('which %s' % v).stdout
print(green('Package manager is %(pkg_manager)s' % env))
return
def hostname():
print(yellow('Starting >> %s()' % _fn()))
run("uname -a")
def vagrant(user='vagrant'):
print(yellow('Starting >> %s()' % _fn()))
print(yellow('Setting user as %s' % user))
env.user = user
vagrant.config = _get_vagrant_ssh_config()
env.key_config = vagrant.config['IdentityFile']
env.hosts = ['%s:%s' % (vagrant.config['HostName'], vagrant.config['Port'])]
env.user = vagrant.config['User']