From patchwork Wed Jan 17 12:55:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Meng X-Patchwork-Id: 1887413 X-Patchwork-Delegate: horms@verge.net.au 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=UTgZGKgb; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=2605:bc80:3010::133; helo=smtp2.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=patchwork.ozlabs.org) Received: from smtp2.osuosl.org (smtp2.osuosl.org [IPv6:2605:bc80:3010::133]) (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 4TFQqQ4fP0z1yPg for ; Wed, 17 Jan 2024 23:56:18 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id D0DF543657; Wed, 17 Jan 2024 12:56:15 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp2.osuosl.org D0DF543657 Authentication-Results: smtp2.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=UTgZGKgb X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp2.osuosl.org ([127.0.0.1]) by localhost (smtp2.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id yT2Ke1LAa7va; Wed, 17 Jan 2024 12:56:13 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp2.osuosl.org (Postfix) with ESMTPS id 8DBDE40144; Wed, 17 Jan 2024 12:56:10 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp2.osuosl.org 8DBDE40144 Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 64A19C0DDB; Wed, 17 Jan 2024 12:56:09 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp2.osuosl.org (smtp2.osuosl.org [140.211.166.133]) by lists.linuxfoundation.org (Postfix) with ESMTP id 4383FC0DD2 for ; Wed, 17 Jan 2024 12:56:07 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id 190FF43622 for ; Wed, 17 Jan 2024 12:56:07 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp2.osuosl.org 190FF43622 X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp2.osuosl.org ([127.0.0.1]) by localhost (smtp2.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id nBqbAcGSHzCS for ; Wed, 17 Jan 2024 12:56:05 +0000 (UTC) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by smtp2.osuosl.org (Postfix) with ESMTPS id 396EC40144 for ; Wed, 17 Jan 2024 12:56:05 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp2.osuosl.org 396EC40144 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705496164; 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=wvFPGFyfFS0d4CGIFmDZZUOOXGoGZ+TChuWEI7m10fU=; b=UTgZGKgbwqG9CQSDQQ6evx9xZZOkAKpMoI0vyGBr5qLPP3TmBihxU4yHFjv511l8TyX9Fe 2Zjuy6OAZktZnCQWOb/l9K1JbffDxTzhc6F4Lbdtm/7Nqdln7gy2/3ZZmA4npXQ4l6+3Kp l1o4+9RXqsy7akjHHyMwQmkFQKoQe0w= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-413-3SBseJm5O_KkF-d2pI11tw-1; Wed, 17 Jan 2024 07:56:02 -0500 X-MC-Unique: 3SBseJm5O_KkF-d2pI11tw-1 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-337bcaef25aso695577f8f.0 for ; Wed, 17 Jan 2024 04:56:02 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705496162; x=1706100962; 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=wvFPGFyfFS0d4CGIFmDZZUOOXGoGZ+TChuWEI7m10fU=; b=Rz2h5YLVzz4jlfqmBbiiF+BOfYQn7luBqCRxLJySa1+2xvpFiG4ly1gW5lRXyraNAt ldyG8+A3/EaMyzutBYu/TclfFwCbhtzZDd2lO1daFMjIkvamcbSGaRAU6K7agn4gBKj3 sA0hloDYJwARdlGzB/rhdJhIXWADbNsg6shbXYZf3E8c79X1yfZ72DhvM67jiVoyokSU AwXtXiG4y0Fd40J0PKQxEXZo2Ppl5VCowRlMTH27sVhvMzl2I79XPbkCKGFasLlIA1/9 9G4xspGsuITe1b7d5eC0dSkhy/kS2vs8ZhGUnwg+oYH1wYFfgibuhw90NH6uIqmZVHwt jqhQ== X-Gm-Message-State: AOJu0Yw5mdvJRwK2ChJ+fHywVM5UmtIci6AtZM5/F2gjeqI1fXBItocE stDWkYyhqXaAPgC1xLJwdvY/ZBVfsBTzHQcEgTnLArMHiTmGMndqoFJPKX35gRDa3GKuUazoz00 AIs/bKjM/oSYVm5YazbV8Ad4MWxQJFQJ2Yj8RW2sLa/3z5BdvWC/doMTX8LRQAvYHHPQyqzp4+4 Y= X-Received: by 2002:adf:a4dc:0:b0:336:86af:ede5 with SMTP id h28-20020adfa4dc000000b0033686afede5mr549790wrb.46.1705496161655; Wed, 17 Jan 2024 04:56:01 -0800 (PST) X-Google-Smtp-Source: AGHT+IH/F7g+1kPuissR4YEKjpUa9jMMPWAt3MMIaRenDlhZsZHz2FsX5yXimSD+DCl8aS4RbY2eBA== X-Received: by 2002:adf:a4dc:0:b0:336:86af:ede5 with SMTP id h28-20020adfa4dc000000b0033686afede5mr549774wrb.46.1705496161187; Wed, 17 Jan 2024 04:56:01 -0800 (PST) Received: from positronik4lide.redhat.com ([87.122.58.78]) by smtp.gmail.com with ESMTPSA id y7-20020adff147000000b00337bdadab6fsm1615551wro.66.2024.01.17.04.56.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Jan 2024 04:56:00 -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: Wed, 17 Jan 2024 13:55:35 +0100 Message-Id: <20240117125539.106700-3-jmeng@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240117125539.106700-1-jmeng@redhat.com> References: <20240117125539.106700-1-jmeng@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [ovs-dev] [PATCH v5 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 --- 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 94dabeb5b..4df02e6b2 100644 --- a/NEWS +++ b/NEWS @@ -29,6 +29,8 @@ Post-v3.2.0 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