From: Cory Johns Date: Thu, 22 Dec 2016 16:42:29 +0000 (-0500) Subject: Fixed detecting controller for non-superuser X-Git-Tag: 0.2.0~9^2 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=fe19bc72a387b03fb157c873313ecac58b65e338;p=osm%2FN2VC.git Fixed detecting controller for non-superuser The `show-controller` command can only be used by superusers, so usually fails on shared controllers. https://bugs.launchpad.net/juju/+bug/1652106 --- diff --git a/juju/client/connection.py b/juju/client/connection.py index 452754e..5ed073f 100644 --- a/juju/client/connection.py +++ b/juju/client/connection.py @@ -12,7 +12,7 @@ import websockets import yaml -from juju.errors import JujuAPIError +from juju.errors import JujuAPIError, JujuConnectionError log = logging.getLogger("websocket") @@ -180,6 +180,8 @@ class Connection: """ jujudata = JujuData() controller_name = jujudata.current_controller() + if not controller_name: + raise JujuConnectionError('No current controller') return await cls.connect_controller(controller_name) @@ -271,10 +273,10 @@ class JujuData: self.path = os.path.abspath(os.path.expanduser(self.path)) def current_controller(self): - cmd = shlex.split('juju show-controller --format yaml') + cmd = shlex.split('juju list-controllers --format yaml') output = subprocess.check_output(cmd) output = yaml.safe_load(output) - return list(output.keys())[0] + return output.get('current-controller', '') def controllers(self): return self._load_yaml('controllers.yaml', 'controllers') diff --git a/juju/errors.py b/juju/errors.py index 9295267..6bb5530 100644 --- a/juju/errors.py +++ b/juju/errors.py @@ -5,3 +5,7 @@ class JujuAPIError(Exception): self.response = result['response'] self.request_id = result['request-id'] super().__init__(self.message) + + +class JujuConnectionError(ConnectionError): + pass