Skip to content

Instantly share code, notes, and snippets.

@kreitje
Created November 11, 2013 17:07
Show Gist options
  • Save kreitje/7416632 to your computer and use it in GitHub Desktop.
Save kreitje/7416632 to your computer and use it in GitHub Desktop.
Fabfile simple deploy script
from fabric.api import *
import os
import os.path
import datetime
now = datetime.datetime.now()
# Where to save the tar file on the local machine
TAR_FILE = '/Users/kreitje/Desktop/site.tar'
# Path to the code on the local machine
LOCAL_DIR = '/Users/kreitje/Development/site.dev'
# Where to copy the files so we can remove extra clutter
LOCAL_COPY_DIR = '/Users/kreitje/Desktop/site'
# Deployment directory on the remote machine
CODE_DIR = '/data/www/site/deploy-%s' % (now.strftime('%Y-%m-%d-%H-%M-%S'))
# Name of the current release directory
CURRENT_RELEASE_DIR = '/data/www/site/current-release'
UPLOADS_DIR = '/data/www/site/uploads'
# Where to upload the file to
DEPLOY_DIR = '/home/kreitje/'
## Staging Settings
STAGE_CODE_DIR = '/data/www/stage.site/deploy-%s' % (now.strftime('%Y-%m-%d-%H-%M-%S'))
STAGE_CURRENT_RELEASE_DIR = '/data/www/stage.site/current-release'
STAGE_UPLOADS_DIR = '/data/www/stage.site/uploads'
env.use_ssh_config = True
env.hosts = ['li']
def stage():
test()
copy()
create_tar()
upload_tar(STAGE_CODE_DIR)
link(STAGE_CODE_DIR, STAGE_CURRENT_RELEASE_DIR, STAGE_UPLOADS_DIR, 'staging')
#restart_supervisor()
cleanup()
endoutput(STAGE_CODE_DIR)
run("rm -f %s/public/robots.txt" % (STAGE_CODE_DIR))
run("touch %s/public/robots.txt" % (STAGE_CODE_DIR))
# Disallow indexing of the stage site
run("echo \"Disallow: /\" > %s/public/robots.txt" % (STAGE_CODE_DIR))
def deploy():
test()
copy()
create_tar()
upload_tar(CODE_DIR)
link(CODE_DIR, CURRENT_RELEASE_DIR, UPLOADS_DIR, 'production')
#restart_supervisor()
cleanup()
endoutput(CODE_DIR)
# Run phpunit
# If the tests pass continue on
def test():
with lcd(LOCAL_DIR):
local("vendor/bin/phpunit")
# Copy files to desktop and remove uneeded extras
def copy():
with lcd(LOCAL_DIR):
local("composer dumpautoload")
# creates a new directory and copies all files there
local("mkdir %s" % (LOCAL_COPY_DIR))
with lcd(LOCAL_DIR):
local("cp -R ./* %s" % (LOCAL_COPY_DIR))
# remove dev environment files
local("rm -rf %s/uploads/*" % (LOCAL_COPY_DIR))
local("rm -rf %s/app/storage/logs/*" % (LOCAL_COPY_DIR))
local("rm -rf %s/app/storage/views/*" % (LOCAL_COPY_DIR))
local("rm -rf `find %s/ -type d -name .git`" % (LOCAL_COPY_DIR))
local("rm -rf %s/fabfile.*" % (LOCAL_COPY_DIR))
# Create a tar file to upload
def create_tar():
with lcd(LOCAL_COPY_DIR):
local("tar --exclude '\._*' -cvf %s ./*" % (TAR_FILE))
# Upload the tar file
def upload_tar(extractpath):
run("mkdir -p %s" % (extractpath))
put(TAR_FILE, DEPLOY_DIR)
run("tar xvf %ssite.tar -C %s" % (DEPLOY_DIR, extractpath))
#def restart_supervisor():
#run("sudo supervisorctl restart site")
### Setup the sym links and what not
# codedir - new code directory ex: /data/www/site/deploy-dtg
# releasedir - current-release folder ex: /data/www/site/current-release
# updir - Uploads directory ex: /data/www/site/uploads
# depto - environmnent ex: staging or production
def link(codedir, releasedir, updir, depto):
#run("php %s/artisan down" % (releasedir))
run("rm -rf %s" % (releasedir))
run("ln -s %s %s" % (codedir, releasedir))
run("php %s/artisan clear-compiled --env=%s" % (releasedir, depto))
run("php %s/artisan migrate --env=%s" % (releasedir, depto))
run("php %s/artisan optimize --env=%s" % (releasedir, depto))
#run("php %s/artisan up" % (releasedir))
run("find %s/ \( -name '.DS_Store' -or -name '._*' \) -delete" % (releasedir))
run("rm -rf %s/site.tar" % (DEPLOY_DIR))
run("rm -rf %s/uploads/*" % (releasedir))
def endoutput(cdir):
print cdir
def cleanup():
local("rm -rf %s" % (TAR_FILE))
local("rm -rf %s" % (LOCAL_COPY_DIR))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment