Skip to content

Instantly share code, notes, and snippets.

@jezdez
Created November 22, 2009 14:36
Show Gist options
  • Save jezdez/240581 to your computer and use it in GitHub Desktop.
Save jezdez/240581 to your computer and use it in GitHub Desktop.
Fabric file to run a command in a openvz guest without outside connectivity
from fabric_openvz import guests, vz_run
@guests('uhura', 'obiwan')
def uname_a(guest):
"""
Show uname -a on given guests
"""
vz_run(guest, 'uname -a')
@guests('uhura', 'obiwan')
def ps_aux(guest):
"""
Show output of ps aux of given guests
"""
vz_run(guest, 'ps aux')
@guests('uhura')
def hostname(guest):
"""
Show output of hostname of given guests
"""
vz_run(guest, 'hostname')
from functools import wraps
from fabric.api import sudo, env
#env.hosts = ['user@vzhost']
vz_commands = {}
def guests(*guest_list):
"""
A decorator executing a list of commands in the given guest container
"""
def run_in_guests(func):
@wraps(func)
def inner_decorator(*args, **kwargs):
# in case someone passes the guest directly to the task
if 'guest' in kwargs:
runner(kwargs.pop('guest'), *args, **kwargs)
else:
for guest in guest_list:
runner(guest, *args, **kwargs)
def runner(guest, *args, **kwargs):
func(guest=guest, *args, **kwargs)
commands = '; '.join(vz_commands.pop(guest))
sudo('vzctl exec %s %s' % (guest, commands))
return inner_decorator
return run_in_guests
def vz_run(guest, cmd):
"""
Runs a command in the given openvz guest
"""
vz_commands.setdefault(guest, []).append(cmd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment