Skip to content

Commit

Permalink
update to latest fabric
Browse files Browse the repository at this point in the history
  • Loading branch information
czue committed Dec 17, 2024
1 parent ffbab38 commit 76d867d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 33 deletions.
2 changes: 1 addition & 1 deletion deploy/malawi/dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Fabric3==1.14.post1
fabric
6 changes: 4 additions & 2 deletions docs/systems-administration/common-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ All of these tasks require [server access](./server-access.md).

## Deploying Updated Code

Deploying code requires installing fabric3:
Deploying code requires installing fabric:

```
pip install -r requrements/deploy/dev-requirements.txt
Expand All @@ -22,9 +22,11 @@ git clone https://github.com/dimagi/logistics.git
Then, to deploy you must first connect to the VPN. Then run the following command in the repository root:

```
fab malawi deploy
fab -H [email protected] deploy --prompt-for-sudo-password
```

You'll need to enter the password for the cstock user on the server.

This command should be run on *your own machine*.
For the remaining sections, you must run the commands *on the server*.

Expand Down
81 changes: 51 additions & 30 deletions fabfile.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,65 @@
from fabric.api import *
import datetime
import posixpath

from fabric import task

VIRTUALENV_HOME = '/home/cstock/.virtualenvs/cstock/bin/'
PIP = f'{VIRTUALENV_HOME}/pip'
PYTHON = f'{VIRTUALENV_HOME}/python'
"""
To use this file, first `pip install fabric` then run:
fab -H [email protected] deploy --prompt-for-sudo-password
"""

def malawi():
"""
Malawi configuration
"""
env.hosts = ['[email protected]']
env.code_dir = '/home/cstock/www/cstock/code_root'
env.branch = "main"
CODE_ROOT = "/home/cstock/www/cstock/code_root/"
VIRTUALENV_ROOT = '/home/cstock/.virtualenvs/cstock/'
BRANCH = "main"


def update_code():
run('git remote prune origin')
run('git fetch')
run(f'git checkout {env.branch}')
run(f'git pull origin {env.branch}')
run("find . -name '*.pyc' -delete") # cleanup pyc files
@task
def deploy(c):
"""
Deploy code to remote host by checking out the latest via git.
"""
start = datetime.datetime.now()
update_code(c)
update_virtualenv(c)
django_stuff(c)
services_restart(c)
print(f"deploy completed in {datetime.datetime.now() - start}")


def update_requirements():
sudo(f'{PIP} install -r {env.code_dir}/requirements.txt')
def update_code(c):
print("updating code...")
with c.cd(CODE_ROOT):
c.run("git fetch")
c.run(f"git checkout {BRANCH}")
c.run(f"git reset --hard origin/{BRANCH}")
c.run("find . -name '*.pyc' -delete")


def django_stuff():
run(f'{PYTHON} manage.py migrate --noinput')
run(f'{PYTHON} manage.py collectstatic --noinput')
def update_virtualenv(c):
"""
Update external dependencies on remote host assumes you've done a code update.
"""
print("updating requirements...")
files = (
posixpath.join(CODE_ROOT, "requirements.txt"),
)
with c.prefix("source {}/bin/activate".format(VIRTUALENV_ROOT)):
for req_file in files:
c.run("pip install -r {}".format(req_file))


def deploy():
def django_stuff(c):
"""
Deploy latest changes
staticfiles, migrate, etc.
"""
sudo("supervisorctl stop all")
with cd(env.code_dir):
update_code()
update_requirements()
django_stuff()
sudo("supervisorctl start all")
print("Running migrations and building staticfiles...")
with c.cd(CODE_ROOT):
c.run("{}/bin/python manage.py migrate".format(VIRTUALENV_ROOT))
c.run("{}/bin/python manage.py collectstatic --noinput".format(VIRTUALENV_ROOT))


def services_restart(c):
print("Restarting services...")
c.sudo("sudo supervisorctl stop all")
c.sudo("sudo supervisorctl start all")

0 comments on commit 76d867d

Please sign in to comment.