# -*- coding: utf-8 -*- #!/usr/bin/env python """ 一系列基本快准狠运维操作 """ import os import click from fabric.api import run, cd, env from base import init_env init_env(interactive=False) env.roledefs = { 'production': [os.environ['PRODUCTION_HOST_PORT']], 'staging': [os.environ['STAGING_HOST_PORT']] } config = { 'directory': { 'production': '/var/www/UserServer', 'staging': '/var/www/UserServer', 'testing': '/var/www/UserServerTest' } } # 用法 fab -f ops.py -R production/staging host_type/restart... # 注意: 要使env.passwords生效, host格式必须是 user@ip:port 端口号一定要显式写出来,即使是使用的默认22端口 # 现在密码统一,如果以后需要改密码,直接修改此字典 格式为 'user@ip:port' : '密码' env.passwords = { env.roledefs['production'][0]: os.environ['PRODUCTION_PASSWORD'], env.roledefs['staging'][0]: os.environ['STAGING_PASSWORD'] } def host_type(): """供测试服务器是否连通""" run('uname -s') def cli(debug): click.echo('Debug mode is %s' % ('on' if debug else 'off')) def sync(): click.echo('syncing...') def restore_db(): click.echo('restoring database...') def dump_memcached(): click.echo('dumping memcached...') def restore_memcached(): click.echo('restoring database...') def restart_server(configKey='testing'): with cd(config['directory'][configKey]): run('svn up') run('supervisorctl restart userserver_test') run('supervisorctl restart managerserver_test') def gulp(configKey='testing', gulpScope='all'): """ fab -f ops.py -R staging gulp:configKey=testing,gulpScope=all :param configKey: :param gulpScope: :return: """ with cd(config['directory'][configKey]): run('svn up static') run('gulp ' + gulpScope)