ops.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # -*- coding: utf-8 -*-
  2. #!/usr/bin/env python
  3. """
  4. 一系列基本快准狠运维操作
  5. """
  6. import os
  7. import click
  8. from fabric.api import run, cd, env
  9. from base import init_env
  10. init_env(interactive=False)
  11. env.roledefs = {
  12. 'production': [os.environ['PRODUCTION_HOST_PORT']],
  13. 'staging': [os.environ['STAGING_HOST_PORT']]
  14. }
  15. config = {
  16. 'directory': {
  17. 'production': '/var/www/UserServer',
  18. 'staging': '/var/www/UserServer',
  19. 'testing': '/var/www/UserServerTest'
  20. }
  21. }
  22. # 用法 fab -f ops.py -R production/staging host_type/restart...
  23. # 注意: 要使env.passwords生效, host格式必须是 user@ip:port 端口号一定要显式写出来,即使是使用的默认22端口
  24. # 现在密码统一,如果以后需要改密码,直接修改此字典 格式为 'user@ip:port' : '密码'
  25. env.passwords = {
  26. env.roledefs['production'][0]: os.environ['PRODUCTION_PASSWORD'],
  27. env.roledefs['staging'][0]: os.environ['STAGING_PASSWORD']
  28. }
  29. def host_type():
  30. """供测试服务器是否连通"""
  31. run('uname -s')
  32. def cli(debug):
  33. click.echo('Debug mode is %s' % ('on' if debug else 'off'))
  34. def sync():
  35. click.echo('syncing...')
  36. def restore_db():
  37. click.echo('restoring database...')
  38. def dump_memcached():
  39. click.echo('dumping memcached...')
  40. def restore_memcached():
  41. click.echo('restoring database...')
  42. def restart_server(configKey='testing'):
  43. with cd(config['directory'][configKey]):
  44. run('svn up')
  45. run('supervisorctl restart userserver_test')
  46. run('supervisorctl restart managerserver_test')
  47. def gulp(configKey='testing', gulpScope='all'):
  48. """
  49. fab -f ops.py -R staging gulp:configKey=testing,gulpScope=all
  50. :param configKey:
  51. :param gulpScope:
  52. :return:
  53. """
  54. with cd(config['directory'][configKey]):
  55. run('svn up static')
  56. run('gulp ' + gulpScope)