Пример #1
0
    def _resource_completion(self, short_mode=True):
        results = []
        if not os.path.exists(settings.SSH_CONFIG_FILE):
            return

        ssh_hosts = []
        with open(settings.SSH_CONFIG_FILE) as f:
            for one_line in f.readlines():
                try:
                    one_line = one_line.strip()
                    if one_line.startswith("Host "):
                        hostname = one_line.replace("Host", "").strip()
                        if hostname:
                            ssh_hosts.append(hostname)
                except:
                    pass

        results.extend(ssh_hosts)

        configs = SshService.get_instance().configs
        results = []
        headers = ('HOST', 'HOSTNAME', 'PORT', 'USER')
        results.append(headers)
        results.extend([(key, value.get('HostName'), value.get('Port', 22),
                         value.get('User')) for key, value in configs.items()])

        return CompletionContent(Formatter.format_completions(results))
Пример #2
0
    def _resource_completion(self, short_mode=True):
        results = []
        headers = ('NAME', 'SERVER', 'CLUSTER', 'CONTEXT')
        yaml = YAML()
        results.append(headers)
        if os.path.exists(settings.OMC_KUBE_CONFIG_DIR):
            resources = os.listdir(settings.OMC_KUBE_CONFIG_DIR)
            for one_resource in resources:
                try:
                    with open(
                            os.path.join(settings.OMC_KUBE_CONFIG_DIR,
                                         one_resource, 'config')) as f:
                        content = yaml.load(f)
                        one_item = (one_resource,
                                    ObjectUtils.get_node(
                                        content, "clusters[0].cluster.server"),
                                    ObjectUtils.get_node(
                                        content, "clusters[0].name"),
                                    ObjectUtils.get_node(
                                        content, "current-context"))
                        results.append(one_item)
                except Exception as e:
                    logger.error(e, exc_info=True)

        return CompletionContent(Formatter.format_completions(results))
Пример #3
0
 def _get_config_key_completion(self):
     resource = self._get_one_resource_value()
     namespace = self.client.get_namespace(
         self._get_kube_api_resource_type(), resource)
     result = self._read_namespaced_resource(resource, namespace)
     prompts = []
     ObjectUtils.get_nodes(result.to_dict(), prompts)
     return CompletionContent(self._get_completion(prompts))
Пример #4
0
 def _resource_complete(self):
     results = []
     jmx = self.context['jmx'][0] if self.context['jmx'] else ''
     cmd = JmxTermUtils.build_command('open %s && beans' % jmx)
     result = self.run_cmd(cmd, capture_output=True)
     output = result.stdout.decode("utf-8").splitlines()
     output = list(map(lambda x: x.replace(":", "\:"), output))
     results.extend(self._get_completion(output, True))
     return CompletionContent(results)
Пример #5
0
    def _resource_completion(self, short_mode=True):
        results = []

        pod_name = self._get_one_resource_value('pod')
        namespace = self.client.get_namespace('pod', pod_name)
        result = self.client.read_namespaced_pod(pod_name, namespace)
        # for one_container in result.spec.containers:
        results.extend(self._get_completion([(one.name,one.image) for one in result.spec.containers], False))

        return CompletionContent(results)
Пример #6
0
    def _resource_completion(self, short_mode=True):
        ret = self._list_resource_for_all_namespaces(
            timeout_seconds=settings.COMPETION_TIMEOUT)
        results = Formatter.format_completions([(
            ObjectUtils.get_node(one, 'metadata.name'),
            ObjectUtils.get_node(one, 'metadata.namespace'),
            self._get_ready_status(one),
            time_util.calculate_age(
                time_util.fromisotime(
                    ObjectUtils.get_node(one, 'metadata.creationTimestamp'))),
        ) for one in ret.get('items')])

        return CompletionContent(results)
Пример #7
0
 def completion(self, parser):
     """
     used for zsh completion
     """
     args = parser.parse_args(self._get_action_params())
     if args.refresh:
         self._clean_completin_cache()
     # list candidates for completions in zsh style (a:"description for a" b:"description for b")
     try:
         console.log(CompletionContent(self._completion()).get_output())
     except Exception as e:
         # keep silent in completion mode
         logger.error(e, exc_info=True)
         return
Пример #8
0
    def _resource_completion(self, short_mode=True):
        results = []
        # list rabbitmq connection instance from config file
        config_file_name = os.path.join(settings.CONFIG_DIR, self.__class__.__name__.lower() + '.json')
        if (os.path.exists(config_file_name)):
            with open(config_file_name) as f:
                instances = json.load(f)
                results.extend(
                    self._get_completion(
                        [(value.get('host') + ':' + str(value.get('port')), key) for key, value in
                         instances.items()],
                        False))

        return CompletionContent(results)
Пример #9
0
    def _completion(self, short_mode=False):
        results = []

        results.append(super()._completion(True))

        if not self._have_resource_value():
            jmx = self.context['jmx'][0] if self.context['jmx'] else ''
            cmd = JmxTermUtils.build_command('open %s && beans' % jmx)
            result = self.run_cmd(cmd, capture_output=True)
            output = result.stdout.decode("utf-8").splitlines()
            output = list(map(lambda x: x.replace(":", "\:"), output))
            results.extend(self._get_completion(output, True))

        return CompletionContent(results)
Пример #10
0
    def _resource_completion(self, short_mode=True):
        results = []

        kube_resource_type = self._get_kube_resource_type()
        completion_file_name = pkg_resources.resource_filename(
            'omc_kube.assets',
            '_%(kube_resource_type)s_completion.json' % locals())
        prompts = []
        # get_all_dict_Keys(result.to_dict(), prompts)
        with open(completion_file_name) as f:
            result = json.load(f)
            ObjectUtils.get_nodes(result, prompts)
            results.extend(self._get_completion(prompts))

        return CompletionContent(results)
Пример #11
0
    def _resource_completion(self, short_mode=True):
        output = self.client.get(self._get_kube_api_resource_type())

        results = []

        for one in output.splitlines()[1:]:
            one_line = one.strip().split()
            # no namespace, no need swap position
            # one_line[0],one_line[1] = one_line[1],one_line[0]
            results.append(one_line)

        # ret = self._list_resource_for_all_namespaces(timeout_seconds=settings.COMPETION_TIMEOUT)
        # results.extend(
        #     self._get_completion([ObjectUtils.get_node(one, 'metadata.name') for one in ret.get('items')], True))

        return CompletionContent(Formatter.format_completions(results))
Пример #12
0
    def _resource_completion(self, short_mode=True):
        output = self.client.get(self._get_kube_api_resource_type(), timeout=3)

        results = []

        for one in output.splitlines():
            # for case like: 'itsma-j6zm1 smarta-sawmeta-dih- 172.16.0.84:31371,172.16.0.84:1444,172.16.0.84:1445  3 more...          4d3h'
            # one = re.sub(' \+ \d+ more...', '...', one)
            one_line = one.strip().split(None, maxsplit=3)
            one_line[0], one_line[1] = one_line[1], one_line[0]
            results.append(one_line)

        # ret = self._list_resource_for_all_namespaces(timeout_seconds=settings.COMPETION_TIMEOUT)
        # results.extend(
        #     self._get_completion([ObjectUtils.get_node(one, 'metadata.name') for one in ret.get('items')], True))

        return CompletionContent(Formatter.format_completions(results))
Пример #13
0
    def _get_resource_type_completion(self):
        results = []

        # for built-in resources
        for resource_type in built_in_resources:
            mod = __import__(".".join(
                ['omc', 'resources', resource_type, resource_type]),
                             fromlist=[resource_type.capitalize()])
            clazz = getattr(mod, resource_type.capitalize())
            results.append(resource_type + ":" + clazz({})._description())

        # for plugins
        for finder, name, ispkg in pkgutil.iter_modules():
            if name.startswith('omc_'):
                resource_type = name.replace('omc_', '').lower()
                mod = __import__(".".join(
                    ['omc_' + resource_type, resource_type, resource_type]),
                                 fromlist=[resource_type.capitalize()])
                clazz = getattr(mod, resource_type.capitalize())
                results.append(resource_type + ":" + clazz({})._description())

        # }

        return CompletionContent(results)
Пример #14
0
    def _completion(self, short_mode=False):
        completions = CompletionContent()
        if self._have_resource_value():
            # for instance methods
            instance_methods = self._get_instance_methods()
            instance_methods_with_doc = self._get_method_with_description(
                instance_methods)
            completions.add_content(
                Formatter.format_completions(instance_methods_with_doc,
                                             enable_line=False))

            modules = self._get_sub_modules()
            modules_with_doc = self._get_module_with_description(modules)
            completions.add_content(
                Formatter.format_completions(modules_with_doc,
                                             enable_line=False))

        else:
            # for list method
            list_methods = self._get_list_methods()
            list_methods_with_doc = self._get_method_with_description(
                list_methods)
            completions.add_content(
                Formatter.format_completions(list_methods_with_doc,
                                             enable_line=False))
            resource_result = self._resource_completion()
            completions.add_content(CompletionContent(resource_result))
        # if self._have_resource_value():
        # public_methods = self._get_public_method_completion()
        # sub_modules = self._get_sub_modules()
        # all_comp = []
        # all_comp.extend(self._get_completion(public_methods, short_mode))
        # all_comp.extend(self._get_completion(sub_modules, short_mode))
        #
        # completions = CompletionContent(all_comp)
        #
        #
        # if not self._have_resource_value():
        #     resource_result = self._resource_completion()
        #     completions.add_content(CompletionContent(resource_result))

        return completions
Пример #15
0
 def _resource_completion(self):
     return CompletionContent()