1 | """ |
---|
2 | Functions to perform remctls. |
---|
3 | """ |
---|
4 | |
---|
5 | from invirt.common import CodeError |
---|
6 | import subprocess |
---|
7 | from socket import getfqdn |
---|
8 | |
---|
9 | def 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 | |
---|
21 | def 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 | |
---|
27 | def 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() |
---|