Fix python3 incompatibility
- socket requires bytes and we need to explicitly convert str to bytes. - check_output() returns bytes, while python2 returns str, passing universal_newlines=True it will return str no matter what python version is used. Story: 2006796 Task: 42297 Signed-off-by: Charles Short <charles.short@windriver.com> Change-Id: Ie3921c4ae6211a8b0d290bdbdb195ce07036afbc
This commit is contained in:
parent
bbafa3451b
commit
26c16b3eb8
@ -314,15 +314,15 @@ class ServiceMonitor(object):
|
|||||||
client, _ = self.command.accept()
|
client, _ = self.command.accept()
|
||||||
request = client.recv(CONFIG.service_socket_bufsize)
|
request = client.recv(CONFIG.service_socket_bufsize)
|
||||||
LOG.debug('Monitor command socket: request=%s', str(request))
|
LOG.debug('Monitor command socket: request=%s', str(request))
|
||||||
cmd = request.split(' ')
|
cmd = request.split(b' ')
|
||||||
cmd, args = cmd[0], cmd[1:]
|
cmd, args = cmd[0], cmd[1:]
|
||||||
if cmd == 'status':
|
if cmd == b'status':
|
||||||
self.send_response(client, request, self.status())
|
self.send_response(client, request, self.status())
|
||||||
elif cmd == 'stop':
|
elif cmd == b'stop':
|
||||||
self.stop()
|
self.stop()
|
||||||
self.send_response(client, request, 'OK')
|
self.send_response(client, request, 'OK')
|
||||||
break
|
break
|
||||||
elif cmd == 'restful-url':
|
elif cmd == b'restful-url':
|
||||||
try:
|
try:
|
||||||
self.restful_plugin_url = args[0]
|
self.restful_plugin_url = args[0]
|
||||||
self.send_response(client, request, 'OK')
|
self.send_response(client, request, 'OK')
|
||||||
@ -330,7 +330,7 @@ class ServiceMonitor(object):
|
|||||||
LOG.warning('Failed to update restful plugin url: '
|
LOG.warning('Failed to update restful plugin url: '
|
||||||
'args=%s', str(args))
|
'args=%s', str(args))
|
||||||
self.send_response(client, request, 'ERR')
|
self.send_response(client, request, 'ERR')
|
||||||
elif cmd == 'certificate':
|
elif cmd == b'certificate':
|
||||||
try:
|
try:
|
||||||
self.certificate = args[0] if args else ''
|
self.certificate = args[0] if args else ''
|
||||||
self.send_response(client, request, 'OK')
|
self.send_response(client, request, 'OK')
|
||||||
@ -338,7 +338,7 @@ class ServiceMonitor(object):
|
|||||||
LOG.warning('Failed to update certificate path: '
|
LOG.warning('Failed to update certificate path: '
|
||||||
'args=%s', str(args))
|
'args=%s', str(args))
|
||||||
self.send_response(client, request, 'ERR')
|
self.send_response(client, request, 'ERR')
|
||||||
elif cmd == 'ceph-mgr-failures':
|
elif cmd == b'ceph-mgr-failures':
|
||||||
try:
|
try:
|
||||||
self.ceph_mgr_failure_count = int(args[0])
|
self.ceph_mgr_failure_count = int(args[0])
|
||||||
self.send_response(client, request, 'OK')
|
self.send_response(client, request, 'OK')
|
||||||
@ -349,7 +349,7 @@ class ServiceMonitor(object):
|
|||||||
LOG.warning('Failed to update ceph-mgr failures: '
|
LOG.warning('Failed to update ceph-mgr failures: '
|
||||||
'args=%s', str(args))
|
'args=%s', str(args))
|
||||||
self.send_response(client, request, 'ERR')
|
self.send_response(client, request, 'ERR')
|
||||||
elif cmd == 'ping-failures':
|
elif cmd == b'ping-failures':
|
||||||
try:
|
try:
|
||||||
self.ping_failure_count = int(args[0])
|
self.ping_failure_count = int(args[0])
|
||||||
self.send_response(client, request, 'OK')
|
self.send_response(client, request, 'OK')
|
||||||
@ -363,7 +363,7 @@ class ServiceMonitor(object):
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def send_response(client, request, response):
|
def send_response(client, request, response):
|
||||||
try:
|
try:
|
||||||
client.send(response)
|
client.send(response.encode('utf-8'))
|
||||||
except socket.error as err:
|
except socket.error as err:
|
||||||
LOG.warning('Failed to send response back. '
|
LOG.warning('Failed to send response back. '
|
||||||
'request=%s, response=%s, reason=%s',
|
'request=%s, response=%s, reason=%s',
|
||||||
@ -642,7 +642,9 @@ class ServiceMonitor(object):
|
|||||||
LOG.info('Run command: %s', ' '.join(command))
|
LOG.info('Run command: %s', ' '.join(command))
|
||||||
return subprocess.check_output(
|
return subprocess.check_output(
|
||||||
['/usr/bin/timeout', str(timeout)] + command,
|
['/usr/bin/timeout', str(timeout)] + command,
|
||||||
stderr=stderr, shell=False).strip()
|
stdin=subprocess.PIPE,
|
||||||
|
stderr=stderr, shell=False,
|
||||||
|
universal_newlines=True).strip()
|
||||||
except subprocess.CalledProcessError as err:
|
except subprocess.CalledProcessError as err:
|
||||||
if err.returncode == GNU_TIMEOUT_EXPIRED_RETCODE:
|
if err.returncode == GNU_TIMEOUT_EXPIRED_RETCODE:
|
||||||
raise CommandTimeout(command=err.cmd, timeout=timeout)
|
raise CommandTimeout(command=err.cmd, timeout=timeout)
|
||||||
@ -805,7 +807,7 @@ class ServiceMonitor(object):
|
|||||||
'[ req_distinguished_name ]\n'
|
'[ req_distinguished_name ]\n'
|
||||||
'0.organizationName = IT\n'
|
'0.organizationName = IT\n'
|
||||||
'commonName = ceph-restful\n').format(
|
'commonName = ceph-restful\n').format(
|
||||||
CONFIG.ceph_mgr_identity))
|
CONFIG.ceph_mgr_identity).encode('utf-8'))
|
||||||
restful_cnf.flush()
|
restful_cnf.flush()
|
||||||
subprocess.check_call([
|
subprocess.check_call([
|
||||||
'/usr/bin/openssl', 'req', '-new', '-nodes', '-x509',
|
'/usr/bin/openssl', 'req', '-new', '-nodes', '-x509',
|
||||||
@ -893,7 +895,7 @@ class ServiceMonitor(object):
|
|||||||
with open(os.devnull, 'wb') as null:
|
with open(os.devnull, 'wb') as null:
|
||||||
certificate = self.run_with_timeout(
|
certificate = self.run_with_timeout(
|
||||||
command, CONFIG.ceph_cli_timeout_sec, stderr=null)
|
command, CONFIG.ceph_cli_timeout_sec, stderr=null)
|
||||||
with open(CONFIG.restful_plugin_cert_path, 'wb') as cert_file:
|
with open(CONFIG.restful_plugin_cert_path, 'w') as cert_file:
|
||||||
cert_file.write(certificate)
|
cert_file.write(certificate)
|
||||||
self.certificate = CONFIG.restful_plugin_cert_path
|
self.certificate = CONFIG.restful_plugin_cert_path
|
||||||
self.request_update_certificate(
|
self.request_update_certificate(
|
||||||
@ -931,10 +933,10 @@ class ServiceMonitor(object):
|
|||||||
try:
|
try:
|
||||||
with contextlib.closing(
|
with contextlib.closing(
|
||||||
ServiceMonitor._make_client_socket()) as sock:
|
ServiceMonitor._make_client_socket()) as sock:
|
||||||
sock.send('status')
|
sock.send(b'status')
|
||||||
status = sock.recv(CONFIG.service_socket_bufsize)
|
status = sock.recv(CONFIG.service_socket_bufsize)
|
||||||
LOG.debug('Status %s', status)
|
LOG.debug('Status %s', status)
|
||||||
return status.startswith('OK')
|
return status.startswith(b'OK')
|
||||||
except socket.error as err:
|
except socket.error as err:
|
||||||
LOG.error('Status error: reason=%s', err)
|
LOG.error('Status error: reason=%s', err)
|
||||||
return False
|
return False
|
||||||
@ -944,7 +946,7 @@ class ServiceMonitor(object):
|
|||||||
try:
|
try:
|
||||||
with contextlib.closing(
|
with contextlib.closing(
|
||||||
ServiceMonitor._make_client_socket()) as sock:
|
ServiceMonitor._make_client_socket()) as sock:
|
||||||
sock.send('stop')
|
sock.send(b'stop')
|
||||||
response = sock.recv(CONFIG.service_socket_bufsize)
|
response = sock.recv(CONFIG.service_socket_bufsize)
|
||||||
LOG.debug('Stop response: %s', response)
|
LOG.debug('Stop response: %s', response)
|
||||||
return True
|
return True
|
||||||
@ -957,7 +959,7 @@ class ServiceMonitor(object):
|
|||||||
try:
|
try:
|
||||||
with contextlib.closing(
|
with contextlib.closing(
|
||||||
ServiceMonitor._make_client_socket()) as sock:
|
ServiceMonitor._make_client_socket()) as sock:
|
||||||
sock.send('ceph-mgr-failures {}'.format(count))
|
sock.send('ceph-mgr-failures {}'.format(count).encode('utf-8'))
|
||||||
sock.recv(CONFIG.service_socket_bufsize)
|
sock.recv(CONFIG.service_socket_bufsize)
|
||||||
return True
|
return True
|
||||||
except socket.error as err:
|
except socket.error as err:
|
||||||
@ -969,7 +971,7 @@ class ServiceMonitor(object):
|
|||||||
try:
|
try:
|
||||||
with contextlib.closing(
|
with contextlib.closing(
|
||||||
ServiceMonitor._make_client_socket()) as sock:
|
ServiceMonitor._make_client_socket()) as sock:
|
||||||
sock.send('ping-failures {}'.format(count))
|
sock.send('ping-failures {}'.format(count).encode('utf-8'))
|
||||||
sock.recv(CONFIG.service_socket_bufsize)
|
sock.recv(CONFIG.service_socket_bufsize)
|
||||||
return True
|
return True
|
||||||
except socket.error as err:
|
except socket.error as err:
|
||||||
@ -981,7 +983,7 @@ class ServiceMonitor(object):
|
|||||||
try:
|
try:
|
||||||
with contextlib.closing(
|
with contextlib.closing(
|
||||||
ServiceMonitor._make_client_socket()) as sock:
|
ServiceMonitor._make_client_socket()) as sock:
|
||||||
sock.send('restful-url {}'.format(url))
|
sock.send('restful-url {}'.format(url).encode('utf-8'))
|
||||||
sock.recv(CONFIG.service_socket_bufsize)
|
sock.recv(CONFIG.service_socket_bufsize)
|
||||||
return True
|
return True
|
||||||
except socket.error as err:
|
except socket.error as err:
|
||||||
@ -993,7 +995,7 @@ class ServiceMonitor(object):
|
|||||||
try:
|
try:
|
||||||
with contextlib.closing(
|
with contextlib.closing(
|
||||||
ServiceMonitor._make_client_socket()) as sock:
|
ServiceMonitor._make_client_socket()) as sock:
|
||||||
sock.send('certificate {}'.format(path))
|
sock.send('certificate {}'.format(path).encode('utf-8'))
|
||||||
sock.recv(CONFIG.service_socket_bufsize)
|
sock.recv(CONFIG.service_socket_bufsize)
|
||||||
return True
|
return True
|
||||||
except socket.error as err:
|
except socket.error as err:
|
||||||
@ -1041,16 +1043,16 @@ class InitWrapper(object):
|
|||||||
LOG = setup_logging(cleanup_handlers=True)
|
LOG = setup_logging(cleanup_handlers=True)
|
||||||
try:
|
try:
|
||||||
monitor = ServiceMonitor()
|
monitor = ServiceMonitor()
|
||||||
status = 'OK'
|
status = b'OK'
|
||||||
except ServiceAlreadyStarted:
|
except ServiceAlreadyStarted:
|
||||||
os.write(pipe[1], 'OK')
|
os.write(pipe[1], b'OK')
|
||||||
os.close(pipe[1])
|
os.close(pipe[1])
|
||||||
return
|
return
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
status = str(err)
|
status = str(err)
|
||||||
os.write(pipe[1], status)
|
os.write(pipe[1], status)
|
||||||
os.close(pipe[1])
|
os.close(pipe[1])
|
||||||
if status == 'OK':
|
if status == b'OK':
|
||||||
try:
|
try:
|
||||||
monitor.run()
|
monitor.run()
|
||||||
except ServiceException as err:
|
except ServiceException as err:
|
||||||
@ -1061,7 +1063,7 @@ class InitWrapper(object):
|
|||||||
os.close(pipe[1])
|
os.close(pipe[1])
|
||||||
try:
|
try:
|
||||||
status = os.read(pipe[0], CONFIG.service_socket_bufsize)
|
status = os.read(pipe[0], CONFIG.service_socket_bufsize)
|
||||||
if status == 'OK':
|
if status == b'OK':
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
else:
|
else:
|
||||||
LOG.warning('Service monitor failed to start: '
|
LOG.warning('Service monitor failed to start: '
|
||||||
|
Loading…
Reference in New Issue
Block a user