Add this to fabfile.py
to clone the project to your server:
# ...
env.project = "MyProject"
env.code_dir = f"/home/sysop/{env.project}"
env.clone_url = "https:///github.com/myuser/MyProject.git"
@task
def clone_project():
run(f"git clone {env.clone_url} {env.code_dir}", pty=False)
run:
fab clone_project
Add this to fabfile.py
to create a virtualenv:
# ...
env.venv_name = "myproject"
env.venvs = f"/home/sysop/.virtualenvs/"
env.venv_path = f"{env.venvs}{env.venv_name}/"
env.venv_command = f"source {env.venv_path}/bin/activate"
@task
def create_venv():
run(f"mkdir -p {env.venvs}")
run(f"virtualenv -p /usr/bin/python3 --prompt='({env.venv_name}) ' {env.venv_path}")
and run:
fab create_venv
We will create a context manager to run commands in our virtualenv:
from contextlib import contextmanager
@contextmanager
def virtualenv():
with cd(env.code_dir):
with prefix(env.venv_command):
yield
Example usage:
@task
def upgrade_pip():
with virtualenv():
run("pip install --upgrade pip", pty=False)
To run:
fab upgrade_pip
Assuming your project has an updated requirements.txt
(created with pipenv lock -r > requirements.txt
):
@task
def pip_install():
with virtualenv():
run("pip install -r requirements.txt", pty=False)
run:
fab pip_install