~ubuntu-branches/ubuntu/trusty/crmsh/trusty-updates

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# Copyright (C) 2008-2011 Dejan Muhamedagic <dmuhamedagic@suse.de>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This software 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#

from vars import Vars
from clidisplay import CliDisplay
from msg import common_err, node_debug
import utils
from utils import vars
import xmlutil


#
# CLI format generation utilities (from XML)
#
def cli_format(pl, format):
    if format > 0:
        return ' \\\n\t'.join(pl)
    else:
        return ' '.join(pl)


def cli_format_xml(pl, format):
    if format > 0:
        return ' \\\n'.join(pl)
    else:
        return ''.join(pl)


def cli_operations(node, format=1):
    l = []
    node_id = node.get("id")
    s = ''
    if node_id:
        s = '$id="%s"' % node_id
    idref = node.get("id-ref")
    if idref:
        s = '%s $id-ref="%s"' % (s, idref)
    if s:
        l.append("%s %s" % (cli_display.keyword("operations"), s))
    for c in node.iterchildren():
        if c.tag == "op":
            l.append(cli_op(c))
    return cli_format(l, format)


def nvpair_format(n, v):
    if v is None:
        return cli_display.attr_name(n)
    return '%s="%s"' % (cli_display.attr_name(n), cli_display.attr_value(v))


def cli_pairs(pl):
    'Return a string of name="value" pairs (passed in a list of pairs).'
    l = []
    for n, v in pl:
        l.append(nvpair_format(n, v))
    return ' '.join(l)


def nvpairs2list(node, add_id=False):
    '''
    Convert nvpairs to a list of pairs.
    The id attribute is normally skipped, since they tend to be
    long and therefore obscure the relevant content. For some
    elements, however, they are included (e.g. properties).
    '''
    pl = []
    # if there's id-ref, there can be then _only_ id-ref
    value = node.get("id-ref")
    if value:
        pl.append(["$id-ref", value])
        return pl
    if add_id or \
            (not len(node) and len(node.attrib) == 1):
        value = node.get("id")
        if value:
            pl.append(["$id", value])
    for c in node.iterchildren():
        if c.tag == "attributes":
            pl = nvpairs2list(c)
        elif c.tag != "nvpair":
            node_debug("expected nvpair got", c)
            continue
        name = c.get("name")
        if "value" in c.keys():
            value = c.get("value")
        else:
            value = None
        pl.append([name, value])
    return pl


def op_instattr(node):
    pl = []
    for c in node.iterchildren():
        if c.tag != "instance_attributes":
            common_err("only instance_attributes are supported in operations")
        else:
            pl += nvpairs2list(c)
    return pl


def cli_op(node):
    action, pl = xmlutil.op2list(node)
    if not action:
        return ""
    pl += op_instattr(node)
    return "%s %s %s" % (cli_display.keyword("op"), action, cli_pairs(pl))


def date_exp2cli(node):
    l = []
    operation = node.get("operation")
    l.append(cli_display.keyword("date"))
    l.append(cli_display.keyword(operation))
    if operation in utils.olist(vars.simple_date_ops):
        value = node.get(utils.keyword_cmp(operation, 'lt') and "end" or "start")
        l.append('"%s"' % cli_display.attr_value(value))
    else:
        if operation == 'in_range':
            for name in vars.in_range_attrs:
                v = node.get(name)
                if v:
                    l.append(nvpair_format(name, v))
        for c in node.iterchildren():
            if c.tag in ("duration", "date_spec"):
                pl = []
                for name in c.keys():
                    if name != "id":
                        pl.append([name, c.get(name)])
                l.append(cli_pairs(pl))
    return ' '.join(l)


def binary_op_format(op):
    l = op.split(':')
    if len(l) == 2:
        return "%s:%s" % (l[0], cli_display.keyword(l[1]))
    else:
        return cli_display.keyword(op)


def exp2cli(node):
    operation = node.get("operation")
    type = node.get("type")
    if type:
        operation = "%s:%s" % (type, operation)
    attribute = node.get("attribute")
    value = node.get("value")
    if not value:
        return "%s %s" % (binary_op_format(operation), attribute)
    else:
        return "%s %s %s" % (attribute, binary_op_format(operation), value)


def abs_pos_score(score):
    return score in ("inf", "+inf", "Mandatory")


def get_kind(node):
    kind = node.get("kind")
    if not kind:
        kind = ""
    return kind


def get_score(node):
    score = node.get("score")
    if not score:
        score = node.get("score-attribute")
    else:
        if score.find("INFINITY") >= 0:
            score = score.replace("INFINITY", "inf")
    if not score:
        score = ""
    return score


