@@ -50,16 +50,14 @@ class BaseNode:
50
50
:param node_id: Node instance identifier
51
51
:param project: Project instance
52
52
:param manager: parent node manager
53
- :param console: console TCP port
54
- :param console_type: console type
55
- :param aux: auxiliary console TCP port
56
- :param aux_type: auxiliary console type
53
+ :param console: TCP console port
54
+ :param aux: TCP aux console port
55
+ :param allocate_aux: Boolean if true will allocate an aux console port
57
56
:param linked_clone: The node base image is duplicate/overlay (Each node data are independent)
58
57
:param wrap_console: The console is wrapped using AsyncioTelnetServer
59
- :param wrap_aux: The auxiliary console is wrapped using AsyncioTelnetServer
60
58
"""
61
59
62
- def __init__ (self , name , node_id , project , manager , console = None , console_type = "telnet" , aux = None , aux_type = "none" , linked_clone = True , wrap_console = False , wrap_aux = False ):
60
+ def __init__ (self , name , node_id , project , manager , console = None , console_type = "telnet" , aux = None , allocate_aux = False , linked_clone = True , wrap_console = False ):
63
61
64
62
self ._name = name
65
63
self ._usage = ""
@@ -70,25 +68,22 @@ def __init__(self, name, node_id, project, manager, console=None, console_type="
70
68
self ._console = console
71
69
self ._aux = aux
72
70
self ._console_type = console_type
73
- self ._aux_type = aux_type
74
71
self ._temporary_directory = None
75
72
self ._hw_virtualization = False
76
73
self ._ubridge_hypervisor = None
77
74
self ._closed = False
78
75
self ._node_status = "stopped"
79
76
self ._command_line = ""
77
+ self ._allocate_aux = allocate_aux
80
78
self ._wrap_console = wrap_console
81
- self ._wrap_aux = wrap_aux
82
- self ._wrapper_telnet_servers = []
79
+ self ._wrapper_telnet_server = None
83
80
self ._wrap_console_reader = None
84
81
self ._wrap_console_writer = None
85
82
self ._internal_console_port = None
86
- self ._internal_aux_port = None
87
83
self ._custom_adapters = []
88
84
self ._ubridge_require_privileged_access = False
89
85
90
86
if self ._console is not None :
91
- # use a previously allocated console port
92
87
if console_type == "vnc" :
93
88
vnc_console_start_port_range , vnc_console_end_port_range = self ._get_vnc_console_port_range ()
94
89
self ._console = self ._manager .port_manager .reserve_tcp_port (
@@ -102,45 +97,25 @@ def __init__(self, name, node_id, project, manager, console=None, console_type="
102
97
else :
103
98
self ._console = self ._manager .port_manager .reserve_tcp_port (self ._console , self ._project )
104
99
100
+ # We need to allocate aux before giving a random console port
105
101
if self ._aux is not None :
106
- # use a previously allocated auxiliary console port
107
- if aux_type == "vnc" :
108
- # VNC is a special case and the range must be 5900-6000
109
- self ._aux = self ._manager .port_manager .reserve_tcp_port (
110
- self ._aux , self ._project , port_range_start = 5900 , port_range_end = 6000
111
- )
112
- elif aux_type == "none" :
113
- self ._aux = None
114
- else :
115
- self ._aux = self ._manager .port_manager .reserve_tcp_port (self ._aux , self ._project )
102
+ self ._aux = self ._manager .port_manager .reserve_tcp_port (self ._aux , self ._project )
116
103
117
104
if self ._console is None :
118
- # allocate a new console
119
105
if console_type == "vnc" :
120
106
vnc_console_start_port_range , vnc_console_end_port_range = self ._get_vnc_console_port_range ()
121
107
self ._console = self ._manager .port_manager .get_free_tcp_port (
122
108
self ._project ,
123
109
port_range_start = vnc_console_start_port_range ,
124
- port_range_end = vnc_console_end_port_range ,
125
- )
110
+ port_range_end = vnc_console_end_port_range )
126
111
elif console_type != "none" :
127
112
self ._console = self ._manager .port_manager .get_free_tcp_port (self ._project )
128
113
129
- if self ._aux is None :
130
- # allocate a new auxiliary console
131
- if aux_type == "vnc" :
132
- # VNC is a special case and the range must be 5900-6000
133
- self ._aux = self ._manager .port_manager .get_free_tcp_port (
134
- self ._project , port_range_start = 5900 , port_range_end = 6000
135
- )
136
- elif aux_type != "none" :
137
- self ._aux = self ._manager .port_manager .get_free_tcp_port (self ._project )
138
-
139
114
if self ._wrap_console :
140
115
self ._internal_console_port = self ._manager .port_manager .get_free_tcp_port (self ._project )
141
116
142
- if self ._wrap_aux :
143
- self ._internal_aux_port = self ._manager .port_manager .get_free_tcp_port (self ._project )
117
+ if self ._aux is None and allocate_aux :
118
+ self ._aux = self ._manager .port_manager .get_free_tcp_port (self ._project )
144
119
145
120
log .debug ("{module}: {name} [{id}] initialized. Console port {console}" .format (module = self .manager .module_name ,
146
121
name = self .name ,
@@ -368,9 +343,6 @@ async def close(self):
368
343
if self ._aux :
369
344
self ._manager .port_manager .release_tcp_port (self ._aux , self ._project )
370
345
self ._aux = None
371
- if self ._wrap_aux :
372
- self ._manager .port_manager .release_tcp_port (self ._internal_aux_port , self ._project )
373
- self ._internal_aux_port = None
374
346
375
347
self ._closed = True
376
348
return True
@@ -394,49 +366,56 @@ def _get_vnc_console_port_range(self):
394
366
395
367
return vnc_console_start_port_range , vnc_console_end_port_range
396
368
397
- async def _wrap_telnet_proxy (self , internal_port , external_port ):
369
+ async def start_wrap_console (self ):
370
+ """
371
+ Start a telnet proxy for the console allowing multiple telnet clients
372
+ to be connected at the same time
373
+ """
398
374
375
+ if not self ._wrap_console or self ._console_type != "telnet" :
376
+ return
399
377
remaining_trial = 60
400
378
while True :
401
379
try :
402
- (reader , writer ) = await asyncio .open_connection (host = "127.0.0.1" , port = internal_port )
380
+ (self ._wrap_console_reader , self ._wrap_console_writer ) = await asyncio .open_connection (
381
+ host = "127.0.0.1" ,
382
+ port = self ._internal_console_port
383
+ )
403
384
break
404
385
except (OSError , ConnectionRefusedError ) as e :
405
386
if remaining_trial <= 0 :
406
387
raise e
407
388
await asyncio .sleep (0.1 )
408
389
remaining_trial -= 1
409
- await AsyncioTelnetServer .write_client_intro (writer , echo = True )
410
- server = AsyncioTelnetServer (reader = reader , writer = writer , binary = True , echo = True )
390
+ await AsyncioTelnetServer .write_client_intro (self ._wrap_console_writer , echo = True )
391
+ server = AsyncioTelnetServer (
392
+ reader = self ._wrap_console_reader ,
393
+ writer = self ._wrap_console_writer ,
394
+ binary = True ,
395
+ echo = True
396
+ )
411
397
# warning: this will raise OSError exception if there is a problem...
412
- telnet_server = await asyncio .start_server (server .run , self ._manager .port_manager .console_host , external_port )
413
- self ._wrapper_telnet_servers .append (telnet_server )
414
-
415
- async def start_wrap_console (self ):
416
- """
417
- Start a Telnet proxy servers for the console and auxiliary console allowing multiple telnet clients
418
- to be connected at the same time
419
- """
420
-
421
- if self ._wrap_console and self ._console_type == "telnet" :
422
- await self ._wrap_telnet_proxy (self ._internal_console_port , self .console )
423
- log .info ("New Telnet proxy server for console started (internal port = {}, external port = {})" .format (self ._internal_console_port ,
424
- self .console ))
425
-
426
- if self ._wrap_aux and self ._aux_type == "telnet" :
427
- await self ._wrap_telnet_proxy (self ._internal_aux_port , self .aux )
428
- log .info ("New Telnet proxy server for auxiliary console started (internal port = {}, external port = {})" .format (self ._internal_aux_port ,
429
- self .aux ))
398
+ self ._wrapper_telnet_server = await asyncio .start_server (
399
+ server .run ,
400
+ self ._manager .port_manager .console_host ,
401
+ self .console
402
+ )
430
403
431
404
async def stop_wrap_console (self ):
432
405
"""
433
- Stops the telnet proxy servers .
406
+ Stops the telnet proxy.
434
407
"""
435
408
436
- for telnet_proxy_server in self ._wrapper_telnet_servers :
437
- telnet_proxy_server .close ()
438
- await telnet_proxy_server .wait_closed ()
439
- self ._wrapper_telnet_servers = []
409
+ if self ._wrapper_telnet_server :
410
+ self ._wrap_console_writer .close ()
411
+ if sys .version_info >= (3 , 7 , 0 ):
412
+ try :
413
+ await self ._wrap_console_writer .wait_closed ()
414
+ except ConnectionResetError :
415
+ pass
416
+ self ._wrapper_telnet_server .close ()
417
+ await self ._wrapper_telnet_server .wait_closed ()
418
+ self ._wrapper_telnet_server = None
440
419
441
420
async def reset_wrap_console (self ):
442
421
"""
@@ -513,6 +492,22 @@ async def telnet_forward(telnet_reader):
513
492
514
493
return ws
515
494
495
+ @property
496
+ def allocate_aux (self ):
497
+ """
498
+ :returns: Boolean allocate or not an aux console
499
+ """
500
+
501
+ return self ._allocate_aux
502
+
503
+ @allocate_aux .setter
504
+ def allocate_aux (self , allocate_aux ):
505
+ """
506
+ :returns: Boolean allocate or not an aux console
507
+ """
508
+
509
+ self ._allocate_aux = allocate_aux
510
+
516
511
@property
517
512
def aux (self ):
518
513
"""
@@ -531,25 +526,18 @@ def aux(self, aux):
531
526
:params aux: Console port (integer) or None to free the port
532
527
"""
533
528
534
- if aux == self ._aux or self . _aux_type == "none" :
529
+ if aux == self ._aux :
535
530
return
536
531
537
- if self ._aux_type == "vnc" and aux is not None and aux < 5900 :
538
- raise NodeError ("VNC auxiliary console require a port superior or equal to 5900, current port is {}" .format (aux ))
539
-
540
532
if self ._aux :
541
533
self ._manager .port_manager .release_tcp_port (self ._aux , self ._project )
542
534
self ._aux = None
543
535
if aux is not None :
544
- if self ._aux_type == "vnc" :
545
- self ._aux = self ._manager .port_manager .reserve_tcp_port (aux , self ._project , port_range_start = 5900 , port_range_end = 6000 )
546
- else :
547
- self ._aux = self ._manager .port_manager .reserve_tcp_port (aux , self ._project )
548
-
549
- log .info ("{module}: '{name}' [{id}]: auxiliary console port set to {port}" .format (module = self .manager .module_name ,
550
- name = self .name ,
551
- id = self .id ,
552
- port = aux ))
536
+ self ._aux = self ._manager .port_manager .reserve_tcp_port (aux , self ._project )
537
+ log .info ("{module}: '{name}' [{id}]: aux port set to {port}" .format (module = self .manager .module_name ,
538
+ name = self .name ,
539
+ id = self .id ,
540
+ port = aux ))
553
541
554
542
@property
555
543
def console (self ):
@@ -637,42 +625,6 @@ def console_type(self, console_type):
637
625
console_type = console_type ,
638
626
console = self .console ))
639
627
640
- @property
641
- def aux_type (self ):
642
- """
643
- Returns the auxiliary console type for this node.
644
- :returns: aux type (string)
645
- """
646
-
647
- return self ._aux_type
648
-
649
- @aux_type .setter
650
- def aux_type (self , aux_type ):
651
- """
652
- Sets the auxiliary console type for this node.
653
- :param aux_type: console type (string)
654
- """
655
-
656
- if aux_type != self ._aux_type :
657
- # get a new port if the aux type change
658
- if self ._aux :
659
- self ._manager .port_manager .release_tcp_port (self ._aux , self ._project )
660
- if aux_type == "none" :
661
- # no need to allocate a port when the auxiliary console type is none
662
- self ._aux = None
663
- elif aux_type == "vnc" :
664
- # VNC is a special case and the range must be 5900-6000
665
- self ._aux = self ._manager .port_manager .get_free_tcp_port (self ._project , 5900 , 6000 )
666
- else :
667
- self ._aux = self ._manager .port_manager .get_free_tcp_port (self ._project )
668
-
669
- self ._aux_type = aux_type
670
- log .info ("{module}: '{name}' [{id}]: console type set to {aux_type} (auxiliary console port is {aux})" .format (module = self .manager .module_name ,
671
- name = self .name ,
672
- id = self .id ,
673
- aux_type = aux_type ,
674
- aux = self .aux ))
675
-
676
628
@property
677
629
def ubridge (self ):
678
630
"""
0 commit comments