Skip to content

Commit

Permalink
Use a more flexible error handling mechanism
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Graber <stgraber@ubuntu.com>
  • Loading branch information
stgraber committed Jul 9, 2014
1 parent 54aef68 commit 83a5e81
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 15 deletions.
6 changes: 5 additions & 1 deletion src/lxccmd/lxc
Expand Up @@ -32,6 +32,7 @@ sys.path.insert(0, os.path.join(sys.path[0], os.pardir))

from lxccmd.commands import get_commands
from lxccmd.certs import generate_cert
from lxccmd.exceptions import LXCError

# Setup i18n
_ = gettext.gettext
Expand Down Expand Up @@ -86,4 +87,7 @@ args = parser.parse_args()
if args.debug:
logging.root.setLevel(args.debug.upper())

args.func(parser, args)
try:
args.func(args)
except LXCError as e:
parser.error(e)
25 changes: 13 additions & 12 deletions src/lxccmd/lxccmd/commands/server/__init__.py
Expand Up @@ -33,6 +33,7 @@
trust_cert_add, trust_cert_list, trust_cert_remove, trust_cert_verify
from lxccmd.config import get_run_path
from lxccmd.cli import render_table
from lxccmd.exceptions import LXCError
from lxccmd.network import server_is_running

try:
Expand Down Expand Up @@ -159,16 +160,16 @@ def cli_subparser(sp):
sp_set.set_defaults(func=cli_set)


def cli_status(parser, args):
def cli_status(args):
render_table([[_("Running"), _("Trusted clients")],
[str(server_is_running()),
", ".join(trust_cert_list("server"))]],
header=True, orientation="vertical")


def cli_start(parser, args):
def cli_start(args):
if server_is_running():
parser.error(_("A server is already running!"))
raise LXCError(_("A server is already running!"))

# Get or generate the server certificate
generate_cert("server")
Expand Down Expand Up @@ -210,14 +211,14 @@ def cli_start(parser, args):
httpd.serve_forever()


def cli_stop(parser, args):
def cli_stop(args):
if not server_is_running():
parser.error(_("The server isn't running at the moment!"))
raise LXCError(_("The server isn't running at the moment!"))

server_pid_path = os.path.join(get_run_path(), "server.pid")

if not os.path.exists(server_pid_path):
parser.error(_("No PID on record for running server!"))
raise LXCError(_("No PID on record for running server!"))

with open(server_pid_path, "r") as fd:
server_pid = int(fd.read().strip())
Expand All @@ -227,30 +228,30 @@ def cli_stop(parser, args):
os.kill(server_pid, 9)


def cli_trust(parser, args):
def cli_trust(args):
if not os.path.exists(args.client_cert):
parser.error(_("The file doesn't exist."))
raise LXCError(_("The file doesn't exist."))

with open(args.client_cert, "r") as fd:
certificate = fd.read()

if not trust_cert_add(certificate, "server"):
parser.error(_("Failed to add the certificate to the trust store."))
raise LXCError(_("Failed to add the certificate to the trust store."))


def cli_forget(parser, args):
def cli_forget(args):
if os.path.exists(args.clientid):
with open(args.clientid, "r") as fd:
certificate = fd.read()
else:
certificate = args.clientid

if not trust_cert_remove(certificate, "server"):
parser.error(
raise LXCError(
_("Failed to remove the certificate from the trust store."))


def cli_set(parser, args):
def cli_set(args):
pass


Expand Down
2 changes: 1 addition & 1 deletion src/lxccmd/lxccmd/commands/status/__init__.py
Expand Up @@ -145,7 +145,7 @@ def cli_subparser(sp):
parser.set_defaults(func=cli_status)


def cli_status(parser, args):
def cli_status(args):
keys = []
values = []

Expand Down
24 changes: 24 additions & 0 deletions src/lxccmd/lxccmd/exceptions.py
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
#
# lxc: New LXC command line client
#
# Authors:
# Stephane Graber <stgraber@ubuntu.com> (Canonical Ltd. 2014)
#
# This library 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 2.1 of the License, or (at your option) any later version.
#
# This library 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 this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA


class LXCError(Exception):
pass
2 changes: 1 addition & 1 deletion src/lxccmd/tests/test_command_status.py
Expand Up @@ -272,7 +272,7 @@ def test_cli_status(self):

sys.stdout = StringIO()
args = parser.parse_args(["status"])
args.func(parser, args)
args.func(args)

self.assertEquals(len(sys.stdout.getvalue().strip().split("\n")), 7)

Expand Down

0 comments on commit 83a5e81

Please sign in to comment.