/
fabfile.py
332 lines (232 loc) · 7.91 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import pip
import subprocess
'''
@author: Bilal Ahmad
@description: I created this tool to make the dev of py based projects easier.
@requirements: requires fabric, fabric-virtualenv, (maybe) paramiko=1.15.2
'''
def subprocess_cmd(cmd):
"""
Runs a local command on the os e.g. cd ~/Desktop
"""
subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read()
def get_os_distributions():
"""
Retrieves list of installed packages (pip)
"""
installed_dist = pip.get_installed_distributions()
flat_installed_dist = [dist.project_name for dist in installed_dist]
return flat_installed_dist
def check_fab_requirements():
"""
Checks requirements for fabric and fabric virtualenv
"""
dists = ''
try:
dists = get_os_distributions()
except ImportError:
print "Check if pip is installed"
raise
try:
if not 'Fabric' in dists or not 'fab' in dists:
subprocess_cmd('pip install fabric')
if not 'fabric-virtualenv':
subprocess_cmd('pip install fabric-virtualenv')
except OSError:
print "Print could not install fabric and fabric-virtualenv"
raise
import os.path
import getpass
from fabric.api import *
try:
import psycopg2
except:
subprocess_cmd("sudo pip install psycopg2")
try:
from fabvenv import virtualenv
except:
subprocess_cmd("sudo pip install fabric-virtualenv")
home = os.path.expanduser('~')
user = getpass.getuser()
virtualenv_dir_name = 'workspace'
virtualenv_dir = os.path.join(home, virtualenv_dir_name)
virtualenv_name = 'env_fifo'
virtualenv_path = os.path.join(virtualenv_dir, virtualenv_name)
#github info
git_addr = 'https://github.com/username/fifo.li.git'
git_branch = 'master'
#db info
db_name = 'fifodb'
db_user = 'fifouser'
db_password = ''
db_host = '127.0.0.1'
db_port = '5432'
#django project info
project_root = os.path.dirname(os.path.abspath(__file__))
manage_path = os.path.join(project_root, 'manage.py')
env.hosts = ['user@localhost']
# local command
def local_command(cmd):
local(cmd)
# virtual_env command
def env_command(cmd, env):
with virtualenv(env):
local(cmd)
def create_virtualenv(dir=virtualenv_dir, name=virtualenv_name):
"""
Creates virtualenv directory if it does not exist
Then creates the virtualenv
:param name: name of virtualenv
:return: None
"""
# try create virtualenv directory
try:
os.makedirs(dir)
except OSError:
if not os.path.isdir(dir):
print "The directory is a file, please rename that or change this."
raise
path = os.path.join(dir, name)
if not os.path.isfile(path):
local_command('virtualenv '+path)
def set_db():
conn = psycopg2.connect(database=db_name, user=db_user)
cur = conn.cursor()
def install_postgresql():
local_command('brew install postgresql')
local_command('export PATH="/usr/local/Cellar/postgresql/9.4.4/bin:$PATH"')
def install_requirements(name='local.txt'):
env_command('pip isntall -r requirements/'+name)
def create_db(name='fifodb'):
local_command('createdb name')
########### git ###########
# git clone
def git_clone(branch=git_branch, folder=git_branch):
local_command('git clone -b '+branch+' '+git_addr+' '+remote_home+'/'+folder)
# git add
def git_add():
local_command('git add -A')
# git commit
def git_commit(msg):
local_command('git commit -am '+msg)
# git status
def git_status():
local_command('git status')
# git new branch
def git_branchout(branch="new_branch"):
local_command('git branch '+branch+'')
local_command('git checkout '+branch+'')
local_command('git branch')
# git merge
def git_merge(branch="new_branch"):
local_command('git checkout master')
local_command('git merge '+branch)
# git pull
def git_pull():
local_command('git pull')
def server(c9=True, ip='127.0.0.1', port='8000'):
if c9:
print "https://<workspace>-<username>.c9users.io/"
if c9:
local_command('python manage.py runserver_plus 0.0.0.0:8080')
else:
local_command('python manage.py runserver_plus '+ip+':'+port)
def migrate():
local_command('python manage.py migrate')
def collectstatic():
local_command('python manage.py collectstatic')
def startapp(name, path='fifo/'):
app_path = project_root+'/'+path+name
try:
local_command('mkdir '+app_path)
local_command('python manage.py startapp '+name+' '+app_path)
local_command('cp '+project_root+'/app_templates/* '+app_path+'/')
except:
print "Could not create app "+name
"""
PostgreSQL users and databases
==============================
This module provides tools for creating PostgreSQL users and databases.
"""
from fabric.api import cd, hide, sudo, settings
def _run_as_pg(command):
"""
Run command as 'postgres' user
"""
with cd('~postgres'):
return sudo(command, user='postgres')
def user_exists(name):
"""
Check if a PostgreSQL user exists.
"""
with settings(hide('running', 'stdout', 'stderr', 'warnings'),
warn_only=True):
res = _run_as_pg('''psql -t -A -c "SELECT COUNT(*) FROM pg_user WHERE usename = '%(name)s';"''' % locals())
return (res == "1")
def create_user(name, password, superuser=False, createdb=False,
createrole=False, inherit=True, login=True,
connection_limit=None, encrypted_password=False):
"""
Create a PostgreSQL user.
Example::
import fabtools
# Create DB user if it does not exist
if not fabtools.postgres.user_exists('dbuser'):
fabtools.postgres.create_user('dbuser', password='somerandomstring')
# Create DB user with custom options
fabtools.postgres.create_user('dbuser2', password='s3cr3t',
createdb=True, createrole=True, connection_limit=20)
"""
options = [
'SUPERUSER' if superuser else 'NOSUPERUSER',
'CREATEDB' if createdb else 'NOCREATEDB',
'CREATEROLE' if createrole else 'NOCREATEROLE',
'INHERIT' if inherit else 'NOINHERIT',
'LOGIN' if login else 'NOLOGIN',
]
if connection_limit is not None:
options.append('CONNECTION LIMIT %d' % connection_limit)
password_type = 'ENCRYPTED' if encrypted_password else 'UNENCRYPTED'
options.append("%s PASSWORD '%s'" % (password_type, password))
options = ' '.join(options)
_run_as_pg('''psql -c "CREATE USER %(name)s %(options)s;"''' % locals())
def drop_user(name):
"""
Drop a PostgreSQL user.
Example::
import fabtools
# Remove DB user if it exists
if fabtools.postgres.user_exists('dbuser'):
fabtools.postgres.drop_user('dbuser')
"""
_run_as_pg('''psql -c "DROP USER %(name)s;"''' % locals())
def database_exists(name):
"""
Check if a PostgreSQL database exists.
"""
with settings(hide('running', 'stdout', 'stderr', 'warnings'),
warn_only=True):
return _run_as_pg('''psql -d %(name)s -c ""''' % locals()).succeeded
def create_database(name, owner, template='template0', encoding='UTF8',
locale='en_US.UTF-8'):
"""
Create a PostgreSQL database.
Example::
import fabtools
# Create DB if it does not exist
if not fabtools.postgres.database_exists('myapp'):
fabtools.postgres.create_database('myapp', owner='dbuser')
"""
_run_as_pg('''createdb --owner %(owner)s --template %(template)s \
--encoding=%(encoding)s --lc-ctype=%(locale)s \
--lc-collate=%(locale)s %(name)s''' % locals())
def drop_database(name):
"""
Delete a PostgreSQL database.
Example::
import fabtools
# Remove DB if it exists
if fabtools.postgres.database_exists('myapp'):
fabtools.postgres.drop_database('myapp')
"""
_run_as_pg('''dropdb %(name)s''' % locals())