Repository: zulip/zulip

Issue:
Can't upgrade from 1.8.1 to any other version
Hello,
we currently have Zulip 1.8.1 running on an Ubuntu 16.04.6 LTS server. We wanted to upgrade to benefit from some new features but find we are unable to do so.

Environment:
-Zulip 1.8.1 running on Ubuntu 16.04.6 LTS, which is running on an ESXi 6.0 Hypervisor
-Every connection to Websites have to be made over a squid-Proxy

The upgrade via the command "/home/zulip/deployments/current/scripts/upgrade-zulip /usr/local/src/zulip-server-2.0.2.tar.gz" hangs at the step "Installing setuptools, pip, wheel"
We expected our Proxy was at fault (even though environmental variables were set) and bypassed it via tethering over a mobile phone.
It all seemed to run well until we get to the point where it executes the script "/home/zulip/deployments/2019-04-02-12-21-11/scripts/setup/generate_secrets.py", at which point the upgrade aborts with the error "NameError: name 'prev' is not defined". I attached a screenshot.
Can anybody help fix this issue?

Thanks

Jan

![grafik](https://user-images.githubusercontent.com/49191973/55395785-9820ce00-5542-11e9-9ee8-6b091e018966.png)



Discussion:
@clk-jkn the first issue is definitely a proxy/networking issue, glad you were able to figure that out.  Does this `prev` error happen reproducibly if you try multiple times?

The issue with `prev` I've never seen before, and I'm puzzled by, because I don't think the tool in question has an identifier called `prev`.  

@andersk do you have any idea what could cause this or how to debug further?  
Hello @zulip/server-production members, this issue was labeled with the "area: production" label, so you may want to check it out!

<!-- areaLabelAddition -->
It does happen reproducibly. Even with different versions. We tried upgrading to 1.9.0 because we thought the error might be related to the jump from 1.8.1 to 2.0.2, but even then it failed with the same exact error.
I looked into the script but I am no programer and couldn't find anything obvious with my limited python knowledge. Which is why I asked for help here. I just can't seem to figure this one out on my own.
Thanks for helping!
Can you provide the content of `/var/log/zulip/upgrade.log`?  It might have details that help investigate this.
The `NameError` is complaining about the `prev` variable in `activate_this.py`. Some versions of `activate_this.py` do use `prev`, but they also define it as expected, so the `NameError` is strange indeed. Can you paste in the contents of `/home/zulip/deployments/2019-04-02-12-21-11/zulip-py3-venv/bin/activate_this.py`?
Sure, no Problem.

Contents of activate_this.py:
```
"""Activate virtualenv for current interpreter:

Use exec(open(this_file).read(), {'__file__': this_file}).

This can be used when you must use an existing Python interpreter, not the virtualenv bin/python.
"""
import os
import site
import sys

try:
    __file__
except NameError:
    raise AssertionError("You must use exec(open(this_file).read(), {'__file__': this_file}))")

# prepend bin to PATH (this file is inside the bin directory)
bin_dir = os.path.dirname(os.path.abspath(__file__))
os.environ["PATH"] = os.pathsep.join([bin_dir] + os.environ.get("PATH", "").split(os.pathsep))

base = os.path.dirname(bin_dir)

# virtual env is right above bin directory
os.environ["VIRTUAL_ENV"] = base

# add the virtual environments site-package to the host python import mechanism
IS_PYPY = hasattr(sys, "pypy_version_info")
IS_JYTHON = sys.platform.startswith("java")
if IS_JYTHON:
    site_packages = os.path.join(base, "Lib", "site-packages")
elif IS_PYPY:
    site_packages = os.path.join(base, "site-packages")
else:
    IS_WIN = sys.platform == "win32"
    if IS_WIN:
        site_packages = os.path.join(base, "Lib", "site-packages")
    else:
        site_packages = os.path.join(base, "lib", "python{}".format(sys.version[:3]), "site-packages")

prev = set(sys.path)
site.addsitedir(site_packages)
sys.real_prefix = sys.prefix
sys.prefix = base

# Move the added items to the front of the path, in place
new = list(sys.path)
sys.path[:] = [i for i in new if i not in prev] + [i for i in new if i in prev]
```

I re-executed the upgrade to give you a good clean upgrade.log. I attached it, as it is rather long thanks to apt...

Once again thank you for helping.

[upgrade.log](https://github.com/zulip/zulip/files/3052972/upgrade.log)



Resolving patch:
diff --git a/scripts/lib/setup_path_on_import.py b/scripts/lib/setup_path_on_import.py
--- a/scripts/lib/setup_path_on_import.py
+++ b/scripts/lib/setup_path_on_import.py
@@ -13,6 +13,6 @@
     # this file will exist in production
     if os.path.exists(activate_this):
         activate_locals = dict(__file__=activate_this)
-        exec(open(activate_this).read(), {}, activate_locals)
+        exec(open(activate_this).read(), activate_locals)
         if not os.path.exists(activate_locals["site_packages"]):
             raise RuntimeError(venv + " was not set up for this Python version")
diff --git a/tools/lib/provision.py b/tools/lib/provision.py
--- a/tools/lib/provision.py
+++ b/tools/lib/provision.py
@@ -421,7 +421,7 @@ def main(options):
     setup_venvs.main()
 
     activate_this = "/srv/zulip-py3-venv/bin/activate_this.py"
-    exec(open(activate_this).read(), {}, dict(__file__=activate_this))
+    exec(open(activate_this).read(), dict(__file__=activate_this))
 
     setup_shell_profile('~/.bash_profile')
     setup_shell_profile('~/.zprofile')


Test patch: