This repository was archived by the owner on May 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
87 lines (74 loc) · 2.88 KB
/
main.py
File metadata and controls
87 lines (74 loc) · 2.88 KB
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
from requests.exceptions import ConnectionError
from utils.reload_credentials import ask_for_new_credentials
from utils.input_dialogs import confirm_input_dialog
from extensions.url import format_url, get_base_url
from utils.local_settings import LocalSettings
from utils.rancher_mediator import RancherMediator
from urllib3 import disable_warnings
from traceback import format_exc
from config import app_config
from utils.logger import Log
from sys import exit
__exit_code: int = 0
__log: Log = Log(app_config['name'], app_config['log_level'])
__log.configure()
disable_warnings()
Log.set_singleton(__log)
try:
__log.info(
f"Hello, world!\n"
f"{'~' * 63}\n"
f"{app_config['title']}\n\n"
f"{app_config['description']}\n"
f"{'~' * 63}")
success, settings = LocalSettings.try_load()
if success and LocalSettings.try_parse(settings):
r_endpoint: str = app_config['rancher']['endpoint']
if not r_endpoint:
r_endpoint = confirm_input_dialog("Insert the Rancher endpoint:")
r_base_url: str = get_base_url(r_endpoint)
r_username: str = app_config['rancher']['username']
r_password: str = app_config['rancher']['password']
if not (r_username or r_password):
__log.warning(
"Provide your credentials to access Rancher API.\n"
"If you don't have any API key yet, consider to visit:\n"
f"\t{format_url(r_base_url, app_config['static']['api_keys'])}"
)
if not r_username:
r_username = confirm_input_dialog(
"Insert your Rancher Access Key (username):")
if not r_password:
r_password = confirm_input_dialog(
"Insert your Rancher Secret Key (password):")
app_config['rancher']['username'] = r_username
app_config['rancher']['password'] = r_password
else:
r_endpoint = confirm_input_dialog("Insert the Rancher endpoint:")
r_base_url: str = get_base_url(r_endpoint)
ask_for_new_credentials()
app_config['rancher']['base_url'] = r_base_url
app_config['rancher']['endpoint'] = r_endpoint
LocalSettings.save()
RancherMediator.core()
__log.info("Press ANY key to quit.")
input()
except KeyboardInterrupt:
__log.warning(f"{app_config['name']} is preparing to shutdown...")
except ConnectionError:
__log.warning(
"There is no connection from Endpoint "
f"'{app_config['rancher']['endpoint']}', consider to check "
"if Rancher IP is listening for inbound connections.")
except Exception as error:
__log.critical(
f"{app_config['name']} stopped suddenly!",
args={
'Exception': type(error),
'Stacktrace': format_exc()
}
)
__exit_code = 1
finally:
__log.warning("Killing main thread.")
exit(__exit_code)