fabric2 commands for django development

Django -- Posted on May 2, 2020

fabric version 2 commands for django development

              
                from fabric import task
from pathlib import Path
import os
from fabric import Connection
from os.path import expanduser
from fabric import Connection, Config
home = expanduser("~")

SUDO_PASS = ''
user = ''
sudo_user = ''
server_ip = ''
project = 'projectname'
repo_dir = '/path/to/projectname'
venv_dir = 'path/to/project/name'
ssh_path = os.path.join(home, '.ssh', 'file.pub')
print(ssh_path)

config = Config(overrides={'sudo': {'password': SUDO_PASS, 'sudo_user': sudo_user}})

# Commands
@task
def updatedeps(c):
    """
    Installs the packages listed in "requirements.txt" in the virtualenv.
    """
    c.run(f'source {venv_dir}/activate && cd {repo_dir} && pip install --upgrade -r requirements.txt')

@task
def cleanpyc(c):
    c.run(f'source {venv_dir}/activate && cd {repo_dir}/{project} && find . -type f -name "*.pyc" -delete')


@task
def update(c):
    """
    Updates the code from the remote repository.
    """
    c.run(f'source {venv_dir}/activate && cd {repo_dir} && git stash && git pull --ff-only origin master && git status')


@task
def listcommands(c):
    """
    Lists all available management commands.
    """
    c.run(f' source {venv_dir}/activate && cd {repo_dir}/{project} && python manage.py')



@task
def runcommand(c, cmd):
    """
    Runs a management command.
    Use like:
        $ fab runcommand:'test -v 3 myapp'
    """
    c.run(f'source {venv_dir}/activate && cd {repo_dir}/{project} && python manage.py {cmd}')


@task
def collectstatic(c):
    """
    Runs "./manage.py collectstatic"
    """
    c.run(f'source {venv_dir}/activate;cd {repo_dir}/{project} && python manage.py collectstatic --noinput -v 1')


@task
def compress(c):
    """
    Runs "./manage.py collectstatic"
    """
    c.run(f'source {venv_dir}/activate;cd {repo_dir}/{project} && python manage.py compress --force')


@task
def loadfixture(c, fixture):
    """
    Installs the given fixture.
    """
    c.run(f'source {venv_dir}/activate;cd {repo_dir}/{project} && python manage.py loaddata {fixture}')

@task
def migrate(c):
    """
    Runs "./manage.py migrate"
    """
    c.run(f'source {venv_dir}/activate;cd {repo_dir}/{project} && python manage.py migrate')

@task
def restart_uwsgi(c):
    """
    Restart the frontend and backend sites for uwsgi emperor.
    """
    c.run(f'cd {repo_dir}/deploy && touch uwsgi.ini')


@task
def restart_supervisor(c):
    """
    Restart the frontend and backend sites for uwsgi emperor.
    """
    c.sudo('supervisorctl reread')
    c.sudo('supervisorctl update')
    c.sudo('supervisorctl status')


@task
def deploy(c):
    """
    Updates the code and restarts the frontend and backend sites.
    """
    c = Connection(
        host=server_ip,
        user=user,
        connect_kwargs={
            "key_filename": ssh_path,
        },
        config=config
    )
    update(c)
    collectstatic(c)
    #compress()
    runcommand(c, 'clear_cache')
    # runcommand(c, 'thumbnail cleanup')
    # runcommand(c, 'thumbnail clear')
    # runcommand(c, 'thumbnail clear_delete_referenced')
    # runcommand(c, 'thumbnail clear_delete_all')
    runcommand(c, 'ping_google "/sitemap.xml"')
    runcommand(c, 'ping_google "/sitemap-category.xml"')
    runcommand(c, 'ping_google "/sitemap-location.xml"')
    runcommand(c, 'ping_google "/sitemap-location1.xml"')
    runcommand(c, 'ping_google "/sitemap-poi1.xml"')
    runcommand(c, 'ping_google "/sitemap-poi2.xml"')
    runcommand(c, 'ping_google "/sitemap-poi.xml"')


    # migrate()
    # cleanpyc()
    # updatedeps()
    restart_supervisor(c)
                  
   
            

Related Posts