1
|
|
|
#!/usr/bin/env python3.6 |
2
|
|
|
"""Start Kytos SDN Platform core.""" |
3
|
|
|
import signal |
4
|
|
|
import sys |
5
|
|
|
|
6
|
|
|
import daemon |
7
|
|
|
from IPython.terminal.embed import InteractiveShellEmbed |
8
|
|
|
from IPython.terminal.prompts import Prompts, Token |
9
|
|
|
from traitlets.config.loader import Config |
10
|
|
|
|
11
|
|
|
from kytos.core import Controller |
12
|
|
|
from kytos.core.config import KytosConfig |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
class KytosPrompt(Prompts): |
16
|
|
|
"""Configure Kytos prompt for interactive shell.""" |
17
|
|
|
|
18
|
|
|
def in_prompt_tokens(self, cli=None): |
19
|
|
|
"""Kytos IPython prompt.""" |
20
|
|
|
return [(Token.Prompt, 'kytos $> ')] |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
def start_shell(controller): |
24
|
|
|
"""Load Kytos interactive shell.""" |
25
|
|
|
# pylint: disable=anomalous-backslash-in-string |
26
|
|
|
banner1 = """\033[95m |
27
|
|
|
_ _ |
28
|
|
|
| | | | |
29
|
|
|
| | ___ _| |_ ___ ___ |
30
|
|
|
| |/ / | | | __/ _ \/ __| |
31
|
|
|
| <| |_| | || (_) \__ \\ |
32
|
|
|
|_|\_\\\\__, |\__\___/|___/ |
33
|
|
|
__/ | |
34
|
|
|
|___/ |
35
|
|
|
\033[0m |
36
|
|
|
Welcome to Kytos SDN Platform! |
37
|
|
|
|
38
|
|
|
We are doing a huge effort to make sure that this console will work fine |
39
|
|
|
but for now it's still experimental. |
40
|
|
|
|
41
|
|
|
Kytos website.: https://kytos.io/ |
42
|
|
|
Documentation.: https://docs.kytos.io/ |
43
|
|
|
OF Address....:""" |
44
|
|
|
|
45
|
|
|
exit_msg = "Stopping Kytos daemon... Bye, see you!" |
46
|
|
|
|
47
|
|
|
address = controller.server.server_address[0] |
48
|
|
|
port = controller.server.server_address[1] |
49
|
|
|
banner1 += " tcp://{}:{}\n".format(address, port) |
50
|
|
|
|
51
|
|
|
api_port = controller.api_server.port |
52
|
|
|
banner1 += " WEB UI........: http://{}:{}/".format(address, api_port) |
53
|
|
|
|
54
|
|
|
cfg = Config() |
55
|
|
|
cfg.TerminalInteractiveShell.autocall = 2 |
56
|
|
|
cfg.TerminalInteractiveShell.show_rewritten_input = False |
57
|
|
|
|
58
|
|
|
ipshell = InteractiveShellEmbed(config=cfg, |
59
|
|
|
banner1=banner1, |
60
|
|
|
exit_msg=exit_msg) |
61
|
|
|
ipshell.prompts = KytosPrompt(ipshell) |
62
|
|
|
|
63
|
|
|
ipshell() |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
def main(): |
67
|
|
|
"""Start main Kytos Daemon.""" |
68
|
|
|
def stop_controller(signum, frame): # pylint: disable=unused-argument |
69
|
|
|
"""Stop the controller before quitting.""" |
70
|
|
|
if controller: |
71
|
|
|
print('Stopping controller...') |
72
|
|
|
# If stop() hangs, old ctrl+c behaviour will be restored |
73
|
|
|
signal.signal(signal.SIGINT, kill_handler) |
74
|
|
|
controller.stop() |
75
|
|
|
|
76
|
|
|
kill_handler = signal.signal(signal.SIGINT, stop_controller) |
77
|
|
|
|
78
|
|
|
config = KytosConfig() |
79
|
|
|
controller = Controller(config.options['daemon']) |
80
|
|
|
|
81
|
|
|
if controller.options.foreground: |
82
|
|
|
try: |
83
|
|
|
controller.start() |
84
|
|
|
except SystemExit as exc: |
85
|
|
|
controller.log.error(exc) |
86
|
|
|
controller.log.info("Kytos start aborted.") |
87
|
|
|
sys.exit() |
88
|
|
|
|
89
|
|
|
start_shell(controller) |
90
|
|
|
|
91
|
|
|
controller.stop() |
92
|
|
|
else: |
93
|
|
|
with daemon.DaemonContext(): |
94
|
|
|
try: |
95
|
|
|
controller.start() |
96
|
|
|
except SystemExit as exc: |
97
|
|
|
controller.log.error(exc) |
98
|
|
|
controller.log.info("Kytos daemon start aborted.") |
99
|
|
|
sys.exit() |
100
|
|
|
|