-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
222 lines (184 loc) · 5.8 KB
/
run.py
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
import re
import sys
from datetime import datetime
from getpass import getpass
from time import sleep
from log import Log
from oc import oc_client
from ping import ping
from settings import settings
settings.load()
oc_client.kill_existing_oc()
ping_address = settings.g.get('ping_address', '8.8.8.8')
current_pid = None
get_from_env = True
settings.is_background = '-b' in sys.argv
if '-s' in sys.argv:
si = sys.argv.index('-s')
if si:
settings.file_path = sys.argv[si + 1]
if '-k' in sys.argv:
ki = sys.argv.index('-k')
if ki:
oc_client.key = sys.argv[ki + 1]
profile_index = 0
if '-i' in sys.argv:
ind = sys.argv.index('-i')
if ind:
try:
profile_index = int(sys.argv[ind + 1])
except:
print('index must be integer')
exit(1)
yes = '-y' in sys.argv
settings.login_pass = None
if '-p' in sys.argv:
ki = sys.argv.index('-p')
if ki:
settings.login_pass = sys.argv[ki + 1]
def setup():
settings.setup(load_from_env=False)
settings.save()
if 'setup' in sys.argv:
setup()
print('Setup successfully completed. you can run <python3 run.py>')
sys.exit(0)
correct = False
do_load_from_env = True
force_list_profile = False
selected_number = None
def is_add(_i):
global is_adding, do_load_from_env
if _i == 'a':
settings.current_profile = dict()
is_adding = True
do_load_from_env = False
return True
return False
def handle_delete(_i):
global do_load_from_env, force_list_profile
d = re.findall('d (\d+)', _i)
if d:
ind = int(d[0])
ind -= 1
if 0 <= ind < len(settings.profiles):
del settings.profiles[ind]
force_list_profile = True
do_load_from_env = True
settings.current_profile = None
return True
return False
def proc():
global is_adding, do_load_from_env, force_list_profile, selected_number
q = 'Which one?'
if selected_number:
q += f' ({selected_number}) '
if profile_index > 0:
_i = profile_index
else:
_i = input(q)
if not is_adding:
if not _i:
_i = selected_number
try:
if _i:
selected_number = int(_i)
return True
except:
pass
if _i:
_i = _i.lower()
if is_add(_i):
return None
if handle_delete(_i):
return None
else:
print('You must pass item number to delete profile (e.g. d 1).')
return proc()
while not settings.is_background and not correct:
settings.get_environments(load_from_env=do_load_from_env)
settings.save()
is_adding = False
print(f'ping timeout: {settings.g["ping_timeout"]}')
if len(settings.profiles) > 0:
print()
print('[option] [arg]')
print('Options:')
print('a\t: add new profile')
print('d\t: delete existing profile(d {index})')
print()
print('profiles:')
print('- Enter profile number/option which is described:')
print()
if force_list_profile or len(settings.profiles) > 1:
selected_number = None
selected_profile = settings.g.get('selected_profile', -1)
for i, conf in enumerate(settings.profiles):
if selected_profile == i + 1:
selected_number = i + 1
selected_char = '*'
else:
selected_char = ' '
print(f'{selected_char} {i + 1}- host: {conf["server"]}, '
f'username: {conf["username"]}, password: ***')
print()
if not proc():
continue
else:
selected_number = 1
settings.select_config(selected_number - 1)
if not is_adding:
print(f'Selected {selected_number}- host: {settings.current_profile["server"]}, '
f'username: {settings.current_profile["username"]}, password: ***')
if yes:
correct = True
else:
i = input('Correct? (Y/n) ')
if is_add(i):
continue
if handle_delete(i):
continue
correct = not i or i.lower() == 'y'
if not correct:
settings.current_profile = dict()
do_load_from_env = False
settings.save()
Log.initialize(settings.is_background)
if not settings.is_background and not settings.login_pass:
settings.login_pass = getpass('System password: ')
oc_client.kill_existing_oc()
oc_client.get_server_cert()
oc_client.reconnect_oc()
# try more times on first connection to let establish
# the connection, then check connectivity
more_try_times = 10
try_times = 3
down_count = -more_try_times
down_ping_count = -more_try_times
ping_timeout = int(settings.current_profile.get('ping_timeout', settings.DEFAULT_PING_TIMEOUT))
while True:
is_up = True
ttl, time = ping(ping_address, ping_timeout)
Log.debug(f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} ttl: {ttl}, time: {time}')
if time < 0 or time > ping_timeout:
down_count += 1
Log.debug('tried {down_count} times'.format(down_count=down_count))
if down_count >= try_times:
down_count = 0
is_up = False
else:
down_count = down_ping_count = 0
if is_up:
is_up = oc_client.is_process_running
if not oc_client.is_connected:
down_count = 999
if is_up:
Log.debug(f'{ping_address} is up!')
else:
down_count = down_ping_count = -more_try_times
oc_client.check_process_enabled = False
if settings.is_background:
sys.exit()
oc_client.reconnect_oc()
Log.debug(f'{ping_address} is down!')
sleep(1)