Add option to have ipautil.run() not raise an exception

There are times where a caller will want to determine the course of
action based on the returncode instead of relying on it != 0.

This also lets the caller get the contents of stdout and stderr.
This commit is contained in:
Rob Crittenden
2009-11-30 15:28:09 -05:00
parent 29aa8fb05d
commit 7c2c2d6130
6 changed files with 17 additions and 17 deletions

View File

@@ -89,7 +89,7 @@ def write_tmp_file(txt):
return fd return fd
def run(args, stdin=None): def run(args, stdin=None, raiseonerr=True):
if stdin: if stdin:
p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True) p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
stdout,stderr = p.communicate(stdin) stdout,stderr = p.communicate(stdin)
@@ -101,10 +101,10 @@ def run(args, stdin=None):
logging.info('stdout=%s' % stdout) logging.info('stdout=%s' % stdout)
logging.info('stderr=%s' % stderr) logging.info('stderr=%s' % stderr)
if p.returncode != 0: if p.returncode != 0 and raiseonerr:
raise CalledProcessError(p.returncode, ' '.join(args)) raise CalledProcessError(p.returncode, ' '.join(args))
return (stdout, stderr) return (stdout, stderr, p.returncode)
def file_exists(filename): def file_exists(filename):
try: try:

View File

@@ -612,7 +612,7 @@ class CAInstance(service.Service):
# pkisilent doesn't return 1 on error so look at the output of # pkisilent doesn't return 1 on error so look at the output of
# /sbin/service pki-ca status. It will tell us if the instance # /sbin/service pki-ca status. It will tell us if the instance
# still needs to be configured. # still needs to be configured.
(stdout, stderr) = ipautil.run(["/sbin/service", "pki-ca", "status"]) (stdout, stderr, returncode) = ipautil.run(["/sbin/service", "pki-ca", "status"])
try: try:
stdout.index("CONFIGURED!") stdout.index("CONFIGURED!")
raise RuntimeError("pkisilent failed to configure instance.") raise RuntimeError("pkisilent failed to configure instance.")
@@ -640,7 +640,7 @@ class CAInstance(service.Service):
def __get_agent_cert(self, nickname): def __get_agent_cert(self, nickname):
args = ["/usr/bin/certutil", "-L", "-d", self.ca_agent_db, "-n", nickname, "-a"] args = ["/usr/bin/certutil", "-L", "-d", self.ca_agent_db, "-n", nickname, "-a"]
(out, err) = ipautil.run(args) (out, err, returncode) = ipautil.run(args)
out = out.replace('-----BEGIN CERTIFICATE-----', '') out = out.replace('-----BEGIN CERTIFICATE-----', '')
out = out.replace('-----END CERTIFICATE-----', '') out = out.replace('-----END CERTIFICATE-----', '')
return out return out
@@ -692,7 +692,7 @@ class CAInstance(service.Service):
'%s:%d' % (self.host_name, AGENT_SECURE_PORT), '%s:%d' % (self.host_name, AGENT_SECURE_PORT),
] ]
logging.debug("running sslget %s" % args) logging.debug("running sslget %s" % args)
(stdout, stderr) = ipautil.run(args) (stdout, stderr, returncode) = ipautil.run(args)
data = stdout.split('\r\n') data = stdout.split('\r\n')
params = get_defList(data) params = get_defList(data)
@@ -713,7 +713,7 @@ class CAInstance(service.Service):
'%s:%d' % (self.host_name, AGENT_SECURE_PORT), '%s:%d' % (self.host_name, AGENT_SECURE_PORT),
] ]
logging.debug("running sslget %s" % args) logging.debug("running sslget %s" % args)
(stdout, stderr) = ipautil.run(args) (stdout, stderr, returncode) = ipautil.run(args)
data = stdout.split('\r\n') data = stdout.split('\r\n')
outputList = get_outputList(data) outputList = get_outputList(data)
@@ -844,7 +844,7 @@ class CAInstance(service.Service):
# makes openssl throw up. # makes openssl throw up.
data = base64.b64decode(chain) data = base64.b64decode(chain)
(certs, stderr) = ipautil.run(["/usr/bin/openssl", (certs, stderr, returncode) = ipautil.run(["/usr/bin/openssl",
"pkcs7", "pkcs7",
"-inform", "-inform",
"DER", "DER",
@@ -989,7 +989,7 @@ class CAInstance(service.Service):
""" """
# Start by checking to see if policy is already installed. # Start by checking to see if policy is already installed.
(stdout, stderr) = ipautils.run(["/usr/sbin/semodule", "-l"]) (stdout, stderr, returncode) = ipautil.run(["/usr/sbin/semodule", "-l"])
# Ok, so stdout is a huge string of the output. Look through that # Ok, so stdout is a huge string of the output. Look through that
# for our policy # for our policy

View File

@@ -382,7 +382,7 @@ class CertDB(object):
root_nicknames = self.find_root_cert(nickname) root_nicknames = self.find_root_cert(nickname)
fd = open(self.cacert_fname, "w") fd = open(self.cacert_fname, "w")
for root in root_nicknames: for root in root_nicknames:
(cert, stderr) = self.run_certutil(["-L", "-n", root, "-a"]) (cert, stderr, returncode) = self.run_certutil(["-L", "-n", root, "-a"])
fd.write(cert) fd.write(cert)
fd.close() fd.close()
os.chmod(self.cacert_fname, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) os.chmod(self.cacert_fname, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
@@ -424,13 +424,13 @@ class CertDB(object):
def get_cert_from_db(self, nickname): def get_cert_from_db(self, nickname):
try: try:
args = ["-L", "-n", nickname, "-a"] args = ["-L", "-n", nickname, "-a"]
(cert, err) = self.run_certutil(args) (cert, err, returncode) = self.run_certutil(args)
return cert return cert
except ipautil.CalledProcessError: except ipautil.CalledProcessError:
return '' return ''
def find_cacert_serial(self): def find_cacert_serial(self):
(out,err) = self.run_certutil(["-L", "-n", self.cacert_name]) (out, err, returncode) = self.run_certutil(["-L", "-n", self.cacert_name])
data = out.split('\n') data = out.split('\n')
for line in data: for line in data:
x = re.match(r'\s+Serial Number: (\d+) .*', line) x = re.match(r'\s+Serial Number: (\d+) .*', line)
@@ -485,7 +485,7 @@ class CertDB(object):
"-f", self.passwd_fname] "-f", self.passwd_fname]
if not self.self_signed_ca: if not self.self_signed_ca:
args.append("-a") args.append("-a")
(stdout, stderr) = self.run_certutil(args) (stdout, stderr, returncode) = self.run_certutil(args)
os.remove(self.noise_fname) os.remove(self.noise_fname)
return (stdout, stderr) return (stdout, stderr)
@@ -746,7 +746,7 @@ class CertDB(object):
if passwd_fname: if passwd_fname:
args = args + ["-w", passwd_fname] args = args + ["-w", passwd_fname]
try: try:
(stdout, stderr) = ipautil.run(args) (stdout, stderr, returncode) = ipautil.run(args)
except ipautil.CalledProcessError, e: except ipautil.CalledProcessError, e:
if e.returncode == 17: if e.returncode == 17:
raise RuntimeError("incorrect password") raise RuntimeError("incorrect password")

View File

@@ -105,7 +105,7 @@ def is_ds_running():
""" """
ret = True ret = True
try: try:
(sout, serr) = ipautil.run(["/sbin/service", "dirsrv", "status"]) (sout, serr, rcode) = ipautil.run(["/sbin/service", "dirsrv", "status"])
if sout.find("is stopped") >= 0: if sout.find("is stopped") >= 0:
ret = False ret = False
except ipautil.CalledProcessError: except ipautil.CalledProcessError:

View File

@@ -100,7 +100,7 @@ class HTTPInstance(service.Service):
if selinux: if selinux:
try: try:
# returns e.g. "httpd_can_network_connect --> off" # returns e.g. "httpd_can_network_connect --> off"
(stdout, stderr) = ipautil.run(["/usr/sbin/getsebool", (stdout, stderr, returncode) = ipautil.run(["/usr/sbin/getsebool",
"httpd_can_network_connect"]) "httpd_can_network_connect"])
self.backup_state("httpd_can_network_connect", stdout.split()[2]) self.backup_state("httpd_can_network_connect", stdout.split()[2])
except: except:

View File

@@ -54,7 +54,7 @@ def chkconfig_del(service_name):
ipautil.run(["/sbin/chkconfig", "--del", service_name]) ipautil.run(["/sbin/chkconfig", "--del", service_name])
def is_enabled(service_name): def is_enabled(service_name):
(stdout, stderr) = ipautil.run(["/sbin/chkconfig", "--list", service_name]) (stdout, stderr, returncode) = ipautil.run(["/sbin/chkconfig", "--list", service_name])
runlevels = {} runlevels = {}
for runlevel in range(0, 7): for runlevel in range(0, 7):