Skip to content

Commit

Permalink
Activate virtualenv in production Python code.
Browse files Browse the repository at this point in the history
The manage.py change effectively switches the Zulip production server
to use the virtualenv, since all of our supervisord commands for the
various Python services go through manage.py.

Additionally, this migrates the production scripts and Nagios plugins
to use the virtualenv as well.
  • Loading branch information
sharmaeklavya2 authored and timabbott committed Jun 28, 2016
1 parent 64affb8 commit a9835c0
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 7 deletions.
8 changes: 6 additions & 2 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
import logging
import subprocess

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(BASE_DIR)
import scripts.lib.setup_path_on_import

if __name__ == "__main__":
if 'posix' in os.name and os.geteuid() == 0:
from django.core.management.base import CommandError
raise CommandError("manage.py should not be run as root.")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "zproject.settings")
os.environ.setdefault("PYTHONSTARTUP", os.path.join(os.path.dirname(__file__), "scripts/lib/pythonrc.py"))
os.environ.setdefault("PYTHONSTARTUP", os.path.join(BASE_DIR, "scripts/lib/pythonrc.py"))

from django.conf import settings

logger = logging.getLogger("zulip.management")
subprocess.check_call([os.path.join(os.path.dirname(__file__), "scripts", "lib", "log-management-command"),
subprocess.check_call([os.path.join(BASE_DIR, "scripts", "lib", "log-management-command"),
" ".join(sys.argv)])

if "--no-traceback" not in sys.argv and len(sys.argv) > 1:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ from __future__ import print_function

import sys
sys.path.append('/home/zulip/deployments/current')
import scripts.lib.setup_path_on_import

from zproject import settings

import glob
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import random
import traceback
import os

sys.path.append('/home/zulip/deployments/current')
import scripts.lib.setup_path_on_import

import django

def total_seconds(timedelta):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ Nagios plugin to check the length of the FTS update log.
"""
from __future__ import print_function

import sys
sys.path.append('/home/zulip/deployments/current')
import scripts.lib.setup_path_on_import

import psycopg2

states = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
#!/usr/bin/env python

from __future__ import print_function
import dateutil.parser
import pytz
import subprocess
from datetime import datetime, timedelta

import sys
sys.path.append('/home/zulip/deployments/current')
import scripts.lib.setup_path_on_import

import dateutil.parser
import pytz

states = {
"OK": 0,
"WARNING": 1,
Expand Down
14 changes: 14 additions & 0 deletions puppet/zulip/files/postgresql/process_fts_updates
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@
# search column search_tsvector in the main zerver_message.
from __future__ import print_function

import sys

# We want to use a virtualenv in production, which will be in /home/zulip/deployments/current.
# So we should add that path to sys.path and then import scripts.lib.setup_path_on_import.
# But this file is also used in development, where the above path will not exist.
# So `import scripts.lib.setup_path_on_import` will raise an ImportError.
# In development, we just want to skip this step since we know that virtualenv will already be in use.
# So catch the ImportError and do nothing.
sys.path.append('/home/zulip/deployments/current')
try:
import scripts.lib.setup_path_on_import
except ImportError:
pass

import psycopg2
import psycopg2.extensions
import select
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import datetime
import os
import sys

import sys
sys.path.append('/home/zulip/deployments/current')
import scripts.lib.setup_path_on_import

import django

os.environ['DJANGO_SETTINGS_MODULE'] = "zproject.settings"
Expand Down
5 changes: 5 additions & 0 deletions puppet/zulip_internal/files/postgresql/pg_backup_and_purge.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#!/usr/bin/env python

from __future__ import print_function

import sys
sys.path.append('/home/zulip/deployments/current')
import scripts.lib.setup_path_on_import

import subprocess
import sys
import logging
Expand Down
4 changes: 4 additions & 0 deletions puppet/zulip_internal/files/zulip-ec2-configure-interfaces
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ import logging.handlers
import subprocess
import re

import sys
sys.path.append('/home/zulip/deployments/current')
import scripts.lib.setup_path_on_import

import boto.utils
import netifaces
from six.moves import range
Expand Down
6 changes: 5 additions & 1 deletion scripts/get-django-setting
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ from __future__ import absolute_import
from __future__ import print_function

import os
from os.path import dirname, abspath
import sys

sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
BASE_DIR = dirname(dirname(abspath(__file__)))
sys.path.append(BASE_DIR)
import scripts.lib.setup_path_on_import

os.environ['DJANGO_SETTINGS_MODULE'] = 'zproject.settings'
from django.conf import settings

Expand Down
7 changes: 6 additions & 1 deletion scripts/lib/log-management-command
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
import sys
import logging
import os
sys.path.append(os.path.join(os.path.dirname(__file__), "..", ".."))
from os.path import dirname, abspath

BASE_DIR = dirname(dirname(dirname(abspath(__file__))))
sys.path.append(BASE_DIR)
import scripts.lib.setup_path_on_import

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "zproject.settings")
from django.conf import settings

Expand Down
15 changes: 15 additions & 0 deletions scripts/lib/setup_path_on_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""
Use libraries from a virtualenv (by modifying sys.path) in production.
Also add Zulip's root directory to sys.path
"""

import os
from os.path import dirname, abspath
import sys

BASE_DIR = dirname(dirname(dirname(abspath(__file__))))
activate_this = os.path.join(BASE_DIR, "zulip-venv", "bin", "activate_this.py")
if os.path.exists(activate_this):
# this file will exist in production
exec(open(activate_this).read(), {}, dict(__file__=activate_this)) # type: ignore # https://github.com/python/mypy/issues/1577
sys.path.append(BASE_DIR)
6 changes: 5 additions & 1 deletion scripts/setup/generate_secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@

from __future__ import print_function
import sys, os, os.path
from os.path import dirname, abspath

BASE_DIR = dirname(dirname(dirname(abspath(__file__))))
sys.path.append(BASE_DIR)
import scripts.lib.setup_path_on_import

sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))
os.environ['DJANGO_SETTINGS_MODULE'] = 'zproject.settings'

from django.utils.crypto import get_random_string
Expand Down
6 changes: 6 additions & 0 deletions zproject/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
"""
import os
from os.path import dirname, abspath
import sys

BASE_DIR = dirname(dirname(abspath(__file__)))
sys.path.append(BASE_DIR)
import scripts.lib.setup_path_on_import

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "zproject.settings")

Expand Down

0 comments on commit a9835c0

Please sign in to comment.