2012-05-02 15:21:36 -07:00
|
|
|
# Copyright (c) 2012 Nicira, Inc.
|
2012-02-16 19:15:01 -08:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at:
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import ovs.daemon
|
|
|
|
import ovs.unixctl
|
2012-05-14 11:26:36 -07:00
|
|
|
import ovs.unixctl.client
|
2012-02-16 19:15:01 -08:00
|
|
|
import ovs.util
|
|
|
|
import ovs.vlog
|
2016-08-02 17:45:39 +00:00
|
|
|
from ovs.fatal_signal import signal_alarm
|
2012-02-16 19:15:01 -08:00
|
|
|
|
|
|
|
|
|
|
|
def connect_to_target(target):
|
|
|
|
error, str_result = ovs.unixctl.socket_name_from_target(target)
|
|
|
|
if error:
|
|
|
|
ovs.util.ovs_fatal(error, str_result)
|
|
|
|
else:
|
|
|
|
socket_name = str_result
|
|
|
|
|
2012-05-14 11:26:36 -07:00
|
|
|
error, client = ovs.unixctl.client.UnixctlClient.create(socket_name)
|
2012-02-16 19:15:01 -08:00
|
|
|
if error:
|
|
|
|
ovs.util.ovs_fatal(error, "cannot connect to \"%s\"" % socket_name)
|
|
|
|
|
|
|
|
return client
|
|
|
|
|
|
|
|
|
2024-07-09 09:14:20 +02:00
|
|
|
def reply_to_string(reply, fmt=ovs.unixctl.UnixctlOutputFormat.TEXT,
|
|
|
|
fmt_flags={}):
|
2024-07-09 09:14:18 +02:00
|
|
|
if fmt == ovs.unixctl.UnixctlOutputFormat.TEXT:
|
|
|
|
body = str(reply)
|
|
|
|
else:
|
2024-07-09 09:14:20 +02:00
|
|
|
body = ovs.json.to_string(reply, **fmt_flags)
|
2024-07-09 09:14:18 +02:00
|
|
|
|
|
|
|
if body and not body.endswith("\n"):
|
|
|
|
body += "\n"
|
|
|
|
|
|
|
|
return body
|
|
|
|
|
|
|
|
|
2012-02-16 19:15:01 -08:00
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser(description="Python Implementation of"
|
|
|
|
" ovs-appctl.")
|
|
|
|
parser.add_argument("-t", "--target", default="ovs-vswitchd",
|
|
|
|
help="pidfile or socket to contact")
|
|
|
|
|
|
|
|
parser.add_argument("command", metavar="COMMAND",
|
|
|
|
help="Command to run.")
|
|
|
|
parser.add_argument("argv", metavar="ARG", nargs="*",
|
|
|
|
help="Arguments to the command.")
|
2012-03-06 16:10:46 -08:00
|
|
|
parser.add_argument("-T", "--timeout", metavar="SECS",
|
|
|
|
help="wait at most SECS seconds for a response")
|
2024-07-09 09:14:18 +02:00
|
|
|
parser.add_argument("-f", "--format", metavar="FMT",
|
|
|
|
help="Output format.", default="text",
|
|
|
|
choices=[fmt.name.lower()
|
|
|
|
for fmt in ovs.unixctl.UnixctlOutputFormat],
|
|
|
|
type=str.lower)
|
2024-07-09 09:14:20 +02:00
|
|
|
parser.add_argument("--pretty", action="store_true",
|
|
|
|
help="Format the output in a more readable fashion."
|
|
|
|
" Requires: --format json.")
|
2012-02-16 19:15:01 -08:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2024-07-09 09:14:20 +02:00
|
|
|
if (args.format != ovs.unixctl.UnixctlOutputFormat.JSON.name.lower()
|
|
|
|
and args.pretty):
|
|
|
|
ovs.util.ovs_fatal(0, "--pretty is supported with --format json only")
|
|
|
|
|
2018-08-14 10:53:16 +03:00
|
|
|
signal_alarm(int(args.timeout) if args.timeout else None)
|
2012-03-06 16:10:46 -08:00
|
|
|
|
2012-02-16 19:15:01 -08:00
|
|
|
ovs.vlog.Vlog.init()
|
|
|
|
target = args.target
|
2024-07-09 09:14:18 +02:00
|
|
|
format = ovs.unixctl.UnixctlOutputFormat[args.format.upper()]
|
2024-07-09 09:14:20 +02:00
|
|
|
format_flags = dict(pretty=True) if args.pretty else {}
|
2012-02-16 19:15:01 -08:00
|
|
|
client = connect_to_target(target)
|
2024-07-09 09:14:18 +02:00
|
|
|
|
|
|
|
if format != ovs.unixctl.UnixctlOutputFormat.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(reply_to_string(error))
|
|
|
|
ovs.util.ovs_error(0, "%s: server returned an error" % target)
|
|
|
|
sys.exit(2)
|
|
|
|
|
2012-02-16 19:15:01 -08:00
|
|
|
err_no, error, result = client.transact(args.command, args.argv)
|
|
|
|
client.close()
|
|
|
|
|
|
|
|
if err_no:
|
|
|
|
ovs.util.ovs_fatal(err_no, "%s: transaction error" % target)
|
|
|
|
elif error is not None:
|
2024-07-09 09:14:18 +02:00
|
|
|
sys.stderr.write(reply_to_string(error))
|
2012-02-16 19:15:01 -08:00
|
|
|
ovs.util.ovs_error(0, "%s: server returned an error" % target)
|
|
|
|
sys.exit(2)
|
|
|
|
else:
|
|
|
|
assert result is not None
|
2024-07-09 09:14:20 +02:00
|
|
|
sys.stdout.write(reply_to_string(result, format, format_flags))
|
2012-02-16 19:15:01 -08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|