|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +__author__ = 'Michael Liao' |
| 5 | + |
| 6 | +''' |
| 7 | +Deployment toolkit. |
| 8 | +''' |
| 9 | + |
| 10 | +import os, re |
| 11 | + |
| 12 | +from datetime import datetime |
| 13 | +from fabric.api import * |
| 14 | + |
| 15 | +env.user = 'michael' |
| 16 | +env.sudo_user = 'root' |
| 17 | +env.hosts = ['192.168.0.3'] |
| 18 | + |
| 19 | +db_user = 'www-data' |
| 20 | +db_password = 'www-data' |
| 21 | + |
| 22 | +_TAR_FILE = 'dist-awesome.tar.gz' |
| 23 | + |
| 24 | +_REMOTE_TMP_TAR = '/tmp/%s' % _TAR_FILE |
| 25 | + |
| 26 | +_REMOTE_BASE_DIR = '/srv/awesome' |
| 27 | + |
| 28 | +def _current_path(): |
| 29 | + return os.path.abspath('.') |
| 30 | + |
| 31 | +def _now(): |
| 32 | + return datetime.now().strftime('%y-%m-%d_%H.%M.%S') |
| 33 | + |
| 34 | +def backup(): |
| 35 | + ''' |
| 36 | + Dump entire database on server and backup to local. |
| 37 | + ''' |
| 38 | + dt = _now() |
| 39 | + f = 'backup-awesome-%s.sql' % dt |
| 40 | + with cd('/tmp'): |
| 41 | + run('mysqldump --user=%s --password=%s --skip-opt --add-drop-table --default-character-set=utf8 --quick awesome > %s' % (db_user, db_password, f)) |
| 42 | + run('tar -czvf %s.tar.gz %s' % (f, f)) |
| 43 | + get('%s.tar.gz' % f, '%s/backup/' % _current_path()) |
| 44 | + run('rm -f %s' % f) |
| 45 | + run('rm -f %s.tar.gz' % f) |
| 46 | + |
| 47 | +def build(): |
| 48 | + ''' |
| 49 | + Build dist package. |
| 50 | + ''' |
| 51 | + includes = ['static', 'templates', 'transwarp', 'favicon.ico', '*.py'] |
| 52 | + excludes = ['test', '.*', '*.pyc', '*.pyo'] |
| 53 | + local('rm -f dist/%s' % _TAR_FILE) |
| 54 | + with lcd(os.path.join(_current_path(), 'www')): |
| 55 | + cmd = ['tar', '--dereference', '-czvf', '../dist/%s' % _TAR_FILE] |
| 56 | + cmd.extend(['--exclude=\'%s\'' % ex for ex in excludes]) |
| 57 | + cmd.extend(includes) |
| 58 | + local(' '.join(cmd)) |
| 59 | + |
| 60 | +def deploy(): |
| 61 | + newdir = 'www-%s' % _now() |
| 62 | + run('rm -f %s' % _REMOTE_TMP_TAR) |
| 63 | + put('dist/%s' % _TAR_FILE, _REMOTE_TMP_TAR) |
| 64 | + with cd(_REMOTE_BASE_DIR): |
| 65 | + sudo('mkdir %s' % newdir) |
| 66 | + with cd('%s/%s' % (_REMOTE_BASE_DIR, newdir)): |
| 67 | + sudo('tar -xzvf %s' % _REMOTE_TMP_TAR) |
| 68 | + with cd(_REMOTE_BASE_DIR): |
| 69 | + sudo('rm -f www') |
| 70 | + sudo('ln -s %s www' % newdir) |
| 71 | + sudo('chown www-data:www-data www') |
| 72 | + sudo('chown -R www-data:www-data %s' % newdir) |
| 73 | + with settings(warn_only=True): |
| 74 | + sudo('supervisorctl stop awesome') |
| 75 | + sudo('supervisorctl start awesome') |
| 76 | + sudo('/etc/init.d/nginx reload') |
| 77 | + |
| 78 | +RE_FILES = re.compile('\r?\n') |
| 79 | + |
| 80 | +def rollback(): |
| 81 | + ''' |
| 82 | + rollback to previous version |
| 83 | + ''' |
| 84 | + with cd(_REMOTE_BASE_DIR): |
| 85 | + r = run('ls -p -1') |
| 86 | + files = [s[:-1] for s in RE_FILES.split(r) if s.startswith('www-') and s.endswith('/')] |
| 87 | + files.sort(cmp=lambda s1, s2: 1 if s1 < s2 else -1) |
| 88 | + r = run('ls -l www') |
| 89 | + ss = r.split(' -> ') |
| 90 | + if len(ss) != 2: |
| 91 | + print ('ERROR: \'www\' is not a symbol link.') |
| 92 | + return |
| 93 | + current = ss[1] |
| 94 | + print ('Found current symbol link points to: %s\n' % current) |
| 95 | + try: |
| 96 | + index = files.index(current) |
| 97 | + except ValueError, e: |
| 98 | + print ('ERROR: symbol link is invalid.') |
| 99 | + return |
| 100 | + if len(files) == index + 1: |
| 101 | + print ('ERROR: already the oldest version.') |
| 102 | + old = files[index + 1] |
| 103 | + print ('==================================================') |
| 104 | + for f in files: |
| 105 | + if f == current: |
| 106 | + print (' Current ---> %s' % current) |
| 107 | + elif f == old: |
| 108 | + print (' Rollback to ---> %s' % old) |
| 109 | + else: |
| 110 | + print (' %s' % f) |
| 111 | + print ('==================================================') |
| 112 | + print ('') |
| 113 | + yn = raw_input ('continue? y/N ') |
| 114 | + if yn != 'y' and yn != 'Y': |
| 115 | + print ('Rollback cancelled.') |
| 116 | + return |
| 117 | + print ('Start rollback...') |
| 118 | + sudo('rm -f www') |
| 119 | + sudo('ln -s %s www' % old) |
| 120 | + sudo('chown www-data:www-data www') |
| 121 | + with settings(warn_only=True): |
| 122 | + sudo('supervisorctl stop awesome') |
| 123 | + sudo('supervisorctl start awesome') |
| 124 | + sudo('/etc/init.d/nginx reload') |
| 125 | + print ('ROLLBACKED OK.') |
| 126 | + |
| 127 | +def restore2local(): |
| 128 | + ''' |
| 129 | + Restore db to local |
| 130 | + ''' |
| 131 | + backup_dir = os.path.join(_current_path(), 'backup') |
| 132 | + fs = os.listdir(backup_dir) |
| 133 | + files = [f for f in fs if f.startswith('backup-') and f.endswith('.sql.tar.gz')] |
| 134 | + files.sort(cmp=lambda s1, s2: 1 if s1 < s2 else -1) |
| 135 | + if len(files)==0: |
| 136 | + print 'No backup files found.' |
| 137 | + return |
| 138 | + print ('Found %s backup files:' % len(files)) |
| 139 | + print ('==================================================') |
| 140 | + n = 0 |
| 141 | + for f in files: |
| 142 | + print ('%s: %s' % (n, f)) |
| 143 | + n = n + 1 |
| 144 | + print ('==================================================') |
| 145 | + print ('') |
| 146 | + try: |
| 147 | + num = int(raw_input ('Restore file: ')) |
| 148 | + except ValueError: |
| 149 | + print ('Invalid file number.') |
| 150 | + return |
| 151 | + restore_file = files[num] |
| 152 | + yn = raw_input('Restore file %s: %s? y/N ' % (num, restore_file)) |
| 153 | + if yn != 'y' and yn != 'Y': |
| 154 | + print ('Restore cancelled.') |
| 155 | + return |
| 156 | + print ('Start restore to local database...') |
| 157 | + p = raw_input('Input mysql root password: ') |
| 158 | + sqls = [ |
| 159 | + 'drop database if exists awesome;', |
| 160 | + 'create database awesome;', |
| 161 | + 'grant select, insert, update, delete on awesome.* to \'%s\'@\'localhost\' identified by \'%s\';' % (db_user, db_password) |
| 162 | + ] |
| 163 | + for sql in sqls: |
| 164 | + local(r'mysql -uroot -p%s -e "%s"' % (p, sql)) |
| 165 | + with lcd(backup_dir): |
| 166 | + local('tar zxvf %s' % restore_file) |
| 167 | + local(r'mysql -uroot -p%s awesome < backup/%s' % (p, restore_file[:-7])) |
| 168 | + with lcd(backup_dir): |
| 169 | + local('rm -f %s' % restore_file[:-7]) |
0 commit comments