diff --git a/deploy/malawi/dev-requirements.txt b/deploy/malawi/dev-requirements.txt index fa5a6af93..2610efb94 100644 --- a/deploy/malawi/dev-requirements.txt +++ b/deploy/malawi/dev-requirements.txt @@ -1 +1 @@ -Fabric3==1.14.post1 +fabric diff --git a/docs/systems-administration/common-tasks.md b/docs/systems-administration/common-tasks.md index 45e051ee1..438fd5b0f 100644 --- a/docs/systems-administration/common-tasks.md +++ b/docs/systems-administration/common-tasks.md @@ -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 @@ -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 cstock@10.10.100.77 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*. diff --git a/fabfile.py b/fabfile.py index a2561b99e..053d690ad 100644 --- a/fabfile.py +++ b/fabfile.py @@ -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 cstock@10.10.100.77 deploy --prompt-for-sudo-password +""" -def malawi(): - """ - Malawi configuration - """ - env.hosts = ['cstock@10.10.100.77'] - 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")