From 065914d696ffb39c77696925b55b9795147cae00 Mon Sep 17 00:00:00 2001 From: Alex Kavanagh Date: Wed, 21 Nov 2018 12:32:36 +0000 Subject: [PATCH] Fix more PY3 issues in check_rabbitmq_queues.py Note that, for PY3, the unbuffered IO in the check_rabbitmq.py script has to be switched off as PY3 doesn't support unbuffered IO for text (think str) rather than binary byte streams. Change-Id: I79af54cdbd7381f88732b24f65e94451350b9ab9 Closes-Bug: #1804349 Closes-Bug: #1804348 --- hooks/rabbitmq_server_relations.py | 13 +++++++++++++ scripts/check_rabbitmq.py | 15 +++++++++++---- scripts/check_rabbitmq_queues.py | 2 +- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/hooks/rabbitmq_server_relations.py b/hooks/rabbitmq_server_relations.py index 2c27f5ec..5d09e13c 100755 --- a/hooks/rabbitmq_server_relations.py +++ b/hooks/rabbitmq_server_relations.py @@ -79,6 +79,11 @@ from charmhelpers.contrib.hardening.harden import harden from charmhelpers.fetch import ( add_source, ) +from charmhelpers.fetch import ( + apt_install, + apt_update, + filter_installed_packages, +) from charmhelpers.core.hookenv import ( open_port, @@ -747,6 +752,14 @@ def upgrade_charm(): # Ensure all client connections are up to date on upgrade update_clients() + # BUG:#1804348 + # for the check_rabbitmq.py script, python3-amqplib needs to be installed; + # if previous version was a python2 version of the charm this won't happen + # unless the source is changed. Ensure it is installed here if needed. + apt_update(fatal=True) + if filter_installed_packages(['python3-amqplib']): + apt_install(['python3-amqplib'], fatal=True) + MAN_PLUGIN = 'rabbitmq_management' diff --git a/scripts/check_rabbitmq.py b/scripts/check_rabbitmq.py index 34415794..dc240b93 100755 --- a/scripts/check_rabbitmq.py +++ b/scripts/check_rabbitmq.py @@ -181,7 +181,10 @@ def main_loop(conn, exname): def main(host, port, exname, extype, user, password, vhost, ssl, ssl_ca): """ setup the connection and the communication channel """ - sys.stdout = os.fdopen(os.dup(1), "w", 0) + # BUG:#1804348 - can't have unbuffered text output in PY3 (unlike PY2), and + # this is essentially duping stdout which has to use text (str) and not + # bytes. The only compromise is to lose the unbuffered output. + sys.stdout = os.fdopen(os.dup(1), "w") host_port = "{}:{}".format(host, port) conn = get_connection(host_port, user, password, vhost, ssl, ssl_ca) @@ -240,9 +243,13 @@ if __name__ == '__main__': (options, args) = parser.parse_args() if options.verbose: - print(""" -Using AMQP setup: host:port={}:{} exchange_name={} exchange_type={} -""".format(options.host, options.port, options.exchange, options.type)) + print("") + print("Using AMQP setup: host:port={}:{} exchange_name={} " + "exchange_type={} " + .format(options.host, + options.port, + options.exchange, + options.type)) ret = main(options.host, options.port, options.exchange, options.type, options.user, options.password, options.vhost, options.ssl, options.ssl_ca) diff --git a/scripts/check_rabbitmq_queues.py b/scripts/check_rabbitmq_queues.py index ad16ceda..40ff5302 100755 --- a/scripts/check_rabbitmq_queues.py +++ b/scripts/check_rabbitmq_queues.py @@ -12,7 +12,7 @@ import sys def gen_data_lines(filename): - with open(filename, "rb") as fin: + with open(filename, "rt") as fin: for line in fin: if not line.startswith("#"): yield line