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

Last change on this file since 1619 was 1614, checked in by broder, 16 years ago

Actually generalize the invirt.remctl module

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