fabfile.py 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. from fabric.api import cd, run, sudo, put
  2. from cStringIO import StringIO
  3. base_dir = '/usr/local'
  4. hostname = 'searx.me'
  5. searx_dir = base_dir + '/searx'
  6. searx_ve_dir = searx_dir + '/searx-ve'
  7. current_user = run('whoami').stdout.strip()
  8. uwsgi_file = '''
  9. [uwsgi]
  10. # Who will run the code
  11. uid = {user}
  12. gid = {user}
  13. # Number of workers
  14. workers = 8
  15. # The right granted on the created socket
  16. chmod-socket = 666
  17. # Plugin to use and interpretor config
  18. single-interpreter = true
  19. master = true
  20. plugin = python
  21. # Module to import
  22. module = searx.webapp
  23. # Virtualenv and python path
  24. virtualenv = {searx_ve_dir}
  25. pythonpath = {searx_dir}
  26. chdir = {searx_dir}/searx
  27. '''.format(user=current_user,
  28. searx_dir=searx_dir,
  29. searx_ve_dir=searx_ve_dir)
  30. nginx_config = '''
  31. server {{
  32. listen 80;
  33. server_name {hostname};
  34. server_name www.{hostname};
  35. root /usr/local/searx;
  36. location / {{
  37. include uwsgi_params;
  38. uwsgi_pass unix:/run/uwsgi/app/searx/socket;
  39. }}
  40. }}
  41. '''.format(hostname=hostname)
  42. def stop():
  43. sudo('/etc/init.d/uwsgi stop')
  44. def start():
  45. sudo('/etc/init.d/uwsgi start')
  46. def restart():
  47. sudo('/etc/init.d/uwsgi restart')
  48. def init():
  49. if not run('test -d ' + searx_dir, warn_only=True).failed:
  50. return
  51. sudo('apt-get update')
  52. sudo('apt-get install git'
  53. ' build-essential'
  54. ' libxslt-dev'
  55. ' python-dev'
  56. ' python-virtualenv'
  57. ' python-pybabel'
  58. ' zlib1g-dev'
  59. ' uwsgi'
  60. ' uwsgi-plugin-python'
  61. ' nginx')
  62. sudo('mkdir -p ' + base_dir)
  63. put(StringIO(nginx_config), '/etc/nginx/sites-enabled/searx', use_sudo=True)
  64. sudo('/etc/init.d/nginx restart')
  65. with cd(base_dir):
  66. sudo('git clone https://github.com/asciimoo/searx')
  67. sudo('chown -R {user}:{user} {searx_dir}'.format(user=current_user, searx_dir=searx_dir))
  68. put(StringIO(uwsgi_file), searx_dir+'/uwsgi.ini')
  69. sudo('ln -s {0}/uwsgi.ini /etc/uwsgi/apps-enabled/searx.ini'.format(searx_dir))
  70. run('virtualenv {0}'.format(searx_ve_dir))
  71. with cd(searx_dir):
  72. run('source {0}/bin/activate && pip install -r requirements.txt'.format(searx_ve_dir))
  73. start()
  74. def deploy():
  75. init()
  76. with cd(searx_dir):
  77. run("git stash", warn_only=True)
  78. run("git pull origin master")
  79. run("git stash pop", warn_only=True)
  80. restart()
  81. def clean():
  82. sudo('rm -rf {searx_dir}'.format(searx_dir=searx_dir), warn_only=True)
  83. sudo('rm /etc/uwsgi/apps-enabled/searx.ini', warn_only=True)
  84. sudo('rm /etc/nginx/sites-enabled/searx', warn_only=True)