From patchwork Thu Jan 18 15:26:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Meng X-Patchwork-Id: 1888111 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=HLf/3ayr; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.136; helo=smtp3.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=patchwork.ozlabs.org) Received: from smtp3.osuosl.org (smtp3.osuosl.org [140.211.166.136]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TG67D2Klkz23dx for ; Fri, 19 Jan 2024 02:27:20 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by smtp3.osuosl.org (Postfix) with ESMTP id 9224A61AD6; Thu, 18 Jan 2024 15:27:17 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp3.osuosl.org 9224A61AD6 Authentication-Results: smtp3.osuosl.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=HLf/3ayr X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp3.osuosl.org ([127.0.0.1]) by localhost (smtp3.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 2aZKq1fLOVDB; Thu, 18 Jan 2024 15:27:16 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp3.osuosl.org (Postfix) with ESMTPS id 493BA60D59; Thu, 18 Jan 2024 15:27:15 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp3.osuosl.org 493BA60D59 Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 228C0C0DD5; Thu, 18 Jan 2024 15:27:13 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp1.osuosl.org (smtp1.osuosl.org [140.211.166.138]) by lists.linuxfoundation.org (Postfix) with ESMTP id 831FAC0DD3 for ; Thu, 18 Jan 2024 15:27:12 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 4F0268402B for ; Thu, 18 Jan 2024 15:27:12 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org 4F0268402B Authentication-Results: smtp1.osuosl.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=HLf/3ayr X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id WR1IP8VBueAl for ; Thu, 18 Jan 2024 15:27:11 +0000 (UTC) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by smtp1.osuosl.org (Postfix) with ESMTPS id D1FD584022 for ; Thu, 18 Jan 2024 15:27:10 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org D1FD584022 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705591629; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=qmAINohITAjYl9STAXFF1G1/WKjbrF+bQ/0r70PjpV4=; b=HLf/3ayrONEIRneQgsC496j4j6HJZqWc/QIzFNtvrwwSoOyD65UHKX5beqL2j/bxeBZCmE 8NVnMKe/uxFk5Zs2xiJ4byQlFVxfsvAMdWeJjcvfOcPKOZIqGIcxYewGb6Xvs5VgzucMGu 5Sn4+iCHQ+GojJYulcKsnm8DKdpQVHk= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-689-Jnio0ve_M5mDeeVy9J-kbg-1; Thu, 18 Jan 2024 10:27:08 -0500 X-MC-Unique: Jnio0ve_M5mDeeVy9J-kbg-1 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-337bf78ef28so1011100f8f.3 for ; Thu, 18 Jan 2024 07:27:08 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705591627; x=1706196427; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qmAINohITAjYl9STAXFF1G1/WKjbrF+bQ/0r70PjpV4=; b=KUgtWxUNJWepsgprRmdNnIzyNlio7fjK9l0dD9qyNhQS8DQxTPVftUDRk1VWrAUO2L 9YlgR21hNHMwGGU53gtnSUCy/mLeWTPe1TC57y7YBVPuLDwPDuD+mQzWZ8zmBwGutqI6 e2N6h4Q9pyCBwLVjabzUKJGuj8n7aX3QXPw/DCn0EYVtS7eKMideyG9C5PXSFb9dVSXR piGdIjPvFtHCBVO/xjgKxVzSSpgsFBc+y9PQ1d8QiaeFLQV30FZv1p8hF2qUOc13Y2ng saEChVeuZFWeGU2JvK/uxbX+wr/4dRISDzgCKNOdtL3ETbr7F5fs33xp7Lh8t0VURGe8 mdpw== X-Gm-Message-State: AOJu0Yy2kplPLIPzAny0MVm2bwabIXaydYHrK3pQSOdFE5ly/kL7s3dy 1WvEMp9kmJ2BzBGRXNXoQWNbv/UFMLb8ceYye3yt6YfcvoQJaGas8G5StjXBx7l+E4K0FY7DRLT B2R7QKTZuCpSet3FBnE4DAt98DGq4Zo/W6cj1snDcUDK9LLRev6UrPpvimB1FVjWJNOX0IIuXsW qV3LyVJyPM+Y1pjBig3H5OqaUUpWAh X-Received: by 2002:a05:600c:44d6:b0:40e:5496:281 with SMTP id f22-20020a05600c44d600b0040e54960281mr685407wmo.67.1705591627284; Thu, 18 Jan 2024 07:27:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IFMoAzJgDoLlUwODrltIwAOzoZE7puw07GSalYy+gtrxsBP/K8qRzmYnVpFGhox6Q9Y8RrwkQ== X-Received: by 2002:a05:600c:44d6:b0:40e:5496:281 with SMTP id f22-20020a05600c44d600b0040e54960281mr685393wmo.67.1705591626977; Thu, 18 Jan 2024 07:27:06 -0800 (PST) Received: from positronik4lide.redhat.com ([87.122.58.78]) by smtp.gmail.com with ESMTPSA id d5-20020a05600c34c500b0040e95a08459sm1251035wmq.34.2024.01.18.07.27.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Jan 2024 07:27:06 -0800 (PST) From: jmeng@redhat.com To: dev@openvswitch.org, i.maximets@ovn.org, echaudro@redhat.com, ktraynor@redhat.com, aconole@redhat.com, rjarry@redhat.com Date: Thu, 18 Jan 2024 16:26:53 +0100 Message-Id: <20240118152657.2816536-3-jmeng@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240118152657.2816536-1-jmeng@redhat.com> References: <20240118152657.2816536-1-jmeng@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [ovs-dev] [PATCH v7 2/6] python: Add global option for JSON output to Python tools. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" From: Jakob Meng This patch introduces support for different output formats to the Python code, as did the previous commit for ovs-xxx tools like 'ovs-appctl --format json dpif/show'. In particular, tests/appctl.py gains a global option '-f,--format' which allows users to request JSON instead of plain-text for humans. This patch does not yet pass the choosen output format to commands. Doing so requires changes to all command_register() calls and all command callbacks. To improve readibility those changes have been split out into a follow up patch. Respectively, whenever an output format other than 'text' is choosen for tests/appctl.py, the script will fail. Reported-at: https://bugzilla.redhat.com/1824861 Signed-off-by: Jakob Meng Acked-by: Eelco Chaudron --- NEWS | 2 ++ python/ovs/unixctl/__init__.py | 39 +++++++++++++++++++++++++++++----- python/ovs/unixctl/server.py | 37 +++++++++++++++++++++++++++++++- python/ovs/util.py | 8 +++++++ tests/appctl.py | 17 +++++++++++++++ tests/unixctl-py.at | 1 + 6 files changed, 98 insertions(+), 6 deletions(-) diff --git a/NEWS b/NEWS index 8631ea45e..94a347246 100644 --- a/NEWS +++ b/NEWS @@ -35,6 +35,8 @@ v3.3.0 - xx xxx xxxx on mark and labels. * Added new option [-f|--format] to choose the output format, e.g. 'json' or 'text' (by default). + - Python: + * Added support for choosing the output format, e.g. 'json' or 'text'. - ovs-vsctl: * New commands 'set-zone-limit', 'del-zone-limit' and 'list-zone-limits' to manage the maximum number of connections in conntrack zones via diff --git a/python/ovs/unixctl/__init__.py b/python/ovs/unixctl/__init__.py index 8ee312943..6d9bd5715 100644 --- a/python/ovs/unixctl/__init__.py +++ b/python/ovs/unixctl/__init__.py @@ -20,10 +20,14 @@ commands = {} class _UnixctlCommand(object): - def __init__(self, usage, min_args, max_args, callback, aux): + # FIXME: Output format will be passed as 'output_fmts' to the command in + # later patch. + def __init__(self, usage, min_args, max_args, # FIXME: output_fmts, + callback, aux): self.usage = usage self.min_args = min_args self.max_args = max_args + # FIXME: self.output_fmts = output_fmts self.callback = callback self.aux = aux @@ -42,10 +46,13 @@ def _unixctl_help(conn, unused_argv, unused_aux): conn.reply(reply) -def command_register(name, usage, min_args, max_args, callback, aux): +def command_register_fmt(name, usage, min_args, max_args, output_fmts, + callback, aux): """ Registers a command with the given 'name' to be exposed by the UnixctlServer. 'usage' describes the arguments to the command; it is used - only for presentation to the user in "help" output. + only for presentation to the user in "help" output. 'output_fmts' is a + bitmap that defines what output formats a command supports, e.g. + OVS_OUTPUT_FMT_TEXT | OVS_OUTPUT_FMT_JSON. 'callback' is called when the command is received. It is passed a UnixctlConnection object, the list of arguments as unicode strings, and @@ -63,8 +70,30 @@ def command_register(name, usage, min_args, max_args, callback, aux): assert callable(callback) if name not in commands: - commands[name] = _UnixctlCommand(usage, min_args, max_args, callback, - aux) + commands[name] = _UnixctlCommand(usage, min_args, max_args, + # FIXME: output_fmts, + callback, aux) + + +# FIXME: command_register() will be replaced with command_register_fmt() in a +# later patch of this series. It is is kept temporarily to reduce the +# amount of changes in this patch. +def command_register(name, usage, min_args, max_args, callback, aux): + """ Registers a command with the given 'name' to be exposed by the + UnixctlServer. 'usage' describes the arguments to the command; it is used + only for presentation to the user in "help" output. + + 'callback' is called when the command is received. It is passed a + UnixctlConnection object, the list of arguments as unicode strings, and + 'aux'. Normally 'callback' should reply by calling + UnixctlConnection.reply() or UnixctlConnection.reply_error() before it + returns, but if the command cannot be handled immediately, then it can + defer the reply until later. A given connection can only process a single + request at a time, so a reply must be made eventually to avoid blocking + that connection.""" + + command_register_fmt(name, usage, min_args, max_args, + ovs.util.OutputFormat.TEXT, callback, aux) def socket_name_from_target(target): diff --git a/python/ovs/unixctl/server.py b/python/ovs/unixctl/server.py index b9cb52fad..1d7f3337d 100644 --- a/python/ovs/unixctl/server.py +++ b/python/ovs/unixctl/server.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import argparse import copy import errno import os @@ -35,6 +36,7 @@ class UnixctlConnection(object): assert isinstance(rpc, ovs.jsonrpc.Connection) self._rpc = rpc self._request_id = None + self._fmt = ovs.util.OutputFormat.TEXT def run(self): self._rpc.run() @@ -116,6 +118,17 @@ class UnixctlConnection(object): elif len(params) > command.max_args: error = '"%s" command takes at most %d arguments' \ % (method, command.max_args) + # FIXME: Uncomment when command.output_fmts is available + # elif self._fmt not in command.output_fmts: + # error = '"%s" command does not support output format' \ + # ' "%s" (supported: %d, requested: %d)' \ + # % (method, self._fmt.name.lower(), command.output_fmts, + # self._fmt) + # FIXME: Remove this check once output format will be passed to the + # command handler below. + elif self._fmt != ovs.util.OutputFormat.TEXT: + error = 'output format "%s" has not been implemented yet' \ + % self._fmt.name.lower() else: for param in params: if not isinstance(param, str): @@ -124,7 +137,8 @@ class UnixctlConnection(object): if error is None: unicode_params = [str(p) for p in params] - command.callback(self, unicode_params, command.aux) + command.callback(self, unicode_params, # FIXME: self._fmt, + command.aux) if error: self.reply_error(error) @@ -136,6 +150,24 @@ def _unixctl_version(conn, unused_argv, version): conn.reply(version) +def _unixctl_set_options(conn, argv, unused_aux): + assert isinstance(conn, UnixctlConnection) + + parser = argparse.ArgumentParser() + parser.add_argument("--format", default="text", + choices=[fmt.name.lower() + for fmt in ovs.util.OutputFormat]) + + try: + args = parser.parse_args(args=argv) + except argparse.ArgumentError as e: + conn.reply_error(str(e)) + return + + conn._fmt = ovs.util.OutputFormat[args.format.upper()] + conn.reply(None) + + class UnixctlServer(object): def __init__(self, listener): assert isinstance(listener, ovs.stream.PassiveStream) @@ -210,4 +242,7 @@ class UnixctlServer(object): ovs.unixctl.command_register("version", "", 0, 0, _unixctl_version, version) + ovs.unixctl.command_register("set-options", "[--format text|json]", 2, + 2, _unixctl_set_options, None) + return 0, UnixctlServer(listener) diff --git a/python/ovs/util.py b/python/ovs/util.py index 3dba022f8..272ca683d 100644 --- a/python/ovs/util.py +++ b/python/ovs/util.py @@ -15,11 +15,19 @@ import os import os.path import sys +import enum PROGRAM_NAME = os.path.basename(sys.argv[0]) EOF = -1 +@enum.unique +# FIXME: Use @enum.verify(enum.NAMED_FLAGS) from Python 3.11 when available. +class OutputFormat(enum.IntFlag): + TEXT = 1 << 0 + JSON = 1 << 1 + + def abs_file_name(dir_, file_name): """If 'file_name' starts with '/', returns a copy of 'file_name'. Otherwise, returns an absolute path to 'file_name' considering it relative diff --git a/tests/appctl.py b/tests/appctl.py index b85b364fa..4e8c4adc0 100644 --- a/tests/appctl.py +++ b/tests/appctl.py @@ -49,13 +49,30 @@ def main(): help="Arguments to the command.") parser.add_argument("-T", "--timeout", metavar="SECS", help="wait at most SECS seconds for a response") + parser.add_argument("-f", "--format", metavar="FMT", + help="Output format.", default="text", + choices=[fmt.name.lower() + for fmt in ovs.util.OutputFormat]) args = parser.parse_args() signal_alarm(int(args.timeout) if args.timeout else None) ovs.vlog.Vlog.init() target = args.target + format = ovs.util.OutputFormat[args.format.upper()] client = connect_to_target(target) + + if format != ovs.util.OutputFormat.TEXT: + err_no, error, _ = client.transact( + "set-options", ["--format", args.format]) + + if err_no: + ovs.util.ovs_fatal(err_no, "%s: transaction error" % target) + elif error is not None: + sys.stderr.write(error) + ovs.util.ovs_error(0, "%s: server returned an error" % target) + sys.exit(2) + err_no, error, result = client.transact(args.command, args.argv) client.close() diff --git a/tests/unixctl-py.at b/tests/unixctl-py.at index 724006118..26c137047 100644 --- a/tests/unixctl-py.at +++ b/tests/unixctl-py.at @@ -100,6 +100,7 @@ The available commands are: exit help log [[arg ...]] + set-options [[--format text|json]] version vlog/close vlog/list