Richard Jones' Log: Using Solaris "priv" with Fabric

Wed, 27 Jul 2011

I needed support for "priv" instead of Fabric's built-in "sudo" support. I went through a number of (sometimes quite horrific) iterations before I settled on this relatively simple solution:

import contextlib

def priv(user):
    '''Context manager to cause all run()'ed operations to be
    Replaces with the priv command for the duration of the
    save_shell = = 'priv su - %s -c' % user
    yield = save_shell

This is then used in a fabfile like so:

    with priv('remote_user'):
        run('do some remote command as remote_user')
        run('another remote command as remote_user')