[1613] | 1 | """ |
---|
| 2 | Functions to perform remctls. |
---|
| 3 | """ |
---|
| 4 | |
---|
| 5 | from invirt.common import CodeError |
---|
| 6 | import subprocess |
---|
[1614] | 7 | from socket import getfqdn |
---|
[1613] | 8 | |
---|
[1614] | 9 | def kinit(principal=None, keytab=None): |
---|
[1613] | 10 | """Kinit with a given username and keytab""" |
---|
[1614] | 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], |
---|
[1613] | 16 | stderr=subprocess.PIPE) |
---|
| 17 | e = p.wait() |
---|
| 18 | if e: |
---|
| 19 | raise CodeError("Error %s in kinit: %s" % (e, p.stderr.read())) |
---|
| 20 | |
---|
[1614] | 21 | def checkKinit(principal=None, keytab=None): |
---|
[1613] | 22 | """If we lack tickets, kinit.""" |
---|
| 23 | p = subprocess.Popen(['klist', '-s']) |
---|
| 24 | if p.wait(): |
---|
[1614] | 25 | kinit(principal, keytab) |
---|
[1613] | 26 | |
---|
[1614] | 27 | def remctl(host, *args, **kws): |
---|
[1613] | 28 | """Perform a remctl and return the output. |
---|
| 29 | |
---|
| 30 | kinits if necessary, and outputs errors to stderr. |
---|
| 31 | """ |
---|
[1614] | 32 | checkKinit(kws.get('principal'), kws.get('keytab')) |
---|
| 33 | p = subprocess.Popen(['remctl', host] |
---|
[1613] | 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() |
---|