2
0
mirror of https://github.com/xcat2/confluent.git synced 2024-11-25 02:52:07 +00:00

Utility function for running commands

Given the python2/3 differences, good to have a single run
that returns stdout and stderr.

This should trigger the same behavior as timeout did, but
in a manner consistent between 2 and 3.
This commit is contained in:
Jarrod Johnson 2021-12-08 10:20:34 -05:00
parent 02da50af8b
commit 343e5eabe5
5 changed files with 22 additions and 27 deletions

View File

@ -1,5 +1,6 @@
import os
import confluent.collective.manager as collective
import confluent.util as util
from os.path import exists
import shutil
import socket
@ -15,10 +16,7 @@ def get_openssl_conf_location():
raise Exception("Cannot find openssl config file")
def get_ip_addresses():
try:
lines = subprocess.check_output('ip addr'.split(' '), timeout=86400)
except TypeError:
lines = subprocess.check_output('ip addr'.split(' '))
lines = util.run(['ip', 'addr'])
if not isinstance(lines, str):
lines = lines.decode('utf8')
for line in lines.split('\n'):
@ -102,12 +100,8 @@ def assure_tls_ca():
if e.errno != 17:
raise
shutil.copy2('/etc/confluent/tls/cacert.pem', fname)
try:
hv = subprocess.check_output(
['openssl', 'x509', '-in', '/etc/confluent/tls/cacert.pem', '-hash', '-noout'], timeout=86400)
except TypeError:
hv = subprocess.check_output(
['openssl', 'x509', '-in', '/etc/confluent/tls/cacert.pem', '-hash', '-noout'])
hv = util.run(
['openssl', 'x509', '-in', '/etc/confluent/tls/cacert.pem', '-hash', '-noout'])
if not isinstance(hv, str):
hv = hv.decode('utf8')
hv = hv.strip()

View File

@ -211,8 +211,7 @@ def handle_request(env, start_response):
pass
if needlocalectl:
try:
langinfo = subprocess.check_output(
['localectl', 'status'], timeout=86400).split(b'\n')
langinfo = util.run(['localectl', 'status'])[0].split(b'\n')
except Exception:
langinfo = []
for line in langinfo:
@ -236,10 +235,7 @@ def handle_request(env, start_response):
if ckeymap == 'n/a':
continue
keymap = ckeymap
try:
tdc = subprocess.check_output(['timedatectl'], timeout=86400).split(b'\n')
except TypeError:
tdc = subprocess.check_output(['timedatectl']).split(b'\n')
tdc = util.run(['timedatectl'])[0].split(b'\n')
for ent in tdc:
ent = ent.strip()
if ent.startswith(b'Time zone:'):

View File

@ -3,6 +3,7 @@
import base64
import confluent.config.configmanager as cfm
import confluent.collective.manager as collective
import confluent.util as util
import eventlet.green.subprocess as subprocess
import eventlet
import glob
@ -43,10 +44,7 @@ def assure_agent():
if agent_pid is None:
try:
agent_starting = True
try:
sai = subprocess.check_output(['ssh-agent'], timeout=86400)
except TypeError:
sai = subprocess.check_output(['ssh-agent'])
sai = util.run(['ssh-agent'])[0]
for line in sai.split(b'\n'):
if b';' not in line:
continue

View File

@ -19,7 +19,7 @@ import os
import shutil
import tempfile
import confluent.sshutil as sshutil
import eventlet.green.subprocess as subprocess
import confluent.util as util
import confluent.noderange as noderange
import eventlet
import pwd
@ -168,12 +168,8 @@ def sync_list_to_node(sl, node, suffixes):
stage_ent(sl.appendoncemap, ent,
os.path.join(targdir, suffixes['appendonce']), True)
sshutil.prep_ssh_key('/etc/confluent/ssh/automation')
try:
output = subprocess.check_output(
['rsync', '-rvLD', targdir + '/', 'root@{}:/'.format(node)], timeout=86400)
except TypeError:
output = subprocess.check_output(
['rsync', '-rvLD', targdir + '/', 'root@{}:/'.format(node)])
output = util.run(
['rsync', '-rvLD', targdir + '/', 'root@{}:/'.format(node)])[0]
except Exception as e:
if 'CalledProcessError' not in repr(e):
# https://github.com/eventlet/eventlet/issues/413

View File

@ -26,6 +26,17 @@ import re
import socket
import ssl
import struct
import subprocess
def run(cmd):
with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as process:
stdout, stderr = process.communicate()
retcode = process.poll()
if retcode:
raise subprocess.CalledProcessError(retcode, process.args, output=stdout, stderr=stderr)
return stdout, stderr
def stringify(instr):
# Normalize unicode and bytes to 'str', correcting for