-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsymfony-fabfile.py
309 lines (268 loc) · 9.57 KB
/
symfony-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
from fabric.api import *
from fabric.colors import green, red
from fabric.contrib.console import confirm
import os
from ConfigParser import ConfigParser
import yaml
import re
import getpass
# define roles host
env.roledefs = {
'test': ['host'],
'prod': ['host'],
}
# define ssh keys
home = os.getenv("HOME")
keys = [home + '/.ssh/run-deploy', home + '/.ssh/id_rsa']
env.key_filename = [key for key in keys if os.access(key, os.R_OK)]
@task
def install(interactive=True, tag=None, config_file='config/properties.ini'):
"""
Installs the project
"""
copy_sample(config_file)
config = parse_config(config_file)
#print(config.get('symfony', 'name'))
print(green('Installation of %s' % config.get('symfony','name'), True))
if env.host is not None:
print('Installing the project...')
delete('.*', 'rf')
delete('.git*', 'rf')
git_clone(config.get('symfony', 'repository'))
configure_db(config, interactive)
create_db(config, interactive)
symfony_install(config)
print(green('Installation done.', True))
@task
@roles('test')
def deploy(tag=None, install=False):
"""
Deploys the project
"""
if tag is None:
tag = get_last_tag()
print(green('Deploying tag "%s"' % tag))
# Connect to the remote server
with cd(get_remote_path()):
if install is not False:
install(tag=tag, interactive=True)
# update the project version
git_fetch()
git_checkout(tag)
git_submodule_update()
if install is False:
symfony_install()
print(green('Installation done.', True))
@task
def rebuild():
"""
Clean the cache, rebuild and publish the assets of the project
"""
symfony_clear_cache()
symfony_build(only_classes=False)
symfony_clean_model_files()
symfony_publish_assets()
@task
def reset_test_data(config_file='config/properties.ini'):
"""
Resets the test data of the project
"""
copy_sample(config_file)
config = parse_config(config_file)
create_db(config)
symfony_build()
symfony_clear_cache()
@task
def run_tests():
"""
Launches all the PHPunit tests
"""
reset_test_data()
do('phpunit')
def configure_db(config, interactive=False):
"""
Configure the database configuration list.
"""
# Read the databases.yml.sample to get the default values
db_config = yaml.load(open(config.get('samples','database')+'.sample', 'r'))
config.add_section('database')
config.add_section('database_default')
config.set('database_default', 'username', db_config['all']['doctrine']['param']['username'])
config.set('database_default', 'password', db_config['all']['doctrine']['param']['password'])
config.set('database_default', 'name', re.split("[;=]", db_config['all']['doctrine']['param']['dsn']).pop())
config.set('database_default', 'name_test', re.split("[;=]", db_config['test']['doctrine']['param']['dsn']).pop())
if interactive is True :
config.set('database', 'username', prompt('Mysql user:', default=config.get('database_default', 'username')))
config.set('database', 'password', getpass.getpass('Mysql user password: [%s]' % config.get('database_default', 'password')))
config.set('database', 'name', prompt('Mysql database name:', default=config.get('database_default', 'name')))
config.set('database', 'name_test', prompt('Mysql test database name:', default=config.get('database_default', 'name_test')))
else:
# Copy default values in the database section if not interactive.
for key, value in config.items('database_default'):
config.set('database', key, value)
def create_db(config, interactive=False):
"""
Recreates the tables and generates the databases.yml file
"""
# Creating user
print(green('Creating database user'))
sql = """GRANT USAGE ON * . * TO '#db.user#'@'localhost' IDENTIFIED BY '#db.password#';
CREATE DATABASE IF NOT EXISTS `#db.name#` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
GRANT ALL PRIVILEGES ON `#db.name#` . * TO '#db.user#'@'localhost';
CREATE DATABASE IF NOT EXISTS `#db.name_test#` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
GRANT ALL PRIVILEGES ON `#db.name_test#` . * TO '#db.user#'@'localhost';
FLUSH PRIVILEGES;"""
sql = sql.replace('#db.user#', config.get('database', 'username'))
sql = sql.replace('#db.password#', config.get('database', 'password'))
sql = sql.replace('#db.name#', config.get('database', 'name'))
sql = sql.replace('#db.name_test#', config.get('database', 'name_test'))
query_file_name = 'config/create_dbs.sql';
query_file = open(query_file_name,'w')
query_file.write(sql);
query_file.close();
with settings(warn_only=True):
if interactive is True:
print green('We need a sql user with root permissions to execute the user and database creation.')
username = prompt('Mysql user with root permissions:', default=config.get('database_default', 'username'))
password = getpass.getpass('Password : [%s]' % config.get('database_default', 'password'))
result = sql_load(user=config.get('database', 'username'), password=config.get('database', 'password'), sql=query_file_name)
if result.failed:
print(red('User creation failed', True))
# Generating database file
print(green('Generating database.yml file'))
db_config = yaml.load(open(config.get('samples','database')+'.sample', 'r'))
db_config['all']['doctrine']['param']['username'] = config.get('database', 'username')
db_config['all']['doctrine']['param']['username'] = config.get('database', 'password')
db_config['all']['doctrine']['param']['dsn'] = db_config['all']['doctrine']['param']['dsn'].replace(config.get('database_default', 'name'), config.get('database', 'name'))
db_config['test']['doctrine']['param']['dsn'] = db_config['test']['doctrine']['param']['dsn'].replace(config.get('database_default', 'name_test'), config.get('database', 'name_test'))
yaml.dump(db_config, open(config.get('samples','database'), 'w'), indent=2, default_flow_style=False)
def symfony_install(config):
"""
Creates symbolic links / checkout symfony's SVN, build classes, clear cache and publish assets
"""
if not os.path.exists('lib/vendor/symfony'):
if env.host is None:
local('cd lib/vendor && ln -s %s symfony' % config.get('symfony', 'dir'))
else:
run('svn co http://svn.symfony-project.com/branches/1.4 lib/vendor/symfony')
symfony_build()
symfony_clear_cache()
symfony_publish_assets()
def get_role():
"""
Return the current role
"""
if env.host in env.roledefs['test']:
return 'test'
else:
return 'prod'
def get_last_tag():
"""
Return the latest tag
"""
git_fetch()
return local('git tag -l | sort | tail -n1', True)
def get_remote_path():
"""
Return the path to the remote server
"""
return path[get_role()]
def copy_sample(file):
"""
Checks if the file exists, if not, creates it from the <file>.sample file
"""
if not os.path.exists(file):
if not os.path.exists(file+'.sample'):
abort(red(file+'.sample does not exists',True))
copy(file+'.sample', file)
else:
print(green(file+' already exists',True))
def parse_config(file):
"""
Reads the config file with the ConfigParser module
"""
config = ConfigParser()
config.read(file)
return config
def do(*args, **kwargs):
"""
Execute a command with the right method
as the env is local or remote
"""
if env.host is None:
return local(*args, **kwargs)
else:
return run(*args, **kwargs)
def go(*args, **kwargs):
"""
Execute cd or lcd depending of the host.
"""
if env.host is None:
lcd(*args, **kwargs)
else:
cd(*args, **kwargs)
def symfony_clear_cache(hard=False):
"""
Clear the symfony cache
"""
if hard is True:
do('rm -rf cache/*')
else:
do('php symfony cc')
def symfony_build(only_classes=True, load=False, env='all'):
"""
Run doctrine build command
"""
options = ['--all-classes' if only_classes is True else '--all']
if load is True:
options.append('--and-load')
options.append('--env=\'%s\'' % env)
options.append('--no-confirmation')
options.append('--quiet')
do('php symfony doctrine:build %s' % ' '.join(options))
def symfony_publish_assets():
"""
Publish assets
"""
do('php symfony plugin:publish-assets')
def symfony_clean_model_files():
"""
Clean model fils that no longer exists in YAML schema
"""
do('php symfony doctrine:clean-model-files')
def git_clone(repository, path='.'):
"""
Clone a repository in a specific path
"""
do('git clone %s %s' % (repository, path))
def git_fetch():
"""
Fetch a repository
"""
do('git fetch --all')
def git_checkout(commit):
"""
Checkout a commit, branch or a tag
"""
do('git checkout -q %s' % commit)
def git_submodule_update():
"""
Update submodules
"""
do('git submodule update --init --recursive --quiet')
def sql_load(user, sql, password=''):
"""
Load an sql file.
"""
return do('mysql -u%s -p%s < %s' % (user, password, sql), capture=True)
def copy(source, target):
"""
Copy a file or a folder in a target
"""
do('cp %s %s' % (source, target))
def delete(path, options=''):
"""
Delete a file or a directory.
Add options to the rm command (rf for instance)
"""
do('rm %s %s' % (options, path))