Repository: djangopackages/djangopackages

Issue:
:tractor: Test/migrate to web2.djangopackages.org server
This is a follow-up from #876 

Over the past week, our primary server ran out of disk space which caused Redis to die and took everything down. Reported in #956 

I created a new server (same resources but 2x the disk space) but need to do some load testing on it and wrap up the server bootstrap code into Fabric. 


Discussion:
📆 I might move the floating IP for this on **2023-03-29** for a few hours and see how things look. 
Currently testing, and everything looks solid so far. https://mastodon.social/@webology/110107688705044133 

Resolving patch:
diff --git a/fabfile.py b/fabfile.py
--- a/fabfile.py
+++ b/fabfile.py
@@ -20,6 +20,7 @@
 from fabric.colors import blue
 from fabric.operations import local as lrun
 from fabric.operations import put, run
+from rich import print
 
 
 def local():
@@ -27,6 +28,7 @@ def local():
     Work on the local environment
     """
     env.compose_file = "docker-compose.yml"
+    env.compose_version = "v1"
     env.project_dir = "."
     env.run = lrun
     env.cd = lcd
@@ -37,23 +39,35 @@ def production():
     Work on the production environment
     """
     env.hosts = [
-        "159.203.191.135"
+        "165.22.184.193"
     ]  # list the ip addresses or domain names of your production boxes here
-    env.port = 56565  # ssh port
     env.user = "root"  # remote user, see `env.run` if you don't log in as root
 
     env.compose_file = "docker-compose.prod.yml"
+    env.compose_version = "v2"
     env.project_dir = "/code/djangopackages"  # this is the project dir where your code lives on this machine
-
-    # if you don't use key authentication, add your password here
-    # env.password = "foobar"
-    # if your machine has no bash installed, fall back to sh
-    # env.shell = "/bin/sh -c"
-
     env.run = run  # if you don't log in as root, replace with 'env.run = sudo'
     env.cd = cd
 
 
+def setup():
+    env.run("apt update")
+    env.run(
+        "apt install apt-transport-https ca-certificates curl software-properties-common"
+    )
+    env.run(
+        "curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg"
+    )
+    env.run(
+        'echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null'
+    )
+    env.run("apt update")
+    env.run("apt-cache policy docker-ce")
+    env.run("apt install docker-ce")
+    env.run("systemctl status docker")
+    env.run("systemctl enable docker.service")
+
+
 def copy_secrets():
     """
     Copies secrets from local to remote.
@@ -94,6 +108,22 @@ def backup():
         env.run("gzip /data/djangopackages/backups/*.sql")
 
 
+def build_and_restart(service):
+    docker_compose(f"build {service}")
+    docker_compose(f"create {service}")
+    docker_compose(f"stop {service}")
+    docker_compose(f"start {service}")
+
+
+def clearsessions():
+    """
+    Clear old database sessions
+    """
+
+    with env.cd(env.project_dir):
+        docker_compose("run django-a python manage.py clearsessions")
+
+
 def cron():
     with env.cd(env.project_dir):
         docker_compose("run django-a python manage.py import_classifiers")
@@ -101,23 +131,32 @@ def cron():
         docker_compose("run django-a python manage.py import_releases")
 
 
-def deploy():
+def deploy(clearsessions: bool = False, stash: bool = False):
     """
     Pulls the latest changes from main, rebuilt and restarts the stack
     """
 
-    # lrun("git push origin main")
     # copy_secrets()
+
     with env.cd(env.project_dir):
-        # Manage Backups
-        # docker_compose("run django-a python manage.py clearsessions")
+        # Clear old database sessions
+        if clearsessions:
+            docker_compose("run django-a python manage.py clearsessions")
 
         # docker_compose("run postgres backup")
         # env.run("gzip /data/djangopackages/backups/*.sql")
 
+        # stash existing changes
+        if stash:
+            env.run("git stash")
+
         # Pull the latest code
         env.run("git pull origin main")
 
+        # stash existing changes
+        if stash:
+            env.run("git stash pop")
+
         # turn maintenance mode on
         # maintenance_mode_on("django-a")
 
@@ -140,13 +179,6 @@ def deploy():
         # maintenance_mode_off("django-a")
 
 
-def build_and_restart(service):
-    docker_compose(f"build {service}")
-    docker_compose(f"create {service}")
-    docker_compose(f"stop {service}")
-    docker_compose(f"start {service}")
-
-
 def collectstatic(service):
     docker_compose(f"exec {service} python manage.py collectstatic --no-input -v 1")
 
@@ -165,10 +197,12 @@ def purge_cache(service):
     )
 
 
-def docker_compose(command):
+def docker_compose(command, old=True):
     """
     Run a docker-compose command
     :param command: Command you want to run
     """
     with env.cd(env.project_dir):
+        if env.compose_version == "v2":
+            return env.run(f"docker compose -f {env.compose_file} {command}")
         return env.run(f"docker-compose -f {env.compose_file} {command}")
diff --git a/homepage/views.py b/homepage/views.py
--- a/homepage/views.py
+++ b/homepage/views.py
@@ -1,4 +1,3 @@
-
 from django.contrib.auth.models import User
 from django.core.cache import cache
 from django.db.models import Count, Q


Test patch: