~abentley/juju-ci-tools/client-from-config-4

« back to all changes in this revision

Viewing changes to jujupy.py

  • Committer: Aaron Bentley
  • Date: 2016-07-05 14:15:26 UTC
  • mfrom: (1468.1.6 client-from-config-3)
  • Revision ID: aaron.bentley@canonical.com-20160705141526-v1gveg82a4swb3fb
Merged client-from-config-3 into client-from-config-4.

Show diffs side-by-side

added added

removed removed

Lines of Context:
920
920
    def get_cache_path(self):
921
921
        return get_cache_path(self.env.juju_home, models=True)
922
922
 
923
 
    def _cmd_model(self, include_e, admin):
924
 
        if admin:
 
923
    def _cmd_model(self, include_e, controller):
 
924
        if controller:
925
925
            return '{controller}:{model}'.format(
926
926
                controller=self.env.controller.name,
927
 
                model=self.get_admin_model_name())
 
927
                model=self.get_controller_model_name())
928
928
        elif self.env is None or not include_e:
929
929
            return None
930
930
        else:
933
933
                model=self.model_name)
934
934
 
935
935
    def _full_args(self, command, sudo, args,
936
 
                   timeout=None, include_e=True, admin=False):
937
 
        model = self._cmd_model(include_e, admin)
 
936
                   timeout=None, include_e=True, controller=False):
 
937
        model = self._cmd_model(include_e, controller)
938
938
        # sudo is not needed for devel releases.
939
939
        return self._backend.full_args(command, args, model, timeout)
940
940
 
1140
1140
        <environment> flag will be placed after <command> and before args.
1141
1141
        """
1142
1142
        model = self._cmd_model(kwargs.get('include_e', True),
1143
 
                                kwargs.get('admin', False))
 
1143
                                kwargs.get('controller', False))
1144
1144
        timeout = kwargs.get('timeout')
1145
1145
        return self._backend.get_juju_output(
1146
1146
            command, args, self.used_feature_flags, self.env.juju_home,
1150
1150
        """Print the status to output."""
1151
1151
        self.juju(self._show_status, ('--format', 'yaml'))
1152
1152
 
1153
 
    def get_status(self, timeout=60, raw=False, admin=False, *args):
 
1153
    def get_status(self, timeout=60, raw=False, controller=False, *args):
1154
1154
        """Get the current status as a dict."""
1155
1155
        # GZ 2015-12-16: Pass remaining timeout into get_juju_output call.
1156
1156
        for ignored in until_timeout(timeout):
1159
1159
                    return self.get_juju_output(self._show_status, *args)
1160
1160
                return self.status_class.from_text(
1161
1161
                    self.get_juju_output(
1162
 
                        self._show_status, '--format', 'yaml', admin=admin))
 
1162
                        self._show_status, '--format', 'yaml',
 
1163
                        controller=controller))
1163
1164
            except subprocess.CalledProcessError:
1164
1165
                pass
1165
1166
        raise Exception(
1211
1212
    def juju(self, command, args, sudo=False, check=True, include_e=True,
1212
1213
             timeout=None, extra_env=None):
1213
1214
        """Run a command under juju for the current environment."""
1214
 
        model = self._cmd_model(include_e, admin=False)
 
1215
        model = self._cmd_model(include_e, controller=False)
1215
1216
        return self._backend.juju(
1216
1217
            command, args, self.used_feature_flags, self.env.juju_home,
1217
1218
            model, check, timeout, extra_env)
1234
1235
          `args`.
1235
1236
 
1236
1237
        """
1237
 
        model = self._cmd_model(include_e, admin=False)
 
1238
        model = self._cmd_model(include_e, controller=False)
1238
1239
        return self._backend.expect(
1239
1240
            command, args, self.used_feature_flags, self.env.juju_home,
1240
1241
            model, timeout, extra_env)
1250
1251
        return stringified_timings
1251
1252
 
1252
1253
    def juju_async(self, command, args, include_e=True, timeout=None):
1253
 
        model = self._cmd_model(include_e, admin=False)
 
1254
        model = self._cmd_model(include_e, controller=False)
