Source

rentalerts / fabfile.py


from fabric import api


class Server(object):

    manager = "manage.py"
    app_name = "rentalerts"
    runtests = True
    python_exec = "python"
    dir = None
    nginx_dir = None
    conf_dir = "conf"

    def run(self, command):
        with api.cd(self.dir):
            api.run(command)

    def pull(self):
        self.run("hg pull -u")

    def upgrade(self):
        self.run("pip install -r requirements.txt")

    def supervisor(self, app_name, action):
        cmd = "supervisorctl -c %s/supervisord.conf %s %s"
        self.run(cmd % (self.conf_dir, action, app_name))

    def copy_nginx_conf(self):
        print "Copying nginx files..."
        self.run("cp %s/nginx.conf %s/conf/nginx.conf" % (
            self.conf_dir,
            self.nginx_dir))

    def nginx_start(self):
        self.copy_nginx_conf()
        self.supervisor("nginx", "start")

    def nginx_stop(self):
        self.copy_nginx_conf()
        self.supervisor("nginx", "stop")

    def nginx_restart(self):
        self.supervisor("nginx", "restart")

    def app_start(self):
        self.supervisor(self.app_name, "start")

    def app_stop(self):
        self.supervisor(self.app_name, "stop")

    def app_restart(self):
        self.supervisor(self.app_name, "restart")

    def manage(self, command):
        self.run("%s %s" % (self.manager, command))

    def migrate(self):
        self.manage("migrate")

    def collectstatic(self):
        self.manage("collectstatic --noinput")
    

server = Server()

@api.task 
def prod():
    
    server.dir = "/home/danjac354/webapps/rentalerts/rentalerts"
    server.nginx_dir = "/home/danjac354/webapps/nginx"
    server.conf_dir = "%s/conf/production" % server.dir
    server.python_exec = "/home/danjac354/.virtualenvs/rentalerts/bin/python2.7"
    server.manager = "%s ./bin/production" % server.python_exec

    api.env.hosts = ['danjac354.webfactional.com']
    api.env.user = 'danjac354'
    api.env.password = 'VN4je2Cj'


@api.task
def nginx_start():

    server.pull()
    server.start_nginx()

@api.task
def pull():

    server.pull()
    server.collectstatic()


@api.task
def upgrade():
    api.execute(pull)
    server.upgrade()


@api.task
def tests():

    pass

    #with api.cd(server.dirname):
    #    api.run("%s test pm site helpers alerts accounts apartments" % server.manager)


@api.task
def migrate():

    api.execute(pull)
    api.execute(tests)

    server.migrate()

@api.task
def nginx_conf():
    api.execute(pull)
    server.copy_nginx_conf()


@api.task
def nginx_stop():

    server.nginx_stop()


@api.task
def nginx_start():

    api.execute(nginx_conf)
    server.nginx_start()


@api.task
def nginx_restart():

    api.execute(nginx_conf)
    server.nginx_restart()


@api.task
def app_stop():

    server.app_stop()

@api.task
def app_start():

    api.execute(migrate)

    server.app_start()


@api.task
def app_restart():

    api.execute(migrate)

    server.app_restart()