summaryrefslogtreecommitdiffstats
path: root/crmsh/completers.py
blob: 28c576b951f41f96c6de3e67d6a05ddc83e89801 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Copyright (C) 2013 Kristoffer Gronlund <kgronlund@suse.com>
# See COPYING for license information.

# Helper completers

from . import xmlutil


def choice(lst):
    '''
    Static completion from a list
    '''
    def completer(args):
        return lst
    return completer


null = choice([])
attr_id = choice(["id="])

def call(fn, *fnargs):
    '''
    Call the given function with the given arguments.
    The function has to return a list of completions.
    '''
    def completer(args):
        return fn(*fnargs)
    return completer


def join(*fns):
    '''
    Combine the output of several completers
    into a single completer.
    '''
    def completer(args):
        ret = []
        for fn in fns:
            ret += list(fn(args))
        return ret
    return completer


booleans = choice(['yes', 'no', 'true', 'false', 'on', 'off'])


def resources(args=None):
    cib_el = xmlutil.resources_xml()
    if cib_el is None:
        return []
    nodes = xmlutil.get_interesting_nodes(cib_el, [])
    rsc_id_list = [x.get("id") for x in nodes if xmlutil.is_resource(x)]
    if args and args[0] in ['promote', 'demote']:
        return [item for item in rsc_id_list if xmlutil.RscState().is_ms_or_promotable_clone(item)]
    if args and args[0] == "started":
        return [item for item in rsc_id_list if xmlutil.RscState().is_running(item)]
    if args and args[0] == "stopped":
        return [item for item in rsc_id_list if not xmlutil.RscState().is_running(item)]
    return rsc_id_list


def resources_started(args=None):
    return resources(["started"])


def resources_stopped(args=None):
    return resources(["stopped"])


def primitives(args):
    cib_el = xmlutil.resources_xml()
    if cib_el is None:
        return []
    nodes = xmlutil.get_interesting_nodes(cib_el, [])
    return [x.get("id") for x in nodes if xmlutil.is_primitive(x)]


nodes = call(xmlutil.listnodes)
online_nodes = call(xmlutil.CrmMonXmlParser().get_node_list, "online")
standby_nodes = call(xmlutil.CrmMonXmlParser().get_node_list, "standby")

shadows = call(xmlutil.listshadows)

status_option = """full bynode inactive ops timing failcounts
                   verbose quiet xml simple tickets noheaders
                   detail brief""".split()