summaryrefslogtreecommitdiffstats
path: root/crmsh/ui_configure.py
blob: fbfc40a0c85395c3c7f0c88b8266c9036b923181 (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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
# Copyright (C) 2008-2011 Dejan Muhamedagic <dmuhamedagic@suse.de>
# Copyright (C) 2013 Kristoffer Gronlund <kgronlund@suse.com>
# See COPYING for license information.

import re
import time
from . import command
from . import completers as compl
from . import config
from . import utils
from . import constants
from . import userdir
from . import xmlutil
from . import ra
from .cibconfig import mkset_obj, cib_factory
from . import clidisplay
from . import term
from . import options
from . import rsctest
from . import schema
from . import ui_cib
from . import ui_cibstatus
from . import ui_ra
from . import ui_template
from . import ui_history
from . import ui_utils
from . import ui_assist
from .crm_gv import gv_types
from .ui_node import get_resources_on_nodes, remove_redundant_attrs


from . import log
logger = log.setup_logger(__name__)
logger_utils = log.LoggerUtils(logger)


def _type_completions():
    "completer for type: use in show"
    typelist = cib_factory.type_list()
    return ['type:%s' % (t) for t in typelist]


def _tag_completions():
    "completer for tag: use in show"
    return ['tag:%s' % (t) for t in cib_factory.tag_list()]


# Tab completion helpers
_id_list = compl.call(cib_factory.id_list)
_id_xml_list = compl.join(_id_list, compl.choice(['xml']))
_id_show_list = compl.join(_id_list,
                           compl.choice(['xml', 'changed']),
                           compl.call(_type_completions),
                           compl.call(_tag_completions))
_prim_id_list = compl.call(cib_factory.prim_id_list)
_f_prim_free_id_list = compl.call(cib_factory.f_prim_free_id_list)
_f_group_id_list = compl.call(cib_factory.f_group_id_list)
_f_children_id_list = compl.call(cib_factory.f_children_id_list)
_rsc_id_list = compl.call(cib_factory.rsc_id_list)
_top_rsc_id_list = compl.call(cib_factory.top_rsc_id_list)
_node_id_list = compl.call(cib_factory.node_id_list)
_rsc_template_list = compl.call(cib_factory.rsc_template_list)
_container_type = compl.choice(constants.container_type)


def _group_completer(args):
    '''
    completer for group resource
    '''
    completing = args[-1]
    id_list = cib_factory.f_prim_free_id_list()
    if completing in id_list:
        return [completing]
    # complete resources id first
    if len(args) == 3:
        return [s for s in id_list if s not in args]
    # complete meta or params attributes
    key_words = ["meta", "params"]
    keyw = last_keyword(args, key_words)
    if keyw in key_words:
        return _advanced_completer(args)

    # otherwise, complete resources ids and some key words
    return [s for s in id_list if s not in args] + _advanced_completer(args)


def _advanced_completer(args):
    '''
    meta completers for group/ms/clone resource type 
    '''
    key_words = ["meta", "params"]
    completing = args[-1]
    resource_type = args[0]
    return_list = []
    if completing.endswith('='):
        # TODO add some help messages
        return []
    keyw = last_keyword(args, key_words)
    if keyw and keyw == "meta":
        if resource_type == "group":
            return_list = utils.filter_keys(constants.group_meta_attributes, args)
        if resource_type == "clone":
            return_list = utils.filter_keys(constants.clone_meta_attributes, args)
        if resource_type in ["ms", "master"]:
            return_list = utils.filter_keys(constants.ms_meta_attributes, args)
    return return_list + key_words


def _list_resource(args):
    if len(args) > 3:
        if args[2] == "remove":
            res = cib_factory.f_prim_list_in_group(args[1])
            if len(res) <= 1:
                return []
            else:
                return res
        if args[2] == "add":
            return cib_factory.f_prim_free_id_list()


def _list_resource_2(args):
    if len(args) > 5:
        return cib_factory.f_prim_list_in_group(args[1])


def _pick_position(args):
    if args[2] == "remove":
        return []
    else:
        return ["after", "before"]


def top_rsc_tmpl_id_list(args):
    return cib_factory.top_rsc_id_list() + cib_factory.rsc_template_list()


def ra_classes_or_tmpl(args):
    return ui_ra.complete_class_provider_type(args) + \
           ['@'+x for x in cib_factory.rsc_template_list()]


def op_attr_list(args):
    schema_attr = [schema.get('attr', 'op', 'o') + '=']
    extra_attrs = [s + '=' for s in constants.op_extra_attrs]
    return schema_attr + extra_attrs


def node_id_colon_list(args):
    return [s + ':' for s in _node_id_list(args)]


def stonith_resource_list(args):
    return [x.obj_id for x in
            cib_factory.get_elems_on_type("type:primitive")
            if x.node.get("class") == "stonith"]


def _load_2nd_completer(args):
    if args[1] == 'xml':
        return ['replace', 'update', 'push']
    return []


# completion for primitives including help for parameters
# (help also available for properties)

def get_prim_token(words, n):
    for key in ("primitive", "rsc_template"):
        try:
            if key in words:
                return words[words.index(key) + n - 1]
        except IndexError:
            pass
    return ''


def ra_agent_for_template(tmpl):
    '''@template -> ra.agent'''
    obj = cib_factory.find_resource(tmpl[1:])
    if obj is None:
        return None
    return ra.get_ra(obj.node)


def ra_agent_for_cpt(cpt):
    '''class:provider:type -> ra.agent'''
    agent = None
    ra_class, provider, rsc_type = ra.disambiguate_ra_type(cpt)
    if ra.ra_type_validate(cpt, ra_class, provider, rsc_type):
        agent = ra.RAInfo(ra_class, rsc_type, provider)
    return agent


class CompletionHelp(object):
    '''
    Print some help on whatever last word in the line.
    '''
    timeout = 60  # don't print again and again
    laststamp = 0
    lasttopic = ''

    @classmethod
    def help(cls, topic, helptxt, args):
        if cls.lasttopic == topic and \
                time.time() - cls.laststamp < cls.timeout:
            return
        if helptxt:
            import readline
            cmdline = readline.get_line_buffer()
            print("\n%s" % helptxt, end='')
            if cmdline.split()[0] != args[0]:
                prompt = '   > '
            else:
                if clidisplay.colors_enabled():
                    prompt = term.render(clidisplay.prompt_noreadline(constants.prompt))
                else:
                    prompt = constants.prompt
            print("\n%s%s" % (prompt, cmdline), end=' ')
            cls.laststamp = time.time()
            cls.lasttopic = topic


def _prim_params_completer(agent, args):
    completing = args[-1]
    if completing == 'params':
        return ['params']
    if completing.endswith('='):
        if len(completing) > 1 and options.interactive:
            topic = completing[:-1]
            CompletionHelp.help(topic, agent.meta_parameter(topic), args)
        return []
    elif '=' in completing:
        return []
    return utils.filter_keys(agent.params(completion=True), args)


def _prim_meta_completer(agent, args):
    completing = args[-1]
    if completing == 'meta':
        return ['meta']
    if '=' in completing:
        return []
    return utils.filter_keys(constants.rsc_meta_attributes, args)


def _prim_op_completer(agent, args):

    def concat_kv(k, v):
        return "{}={}".format(k, v)

    if args[-1] == 'op':
        return ['op']
    actions = agent.actions()
    if not actions:
        return []
    # list all actions, select one to complete
    if args[-2] == 'op':
        return actions.keys()
    # list all attributes of the action, select one to complete
    if args[-3] == 'op':
        res = []
        op_name = args[-2]
        if op_name == 'monitor':
            for one_monitor in actions[op_name]:
                res += [concat_kv(k, v) for k, v in one_monitor.items()]
        else:
            res = [concat_kv(k, v) for k, v in actions[op_name].items()]
        return res

    args.pop()
    if '=' in args[-1]:
        res = []
        # find latest action
        op_name = None
        for i, item in enumerate(reversed(args)):
            if item in actions:
                op_name = item
                break
        if not op_name:
            return []
        # list all left attributes of the action, select one to complete
        actions_list_in_args = [arg.split('=')[0] for arg in args[len(args)-i:]]
        if op_name == 'monitor':
            for one_monitor in actions[op_name]:
                res += [concat_kv(k, v) for k, v in one_monitor.items() if k not in actions_list_in_args]
        else:
            res = [concat_kv(k, v) for k, v in actions[op_name].items() if k not in actions_list_in_args]
        return res

    return []


def last_keyword(words, keyw):
    '''returns the last occurance of an element in keyw in words'''
    for w in reversed(words):
        if w in keyw:
            return w
    return None


def _property_completer(args):
    '''context-sensitive completer'''
    agent = ra.get_properties_meta()
    return _prim_params_completer(agent, args)


def primitive_complete_complex(args):
    '''
    This completer depends on the content of the line, i.e. on
    previous tokens, in particular on the type of the RA.
    '''
    cmd = get_prim_token(args, 1)
    type_word = get_prim_token(args, 3)
    with_template = cmd == 'primitive' and type_word.startswith('@')

    if with_template:
        agent = ra_agent_for_template(type_word)
    else:
        agent = ra_agent_for_cpt(type_word)
    if agent is None:
        return []

    completers_set = {
        "params": _prim_params_completer,
        "meta": _prim_meta_completer,
        "op": _prim_op_completer,
    }

    keywords = list(completers_set.keys())
    if len(args) == 4:  # <cmd> <id> <type> <?>
        return keywords

    last_keyw = last_keyword(args, keywords)
    if last_keyw is None:
        return []

    complete_results = completers_set[last_keyw](agent, args)
    if len(args) > 4 and '=' in args[-1]:
        return complete_results + keywords

    return complete_results


def container_helptxt(params, helptxt, topic):
    for item in reversed(params):
        if item in ["storage", "network", "docker", "rkt"]:
            return helptxt[item][topic] + "\n"
        if item == "port-mapping":
            return helptxt["network"][item][topic] + "\n"


def _container_remove_exist_keywords(args, _keywords):
    for item in ["network", "primitive"]:
        if item in args:
            _keywords.remove(item)


def _container_network_completer(args, _help, _keywords):
    key_words = ["network", "port-mapping"]
    completing = args[-1]
    token = args[-2]
    if completing.endswith("="):
        return []
    if completing in key_words:
        return [completing]

    tmp = list(_help["network"].keys())
    # port-mapping is element, not a network option
    tmp.remove("port-mapping")
    network_keys = utils.filter_keys(tmp, args)
    # bundle contain just one <network>/<primitive> element
    _container_remove_exist_keywords(args, _keywords)

    last_keyw = last_keyword(args, key_words)
    if last_keyw == "network":
        if token == "network":
            return network_keys
        else:
            # complete port-mapping or other parts
            return network_keys + ["port-mapping"] + _keywords

    if last_keyw == "port-mapping":
        mapping_required = ["id"]
        mapping_params = args[utils.rindex(args, "port-mapping"):]
        mapping_keys = utils.filter_keys(_help["network"]["port-mapping"].keys(), mapping_params)
        if token == "port-mapping":
            return mapping_keys
        # required options must be completed
        for s in mapping_required:
            if utils.any_startswith(mapping_params, s+'=') is None:
                return mapping_keys
        # complete port-mapping or other parts
        return mapping_keys + ["port-mapping"] + _keywords


def _container_storage_completer(args, _help, _keywords):
    completing = args[-1]
    if completing.endswith("="):
        return []
    if completing == "storage":
        return [completing]
    if args[-2] == "storage":
        return ["storage-mapping"]

    storage_required = ["id", "target-dir"]
    # get last storage part
    mapping_params = args[utils.rindex(args, "storage-mapping"):]
    storage_keys = utils.filter_keys(_help["storage"].keys(), mapping_params)

    # required options must be completed
    for s in storage_required:
        if utils.any_startswith(mapping_params, s+"=") is None:
            return storage_keys
    # bundle contain just one <network>/<primitive> element
    _container_remove_exist_keywords(args, _keywords)
    # complete storage or other parts
    return storage_keys + _keywords


def _container_primitive_completer(args, _help, _keywords):
    completing = args[-1]
    if completing == "primitive":
        return [completing]

    _id_list = cib_factory.f_prim_free_id_list()
    if _id_list is None:
        return []
    # bundle contain just one <network>/<primitive> element
    _container_remove_exist_keywords(args, _keywords)
    if args[-3] == "primitive" and args[-2] in _id_list:
        return _keywords
    return _id_list


def _container_meta_completer(args, helptxt, _keywords):
    completing = args[-1]
    if completing.endswith("="):
        return []
    if completing == "meta":
        return [completing]

    # bundle contain just one <network>/<primitive> element
    _container_remove_exist_keywords(args, _keywords)

    return utils.filter_keys(constants.bundle_meta_attributes, args) + _keywords


def container_complete_complex(args):
    '''
    Complete five parts:
    container options, network, storage, primitive and meta
    '''
    container_options_required = ["image"]
    completing = args[-1]
    container_type = args[2]

    completers_set = {
        "network": _container_network_completer,
        "storage": _container_storage_completer,
        "primitive": _container_primitive_completer,
        "meta": _container_meta_completer
    }
    keywords = list(completers_set.keys())
    last_keyw = last_keyword(args, keywords)

    # to show help messages
    if completing.endswith('='):
        if len(completing) > 1 and options.interactive:
            topic = completing[:-1]
            CompletionHelp.help(topic, container_helptxt(args, constants.container_helptxt, topic), args)
        return []

    container_options = utils.filter_keys(constants.container_helptxt[container_type].keys(), args)

    # required options must be completed
    for s in container_options_required:
        if utils.any_startswith(args, s+'=') is None:
            return container_options

    if last_keyw is None:
        return container_options + keywords

    # to complete network, storage, primitive and meta
    return completers_set[last_keyw](args, constants.container_helptxt, keywords)


class CibConfig(command.UI):
    '''
    The configuration class
    '''
    name = "configure"

    def __init__(self):
        command.UI.__init__(self)
        # for interactive use, we want to populate the CIB
        # immediately so that tab completion works

    def requires(self):
        if not cib_factory.initialize():
            return False
        # see the configure ptest/simulate command
        has_ptest = utils.is_program('ptest')
        has_simulate = utils.is_program('crm_simulate')
        if not has_ptest:
            constants.simulate_programs["ptest"] = "crm_simulate"
        if not has_simulate:
            constants.simulate_programs["simulate"] = "ptest"
        if not (has_ptest or has_simulate):
            logger.warning("neither ptest nor crm_simulate exist, check your installation")
            constants.simulate_programs["ptest"] = ""
            constants.simulate_programs["simulate"] = ""
        return True

    @command.name('_test')
    @command.skill_level('administrator')
    def do_check_structure(self, context):
        cib_factory.ensure_cib_updated()
        return cib_factory.check_structure()

    @command.name('_regtest')
    @command.skill_level('administrator')
    def do_regression_testing(self, context, param):
        return cib_factory.regression_testing(param)

    @command.name('_objects')
    @command.skill_level('administrator')
    def do_showobjects(self, context):
        cib_factory.showobjects()

    @command.name('_keywords')
    @command.skill_level('administrator')
    def do_keywords(self, context):
        for k, v in sorted(iter(constants.keywords.items()), key=lambda v: v[0].lower()):
            print("%-16s %s" % (k, v))

    @command.level(ui_ra.RA)
    def do_ra(self):
        pass

    @command.level(ui_cib.CibShadow)
    def do_cib(self):
        pass

    @command.level(ui_cibstatus.CibStatusUI)
    def do_cibstatus(self):
        pass

    @command.level(ui_template.Template)
    def do_template(self):
        pass

    @command.level(ui_history.History)
    def do_history(self):
        pass

    @command.level(ui_assist.Assist)
    def do_assist(self):
        pass

    @command.skill_level('administrator')
    @command.completers_repeating(_id_show_list)
    def do_show(self, context, *args):
        "usage: show [xml] [<id>...]"
        from .utils import obscure
        osargs = [arg[8:] for arg in args if arg.startswith('obscure:')]
        if not osargs and config.core.obscure_pattern:
            # obscure_pattern could be
            #   1. "pattern1 pattern2 pattern3"
            #   2. "pattern1|pattern2|pattern3"
            # regrex here also filter out possible spaces
            osargs = re.split('\s*\|\s*|\s+', config.core.obscure_pattern.strip('|'))
        args = [arg for arg in args if not arg.startswith('obscure:')]
        cib_factory.ensure_cib_updated()
        with obscure(osargs):
            set_obj = mkset_obj(*args)
            return set_obj.show()

    @command.name("get_property")
    @command.alias("get-property")
    @command.skill_level('administrator')
    @command.completers_repeating(compl.call(ra.get_properties_list))
    def do_get_property(self, context, *args):
        "usage: get-property [-t|--true [<name>...]"
        properties = [a for a in args if a not in ('-t', '--true')]
        truth = any(a for a in args if a in ('-t', '--true'))

        if not properties:
            utils.multicolumn(ra.get_properties_list())
            return

        def print_value(v):
            if truth:
                print(utils.canonical_boolean(v))
            else:
                print(v)
        cib_factory.ensure_cib_updated()
        for p in properties:
            v = cib_factory.get_property_w_default(p)
            if v is not None:
                print_value(v)
            elif truth:
                print("false")
            else:
                context.fatal_error("%s: Property not set" % (p))

    @command.skill_level('administrator')
    @command.completers_repeating(compl.null, _id_xml_list, _id_list)
    def do_filter(self, context, filterprog, *args):
        "usage: filter <prog> [xml] [<id>...]"
        cib_factory.ensure_cib_updated()
        set_obj = mkset_obj(*args)
        return set_obj.filter(filterprog)

    @command.skill_level('administrator')
    @command.completers(_id_list)
    def do_set(self, context, path, value):
        """
        usage: set <path> <value>

        path:: id.[op_type.][interval.]name
        """
        path_errmsg = "Invalid path: \"{}\"; Valid path: \"id.[op_type.][interval.]name\"".format(path)
        path_list = path.split('.')
        if len(path_list) < 2 or len(path_list) > 4:
            context.fatal_error(path_errmsg)

        cib_factory.ensure_cib_updated()
        obj_id, *other_path_list = path_list
        rsc = cib_factory.find_object(obj_id)
        if not rsc:
            context.fatal_error("Object {} not found".format(obj_id))

        # Use case for: set id.name value
        if len(other_path_list) == 1:
            obj_attr = other_path_list[0]
            nvpairs = rsc.node.xpath(".//nvpair[@name='{}']".format(obj_attr))
            if not nvpairs:
                context.fatal_error("Attribute not found: {}".format(path))
            if len(nvpairs) != 1:
                context.fatal_error("Expected 1 attribute named {}, found {}".format(obj_attr, len(nvpairs)))
            nvpairs[0].set("value", value)

        # Use case for: set id.op_type.name value
        if len(other_path_list) == 2:
            op_type, name = other_path_list
            op_res = rsc.node.xpath(".//operations/op[@name='{}']".format(op_type))
            if not op_res:
                context.fatal_error("Operation \"{}\" not found for resource {}".format(op_type, obj_id))
            if len(op_res) > 1:
                context.fatal_error("Should specify interval of {}".format(op_type))
            op_res[0].set(name, value)

        # Use case for: set id.op_type.interval.name value
        if len(other_path_list) == 3:
            op_type, iv, name = other_path_list
            op_res = rsc.node.xpath(".//operations/op[@id='{}-{}-{}']".format(obj_id, op_type, iv))
            if not op_res:
                context.fatal_error("Operation \"{}\" interval \"{}\" not found for resource {}".format(op_type, iv, obj_id))
            op_res[0].set(name, value)

        rsc.set_updated()
        return True

    @command.skill_level('administrator')
    @command.completers(_f_group_id_list, compl.choice(['add', 'remove']),
                        _list_resource, _pick_position, _list_resource_2)
    def do_modgroup(self, context, group_id, subcmd, prim_id, *args):
        """usage: modgroup <id> add <id> [after <id>|before <id>]
        modgroup <id> remove <id>"""
        if subcmd not in ("add", "remove"):
            logger.error("modgroup subcommand %s unknown" % subcmd)
            return False
        after_before = None
        if args:
            if subcmd != 'add':
                context.fatal_error("Expected add (found %s)" % subcmd)
            if args[0] not in ("after", "before"):
                context.fatal_error("Expected after|before (found %s)" % args[0])
            if len(args) != 2:
                context.fatal_error("Expected 'after|before <id>' (%d arguments given)" %
                                    len(args))
            after_before = args[0]
            ref_member_id = args[1]
        cib_factory.ensure_cib_updated()
        g = cib_factory.find_object(group_id)
        if not g:
            context.fatal_error("group %s does not exist" % group_id)
        if not xmlutil.is_group(g.node):
            context.fatal_error("element %s is not a group" % group_id)
        children = xmlutil.get_rsc_children_ids(g.node)
        if after_before and ref_member_id not in children:
            context.fatal_error("%s is not member of %s" % (ref_member_id, group_id))
        if subcmd == "remove" and prim_id not in children:
            context.fatal_error("%s is not member of %s" % (prim_id, group_id))
        # done checking arguments
        # have a group and children
        if not after_before:
            after_before = "after"
            ref_member_id = children[-1]
        # just do the filter
        # (i wonder if this is a feature abuse?)
        if subcmd == "add":
            if after_before == "after":
                sed_s = r's/ %s( |$)/& %s /' % (ref_member_id, prim_id)
            else:
                sed_s = r's/ %s( |$)/ %s& /' % (ref_member_id, prim_id)
        else:
            sed_s = r's/ %s( |$)/ /' % prim_id
        l = (group_id,)
        set_obj = mkset_obj(*l)
        return set_obj.filter("sed -r '%s'" % sed_s)

    @command.skill_level('administrator')
    @command.completers_repeating(_id_xml_list, _id_list)
    def do_edit(self, context, *args):
        "usage: edit [xml] [<id>...]"
        cib_factory.ensure_cib_updated()
        with logger_utils.buffer():  # keep error messages
            set_obj = mkset_obj(*args)
        return set_obj.edit()

    def _verify(self, set_obj_semantic, set_obj_all):
        rc1 = set_obj_all.verify()
        if config.core.check_frequency != "never":
            rc2 = set_obj_semantic.semantic_check(set_obj_all)
        else:
            rc2 = 0
        return rc1 and rc2 <= 1

    @command.skill_level('administrator')
    def do_verify(self, context):
        "usage: verify"
        cib_factory.ensure_cib_updated()
        set_obj_all = mkset_obj("xml")
        return self._verify(set_obj_all, set_obj_all)

    @command.name('validate-all')
    @command.alias('validate_all')
    @command.skill_level('administrator')
    @command.completers_repeating(_id_list)
    def do_validate_all(self, context, rsc):
        "usage: validate-all <rsc>"
        cib_factory.ensure_cib_updated()
        from . import ra
        from . import cibconfig
        from . import cliformat
        obj = cib_factory.find_object(rsc)
        if not obj:
            context.error("Not found: %s" % (rsc))
        if obj.obj_type != "primitive":
            context.error("Not a primitive: %s" % (rsc))
        rnode = cibconfig.reduce_primitive(obj.node)
        if rnode is None:
            context.error("No resource template %s for %s" % (obj.node.get("template"), rsc))
        params = []
        for attrs in rnode.iterchildren("instance_attributes"):
            params.extend(cliformat.nvpairs2list(attrs))
        if not all(nvp.get('name') is not None and nvp.get('value') is not None for nvp in params):
            context.error("Primitive too complex: %s" % (rsc))
        params = dict([(nvp.get('name'), nvp.get('value')) for nvp in params])
        agentname = xmlutil.mk_rsc_type(rnode)
        if not ra.can_validate_agent(agentname):
            context.error("%s: Cannot run validate-all for agent: %s" % (rsc, agentname))
        rc, _ = ra.validate_agent(agentname, params, log=True)
        return rc == 0

    @command.skill_level('administrator')
    @command.completers_repeating(_id_show_list)
    def do_save(self, context, *args):
        "usage: save [xml] [<id>...] <filename>"
        if not args:
            context.fatal_error("Expected 1 argument (0 given)")
        cib_factory.ensure_cib_updated()
        filename = args[-1]
        setargs = args[:-1]
        set_obj = mkset_obj(*setargs)
        return set_obj.save_to_file(filename)

    @command.skill_level('administrator')
    @command.completers(compl.choice(['xml', 'replace', 'update', 'push']), _load_2nd_completer)
    def do_load(self, context, *args):
        "usage: load [xml] {replace|update|push} {<url>|<path>}"
        if len(args) < 2:
            context.fatal_error("Expected 2 arguments (0 given)")
        if args[0] == "xml":
            if len(args) != 3:
                context.fatal_error("Expected 3 arguments (%d given)" % len(args))
            url = args[2]
            method = args[1]
            xml = True
        else:
            if len(args) != 2:
                context.fatal_error("Expected 2 arguments (%d given)" % len(args))
            url = args[1]
            method = args[0]
            xml = False
        if method not in ("replace", "update", "push"):
            context.fatal_error("Unknown method %s" % method)
        cib_factory.ensure_cib_updated()
        if method == "replace":
            if options.interactive and cib_factory.has_cib_changed():
                if not utils.ask("This operation will erase all changes. Do you want to proceed?"):
                    return False
            cib_factory.erase()
        if xml:
            set_obj = mkset_obj("xml")
        else:
            set_obj = mkset_obj()
        return set_obj.import_file(method, url)

    @command.skill_level('administrator')
    @command.completers(compl.choice(list(gv_types.keys()) + ['exportsettings']))
    def do_graph(self, context, *args):
        "usage: graph [<gtype> [<file> [<img_format>]]]"
        if args and args[0] == "exportsettings":
            return utils.save_graphviz_file(userdir.GRAPHVIZ_USER_FILE, constants.graph)
        cib_factory.ensure_cib_updated()
        set_obj = mkset_obj()
        rc = set_obj.query_graph(*args)
        if rc is None:
            context.fatal_error("Failed to create graph")
        return rc

    def _stop_if_running(self, rscs):
        rscstate = xmlutil.RscState()
        to_stop = [rsc for rsc in rscs if rscstate.is_running(rsc)]
        from .ui_resource import set_deep_meta_attr
        if len(to_stop) > 0:
            ok = all(set_deep_meta_attr(rsc, 'target-role', 'Stopped',
                                        commit=False) for rsc in to_stop)
            if not ok or not cib_factory.commit():
                raise ValueError("Failed to stop one or more running resources: %s" %
                                 (', '.join(to_stop)))
        return len(to_stop)

    @command.skill_level('administrator')
    @command.completers_repeating(_id_list)
    @command.alias('rm')
    def do_delete(self, context, *args):
        "usage: delete [-f|--force] <id> [<id>...]"
        argl = list(args)
        arg_force = any((x in ('-f', '--force')) for x in argl)
        argl = [x for x in argl if x not in ('-f', '--force')]
        if arg_force or config.core.force:
            if self._stop_if_running(argl) > 0:
                utils.wait4dc(what="Stopping %s" % (", ".join(argl)))
        cib_factory.ensure_cib_updated()
        return cib_factory.delete(*argl)

    @command.name('default-timeouts')
    @command.alias('default_timeouts')
    @command.completers_repeating(_id_list)
    def do_default_timeouts(self, context, *args):
        "usage: default-timeouts <id> [<id>...]"
        cib_factory.ensure_cib_updated()
        return cib_factory.default_timeouts(*args)

    @command.skill_level('administrator')
    @command.completers(_id_list)
    def do_rename(self, context, old_id, new_id):
        "usage: rename <old_id> <new_id>"
        cib_factory.ensure_cib_updated()
        return cib_factory.rename(old_id, new_id)

    @command.skill_level('administrator')
    @command.completers(compl.choice(['nodes']))
    def do_erase(self, context, nodes=None):
        "usage: erase [nodes]"
        if not options.regression_tests:
            logger.warning("`crm configure erase` is deprecated. The replacement could be `crm cluster remove [node]`")
            return True
        cib_factory.ensure_cib_updated()
        if nodes is None:
            return cib_factory.erase()
        if nodes != 'nodes':
            context.fatal_error("Expected 'nodes' (found '%s')" % (nodes))
        return cib_factory.erase_nodes()

    @command.skill_level('administrator')
    def do_refresh(self, context):
        "usage: refresh"
        if options.interactive and cib_factory.has_cib_changed():
            if not utils.ask("All changes will be dropped. Do you want to proceed?"):
                return
        cib_factory.refresh()

    @command.alias('simulate')
    @command.completers(compl.choice(['nograph']))
    def do_ptest(self, context, *args):
        "usage: ptest [nograph] [v...] [scores] [utilization] [actions]"
        # use ptest/crm_simulate depending on which command was
        # used
        config.core.ptest = constants.simulate_programs[context.get_command_name()]
        if not config.core.ptest:
            return False
        set_obj = mkset_obj("xml")
        return ui_utils.ptestlike(set_obj.ptest, 'vv', context.get_command_name(), args)

    def _commit(self, force=False, replace=False):
        if not cib_factory.has_cib_changed():
            logger.info("apparently there is nothing to commit")
            logger.info("try changing something first")
            return True
        rc1 = True
        if replace and not force:
            rc1 = cib_factory.is_current_cib_equal()
        rc2 = cib_factory.has_no_primitives() or \
            self._verify(mkset_obj("xml", "changed"), mkset_obj("xml"))
        if rc1 and rc2:
            return cib_factory.commit(replace=replace)
        if force or config.core.force:
            logger.info("commit forced")
            return cib_factory.commit(force=True, replace=replace)
        if utils.ask("Do you still want to commit?"):
            return cib_factory.commit(force=True, replace=replace)
        return False

    @command.skill_level('administrator')
    @command.wait
    @command.completers(compl.choice(['force', 'replace']), compl.choice(['force', 'replace']))
    def do_commit(self, context, arg0=None, arg1=None):
        "usage: commit [force] [replace]"
        force = "force" in [arg0, arg1]
        replace = "replace" in [arg0, arg1]
        if arg0 is not None and arg0 not in ("force", "replace"):
            logger_utils.syntax_err(('configure.commit', arg0))
            return False
        if arg1 is not None and arg1 not in ("force", "replace"):
            logger_utils.syntax_err(('configure.commit', arg1))
            return False
        return self._commit(force=force, replace=replace)

    @command.skill_level('administrator')
    @command.completers(compl.choice(['force']))
    def do_upgrade(self, context, force=None):
        "usage: upgrade [force]"
        if force and force != "force":
            context.fatal_error("Expected 'force' or no argument")
        cib_factory.ensure_cib_updated()
        return cib_factory.upgrade_validate_with(force=config.core.force or force)

    @command.skill_level('administrator')
    def do_schema(self, context, schema_st=None):
        "usage: schema [<schema>]"
        if not schema_st:
            print(cib_factory.get_schema())
            return True
        return cib_factory.change_schema(schema_st)

    def __override_lower_level_attrs(self, *args):
        """
        When setting up an attribute of a cluster, the same
        attribute may already exist in one of the nodes an/or
        any resource.
        The user should be informed about it and, if he wants,
        he will have an option to delete the already existing
        attribute.
        """
        if not args:
            return

        nvpair = args[0].split('=', 1)
        if 2 != len(nvpair):
            return

        attr_name, attr_value = nvpair

        if "maintenance-mode" == attr_name:
            attr = "maintenance"
            conflicting_lower_level_attr = 'is-managed'
            # FIXME! the first argument is hardcoded
            objs = get_resources_on_nodes(cib_factory.node_id_list(), [ "primitive", "group", "clone"])
            remove_redundant_attrs(objs, "meta_attributes", attr, conflicting_lower_level_attr)

            objs = get_resources_on_nodes(cib_factory.node_id_list(), [ "node" ])
            remove_redundant_attrs(objs, "instance_attributes", attr, conflicting_lower_level_attr)

    def __conf_object(self, cmd, *args):
        "The configure object command."
        if cmd in list(constants.cib_cli_map.values()) and \
                not cib_factory.is_elem_supported(cmd):
            logger.error("%s not supported by the RNG schema" % cmd)
            return False
        cib_factory.ensure_cib_updated()
        if not args:
            return cib_factory.create_object(cmd, *args)
        if args[0].startswith("id="):
            object_id = args[0][3:]
        else:
            object_id = args[0]
        params = (object_id,) + args[1:]
        return cib_factory.create_object(cmd, *params)

    @command.skill_level('administrator')
    @command.completers(_node_id_list, compl.choice(constants.node_attributes_keyw))
    def do_node(self, context, *args):
        """usage: node <uname>[:<type>]
           [attributes <param>=<value> [<param>=<value>...]]
           [utilization <param>=<value> [<param>=<value>...]]"""
        return self.__conf_object(context.get_command_name(), *args)

    @command.skill_level('administrator')
    @command.completers_repeating(compl.attr_id, ra_classes_or_tmpl, primitive_complete_complex)
    @command.alias('resource')
    def do_primitive(self, context, *args):
        """usage: primitive <rsc> {[<class>:[<provider>:]]<type>|@<template>}
        [[params] <param>=<value> [<param>=<value>...]]
        [meta <attribute>=<value> [<attribute>=<value>...]]
        [utilization <attribute>=<value> [<attribute>=<value>...]]
        [operations id_spec
            [op op_type [<attribute>=<value>...]
                        [[op_params] <param>=<value> [<param>=<value>...]]
                        [op_meta <attribute>=<value> [<attribute>=<value>...]] ...]]"""
        return self.__conf_object(context.get_command_name(), *args)

    @command.completers_repeating(compl.attr_id, _container_type, container_complete_complex)
    def do_bundle(self, context, *args):
        """usage: bundle <bundle id> <container type> [<container option>...]
        network [<network option>...]
        storage [<storage option>...]
        primitive <resource id> {[<class>:[<provider>:]]<type>|@<template>}"""
        return self.__conf_object(context.get_command_name(), *args)

    @command.skill_level('administrator')
    @command.completers_repeating(compl.attr_id, _group_completer)
    def do_group(self, context, *args):
        """usage: group <name> <rsc> [<rsc>...]
        [params <param>=<value> [<param>=<value>...]]
        [meta <attribute>=<value> [<attribute>=<value>...]]"""
        return self.__conf_object(context.get_command_name(), *args)

    @command.skill_level('administrator')
    @command.completers_repeating(compl.attr_id, _f_children_id_list, _advanced_completer)
    def do_clone(self, context, *args):
        """usage: clone <name> <rsc>
        [params <param>=<value> [<param>=<value>...]]
        [meta <attribute>=<value> [<attribute>=<value>...]]"""
        return self.__conf_object(context.get_command_name(), *args)

    @command.alias('master')
    @command.skill_level('administrator')
    @command.completers_repeating(compl.attr_id, _f_children_id_list, _advanced_completer)
    def do_ms(self, context, *args):
        """usage: ms <name> <rsc>
        [params <param>=<value> [<param>=<value>...]]
        [meta <attribute>=<value> [<attribute>=<value>...]]"""
        format_str = " " if "meta" in args else " meta "
        new_cmd_str = ' '.join(args) + "{}promotable=true".format(format_str)
        logger.warning('"ms" is deprecated. Please use "clone {}"'.format(new_cmd_str))
        return self.__conf_object(context.get_command_name(), *args)

    @command.skill_level('administrator')
    @command.completers_repeating(compl.attr_id, ui_ra.complete_class_provider_type,
                                  primitive_complete_complex)
    def do_rsc_template(self, context, *args):
        """usage: rsc_template <name> [<class>:[<provider>:]]<type>
        [params <param>=<value> [<param>=<value>...]]
        [meta <attribute>=<value> [<attribute>=<value>...]]
        [utilization <attribute>=<value> [<attribute>=<value>...]]
        [operations id_spec
            [op op_type [<attribute>=<value>...] ...]]"""
        return self.__conf_object(context.get_command_name(), *args)

    @command.skill_level('administrator')
    @command.completers(compl.attr_id, _top_rsc_id_list)
    def do_location(self, context, *args):
        """usage: location <id> <rsc> {node_pref|rules}

        node_pref :: <score>: <node>

        rules ::
          rule [id_spec] [$role=<role>] <score>: <expression>
          [rule [id_spec] [$role=<role>] <score>: <expression> ...]

        id_spec :: $id=<id> | $id-ref=<id>
        score :: <number> | <attribute> | [-]inf
        expression :: <simple_exp> [bool_op <simple_exp> ...]
        bool_op :: or | and
        simple_exp :: <attribute> [type:]<binary_op> <value>
                      | <unary_op> <attribute>
                      | date <date_expr>
        type :: string | version | number
        binary_op :: lt | gt | lte | gte | eq | ne
        unary_op :: defined | not_defined"""
        return self.__conf_object(context.get_command_name(), *args)

    @command.alias('collocation')
    @command.skill_level('administrator')
    @command.completers_repeating(compl.attr_id, compl.null, top_rsc_tmpl_id_list)
    def do_colocation(self, context, *args):
        """usage: colocation <id> <score>: <rsc>[:<role>] <rsc>[:<role>] ...
        [node-attribute=<node_attr>]"""
        return self.__conf_object(context.get_command_name(), *args)

    @command.skill_level('administrator')
    @command.completers_repeating(compl.attr_id,
                                  compl.call(schema.rng_attr_values, 'rsc_order', 'kind'),
                                  top_rsc_tmpl_id_list)
    def do_order(self, context, *args):
        """usage: order <id> [kind]: <rsc>[:<action>] <rsc>[:<action>] ...
        [symmetrical=<bool>]"""
        return self.__conf_object(context.get_command_name(), *args)

    @command.skill_level('administrator')
    @command.completers_repeating(compl.attr_id, compl.null, top_rsc_tmpl_id_list)
    def do_rsc_ticket(self, context, *args):
        """usage: rsc_ticket <id> <ticket_id>: <rsc>[:<role>] [<rsc>[:<role>] ...]
        [loss-policy=<loss_policy_action>]"""
        return self.__conf_object(context.get_command_name(), *args)

    @command.skill_level('administrator')
    @command.completers_repeating(_property_completer)
    def do_property(self, context, *args):
        "usage: property [$id=<set_id>] <option>=<value>"
        self.__override_lower_level_attrs(*args)
        return self.__conf_object(context.get_command_name(), *args)

    @command.skill_level('administrator')
    @command.completers_repeating(_prim_meta_completer)
    def do_rsc_defaults(self, context, *args):
        "usage: rsc_defaults [$id=<set_id>] <option>=<value>"
        return self.__conf_object(context.get_command_name(), *args)

    @command.skill_level('administrator')
    @command.completers_repeating(op_attr_list)
    def do_op_defaults(self, context, *args):
        "usage: op_defaults [$id=<set_id>] <option>=<value>"
        return self.__conf_object(context.get_command_name(), *args)

    @command.skill_level('administrator')
    @command.completers_repeating(node_id_colon_list, stonith_resource_list)
    def do_fencing_topology(self, context, *args):
        "usage: fencing_topology [<node>:] stonith_resources [stonith_resources ...]"
        return self.__conf_object(context.get_command_name(), *args)

    @command.skill_level('administrator')
    def do_xml(self, context, *args):
        "usage: xml <xml>"
        return self.__conf_object(context.get_command_name(), *args)

    @command.skill_level('administrator')
    @command.completers(_f_children_id_list)
    def do_monitor(self, context, *args):
        "usage: monitor <rsc>[:<role>] <interval>[:<timeout>]"
        return self.__conf_object(context.get_command_name(), *args)

    @command.skill_level('expert')
    @command.completers_repeating(compl.null, compl.choice(["role:", "read", "write", "deny"]))
    def do_user(self, context, *args):
        """user <uid> {roles|rules}

        roles :: role:<role-ref> [role:<role-ref> ...]
        rules :: rule [rule ...]

        (See the role command for details on rules.)"""
        return self.__conf_object(context.get_command_name(), *args)

    @command.skill_level('expert')
    @command.completers_repeating(compl.null, compl.choice(["read", "write", "deny"]))
    def do_role(self, context, *args):
        """role <role-id> rule [rule ...]

        rule :: acl-right cib-spec [attribute:<attribute>]

        acl-right :: read | write | deny

        cib-spec :: xpath-spec | tag-ref-spec
        xpath-spec :: xpath:<xpath> | shortcut
        tag-ref-spec :: tag:<tag> | ref:<id> | tag:<tag> ref:<id>

        shortcut :: meta:<rsc>[:<attr>]
                    params:<rsc>[:<attr>]
                    utilization:<rsc>
                    location:<rsc>
                    property[:<attr>]
                    node[:<node>]
                    nodeattr[:<attr>]
                    nodeutil[:<node>]
                    status"""
        return self.__conf_object(context.get_command_name(), *args)

    @command.skill_level('expert')
    def do_acl_target(self, context, *args):
        return self.__conf_object(context.get_command_name(), *args)

    @command.skill_level('administrator')
    @command.completers_repeating(compl.null, top_rsc_tmpl_id_list)
    def do_tag(self, context, *args):
        return self.__conf_object(context.get_command_name(), *args)

    @command.skill_level('administrator')
    def do_alert(self, context, *args):
        return self.__conf_object(context.get_command_name(), *args)

    @command.skill_level('expert')
    @command.completers_repeating(_rsc_id_list)
    def do_rsctest(self, context, *args):
        "usage: rsctest <rsc_id> [<rsc_id> ...] [<node_id> ...]"
        cib_factory.ensure_cib_updated()
        rc = True
        rsc_l = []
        node_l = []
        current = "r"
        for ident in args:
            el = cib_factory.find_object(ident)
            if not el:
                logger.error("element %s does not exist" % ident)
                rc = False
            elif current == "r" and xmlutil.is_resource(el.node):
                if xmlutil.is_container(el.node):
                    rsc_l += el.node.findall("primitive")
                else:
                    rsc_l.append(el.node)
            elif xmlutil.is_normal_node(el.node):
                current = "n"
                node_l.append(el.node.get("uname"))
            else:
                logger_utils.syntax_err((context.get_command_name(), ident), context='rsctest')
                return False
        if not rc:
            return False
        if not rsc_l:
            logger.error("specify at least one resource")
            return False
        all_nodes = cib_factory.node_id_list()
        if not node_l:
            node_l = all_nodes
        return rsctest.test_resources(rsc_l, node_l, all_nodes)

    def should_wait(self):
        return cib_factory.has_cib_changed()

    def end_game(self, no_questions_asked=False):
        ok = True
        if cib_factory.has_cib_changed():
            if no_questions_asked or not options.interactive:
                ok = self._commit()
            elif utils.ask("There are changes pending. Do you want to commit them?"):
                ok = self._commit()
        cib_factory.reset()
        return ok