5
5
import logging
6
6
import os
7
7
import re
8
- from concurrent .futures import wait
9
- from threading import Event
8
+ from concurrent .futures import wait , TimeoutError
9
+ import threading
10
10
from typing import List , Optional
11
11
import docker .errors
12
12
13
13
from launcher .table import ServiceTable
14
14
from launcher .utils import yes_or_no , normalize_path
15
+ from launcher .errors import NoWaiting
15
16
from .base import Node , CliBackend , CliError
16
17
from .lnd import CFHeaderState
17
18
@@ -78,7 +79,6 @@ def _get_environment(self) -> List[str]:
78
79
]
79
80
80
81
lndbtc = self .config .nodes ["lndbtc" ]
81
- env .append ("LNDBTC_MODE={}" .format (lndbtc ["mode" ]))
82
82
if lndbtc ["mode" ] == "external" :
83
83
env .extend ([
84
84
"LNDBTC_RPC_HOST={}" .format (lndbtc ["rpc_host" ]),
@@ -88,7 +88,6 @@ def _get_environment(self) -> List[str]:
88
88
])
89
89
90
90
lndltc = self .config .nodes ["lndltc" ]
91
- env .append ("LNDLTC_MODE={}" .format (lndltc ["mode" ]))
92
91
if lndltc ["mode" ] == "external" :
93
92
env .extend ([
94
93
"LNDLTC_RPC_HOST={}" .format (lndbtc ["rpc_host" ]),
@@ -178,7 +177,7 @@ def extract_exception(self, cmd: str, output: str):
178
177
print ("^C" )
179
178
raise KeyboardInterrupt ()
180
179
181
- def _ensure_dependencies_ready (self , stop : Event ):
180
+ def _ensure_dependencies_ready (self , stop : threading . Event ):
182
181
deps = [
183
182
self .get_service ("lndbtc" ),
184
183
self .get_service ("lndltc" ),
@@ -428,9 +427,13 @@ def _ensure_lnds_synced(self, stop):
428
427
429
428
if len (lnds ) > 0 :
430
429
def all_ready ():
430
+ nonlocal lnds
431
+
431
432
return functools .reduce (lambda r , item : r and item .ready , lnds .values (), True )
432
433
433
- def print_syncing (stop ):
434
+ def print_syncing (stop : threading .Event ):
435
+ nonlocal lnds
436
+
434
437
print ("Syncing light clients:" )
435
438
436
439
rows = {}
@@ -443,6 +446,7 @@ def print_syncing(stop):
443
446
444
447
while not stop .is_set ():
445
448
i = 0
449
+ logger .debug ("lnds %r" , lnds )
446
450
for lnd , state in lnds .items ():
447
451
old_msg = rows [lnd .name ]
448
452
msg = state .message
@@ -461,44 +465,72 @@ def print_syncing(stop):
461
465
462
466
stop .wait (1 )
463
467
464
- futs = []
468
+ logger . debug ( "Light clients syncing ends" )
465
469
466
470
executor = self .config .executor
467
471
472
+ f = executor .submit (print_syncing , stop )
473
+
468
474
for lnd in lnds :
469
- futs . append ( executor .submit (lnd .update_cfheader , lnds [lnd ], stop ) )
475
+ executor .submit (lnd .update_cfheader , lnds [lnd ], stop )
470
476
471
- # Thread(target=print_syncing, args=(stop,)).start ()
472
- futs . append ( executor . submit ( print_syncing , stop ) )
477
+ f . result ()
478
+ logger . debug ( " print_syncing ends" )
473
479
474
- wait (futs )
480
+ def _wait_tls_cert (self , stop : threading .Event ):
481
+ tls_file = os .path .join (self .data_dir , "tls.cert" )
482
+ while not stop .is_set ():
483
+ if os .path .exists (tls_file ):
484
+ break
485
+ stop .wait (1 )
475
486
476
- def ensure_ready (self , stop : Event ):
477
- logger .info ("Ensuring ready" )
487
+ def ensure_ready (self , stop : threading . Event ):
488
+ logger .info ("Ensuring XUD is ready" )
478
489
479
490
if self .node_manager .newly_installed :
491
+ logger .info ("Ensuring LNDs are synced (light)" )
480
492
self ._ensure_lnds_synced (stop )
481
493
494
+ logger .info ("Ensuring XUD dependencies are ready (lndbtc, lndltc and connext)" )
482
495
self ._ensure_dependencies_ready (stop )
483
496
497
+ logger .info ("Waiting for XUD to be ready" )
498
+ executor = self .config .executor
499
+ f = executor .submit (self ._wait_tls_cert , stop )
500
+
501
+ while not stop .is_set ():
502
+ logger .info ("Waiting for XUD tls.cert to be created" )
503
+ try :
504
+ f .result (30 )
505
+ except TimeoutError :
506
+ print ("XUD should not take so much time to create \" tls.cert\" file. please check container \" %s\" logs for more details." % self .container_name )
507
+ reply = yes_or_no ("Would you like to keep waiting?" )
508
+ if reply == "no" :
509
+ raise NoWaiting
510
+
484
511
# Error: ENOENT: no such file or directory, open '/root/.xud/tls.cert'
485
512
# xud is starting... try again in a few seconds
486
513
# xud is locked, run 'xucli unlock', 'xucli create', or 'xucli restore' then try again
514
+ cmd = self ._cli + " getinfo -j"
487
515
while not stop .is_set ():
488
516
try :
489
- exit_code , output = self .exec (self . _cli + " getinfo -j" )
517
+ exit_code , output = self .exec (cmd )
490
518
if exit_code == 0 :
491
519
break
492
520
if "xud is locked" in output :
493
521
break
522
+ logger .debug ("[Execute] %s (exit_code=%s)\n %s" , cmd , exit_code , output .rstrip ())
494
523
except docker .errors .APIError :
495
524
logger .exception ("Failed to getinfo" )
496
- stop .wait (3 )
525
+ stop .wait (1 )
497
526
498
527
if not self .has_wallets ():
528
+ logger .info ("Setting up XUD wallets" )
499
529
self ._setup_wallets ()
500
530
531
+ logger .info ("Setting up XUD backup" )
501
532
self ._setup_backup ()
502
533
503
534
if self ._is_locked ():
535
+ logger .info ("Unlock XUD" )
504
536
self ._unlock ()
0 commit comments