File: mirror.py

package info (click to toggle)
ecflow 5.15.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,868 kB
  • sloc: cpp: 269,341; python: 22,756; sh: 3,609; perl: 770; xml: 333; f90: 204; ansic: 141; makefile: 70
file content (1034 lines) | stat: -rwxr-xr-x 32,572 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python
# ////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
# Name        :
# Author      : Avi
# Revision    : $Revision: #10 $
#
# Copyright 2009- ECMWF.
# This software is licensed under the terms of the Apache Licence version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.
# ////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8

# -m trace --count -C .
"""an example for a simple python client to help ecFlow scheduling
 + unit test
 + task loading in the suite

one script DO-IT-ALL:
  - play suite + update suite holder aka def-file

  - script as a wrapper for a task

  - download content (passive client)

  - upload statuses (active client)

$manual

  MIRRORING suite from server to server

  test - suspend suite and rerun job if aborted... Thanks

$end
$comment
  a comment in a comment
$end

TODO: check MICRO robust sync
"""
import argparse
import os
import pwd
import sys
import time
import unittest
from time import gmtime, strftime

# ECFLOWP = "/usr/local/apps/ecflow/current"
# ECFLOWC = ECFLOWP + "/bin/ecflow_client "
# sys.path.append(ECFLOWP + "/lib/python2.7/site-packages/ecflow")
ECFLOWC = "ecflow_client "
child = None


def getreq():
    return Family("getreq").add(Task("collectreq"))


def ymd():
    return [Edit("YMD", "29090909"), Label("ymd", "29090909")]


def excepthook(exctype, value, traceback):
    if exctype == KeyboardInterrupt:
        if child:
            child.report("abort", "keyb")
    else:
        sys.__excepthook__(exctype, value, traceback)
        if child:
            child.report("abort", "gen")


def fullname(item):
    return ""


sys.excepthook = excepthook
from ecf import *  # mirror
import ecf
import ecflow


# class Edit(ecf.Variables):    pass

MICRO = "$$"  # keep ecFlow pleased with micro character balance
DEBUG = 1
DEBUG = 0
USER = "emos"
# USER = "map"

sms_status = {
    -1: "unknown",
    0: "unknown",
    1: "suspended",
    2: "complete",
    3: "queued",
    4: "submitted",
    5: "active",
    6: "aborted",
    7: "shutdown",
    8: "halted",
    9: "unknown",
}
sms_type = {
    13: "definition",
    12: "suite",
    11: "family",
    10: "task",
    32: "alias",
}


############################
def usage():
    print(
        sys.argv[0],
        """
      -h # help
      -m <server@port> # mirror path fri
      -t # unit test
      -r # load the task definition in a suite, associated with -p option
      -r # with -m, allow suite loading in destination
      -u usage

can also be used as a module: gen_task, gen_suite

    """,
    )
    for num, val in enumerate(sys.argv):
        print(num, val)
    child.report("abort")
    sys.exit(2)


############################
def get_uid():
    return pwd.getpwnam(get_username()).pw_uid


############################
def gen_task(var=True, load_only=False, kind=None):
    from ecf import Task, Label, Variables

    pwd = os.getcwd()
    name = sys.argv[0]
    if "/" in name:
        name = name.split("/")[-1]
    if "." in name:
        name = name.split(".")[0]
    rec = "00:30"
    cmd = "/home/ma/emos/bin/trimurti emos eurus $ECF_JOB$ $ECF_JOBOUT$"
    cmd = "/usr/local/apps/python/2.7.12-01/bin/python $ECF_JOB$"
    WDIR = pwd
    ODIR = pwd

    load = (
        Label("info", "mirror suites..."),
        Variables(
            ECF_MICRO=MICRO[0],
            ECF_HOME=ODIR,
            ECF_EXTN=".py",
            ECF_JOB_CMD=cmd,
        ),
    )
    if load_only:
        return load
    return Task(name).add(Cron("04:00 20:00 " + rec), If(var, load))


############################
# def stop(msg, num, child=None):
#     print(msg);
#     if child:
#         child.report(msg)
#         if num == 0: child.report("complete")
#     sys.stdout.flush()
#     sys.stderr.flush()
#     sys.exit(num)
def timer(a, b):
    if child is None:
        return
    child.report("timeout")
    child.report("complete")


import signal

signal.signal(signal.SIGALRM, timer)
signal.alarm(180)


############################
class Child(object):
    """facilitate job behaviour with ecFlow server:
    just instanciate and call complete when finished

    obj = Child(); obs.report("complete")

    this does nothing when script is called from command line
    """

    def __init__(self):
        import signal

        env = {
            "ECF_HOST": "$ECF_HOST:$",
            # check can be on a server, child on another
            "ECF_PASS": "$ECF_PASS$",
            "ECF_NAME": "$ECF_NAME$",
            "ECF_PORT": "$ECF_PORT:0$",
            "ECF_TRYNO": "$ECF_TRYNO$",
        }
        self.client = None
        if MICRO[0] in env["ECF_PORT"]:
            print("#MSG: cli mode")
            return
        print("#MSG: will communicate with server...")
        print("#MSG: kill: ssh %s kill -15 %d" % (os.uname()[1], os.getpid()))
        for sig in (
            signal.SIGINT,
            signal.SIGHUP,
            signal.SIGQUIT,
            signal.SIGILL,
            signal.SIGTRAP,
            signal.SIGIOT,
            signal.SIGBUS,
            signal.SIGFPE,
            signal.SIGUSR1,
            signal.SIGUSR2,
            signal.SIGPIPE,
            signal.SIGTERM,
            signal.SIGXCPU,
            signal.SIGPWR,
        ):
            signal.signal(sig, self.signal_handler)
        self.set_client()

    def set_client(self):
        self.client = ecflow.Client()
        if len(MICRO) == 2:
            host = env["ECF_HOST"]
            print(host)
            self.client.set_host_port(host, int(env["ECF_PORT"]))
            self.client.set_child_pid(os.getpid())
            self.client.set_child_path(env["ECF_NAME"])
            self.client.set_child_password(env["ECF_PASS"])
            self.client.set_child_try_no(int(env["ECF_TRYNO"]))
        else:
            host = "$ECF_HOST:$"
            print(host)
            self.client.set_host_port(host, int("$ECF_PORT:0$"))
            self.client.set_child_pid(os.getpid())
            self.client.set_child_path("$ECF_NAME$")
            self.client.set_child_password("$ECF_PASS$")
            self.client.set_child_try_no(int("$ECF_TRYNO$"))
        self.client.child_init()

        self.client.child_label("info", "%s" % strftime("%Y-%m-%d %H:%M:%S", gmtime()))
        self.client.set_child_timeout(20)
        print(self.client)

    def signal_handler(self, signum, frame):
        """ catch signal """
        print("Aborting: Signal handler called with signal ", signum)
        self.report("abort", "Signal handler called with signal " + str(signum))

    def __exit__(self, exc_type, exc_value, traceback):
        self.report("abort", "__exit__")

    def report(self, msg, meter=None):
        """ communicate with ecFlow server """
        if not self.client:
            print("#MSG: no child")
            if msg in ("stop", "complete"):
                sys.stdout.flush()
                sys.stderr.flush()
                print("#MSG: no child stop")
                sys.exit(0)
            else:
                print(msg, meter)
            return
        elif msg in ("stop", "complete"):
            self.client.child_complete()
            self.client = None
            sys.stdout.flush()
            sys.stderr.flush()
            print("#MSG: stop")
            sys.exit(0)
        elif msg in ("abort",):
            self.client.child_abort()
            self.client = None
            raise Exception(msg)
        elif meter:
            self.client.child_meter(msg, meter)
        else:
            self.client.child_label("info", msg)


############################
destinations = {
    "emos": "${DEST_HOST:=localhost}@${DEST_PORT=5001}",
    "test": "${DEST_HOST:=localhost}@${DEST_PORT=5001}",
    "mc": "localhost@5003",
}


