From 56c1914ad89bbcb144a634b08e63e6af4367b0c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Luttringer?= Date: Thu, 30 Jul 2015 18:01:59 +0200 Subject: [PATCH] Merge forward commands into a module --- cloudcontrol/cli/commands/delforward.py | 44 --------------- .../commands/{addforward.py => forward.py} | 55 ++++++++++++++++++- cloudcontrol/cli/commands/forwards.py | 50 ----------------- 3 files changed, 53 insertions(+), 96 deletions(-) delete mode 100644 cloudcontrol/cli/commands/delforward.py rename cloudcontrol/cli/commands/{addforward.py => forward.py} (60%) delete mode 100644 cloudcontrol/cli/commands/forwards.py diff --git a/cloudcontrol/cli/commands/delforward.py b/cloudcontrol/cli/commands/delforward.py deleted file mode 100644 index 66caee1..0000000 --- a/cloudcontrol/cli/commands/delforward.py +++ /dev/null @@ -1,44 +0,0 @@ -#coding=utf8 - -# This file is part of CloudControl. -# -# CloudControl is free software: you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# CloudControl is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with CloudControl. If not, see . - -''' -CloudControl delforward command -''' - -from cloudcontrol.cli.command import Command -from cloudcontrol.cli.exception import * - -class Command_delforward(Command): - '''Delete forwarded connection''' - - def __init__(self, cli, argv0): - Command.__init__(self, cli, argv0) - - def __call__(self, argv): - # argv check - if len(argv) < 2: - raise cmdBadArgument() - for fwd_id in argv[1:]: - if fwd_id in self.cli.forwards: - self.printer.out("Closing %s" % fwd_id) - self.cli.forwards[fwd_id].stop() - del self.cli.forwards[fwd_id] - else: - self.printer.warn("No such forward: %s" % fwd_id) - - def usage(self): - return "Usage: delforward [tunnel id]..." diff --git a/cloudcontrol/cli/commands/addforward.py b/cloudcontrol/cli/commands/forward.py similarity index 60% rename from cloudcontrol/cli/commands/addforward.py rename to cloudcontrol/cli/commands/forward.py index 9b7c57b..c446943 100644 --- a/cloudcontrol/cli/commands/addforward.py +++ b/cloudcontrol/cli/commands/forward.py @@ -16,13 +16,42 @@ # along with CloudControl. If not, see . ''' -CloudControl addforward command +CloudControl forward related commands ''' -from cloudcontrol.cli.command import TqlCommand +from cloudcontrol.cli.command import Command, TqlCommand from cloudcontrol.cli.tunnel import Forward +from cloudcontrol.cli.printer import color from cloudcontrol.cli.exception import * + +class Command_forwards(Command): + '''List open forwards''' + + def __init__(self, cli, argv0): + Command.__init__(self, cli, argv0) + + def __call__(self, argv): + # argv check + if len(argv) != 1: + raise cmdBadArgument() + out = self.printer.out + for fwd in self.cli.forwards.values(): + out("Forward: %s%s%s" % (color["lpurple"], fwd.id, color["reset"])) + out(" HostID: %s" % fwd.host_id) + out(" Local socket: %s %s" % (fwd.source_ip, fwd.source_port)) + out(" Remote socket: %s %s" % (fwd.dest_ip, fwd.dest_port)) + out(" Tunnels:") + out(" count: %s" % len(fwd.tunnels)) + for label, tun in fwd.tunnels.items(): + remote_ip, remote_port = tun.endpoint.getpeername() + out(" label: %s, remote ip: %s, remote port: %s" + % (label, remote_ip, remote_port)) + out("Forward count: %d, Tunnels count: %d" % + (len(self.cli.forwards), + sum(len(f.tunnels) for f in self.cli.forwards.values()))) + + class Command_addforward(TqlCommand): '''Create a forward''' @@ -74,3 +103,25 @@ class Command_addforward(TqlCommand): def remote_functions(self): return set(("list", "forward")) + + +class Command_delforward(Command): + '''Delete forwarded connection''' + + def __init__(self, cli, argv0): + Command.__init__(self, cli, argv0) + + def __call__(self, argv): + # argv check + if len(argv) < 2: + raise cmdBadArgument() + for fwd_id in argv[1:]: + if fwd_id in self.cli.forwards: + self.printer.out("Closing %s" % fwd_id) + self.cli.forwards[fwd_id].stop() + del self.cli.forwards[fwd_id] + else: + self.printer.warn("No such forward: %s" % fwd_id) + + def usage(self): + return "Usage: delforward [tunnel id]..." diff --git a/cloudcontrol/cli/commands/forwards.py b/cloudcontrol/cli/commands/forwards.py deleted file mode 100644 index 8b9ffc1..0000000 --- a/cloudcontrol/cli/commands/forwards.py +++ /dev/null @@ -1,50 +0,0 @@ -#coding=utf8 - -# This file is part of CloudControl. -# -# CloudControl is free software: you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# CloudControl is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with CloudControl. If not, see . - -''' -CloudControl forwards command -''' - -from cloudcontrol.cli.command import Command -from cloudcontrol.cli.printer import color -from cloudcontrol.cli.exception import * - -class Command_forwards(Command): - '''List open forwards''' - - def __init__(self, cli, argv0): - Command.__init__(self, cli, argv0) - - def __call__(self, argv): - # argv check - if len(argv) != 1: - raise cmdBadArgument() - out = self.printer.out - for fwd in self.cli.forwards.values(): - out("Forward: %s%s%s" % (color["lpurple"], fwd.id, color["reset"])) - out(" HostID: %s" % fwd.host_id) - out(" Local socket: %s %s" % (fwd.source_ip, fwd.source_port)) - out(" Remote socket: %s %s" % (fwd.dest_ip, fwd.dest_port)) - out(" Tunnels:") - out(" count: %s" % len(fwd.tunnels)) - for label, tun in fwd.tunnels.items(): - remote_ip, remote_port = tun.endpoint.getpeername() - out(" label: %s, remote ip: %s, remote port: %s" - % (label, remote_ip, remote_port)) - out("Forward count: %d, Tunnels count: %d" % - (len(self.cli.forwards), - sum(len(f.tunnels) for f in self.cli.forwards.values()))) -- GitLab