-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin_console
executable file
·285 lines (239 loc) · 8.06 KB
/
admin_console
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Flask and mongodb sample web project.
Usage:
admin_console -h | --help | --version
admin_console web [--dev] [--debug] (--start | --stop | --status)
admin_console database (--start | --stop | --status)
admin_console console
admin_console build
admin_console install
admin_console clean
Commands:
web Flask web server.
database MongoDB Server.
console Enter the command line interface.
build Compile project and zip.
install install python libs.
clean remove dist directory.
Options:
-h --help help document.
--version verbose mode.
--debug flask development(templates auto reload) mode.
--dev development mode.
"""
# default libs
import os
import sys
import cmd
import platform
import functools
# 3rd party libs
import yaml
import docopt
from colorama import Fore, Style
# custom develop libs
from server import server_app, database
from server.common import utils
from server.exceptions import QuitException, ArgsException
from config import Config
def parse_command(argv=None):
return docopt.docopt(__doc__, argv, version='0.0.1')
def run_sub_command(args):
options = []
cmd = ''
for k, v in args.items():
if k[:2] != '--' and v == True:
cmd = k
continue
if k[:2] == '--' and v == True:
options.append(k)
if cmd:
os.system(f'make {cmd} {" ".join(options)}')
def colorize(s, color):
color = eval(f'Fore.{color.upper()}')
return f'{color}{s}{Style.RESET_ALL}'
def terminal_clear():
os.system("cls" if platform.system().lower() == "windows" else "clear")
def load_quit_input(func):
@functools.wraps(func)
def wrappers(*args, **kwargs):
try:
func(*args, **kwargs)
except Exception as e:
print(e)
finally:
input('\n\nPlease enter to continue...')
terminal_clear()
g_cmd_shell.show_menu()
return wrappers
class CommandShell(cmd.Cmd):
"""Common command shell interface."""
description = {"main":
{
1: """================================ Management Program ==============================================
[P] Process task.
[Q] Quit."""
},
"process task":
{
1: """================================== Process Task ==================================================
[A] All service check.
[W] Web service start.
[E] Web service shutdown.
[R] Web service status.
[D] Database service start.
[F] Database service shutdown.
[G] Database service status.
[Q] quit.
"""
}
}
def __init__(self, name, config=None):
"""
Create common shell class
"""
cmd.Cmd.__init__(self)
self.config = config
self.options = config.options
self.context = 'main'
self.prev_context = ''
self.step = 1
self.intro = self.description[self.context][self.step]
self.help_text = "press <Tab> to expand command or type ? to get any helps."
self.prompt = f'{colorize(name, "lightgreen_ex")} > '
self.choice = False
global g_cmd_shell
g_cmd_shell = self
def emptyline(self):
"""빈 입력값인 경우 마지막 명령어를 실행하는게 기본이나 마지막 명령어 반복을 막기 위해 해당 메서드 재정의"""
terminal_clear()
self.show_menu()
print("Please select a Menu")
pass
def default(self, line):
"""입력값의 맞는 실행 함수가 없는 경우 실행"""
if not self.choice:
terminal_clear()
print(f"Please select a Menu.\nDoes not exists. [{line}]")
pass
def get_cmd(self, cmd):
"""기본 규칙인 함수 앞에 do_ 를 붙인 함수명을 반환하지 않도록 커스텀 하기 위한 함수."""
find_str = f'[{cmd.upper()}]'
word = [i for i in self.description[self.context][self.step].split('\n') if i.find(find_str) != -1]
if word == []:
return cmd
cmd = f"cmd{word[0].replace(find_str, '').lower().replace('.', '').replace(' ', '_')}"
func_list = self.get_names()
for func_name in func_list:
if func_name.startswith(cmd):
cmd = func_name
break
return cmd
def precmd(self, line):
terminal_clear()
return line
def onecmd(self, line):
"""기본 명령 실행 함수."""
cmd, arg, line = self.parseline(line)
if not line:
return self.emptyline()
if cmd is None:
return self.default(line)
self.lastcmd = line
if line == 'EOF':
self.lastcmd = ''
if cmd == '':
return self.default(line)
else:
try:
# print(f'{self.get_cmd(cmd)}')
func = getattr(self, self.get_cmd(cmd))
except AttributeError:
return self.default(line)
return func(arg)
def show_menu(self):
print(self.get_menu())
def get_menu(self):
return self.description[self.context][self.step]
def cmd_process_task(self, arg):
"""[P] Process controll task."""
self.prev_context = self.context
self.context = 'process task'
self.step = 1
self.show_menu()
@load_quit_input
def cmd_database_service_start(self, arg):
"""[D] Database service start."""
database.database_start(self.config)
@load_quit_input
def cmd_database_service_shutdown(self, arg):
"""[F] Database service start."""
database.database_shutdown()
@load_quit_input
def cmd_database_service_status(self, arg):
"""[G] Database service start."""
database.database_status()
@load_quit_input
def cmd_web_service_start(self, arg):
"""[W] Web service start."""
server_app.web_start(self.config)
@load_quit_input
def cmd_web_service_shutdown(self, arg):
"""[E] Web service shutdown."""
server_app.web_shutdown()
@load_quit_input
def cmd_web_service_status(self, arg):
"""[R] Web service status."""
server_app.web_status()
@load_quit_input
def cmd_all_service_check(self, arg):
"""[A] All service check."""
print(f'Web service is {"" if server_app.check_service() else "not"} running.')
print(f'Database service is {"" if database.check_service() else "not"} running.')
def cmd_quit(self, arg):
if self.context == 'main' and self.step == 1:
return self._quit()
else:
if self.step > 1:
self.step -= 1
else:
self.context = self.prev_context
terminal_clear()
self.show_menu()
def _quit(self):
raise QuitException("bye.")
def cmdloop(self, intro=None):
while True:
try:
terminal_clear()
cmd.Cmd.cmdloop(self, intro)
except QuitException as qe:
print(f'{qe}')
break
except KeyboardInterrupt:
print('Program Exit...')
break
except ArgsException as e:
print(f"Error parsing arguments!\n {e}")
continue
except Exception as e:
print(f'Unknown Exception : {e}')
break
if __name__ == '__main__':
try:
config = Config(parse_command(sys.argv[1:]))
if config.options['console']:
CommandShell('Admin Console', config).cmdloop()
elif config.options['web']:
server_app.main(config)
elif config.options['database']:
database.main(config)
else:
run_sub_command(config)
except KeyboardInterrupt:
print('Program Exit...')
sys.exit(1)
except Exception as err:
print(f'{err}')
# utils.print_stack_trace()