Flush Memcached using Fabric

Posted by Bryan in Web Development

Looking for a quick method to reload a local or remote memcached instance? Enter Fabric.

Fabric is the Python library used to automate deployments using SSH. It is a necessary tool for every Python developer's toolbox.

Here is an example Fabric script for flushing memcached:

from fabric.api import *

# list of remote memcached servers
env.hosts = ['live',]

def memcached_flush():
    """
    Flush memcached on live
    """
    print "Flushing memcached live"
    run('echo "flush_all" | nc 127.0.0.1 11211')

def memcached_flush_local():
    """
    Flush local memcached
    """
    print "Flushing memcached"
    local('echo "flush_all" | nc 127.0.0.1 11211')

The assumption with the aforementioned code is the memcached instance is bound to 127.0.0.1 on port 11211 (the memcached default). It relies on nc (netcat) to write the flush_all command.

Comments

you might look to using parameters in your fab tasks so that you could call: fab memflush:local or fab memflush:live http://docs.fabfile.org/0.9.2/usage/fab.html#per-task-arguments
Posted by Morgan Goose on Oct 27, 2010