source: trunk/packages/invirt-base/python/invirt/remctl.py @ 2396

Last change on this file since 2396 was 2097, checked in by broder, 16 years ago

For now, revert r2093-2095, which were committed by accident; I'm not
comfortable with them sitting on HEAD when I don't have time to test
them.

File size: 1.4 KB
RevLine 
[1613]1"""
2Functions to perform remctls.
3"""
4
5from invirt.common import CodeError
6import subprocess
[1621]7import sys
[1614]8from socket import getfqdn
[1613]9
[1614]10def kinit(principal=None, keytab=None):
[1613]11    """Kinit with a given username and keytab"""
[1614]12    if principal is None:
13        principal = 'daemon/' + getfqdn()
14    if keytab is None:
15        keytab = '/etc/invirt/keytab'
16    p = subprocess.Popen(['kinit', "-k", "-t", keytab, principal],
[1613]17                         stderr=subprocess.PIPE)
18    e = p.wait()
19    if e:
20        raise CodeError("Error %s in kinit: %s" % (e, p.stderr.read()))
21
[1614]22def checkKinit(principal=None, keytab=None):
[1613]23    """If we lack tickets, kinit."""
24    p = subprocess.Popen(['klist', '-s'])
25    if p.wait():
[1614]26        kinit(principal, keytab)
[1613]27
[1614]28def remctl(host, *args, **kws):
[1613]29    """Perform a remctl and return the output.
30
31    kinits if necessary, and outputs errors to stderr.
32    """
[1614]33    checkKinit(kws.get('principal'), kws.get('keytab'))
34    p = subprocess.Popen(['remctl', host]
[1613]35                         + list(args),
36                         stdout=subprocess.PIPE,
37                         stderr=subprocess.PIPE)
38    v = p.wait()
[2097]39    if kws.get('err'):
40        return p.stdout.read(), p.stderr.read()
[1613]41    if v:
[2097]42        print >> sys.stderr, 'Error', v, 'on remctl', args, ':'
43        print >> sys.stderr, p.stderr.read()
44        raise CodeError('ERROR on remctl')
[1613]45    return p.stdout.read()
Note: See TracBrowser for help on using the repository browser.