Skip to content

Instantly share code, notes, and snippets.

@iflowfor8hours
Created April 9, 2014 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iflowfor8hours/10300344 to your computer and use it in GitHub Desktop.
Save iflowfor8hours/10300344 to your computer and use it in GitHub Desktop.
from invoke import run, task, Collection
import os
import shutil
import tarfile
@task
def install_dependencies():
run("source .looksee_venv/bin/activate")
run("pip install -r webapp/requirements.txt")
@task()
def deploy(env):
print("Deploying to %s environment" % env)
if env:
deploytgz = tarfile.open("build/deploy.tgz", "r:gz")
deploytgz.extractall("build")
run("ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook \
build/deploy/playbooks/deploy-looksee.yaml \
--limit=%s -i build/deploy/inventory -v" % env)
@task(pre=['clean', 'test.unit'])
def build():
print "Creating build artifacts..."
shutil.rmtree("build", True)
os.mkdir("build")
shutil.copyfile("test.sh", "build/test.sh")
shutil.copyfile("tasks.py", "build/tasks.py")
tar = tarfile.open("build/looksee.tgz", "w:gz")
tar.add("webapp")
tar.close()
tar = tarfile.open("build/deploy.tgz", "w:gz")
tar.add("deploy")
tar.close()
print "Sucessfully created build artifacts!"
@task
def vagrantup():
print "Creating vagrant environment..."
run("vagrant up")
@task
def vagrantdestroy():
print "Destroying vagrant environment..."
run("vagrant destroy -f")
@task
def clean():
print "Deleting current build directory..."
if os.path.exists(os.path.join(os.getcwd(), "build")):
shutil.rmtree("build")
@task
def unit():
print "Running unit tests..."
run("scripts/setup-dev-env.sh")
run(". .looksee_venv/bin/activate && pip install -r webapp/requirements.txt && \
nosetests -v webapp/webapp/tests/unit/model")
@task
def functional(host):
print "Running functional tests..."
shutil.rmtree("looksee-func/webapp/webapp/tests/functional", True)
looksee_tarfile = "looksee.tgz" if os.path.exists("looksee.tgz") else "build/looksee.tgz"
tar = tarfile.open(looksee_tarfile)
test_subdir = [ tarinfo for tarinfo in tar
if tarinfo.name.startswith("webapp/webapp/tests/functional") ]
tar.extractall("looksee-func", members=test_subdir)
tar.close()
test_commands = ("cd looksee-func/webapp && virtualenv --python=python2.7 .test_venv && "
". ./.test_venv/bin/activate && pip install -q -r webapp/tests/functional/requirements-func.txt && "
"touch webapp/__init__.py && touch webapp/tests/__init__.py && "
"python -m webapp.tests.functional.__init__ -host %s" % host )
run(test_commands)
@task(pre=['vagrantdestroy', 'clean', 'test.unit', 'build', 'vagrantup', 'deploy --env=vagrant'])
def endtoend():
print "Running an end to end build and deploy to vagrant..."
#Either that, by defining a task, and then calling the deploy task from inside the task (so I can pass that parameter), or running it as a collection, which I don't fully understand.
#endtoend = Collection(vagrantdestroy, clean, build, vagrantup, deploy)
#ns.add_collection(endtoend, 'endtoend')
tests = Collection(unit, functional)
ns = Collection()
ns.add_collection(tests, 'test')
misc_tasks = ['build','clean','deploy','install_dependencies','vagrantup', 'vagrantdestroy', 'endtoend']
for task in misc_tasks:
ns.add_task(eval(task))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment