2 Functions to perform remctls.
5 from invirt.common import CodeError
8 from socket import getfqdn
10 def kinit(principal=None, keytab=None):
11 """Kinit with a given username and keytab"""
13 principal = 'daemon/' + getfqdn()
15 keytab = '/etc/invirt/keytab'
16 p = subprocess.Popen(['kinit', "-k", "-t", keytab, principal],
17 stderr=subprocess.PIPE)
20 raise CodeError("Error %s in kinit: %s" % (e, p.stderr.read()))
22 def checkKinit(principal=None, keytab=None):
23 """If we lack tickets, kinit."""
24 p = subprocess.Popen(['klist', '-s'])
26 kinit(principal, keytab)
28 def remctl(host, *args, **kws):
29 """Perform a remctl and return the output.
31 kinits if necessary, and outputs errors to stderr.
33 checkKinit(kws.get('principal'), kws.get('keytab'))
34 p = subprocess.Popen(['remctl', host]
36 stdout=subprocess.PIPE,
37 stderr=subprocess.PIPE)
40 return p.stdout.read(), p.stderr.read()
42 print >> sys.stderr, 'Error', v, 'on remctl', args, ':'
43 print >> sys.stderr, p.stderr.read()
44 raise CodeError('ERROR on remctl')
45 return p.stdout.read()