def gen_suite(host=None, port=None, path=None):
    import ecf
    from ecf import (
        Task,
        Label,
        Variables,
        Suite,
        Defs,
        Family,
        Trigger,
        Defstatus,
        Meter,
        Event,
    )

    # destinations = dict()
    defs = Defs()
    sname = "mirror"
    print(list(definitions.keys()), list(destinations.keys()))
    defs.add_suite(
        Suite(sname).add(
            ecf.Defstatus("suspended"),
            Limit("one", 2),
            Inlimit("one"),
            gen_task(load_only=True),
            Variables(ALL_ECF=" "),
            [
                Family(name).add(
                    Variables(DESTINATIONS=destinations[name]),
                    Label("info", ""),
                    gen_task(0, kind=name),
                )
                for name in list(definitions.keys())
                # if name in destinations.keys()
            ],
        )
    )
    if DEBUG:
        print(defs)
    return defs


definitions = {  # strings as path for all nodes below sync
    # or suites, subset of the original, leaves are task...
    # or else not sync'ed
    # shall we add list of string/path ???
    "emos": Suite("emos").add(
        Defstatus("suspended"), Family("f1").add(Edit(YMD=20320101), Task("t1"))
    ),
    "test": Suite("test").add(
        Defstatus("suspended"), Family("f1").add(Edit(YMD=20320101), Task("t1"))
    ),
    "mc": Suite("mc").add(
        Defstatus("suspended"), Family("main").add(
            Edit(YMD=20990101), 
            Family("06bc").add(
                Task("sweeper"), 
                Family("fc0006d").add(Task("fc")),
            ),
    ), ),
}

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


class Mirror(object):

    """    """

    def _protect_oper(self, server):
        if server == "xyz@123":
            if 1:
                child.report("abort", "protect")
            elif (
                "/o" in self.path
                or self.path == "all"
                or "/eda" in self.path
                or "/mc" in self.path
            ):
                raise Exception("# wont do that...")

    def _warmup(self):
        self._set_register(self.path)
        if not self.is_sms:
            self.source.ch_register(False, self.register)
            self.source.sync_local()

    def _send(self, msg="", client=None, send=False):
        """self.grouped is None: no buffering, immediate send
        msg accumulate (send False by default)
        msg is sent ("", client, send=True)
        """
        if self.grouped is None:
            print("#MSG: send", self.grouped)
            client.group(msg)
            time.sleep(self.snap)
            return

        self.grouped += msg
        if send and self.grouped != "":
            print("#MSG: send", self.grouped)
            client.group(self.grouped)
            client.sync_local()
            self.grouped = ""
            time.sleep(self.snap)

    def __init__(self, server="eurus@1630", path="/e_45r1", replay=False, is_sms=False):
        self.is_sms = is_sms
        self.replay = replay  # allow replay from skel definition
        self.act = 1  # active ie force change allowed
        self.snap = 1  # sleep interval
        self.wait = 0  # refresh interval,continue as long as sub/act node exist
        self.path = path
        self.grouped = None  # would use atomic alter
        self.grouped = ""  # command to send to the server ... grouped
        self._set_register(path)

        if server == "all":
            self.servers = "$DESTINATIONS$".split(" ")
        elif " " in server:
            self.servers = server.split(" ")
        elif "@" in server:
            host, port = server.split("@")
            self.servers = ("%s@%s" % (host, port),)
        elif ":" in server:
            host, port = server.split(":")
            self.servers = ("%s:%s" % (host, port),)
        if self.servers[-1] == "":
            del self.servers[-1]
        host = os.getenv("ECF_HOST", "localhost")
        # if MICRO[0] in host: host = "localhost"
        port = os.getenv("ECF_PORT", "3141")
        if MICRO[0] in port:
            port = 1500 + int(get_uid())
        # when started with ecflow_start.sh
        # aka reference/source  server name
        self.sname = "%s:%s" % (host, port)

        if "$SOURCE:$" != "" and MICRO[0] not in "$SOURCE:$":
            self.sname = "$SOURCE:$"
        if DEBUG:
            print("#DBG: source is", self.sname)
        self.set_source()

    def set_source(self):
        self.source = ecflow.Client(self.sname)
        self._warmup()

    def __exit__(self):
        if type(self.source) == ecflow.Client:
            try:
                self.source.ch_drop()
            except:
                pass

    def _set_register(self, path):
        if "/" == path[0]:
            suite = str(path.split("/")[1])
            self.register = [
                suite,
            ]

        elif path == "all":
            self.register = list(skel.keys())

        else:
            child.report("abort", "register")

    def _check(self, server, client, stop=0):
        if client == self.source:
            stop = 1

        for suite in self.register:
            top = client.get_defs().find_abs_node("/" + suite)

            if top is None:
                print(
                    "#WAR: node not found",
                    suite,
                    server,
                )
                if stop:
                    child.report("abort", "check")
                elif self.act and server != self.sname:
                    self._protect_oper(server)
                    if self.replay:
                        if type(definitions[suite]) == str:
                            print("#WAR: not yet")
                            child.report("abort", "str")
                        print("replaying")

                        defs = ecflow.Defs()
                        defs.add_suite(definitions[suite])
                        host = "localhost"
                        client.load(defs)
                        client.suspend("/" + suite)
                        client.begin_suite(suite)
                        client.sync_local()
                    else:
                        print("replay?")
                else:
                    pass

            elif server != self.sname and not (
                top.is_suspended()
                or "%s" % top.get_state() == "unknown"  # normal backup server
            ):  # test server
                print("#ERR: top node shall be suspended - NOGO")
                print("#ERR:", server, top.name(), top.get_state())
                if 1:
                    child.report("abort", "state")
                print("#MSG: stop")
                child.report("abort", "state")  # sys.exit(1)

            else:
                pass

            if suite not in list(definitions.keys()):
                raise Exception(list(definitions.keys()), suite)

            if type(definitions[suite]) == str:
                path = definitions[suite]

            elif suite in self.path:
                path = self.path

            else:
                return

            node = client.get_defs().find_abs_node(path)
            if node:
                return
            print("#WAR: node not found", path, server)
            if stop:
                child.report("abort", "stop")

    def _update_var(self, var, nfrom, ndest, server, client, kind="edit"):
        name = var.name()
        vfrom = None
        vdest = None
        repeat = nfrom.get_repeat()
        print("############# %s" % repeat, nfrom.get_abs_node_path())
        if repeat and repeat.name() == name:
            vfrom = repeat
        else:
            for vfrom in nfrom.variables:
                if vfrom.name() == name:
                    break

        for vdest in ndest.variables:
            if vdest.name() == name:
                break
        pnode = ndest.get_abs_node_path()
        pnode = nfrom.get_abs_node_path()

        if vfrom is None:
            return

        elif vfrom.name() != name:
            return

        if vdest is None:
            return

        vname = vdest.name()
        vval = vdest.value()
        if vname != name:
            return

        if vfrom.name() == vname and str(vfrom.value()) != str(vval) or FORCE:
            if type(vfrom) == Repeat:
                self.action(
                    ndest.get_abs_node_path(),
                    server,
                    client,
                    kind="repeat",
                    value=str(vfrom.value()),
                    msg="repeat requeue",
                )
            else:
                self.action(
                    pnode,
                    server,
                    client,
                    kind=name,
                    value=str(vfrom.value()),
                    msg="value update",
                )

    def process(self, server=None, client=None, item=None, var=False):
        if server is None:
            self._check(self.sname, self.source, 1)

            for server in self.servers:
                server = server.replace("@", ":")
                if DEBUG:
                    print("#DBG: target is", server)
                host, port = server.split(":")

                if int(port) < 65536 and 0 == comm(
                    ECFLOWC + "--ping --port %s --host %s" % (port, host), False
                ):
                    client = ecflow.Client(server)
                    client.ch_register(False, self.register)
                    client.sync_local()
                    self._check(server, client)
                    for suite in self.register:
                        node = definitions[suite]
                        if type(node) == str:
                            node = self.source.get_defs().find_abs_node(node)
                        # try: node.add(Label("memo", "This suite was generated by mirror.py"))
                        # except: pass # used to sync, too late
                        # update variables
                        self.process(server, client, node, "repeat")
                        # self.process(server, client, node) # update statuses
                        # self.process(server, client, node, "edit") # update
                        # variables
                    client.ch_drop()

                else:
                    # child.report("abort","res")
                    print("server is not responding")

            if self.wait > 0:
                print("#MSG: start again... snap ", self.wait)
                time.sleep(self.wait)
                self.wait = 0
                self.source.sync_local()
                self.process()
            return

        elif server == self.sname:
            child.report("abort", "same name " + server + " " + self.sname)  # DONT

        elif client is None:
            child.report("abort", "no client")  # DONT

        elif type(item) in (ecflow.Suite, ecf.Suite):

            if 0:
                pass
            else:
                # suite =
                # client.get_defs().find_abs_node(item.get_abs_node_path())
                suite = client.get_defs().find_abs_node(item.fullname())
                # print(type(client), item.fullname(), type(suite))
                if suite:
                    if not suite.is_suspended():
                        print("#ERR: top node shall be suspended - stop", suite, server)
                        return
                    for node in suite.nodes:
                        self.process(server, client, node, var)
            return

        elif type(item) in (ecflow.Family, ecflow.Task):
            pnode = item.get_abs_node_path()
            nfrom = self.source.get_defs().find_abs_node(pnode)
            if 0:
                pass
            else:
                ndest = client.get_defs().find_abs_node(pnode)

            if nfrom is None:
                if var:
                    return  # ignore
                print(
                    "#ERR: node not found, please update mirror.py skel",
                    pnode,
                    self.sname,
                )
                child.report("abort", "nfrom")

            elif ndest is None:
                if var:
                    return  # ignore
                print("#ERR: node not found, replay?", pnode, server)
                return

            else:
                pass
            if nfrom is not None:
                status = "%s" % nfrom.get_state()
            else:
                raise Exception(pnode, self.source, "nfrom is not")

            if 0:
                pass
            else:
                dstate = "%s" % ndest.get_state()
            is_task = type(item) == ecf.Task

            for evs in nfrom.events:
                for evd in ndest.events:
                    # if evd.name_or_number() != evs.name_or_number(): continue
                    if evd.name() == "" and evd.number() != evs.number():
                        continue
                    if evd.value() == evs.value():
                        break
                    print(
                        "#event",
                        evs.name(),
                        evd.number(),
                        evs.number(),
                        evd.value(),
                        evs.value(),
                    )
                    if evs.value():
                        if evs.name() == "":
                            self._send(
                                "alter change event %d set %s;" % (evs.number(), pnode),
                                client,
                            )
                            # client.alter(pnode, "change", "event", "%d" %
                            # evs.number(), "set")
                        else:
                            self._send(
                                "alter change event %s set %s;" % (evs.name(), pnode),
                                client,
                            )
                        # else: client.alter(pnode, "change", "event",
                        # evs.name(), "set")

            for evs in nfrom.meters:
                for evd in ndest.meters:
                    if evd.name() != evs.name():
                        continue
                    if evd.value() == evs.value():
                        break
                    print("#meter", evd.value(), evs.value())
                    try:
                        value = evs.value()
                        if value > 100:
                            value = 100
                        if value > 0:
                            self._send(
                                "alter change meter %s %s %s;"
                                % (evs.name(), value, pnode),
                                client,
                            )
                        # client.alter(pnode, "change", "meter", evs.name(),
                        # "%s" % evs.value())
                    except:
                        pass

            if status != dstate or FORCE:
                print(
                    "#WAR: diff ",
                    pnode,
                    status,
                    dstate,
                )
                if status == "complete":
                    self.action(
                        pnode, server, client, status, is_task, msg="forced complete"
                    )
                    if not is_task:
                        self._send("", client, send=True)
                        return

                elif status == "queued":
                    # self._send("force queued %s;" % (pnode), client)
                    self.action(
                        pnode, server, client, status, is_task, msg="forced queued"
                    )
                elif status in (
                    "submitted",
                    "active",
                    "unknown",
                    "aborted",
                ):
                    print("#IGN: status is active/submit, ignored")
                else:
                    raise Exception(status)

            if type(item) in (ecflow.Suite, ecflow.Family):
                for node in item.nodes:
                    self.process(server, client, node, var)

            if var:
                for edit in item.variables:
                    self._update_var(edit, nfrom, ndest, server, client, var)

            self._send("", client, send=True)
        else:
            raise Exception(type(item), item)

    def action(
        self, pnode, server, client, kind, is_task=False, value="20010101", msg=""
    ):
        if not self.act:
            return

        self._protect_oper(server)

        print("#MSG:", msg)
        if kind in (
            "active",
            "submitted",
        ):
            return
        elif FORCE:
            pass  # SMS ONLY

        elif kind == "complete":
            if is_task:
                self._send("", client, True)
                client.force_state(pnode, ecflow.State.complete)
            else:
                self._send("", client, True)
                client.force_state_recursive(pnode, ecflow.State.complete)
        elif kind == "queued":  # and is_task:
            self._send("force queued %s;" % pnode, client)

        elif kind == "repeat":
            raise Exception

        else:  # var
            if DEBUG:
                print("#DBG: update var", kind, value)
            if kind in ("queued",):
                pass
            elif kind not in (
                "YMD",
                "JUL",
                "DAY",
                "SCHOST",
                "STHOST",
                "YYYY",
                "MM",
                "ECF_FILES",
                "INIBEGINDATE",
                "MIXTASK",
            ):
                raise Exception(kind)
            if 0:
                pass
            else:
                client.requeue(pnode)
                self._send(
                    "alter change variable %s %s %s;" % (kind, value, pnode),
                    client,
                    True,
                )


# ignore Family + queued while is might just be that we dont mirror all
# fam/tasks and inheritance get it already complete in dest while still
# queued in source


############################
def replay(path, defs=None):
    import ecf

    miss = "@undef@"
    host = os.getenv("ECF_HOST", miss)
    if host == miss:
        print("ECF_HOST not defined")
        exit(1)
    port = os.getenv("ECF_PORT", 31415)
    client = ecf.Client(host, port)
    if defs is None:
        defs = gen_suite(host, port, path)
    if DEBUG:
        print(defs)
    # print(defs)
    if (
        "localhost" in host
        and port in ("3141", 3141)
        and ("/test" in path)
        and "/mirror" not in path
    ):
        raise Exception("dont")

    print("#MSG: replacing %s in " % path, host, port, defs)
    client.replace(path, defs, 1, 1)
    return client


def comm(cmd, rem=True):
    import subprocess

    if rem and cmd in list(memo.keys()):
        return memo[cmd]
    (rc, res) = subprocess.getstatusoutput(cmd)
    print("#COMM:", rc, res, cmd)
    if rem:
        memo[cmd] = rc
    return rc


############################
class TestMirror(unittest.TestCase):
    """ a test case """

    def test_1(self, test_ok=1):
        sname = "mirror"
        replay("/" + sname, gen_suite())


memo = dict()
############################
if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser(add_help=False)
    parser.add_argument("-?", default=0, help="help")
    parser.add_argument("-f", "--force", action="store_true", help="force")
    parser.add_argument("-h", default=0, help="help")
    parser.add_argument("-m", "--mirror", default="localhost@31415", help="mirror")
    parser.add_argument("-p", "--path", default="/mirror/test", help="path")
    parser.add_argument("-r", "--replay", action="store_true", help="replay")
    parser.add_argument("-t", "--test", action="store_true", help="test")
    parser.add_argument("-u", "--usage", action="store_true", help="test")

    parsed = parser.parse_args()
    global FORCE
    FORCE = parsed.force
    print(parsed.path, parsed.mirror, parsed.replay, parsed.force, parsed.test)
    # raise Exception
    child = Child()

    if parsed.usage:
        print(
            """

* mirrors (slow real-time) a source suite, from a source server, to a destination server
* mirror suite can be hosted on the source, on the destination or another server
* this script was used in ecflow course to demonstrate
  * one script that can be used from the command line
  * defines a suite
  * play/replace a suite/node
  * a script can be used as task wrapper/template to generate jobs
  * an ecflow client (passive) to download status/value from source
  * an ecflow client (active) to change status/value on destination

* update mirror.py: destination variable with the triplet suite host port

* update mirror.py: definition with the key(suite) and the minimum suite-tree to mirror

* update mirror.py: replay add "if" for "protection" not to overwrite source suite by mistake

* download a copy from the source suite
  ecflow_client --port 3141 --host localhost --get > tmp.def

* load the mirror suite on the source server
  SUITE=test ECF_HOST=localhost ECF_PORT=3141 ./mirror.py -p /mirror/test -r # load

* play (suspended) mirrored suite in destination
  ECF_HOST=ecgate ECF_PORT=31415 ./mirror.py -m ecgate:31415 -p /test

module load python3; module load ecflow/5.7.0
ecflow_start.sh -p 5001 # source
ecflow_start.sh -p 5003 # destination
# ecflow_client --host vecf1 --port 44444 --get mc > mc.dump # an example suite
ecflow_client --host localhost --port 5001 --replace /mc mc.dump # we take it as a source
# definitions =  # contains "mc" definition
# destinations = { # contains one destination for mc at least
 export  SUITE=mc ECF_HOST=localhost ECF_PORT=5001
 ./mirror.py -p /mirror/mc -r # load mirror on source server # might be destination or another server yet
# ImportError: No module named ecf # ecf shall be in the path, at least it can found in ecflow distrib as 
# ecflow/libs/pyext/samples/api/ecf.py
# ('#MSG: replacing /mirror/test in ', 'localhost', '5001', #5.7.0 # loading OK
# begin mirror - 
# Script OK? mirror.py undef ECF_FILES directory?
# Edit OK? $ is used as ECF_MICRO
# cron OK?
# set DESTINATIONS variable as expected

ECF_HOST=localhost ECF_PORT=5003 python3 ./mirror.py -m localhost:5003 -p /mc # ok
#COMM: 0 ping server(localhost:5003) succeeded in 00:00:00.001058  ~1 milliseconds ecflow_client --ping --port 5003 --host localhost # OK
# replay?
ECF_PORT=5003 python3 ./mirror.py -m localhost:5003 -p /mc -r # ok mc loaded on target
# begin /mc in 5003 (it looks suspended
# edit ECF_JOB_CMD "python3 $ECF_JOB$ > $ECF_JOBOUT$ &"

# python3 /home/ma/map/mirror/mc/mirror.job1 # OK after ECF_PASS FREE running as zombie ommand line
# ImportError: No module named 'ecf' # jobs lost in submit

# mirror jobs submits and complete OKWITH ECF_JOB_CMD updated as 
module load python3 && module load ecflow/5.7.0 && cd /home/ma/map && python3 $ECF_JOB$ > $ECF_JOBOUT$ 2>&1 &
"""
        )
        sys.exit(2)

    if not MICRO[0] in "$ECF_PORT$":
        parsed.mirror = "$DESTINATIONS$"
        parsed.path = "/$FAMILY1$"
        print("#MSG: ssh %s kill -9 %d" % (os.uname()[1], os.getpid()))

    if parsed.test:
        sys.argv.pop()
        unittest.main()

    elif parsed.path and parsed.replay:
        print("#MSG: path and replay", parsed.path, parsed.replay)
        port = int(os.getenv("SMS_PROG", 0))
        s = parsed.path.split("/")[1]
        defs = None
        if "mirror" not in parsed.path:
            defs = Defs()
            defs.add_suite(definitions[s])
        replay(parsed.path, defs)

    elif parsed.mirror and parsed.path:
        print(parsed.mirror, "#", parsed.path, parsed.replay, type(Mirror))
        mirror = Mirror(parsed.mirror, parsed.path, parsed.replay)
        print("#MSG: step")
        mirror.process()
        print("#MSG: step")
        child.report("complete")
        print("#MSG: step")

    else:
        usage()
    print("#MSG: step")
    child.report("complete")
"""
  export ECF_HOST=localhost ECF_PORT=5001 SUITE=mc
  ecflow_client --get /mc > tmp.def
  ./mirror.py -p /mirror/mc -r # load

  ECF_HOST=localhost ECF_PORT=31415 ./mirror.py -m localhost:31415 -p /mc

/tmp/map/work/git/ecflow/libs/pyext/samples/mirror.py
"""