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

Last change on this file since 2095 was 2095, checked in by broder, 15 years ago

Get rid of confusing err=True option to invirt.remctl.remctl.

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