-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
56 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
Fabric3==1.14.post1 | ||
fabric |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 [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*. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |