Created
January 20, 2015 12:22
-
-
Save saro0h/4c48757175474e319160 to your computer and use it in GitHub Desktop.
Example fabric script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import with_statement | |
from fabric.api import * | |
from fabric.colors import * | |
from time import gmtime, strftime | |
def stage(): | |
env.hosts = [ | |
'[email protected]' | |
] | |
def production(): | |
env.hosts = [ | |
'[email protected]' | |
] | |
def backup(env='preprod'): | |
if (env == 'preprod'): | |
database_pwd = 'pwd' | |
database_host = 'localhost' | |
elif (env == prod): | |
database_pwd = 'pwd' | |
database_host = '127.0.1.2' | |
print(green('Backup application')) | |
now = strftime("%Y%m%d", gmtime()) | |
backup_name = 'backup_%(time)s' % { 'time' : now } | |
run('tar cvzf %(backup_name)s.tar.gz . --exclude .git cache %(backup_name)s.tar.gz' % { 'backup_name': backup_name }) | |
print(green('Backup database')) | |
run('mysqldump -h%(database_host)s -ulalique -p%(database_pwd)s lalique > %(backup_name)s.sql' % {'database_host' : database_host, 'database_pwd' : database_pwd, 'backup_name' : backup_name }) | |
run('mkdir backup') | |
run('mv %(backup_name)s.tar.gz backup' % {'backup_name' : backup_name}) | |
run('mv %(backup_name)s.sql backup' % {'backup_name' : backup_name}) | |
print(green('Backup done')) | |
def rollbackApp(env='preprod'): | |
if (env == 'preprod'): | |
database_pwd = 'pwd' | |
database_host = 'localhost' | |
elif (env == prod): | |
database_pwd = 'pwd' | |
database_host = '128.121.152.150' | |
backup_dir = prompt('Name of the backup tar.gz ?') | |
if backup_dir != '' : | |
print(green('Delete of all files from the last deployment')) | |
run('find . -not \( -name backup \) -exec rm -rf {} \ '); | |
print(green('Deployment of the last version')) | |
run('mv backup/* .') | |
run('tar xvfz %(backup_dir)s' % { 'backup_dir' : backup_dir }) | |
print(green('Application of good rights')) | |
clear() | |
database_file = prompt('Name of the sql file to load ?') | |
if database_file != '' : | |
print(green('/!\ DROP DATABASE /!\ ')) | |
run('./symfony doctrine:drop-db --env=%(env)s' % {'env' : env }) | |
print(green('Create database ')) | |
run('./symfony doctrine:create-db --env=%(env)s' % {'env' : env }) | |
print(green('Put Data back')) | |
run('mysql -h%(database_host)s -uuser -p%(database_pwd)s database < %(database_file)s' % {'database_file' : database_file }) | |
print(green('Rollback done')) | |
def deploy(dry_run=True, env='preprod'): | |
print(dry_run) | |
backup(env) | |
print(green('Deploy application')) | |
if (env == 'preprod'): | |
local_path = '.' | |
user = 'webdata_pre' | |
host = '127.0.0.1' | |
remote_path = '/website/' | |
local_path = '/path/to/my/app/' | |
elif (env == 'prod'): | |
local_path = '.' | |
user = 'webdata_pro' | |
host = '127.0.0.1' | |
remote_path = '/website/' | |
local_path = '/path/to/my/app/' | |
if (dry_run == True): | |
dry_run = '--dry-run' | |
else: | |
dry_run = '' | |
local('rsync -e ssh -rzvcl %(local_path)s %(user)s@%(host)s:%(remote_path)s --progress --delete --exclude-from=%(local_path)s/config/rsync_exclude.txt %(dry_run)s' % \ | |
{ 'local_path': local_path, 'user': user, 'host': host, 'remote_path': remote_path, 'dry_run': dry_run }) | |
rsync_true = prompt('Rsync without dry_run ? [y/n]') | |
if(rsync_true == 'y'): | |
local('rsync -e ssh -rzvcl %(local_path)s %(user)s@%(host)s:%(remote_path)s --progress --delete --exclude-from=%(local_path)s/config/rsync_exclude.txt %(dry_run)s' % \ | |
{ 'local_path': local_path, 'user': user, 'host': host, 'remote_path': remote_path, 'dry_run': '' }) | |
clear() | |
print(yellow('Apply migration if needed')) | |
migrate(env) | |
print(yellow('Deployment is finished')) | |
def clear(): | |
print(blue('Clear project')) | |
run('./symfony cc') | |
run('./symfony project:permissions') | |
run('chmod -R 777 /website/web/media') | |
run('./symfony project:clear-controllers') | |
def migrate(env='preprod'): | |
run('./symfony doctrine:migrate --env=%(env)s' % {'env' : env }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment