2 Functions to perform remctls.
5 from invirt.common import CodeError
7 from socket import getfqdn
9 def kinit(principal=None, keytab=None):
10 """Kinit with a given username and keytab"""
12 principal = 'daemon/' + getfqdn()
14 keytab = '/etc/invirt/keytab'
15 p = subprocess.Popen(['kinit', "-k", "-t", keytab, principal],
16 stderr=subprocess.PIPE)
19 raise CodeError("Error %s in kinit: %s" % (e, p.stderr.read()))
21 def checkKinit(principal=None, keytab=None):
22 """If we lack tickets, kinit."""
23 p = subprocess.Popen(['klist', '-s'])
25 kinit(principal, keytab)
27 def remctl(host, *args, **kws):
28 """Perform a remctl and return the output.
30 kinits if necessary, and outputs errors to stderr.
32 checkKinit(kws.get('principal'), kws.get('keytab'))
33 p = subprocess.Popen(['remctl', host]
35 stdout=subprocess.PIPE,
36 stderr=subprocess.PIPE)
39 return p.stdout.read(), p.stderr.read()
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()