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, ""
python类settings()的实例源码
def restart_nginx():
with settings(warn_only=True):
sudo('pkill nginx')
sudo('nginx -c combine/etc/nginx.conf -p `pwd`')
def ensure_venv(name):
sudo('mkdir -p /venvs')
sudo('chmod 777 /venvs')
with settings(warn_only=True):
venv_exists = run('test -d /venvs/%s' % name).succeeded
if not venv_exists:
run('python3.6 -m venv /venvs/%s' % name)
def run_remote_command(host_string, command, timeout=NODE_COMMAND_TIMEOUT,
jsonresult=False,
catch_exitcodes=None):
"""Executes command on remote host via fabric run.
Optionally timeout may be specified.
If result of execution is expected in json format, then the output will
be treated as json.
"""
with settings(hide(NODE_STATUSES.running, 'warnings', 'stdout', 'stderr'),
host_string=host_string,
warn_only=True):
return execute_run(command, timeout=timeout, jsonresult=jsonresult,
catch_exitcodes=catch_exitcodes)
def get_monitors(self):
"""
Parse ceph config and return ceph monitors list
:return: list -> list of ip addresses
"""
if self._monitors is not None:
return self._monitors
try:
from ..settings import MONITORS
self._monitors = [i.strip() for i in MONITORS.split(',')]
except ImportError:
# We have no monitor predefined configuration
conf = self._get_client_config()
if not conf:
return
fo = StringIO(conf)
cp = ConfigParser()
try:
cp.readfp(fo)
except Exception:
raise APIError("Cannot get CEPH monitors."
" Make sure your CEPH cluster is available"
" and KuberDock is configured to use CEPH")
if not cp.has_option('global', 'mon_host'):
self._monitors = ['127.0.0.1']
else:
self._monitors = [
i.strip() for i in cp.get('global', 'mon_host').split(',')
]
return self._monitors
def get_storage_class():
"""Returns storage class according to current settings
"""
if CEPH:
return CephStorage
return LocalStorage
def create_db():
with settings(warn_only=True), hide('output', 'running'):
if env.get('settings'):
execute('servers.stop_service', 'uwsgi')
with shell_env(**app_config.database):
local('dropdb --if-exists %s' % app_config.database['PGDATABASE'])
if not env.get('settings'):
local('psql -c "DROP USER IF EXISTS %s;"' % app_config.database['PGUSER'])
local('psql -c "CREATE USER %s WITH SUPERUSER PASSWORD \'%s\';"' % (app_config.database['PGUSER'], app_config.database['PGPASSWORD']))
with shell_env(**app_config.database):
local('createdb %s' % app_config.database['PGDATABASE'])
if env.get('settings'):
execute('servers.start_service', 'uwsgi')
def create_directories():
"""
Create server directories.
"""
require('settings', provided_by=['production', 'staging'])
run('mkdir -p %(SERVER_PROJECT_PATH)s' % app_config.__dict__)
sudo('mkdir -p /etc/uwsgi')
sudo('mkdir -p /etc/uwsgi/sites')
# run('mkdir -p /var/www/uploads/%(PROJECT_FILENAME)s' % app_config.__dict__)
def create_virtualenv():
"""
Setup a server virtualenv.
"""
require('settings', provided_by=['production', 'staging'])
run('virtualenv -p %(SERVER_PYTHON)s %(SERVER_VIRTUALENV_PATH)s' % app_config.__dict__)
run('source %(SERVER_VIRTUALENV_PATH)s/bin/activate' % app_config.__dict__)
def clone_repo():
"""
Clone the source repository.
"""
require('settings', provided_by=['production', 'staging'])
run('git clone %(REPOSITORY_URL)s %(SERVER_REPOSITORY_PATH)s' % app_config.__dict__)
if app_config.REPOSITORY_ALT_URL:
run('git remote add bitbucket %(REPOSITORY_ALT_URL)s' % app_config.__dict__)
def install_requirements():
"""
Install the latest requirements.
"""
require('settings', provided_by=['production', 'staging'])
run('%(SERVER_VIRTUALENV_PATH)s/bin/pip install -U -r %(SERVER_REPOSITORY_PATH)s/requirements.txt' % app_config.__dict__)
def setup_logs():
"""
Create log directories.
"""
require('settings', provided_by=['production', 'staging'])
sudo('mkdir %(SERVER_LOG_PATH)s' % app_config.__dict__)
sudo('chown ubuntu:ubuntu %(SERVER_LOG_PATH)s' % app_config.__dict__)
def install_crontab():
"""
Install cron jobs script into cron.d.
"""
require('settings', provided_by=['production', 'staging'])
sudo('cp %(SERVER_REPOSITORY_PATH)s/cronjobs/* /etc/cron.d' % app_config.__dict__)
def uninstall_crontab():
"""
Remove a previously install cron jobs script from cron.d
"""
require('settings', provided_by=['production', 'staging'])
sudo('rm /etc/cron.d/%(PROJECT_FILENAME)s' % app_config.__dict__)
def start_service(service):
"""
Start a service on the server.
"""
require('settings', provided_by=['production', 'staging'])
service_name = _get_installed_service_name(service)
sudo('service %s start' % service_name)
def stop_service(service):
"""
Stop a service on the server
"""
require('settings', provided_by=['production', 'staging'])
service_name = _get_installed_service_name(service)
sudo('service %s stop' % service_name)
def restart_service(service):
"""
Start a service on the server.
"""
require('settings', provided_by=['production', 'staging'])
service_name = _get_installed_service_name(service)
sudo('service %s restart' % service_name)
def fabcast(command):
"""
Actually run specified commands on the server specified
by staging() or production().
"""
require('settings', provided_by=['production', 'staging'])
if not app_config.DEPLOY_TO_SERVERS:
logging.error('You must set DEPLOY_TO_SERVERS = True in your app_config.py and setup a server before fabcasting.')
run('cd %s && bash run_on_server.sh fab %s $DEPLOYMENT_TARGET %s' % (app_config.SERVER_REPOSITORY_PATH, env.branch, command))