def cli_rule(node):
    s = []
    node_id = node.get("id")
    if node_id:
        s.append('$id="%s"' % node_id)
    else:
        idref = node.get("id-ref")
        if idref:
            return '$id-ref="%s"' % idref
    rsc_role = node.get("role")
    if rsc_role:
        s.append('$role="%s"' % rsc_role)
    s.append("%s:" % cli_display.score(get_score(node)))
    bool_op = node.get("boolean-op")
    if not bool_op:
        bool_op = "and"
    exp = []
    for c in node.iterchildren():
        if c.tag == "date_expression":
            exp.append(date_exp2cli(c))
        elif c.tag == "expression":
            exp.append(exp2cli(c))
    expression = (" %s " % cli_display.keyword(bool_op)).join(exp)
    return "%s %s" % (' '.join(s), expression)


def mkrscrole(node, n):
    rsc = cli_display.rscref(node.get(n))
    rsc_role = node.get(n + "-role")
    rsc_instance = node.get(n + "-instance")
    if rsc_role:
        return "%s:%s" % (rsc, rsc_role)
    elif rsc_instance:
        return "%s:%s" % (rsc, rsc_instance)
    else:
        return rsc


def mkrscaction(node, n):
    rsc = cli_display.rscref(node.get(n))
    rsc_action = node.get(n + "-action")
    rsc_instance = node.get(n + "-instance")
    if rsc_action:
        return "%s:%s" % (rsc, rsc_action)
    elif rsc_instance:
        return "%s:%s" % (rsc, rsc_instance)
    else:
        return rsc


def rsc_set_constraint(node, obj_type):
    col = []
    cnt = 0
    for n in node.findall("resource_set"):
        add_seq = False
        sequential = utils.get_boolean(n.get("sequential"), True)
        require_all = utils.get_boolean(n.get("require-all"), True)
        if not require_all:
            col.append("[")
            if sequential:
                add_seq = True
        elif not sequential:
            col.append("(")
        role = n.get("role")
        action = n.get("action")
        for r in n.findall("resource_ref"):
            rsc = cli_display.rscref(r.get("id"))
            q = (obj_type == "order") and action or role
            col.append(q and "%s:%s" % (rsc, q) or rsc)
            cnt += 1
        if not require_all:
            if add_seq:
                col.append('sequential="true"')
            col.append("]")
        elif not sequential:
            col.append(")")
    if (sequential and require_all and obj_type != "rsc_ticket" and cnt <= 2) or \
            (obj_type == "rsc_ticket" and cnt <= 1):  # a degenerate thingie
        col.insert(0, "_rsc_set_")
    return col


def simple_rsc_constraint(node, obj_type):
    col = []
    if obj_type == "colocation":
        col.append(mkrscrole(node, "rsc"))
        col.append(mkrscrole(node, "with-rsc"))
    elif obj_type == "order":
        col.append(mkrscaction(node, "first"))
        col.append(mkrscaction(node, "then"))
    else:  # rsc_ticket
        col.append(mkrscrole(node, "rsc"))
    return col


# this pre (or post)-processing is oversimplified
# but it will do for now
# (a shortcut with more than one placeholder in a single expansion
# cannot have more than one expansion)
# ("...'@@'...'@@'...","...") <- that won't work
def build_exp_re(exp_l):
    return [x.replace(r'@@', r'([a-zA-Z_][a-zA-Z0-9_.-]*)') for x in exp_l]


def match_acl_shortcut(xpath, re_l):
    import re
    for i in range(len(re_l)):
        s = ''.join(re_l[0:i+1])
        r = re.match(s + r"$", xpath)
        if r:
            return (True, r.groups()[0:i+1])
    return (False, None)


def find_acl_shortcut(xpath):
    for shortcut in vars.acl_shortcuts:
        l = build_exp_re(vars.acl_shortcuts[shortcut])
        (ec, spec_l) = match_acl_shortcut(xpath, l)
        if ec:
            return (shortcut, spec_l)
    return (None, None)


def acl_spec_format(xml_spec, v):
    key_f = cli_display.keyword(vars.acl_spec_map[xml_spec])
    if xml_spec == "xpath":
        (shortcut, spec_l) = find_acl_shortcut(v)
        if shortcut:
            key_f = cli_display.keyword(shortcut)
            v_f = ':'.join([cli_display.attr_value(x) for x in spec_l])
        else:
            v_f = '"%s"' % cli_display.attr_value(v)
    elif xml_spec == "ref":
        v_f = '%s' % cli_display.attr_value(v)
    else:  # tag and attribute
        v_f = '%s' % cli_display.attr_value(v)
    return v_f and '%s:%s' % (key_f, v_f) or key_f


def cli_acl_rule(node, format=1):
    l = []
    acl_rule_name = node.tag
    l.append(cli_display.keyword(acl_rule_name))
    for xml_spec in vars.acl_spec_map:
        v = node.get(xml_spec)
        if v:
            l.append(acl_spec_format(xml_spec, v))
    return ' '.join(l)


def cli_acl_roleref(node, format=1):
    l = []
    l.append(cli_display.keyword("role"))
    l.append(":")
    l.append(cli_display.attr_value(node.get("id")))
    return ''.join(l)

#
################################################################

vars = Vars.getInstance()
cli_display = CliDisplay.getInstance()

# vim:ts=4:sw=4:et: