From 41a33e1ce9b03e2cabd15f5b060a5f0839e32182 Mon Sep 17 00:00:00 2001 From: dsc Date: Tue, 8 May 2012 15:29:45 -0700 Subject: [PATCH] Moves fabfile to a directory; adds utils; implements collapse_trees() --- fabfile.py | 122 ------------------------------------------- fabfile/__init__.py | 145 +++++++++++++++++++++++++++++++++++++++++++++++++++ fabfile/util.py | 19 +++++++ 3 files changed, 164 insertions(+), 122 deletions(-) delete mode 100644 fabfile.py create mode 100644 fabfile/__init__.py create mode 100644 fabfile/util.py diff --git a/fabfile.py b/fabfile.py deleted file mode 100644 index 5a0cd2f..0000000 --- a/fabfile.py +++ /dev/null @@ -1,122 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -from fabric.api import * -from fabric.colors import white, blue, cyan, green, yellow, red, magenta - -env.project_name = 'kraken-ui' -env.use_ssh_config = True # this is not working for me! -env.colors = True - - -# defaults - -# Hm. -env.local_dist_directory = 'tmp/dist' -env.dist_directory = 'dist' - - -# There should be a way to do this using stages. -# See: http://tav.espians.com/fabric-python-with-cleaner-api-and-parallel-deployment-support.html -# env.config_file = False -# env.stages = ['production', 'staging'] - -# environments -@task(alias='prod') -def production(): - """Set production environment variables""" - env.hosts = ['reportcard2.pmtpa.wmflabs'] - env.directory = '/srv/reportcard/kraken-ui' - env.owner = 'www-data' - env.group = 'www' - -@task -def staging(): - """Set staging environment variables""" - env.hosts = ['less.ly'] - env.directory = '/home/wmf/projects/kraken-ui' - env.user = 'wmf' - env.owner = 'wmf' - env.group = 'www' - -@task -def update_version(): - """ Updates `lib/version.js`. - """ - local('coke update_version') - -@task -def collapse_trees(): - """ - # Ensure gitrev is up to date - coke update_version - - # Ensure clean dist directory - [ -e "tmp/dist" ] && rm -rf tmp/dist - mkdir -p tmp/dist - - ### Collapse the serve trees into one directory - log "Collapsing serve trees..." - - # Copy the static files, derived files, and the whole data directory (bc lack of trailing /) - # into dist. Note that you will need to load all the site pages in your browser to populate var - # with the derived files. - rsync -Ca static/ var/ data tmp/dist/ - - # We copy lib (which contains .co source files) to src to make it easy to link source content - # to each other. Finding it in gitweb is a pain. Finding it in gerrit is almost impossible. - # But this could go away when we move to github. - rsync -Ca lib/ tmp/dist/src/ - - # For some reason, the shell tool does not generate a file identical to the middleware. So whatever. - # We curl here because we know that version works. - curl --silent --fail --url http://$DEV_HOST/vendor/browserify.js > tmp/dist/vendor/browserify.js - # browserify -o tmp/dist/vendor/browserify.js -r events -r seq - """ - update_version() - - -@task -def bundle(): - """Builds and bundles static files in tmp/dist for deployment.""" - pass - -@task -def fix_permissions(): - """Recursively fixes permissions on the deployment host.""" - sudo('chmod -R g+w %s') - sudo('chown -R %(owner)s:%(group)s %(directory)s' % env) - pass - -@task -def update(): - """Runs git pull on the deployment host.""" - with cd(env.directory): - run('git pull') - - -@task -def distribute(): - """Copies tmp/dist to deployment host.""" - local("rsync -Caz -v %(local_dist_directory)s %(user)s@%(host)s:%(directory)s/%(dist_directory)s" % env) - # TODO: make sure the following works. - # rsync_project(local_dir=env.local_dist_directory, remote_dir="%(user)s@%(host)s:%(directory)s/%(dist_directory)s" % env) - pass - -@task -def restart_server(): - """Restarts node.js server on the deployment host.""" - # need to work on this for less.ly - sudo("supervisor restart reportcard") - pass - -@task -def deploy(): - """Full deployment of latest project""" - pass - - -@task -def test(): - puts("user: %(user)s" % env) - run('whoami') diff --git a/fabfile/__init__.py b/fabfile/__init__.py new file mode 100644 index 0000000..0c24425 --- /dev/null +++ b/fabfile/__init__.py @@ -0,0 +1,145 @@ +#!/usr/bin/env fab +# -*- coding: utf-8 -*- + +""" GraphKit Fabric File +""" + +# Deal with the fact that we aren't *really* a Python project, +# so we haven't declared python dependencies. +import sys +try: + import fabric + from path import path +except ImportError: + print "ERROR: You're missing a dependency! " + "To build this project using fabric, you'll need to install fabric (and some other stuff):" + "\n\n" + "pip install -U fabric path.py" + sys.exit(1) + + + +from fabric.api import * +from fabric.colors import white, blue, cyan, green, yellow, red, magenta +from fabric.contrib.project import rsync_project + +env.project_name = 'kraken-ui' +env.use_ssh_config = True # this is not working for me! +env.colors = True + + +# defaults + +# Hm. +env.local_dist_directory = path('tmp/dist') +env.dist_directory = path('dist') +env.dev_server = 'localhost:8081' + +# There should be a way to do this using stages. +# See: http://tav.espians.com/fabric-python-with-cleaner-api-and-parallel-deployment-support.html +# env.config_file = False +# env.stages = ['production', 'staging'] + +# environments +@task(alias='prod') +def production(): + """Set production environment variables""" + env.hosts = ['reportcard2.pmtpa.wmflabs'] + env.directory = '/srv/reportcard/kraken-ui' + env.owner = 'www-data' + env.group = 'www' + +@task +def staging(): + """Set staging environment variables""" + env.hosts = ['less.ly'] + env.directory = '/home/wmf/projects/kraken-ui' + env.user = 'wmf' + env.owner = 'wmf' + env.group = 'www' + + +### Build Deploy Bundle + +@task +def update_version(): + """ Ensure `lib/version.js` has up to date git revision. + """ + local('coke update_version') + +@task +def collapse_trees(): + """ Collapse the serve trees into one directory. + """ + dist = env.local_dist_directory + + # Update version (derf) + update_version() + + # Ensure clean dist directory + dist.rmtree(ignore_errors=True) + dist.makedirs() + + # XXX: Unfortunately, we can't use rsync_project() for local-to-local copies, as it insists on + # inserting a : before the remote path, which indicates a local-to-remote copy. :( + + # Copy the static files, derived files, and the whole data directory (bc lack of trailing /) + # into dist. Note that you will need to load all the site pages in your browser to populate var + # with the derived files. + local('rsync -Ca static/ var/ data %s/' % dist) + + # We copy lib (which contains .co source files) to src to make it easy to link source content + # to each other. Finding it in gitweb is a pain. Finding it in gerrit is almost impossible. + # But this could go away when we move to github. + local('rsync -Ca lib/ %s/src/' % dist) + + # For some reason, the shell tool does not generate a file identical to the middleware. So whatever. + # We curl here because we know that version works. + # local('browserify -o %s/vendor/browserify.js -r events -r seq' % dist) + with open('%s/vendor/browserify.js' % dist, 'w') as f: + f.write( local('curl --silent --fail --url http://%s/vendor/browserify.js', capture=True) ) + +@task +def bundle(): + """Builds and bundles static files in tmp/dist for deployment.""" + pass + +@task +def fix_permissions(): + """Recursively fixes permissions on the deployment host.""" + sudo('chmod -R g+w %s') + sudo('chown -R %(owner)s:%(group)s %(directory)s' % env) + pass + +@task +def update(): + """Runs git pull on the deployment host.""" + with cd(env.directory): + run('git pull') + + +@task +def distribute(): + """Copies tmp/dist to deployment host.""" + local("rsync -Caz -v %(local_dist_directory)s %(user)s@%(host)s:%(directory)s/%(dist_directory)s" % env) + # TODO: make sure the following works. + # rsync_project(local_dir=env.local_dist_directory, remote_dir="%(user)s@%(host)s:%(directory)s/%(dist_directory)s" % env) + pass + +@task +def restart_server(): + """Restarts node.js server on the deployment host.""" + # need to work on this for less.ly + sudo("supervisor restart reportcard") + pass + +@task +def deploy(): + """Full deployment of latest project""" + pass + + +@task +def test(): + puts("user: %(user)s" % env) + run('whoami') diff --git a/fabfile/util.py b/fabfile/util.py new file mode 100644 index 0000000..bcffa5f --- /dev/null +++ b/fabfile/util.py @@ -0,0 +1,19 @@ +#!/usr/bin/env fab +# -*- coding: utf-8 -*- + +# Liberated from fabric source: +# https://github.com/fabric/fabric/blob/master/fabfile/utils.py + +from __future__ import with_statement +from contextlib import contextmanager +from fabric.api import hide, puts + +__all__ = ('msg',) + + +@contextmanager +def msg(txt): + puts(txt + "...", end='', flush=True) + with hide('everything'): + yield + puts("done.", show_prefix=False, flush=True) -- 1.7.0.4