def run(command, remote=True):
''' Run a command using fabric. '''
if remote:
return _run(command)
else:
return _local(command)
python类local()的实例源码
def localhost():
""" local server """
env.srvr = 'local'
env.path = os.path.dirname(os.path.realpath(__file__))
env.within_virtualenv = 'workon dprr'
env.hosts = [gethostname()]
env.user = getuser()
def collect_static(process=False):
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
if env.srvr in ['local', 'vagrant']:
print(yellow('Do not run collect_static on local servers'))
return
with cd(env.path), prefix(env.within_virtualenv):
run('./manage.py collectstatic {process} --noinput'.format(
process=('--no-post-process' if not process else '')))
def own_django_log():
""" make sure logs/django.log is owned by www-data """
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
if env.srvr in ['local', 'vagrant']:
print(yellow('Do not change ownership of django log on local servers'))
return
sudo(
'chown www-data:www-data {}'.format(
os.path.join(env.path, 'logs', 'django.log')))
def zfs_unmount(self, target):
cmd = '/sbin/zfs unmount {}'.format(target)
logger.debug('Unmounting {}'.format(target))
logger.debug('Executing: {}'.format(cmd))
if local(cmd).return_code != 0:
raise SnapshotException('Failed to unmount filesystem')
def zfs_promote(self, target):
cmd = '/sbin/zfs promote {target}'.format(target=target)
logger.debug('Promoting {}'.format(target))
logger.debug('Executing: {}'.format(cmd))
if local(cmd).return_code != 0:
raise SnapshotException('Failed to promote {}'.format(target))
def zfs_rename(self, source, target):
cmd = '/sbin/zfs rename {source} {target}'.format(
source=source,
target=target
)
logger.debug('Renaming {source} to {target}'.format(source=source, target=target))
logger.debug('Executing: {}'.format(cmd))
if local(cmd).return_code != 0:
raise SnapshotException('Failed to clone snapshot')
def zfs_destroy(self, target):
cmd = '/sbin/zfs destroy -r {target}'.format(target=target)
logger.debug('Destroying {}'.format(target))
logger.debug('Executing: {}'.format(cmd))
if local(cmd).return_code != 0:
raise SnapshotException('Failed to destroy {}'.format(target))
def zfs_snapshot_list(self, target):
cmd = '/sbin/zfs get -r -H -o value name -t snapshot {target}'.format(target=target)
logger.debug("Executing {}".format(cmd))
result = local(cmd, capture=True)
if result.return_code != 0:
raise SnapshotException('Failed to list snapshots')
return result.split('\n')
def start_restore(self, *args, **kwargs):
cmd = local("fuser -k -9 -m /$(zfs get -H -o value mountpoint {})".format(self.filesystem))
logger.debug(cmd)
return True, ''
def stop_mysql(self):
with settings(hide('running', 'stdout')):
result = local('service mysql stop')
return result.return_code == 0, "stop_mysql"
def failover(self, *args, **kwargs):
cred_file = self.config.get('failover_creds', '/etc/mysql/failover.cnf')
master = kwargs.get('master_host')
if not master:
return False, "No master_host given"
with settings(hide('running')):
return local("/usr/bin/mysqlmaster.py switch --new-master {} --defaults-extra-file={} "
"--dead-master --assume-yes".format(master, cred_file)).return_code == 0, ""
def ssh():
"""Ssh to a given server"""
require('environment')
local("ssh %s" % env.hosts[0])
def make_messages():
"""Extract English text from code and templates, and update the
.po files for translators to translate"""
# Make sure gettext is installed
local("gettext --help >/dev/null 2>&1")
if os.path.exists("locale/fr/LC_MESSAGES/django.po"):
local("python manage.py makemessages -a")
else:
local("python manage.py makemessages -l fr")
def compile_messages():
"""Compile the translated .po files into more efficient .mo
files for runtime use"""
# Make sure gettext is installed
local("gettext --help >/dev/null 2>&1")
local("python manage.py compilemessages")
def todo(*args):
"""List the TODOs and FIXMEs in the code and documentation."""
with lcd(_relative_to_fabfile()):
local(
'grin -e ".pyc,.pyo" "FIXME|TODO" *')
local(
'grind -0 \'*.feature\' | '
'grin -I \'*.feature\' -0 -f - "FIXME|TODO"')
def isort():
"""Use isort to automatically (re-)order the import statements on the top of files"""
with lcd(_relative_to_fabfile()):
local('isort **/*.py')
def clean():
"""Remove all generated files (.pyc, .coverage, .egg, etc)."""
with lcd(_relative_to_fabfile()):
local('find -name "*.pyc" | xargs rm -f')
local('find -name .coverage | xargs rm -f')
local('find -name .DS_Store | xargs rm -f') # Created by OSX
local('find -name ._DS_Store | xargs rm -f') # Created by OSX
local('find -name "._*.*" | xargs rm -f') # E.g. created by Caret
local('rm -f .coverage.*')
local('rm -rf build')
local('rm -rf dist')