1254
1255
        return self._backend.juju_async(command, args, self.used_feature_flags,
1255
1256
                                        self.env.juju_home, model, timeout)
1256
1257
 
1468
1469
        for model in models:
1469
1470
            yield self._acquire_model_client(model['name'])
1470
1471
 
1471
 
    def get_admin_model_name(self):
1472
 
        """Return the name of the 'admin' model.
 
1472
    def get_controller_model_name(self):
 
1473
        """Return the name of the 'controller' model.
1473
1474
 
1474
 
        Return the name of the environment when an 'admin' model does
 
1475
        Return the name of the environment when an 'controller' model does
1475
1476
        not exist.
1476
1477
        """
1477
1478
        return 'controller'
1487
1488
            env = self.env.clone(model_name=name)
1488
1489
            return self.clone(env=env)
1489
1490
 
1490
 
    def get_admin_client(self):
1491
 
        """Return a client for the admin model.  May return self.
 
1491
    def get_controller_client(self):
 
1492
        """Return a client for the controller model.  May return self.
1492
1493
 
1493
1494
        This may be inaccurate for models created using add_model
1494
1495
        rather than bootstrap.
1495
1496
        """
1496
 
        return self._acquire_model_client(self.get_admin_model_name())
 
1497
        return self._acquire_model_client(self.get_controller_model_name())
1497
1498
 
1498
1499
    def list_controllers(self):
1499
1500
        """List the controllers."""
1545
1546
        reporter = GroupReporter(sys.stdout, desired_state)
1546
1547
        try:
1547
1548
            for remaining in until_timeout(timeout):
1548
 
                status = self.get_status(admin=True)
 
1549
                status = self.get_status(controller=True)
1549
1550
                states = {}
1550
1551
                for machine, info in status.iter_machines():
1551
1552
                    status = self.get_controller_member_status(info)
1853
1854
 
1854
1855
class EnvJujuClient2B7(EnvJujuClient2B8):
1855
1856
 
1856
 
    def get_admin_model_name(self):
1857
 
        """Return the name of the 'admin' model.
 
1857
    def get_controller_model_name(self):
 
1858
        """Return the name of the 'controller' model.
1858
1859
 
1859
 
        Return the name of the environment when an 'admin' model does
 
1860
        Return the name of the environment when an 'controller' model does
1860
1861
        not exist.
1861
1862
        """
1862
1863
        return 'admin'
1894
1895
            args.extend(['--bootstrap-series', bootstrap_series])
1895
1896
        return tuple(args)
1896
1897
 
1897
 
    def get_admin_client(self):
1898
 
        """Return a client for the admin model.  May return self."""
 
1898
    def get_controller_client(self):
 
1899
        """Return a client for the controller model.  May return self."""
1899
1900
        return self
1900
1901
 
1901
 
    def get_admin_model_name(self):
1902
 
        """Return the name of the 'admin' model.
 
1902
    def get_controller_model_name(self):
 
1903
        """Return the name of the 'controller' model.
1903
1904
 
1904
 
        Return the name of the environment when an 'admin' model does
 
1905
        Return the name of the environment when an 'controller' model does
1905
1906
        not exist.
1906
1907
        """
1907
1908
        models = self.get_models()
1908
1909
        # The dict can be empty because 1.x does not support the models.
1909
1910
        # This is an ambiguous case for the jes feature flag which supports
1910
1911
        # multiple models, but none is named 'admin' by default. Since the
1911
 
        # jes case also uses '-e' for models, the env is the admin model.
 
1912
        # jes case also uses '-e' for models, the env is the controller model.
1912
1913
        for model in models.get('models', []):
1913
1914
            if 'admin' in model['name']:
1914
1915
                return 'admin'
2126
2127
 
2127
2128
    supported_container_types = frozenset([KVM_MACHINE, LXC_MACHINE])
2128
2129
 
2129
 
    def _cmd_model(self, include_e, admin):
2130
 
        if admin:
2131
 
            return self.get_admin_model_name()
 
2130
    def _cmd_model(self, include_e, controller):
 
2131
        if controller:
 
2132
            return self.get_controller_model_name()
2132
2133
        elif self.env is None or not include_e:
2133
2134
            return None
2134
2135
        else: