-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.py
More file actions
executable file
·145 lines (122 loc) · 4.18 KB
/
controller.py
File metadata and controls
executable file
·145 lines (122 loc) · 4.18 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
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
#!/usr/bin/python3
# **************************************************************************** #
# #
# ::: :::::::: #
# controller.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: yoyassin <yoyassin@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2019/10/13 10:15:23 by yoyassin #+# #+# #
# Updated: 2019/10/13 10:15:23 by yoyassin ### ########.fr #
# #
# **************************************************************************** #
import socket, sys, readline, os, loader, re, pickle, rlcompleter
import signal, parse_cfgfile
from app import App
import threading
#initializing connection
names = []
def read(sock):
strdata = "";
while not strdata.endswith("end\n"):
data = sock.recv(100)
strdata = strdata + str(data, "UTF-8")
strdata = strdata[:-4];
print(strdata, end='')
def init_conn(_exit_):
proc_list,socket_addr = parse_cfgfile.validate(_exit_)
if proc_list == False :
if _exit_:
sys.exit(1)
print("Something is wrong, couldn't reload config file.")
if proc_list :
for proc in proc_list :
names.append(proc.name)
for key in builtins:
names.append(key)
try:
App.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
App.socket.connect(socket_addr)
except socket.error:
print("error connecting to socket: {}".format(socket_addr), file=sys.stderr)
sys.exit(1)
def start(action):
App.socket.sendall(action)
read(App.socket)
def status(action):
App.socket.sendall(action)
read(App.socket)
def restart(action):
App.socket.sendall(action)
read(App.socket)
def stop(action):
App.socket.sendall(action)
read(App.socket)
def reload(action):
App.socket.sendall(action)
read(App.socket)
App.socket.close()
names = []
init_conn(False)
def ft_exit(action):
print("exit")
sys.exit(0)
def shutdown(action):
print("GETTING THE FUCK OUT...")
App.socket.sendall(action)
read(App.socket)
ft_exit(None)
builtins = {'start': start,
'status': status,
'restart': restart,
'stop': stop,
'reload': reload,
'exit': ft_exit,
'shutdown': shutdown}
#controller loop
def autocomplete(text, state):
actions = names
options = [action for action in actions if action.startswith(text)]
if state < len(actions):
return options[state]
else:
return None
#autocomplete setup
readline.parse_and_bind("bind ^I rl_complete")
readline.set_completer(autocomplete)
def print_help(action):
print("Usage:\n\tstatus\n\tstart [...]\n\tstop [...]\n\treload\n\texit\n")
def validate_line(line):
actions = line.split()
patt = re.compile('^[a-zA-Z]+$|^[a-zA-Z]+:?\s?[a-zA-Z0-9]+')
for action in actions:
if bool(re.match(patt, action)) :
continue
else :
return ""
return actions
def sighandler(sig, frame):
ft_exit(None)
signal.signal(signal.SIGINT, sighandler)
signal.signal(signal.SIGQUIT, signal.SIG_IGN)
init_conn(True)
while True:
line = input('Taskmasterctl $> ')
if not line:
continue
line = line.strip()
action = list(validate_line(line))
if not action:
print("Invalid syntax.")
continue
if action[0] != "shutdown" and action[0] != "exit" and action[0] != "status" and action[0] != "reload" and len(action) < 2:
print("Missing arguments")
print_help(action)
continue
if builtins.get(action[0]):
try:
builtins[action[0]](pickle.dumps(action, pickle.HIGHEST_PROTOCOL))
except IOError:
print("can't connect to daemon")
continue
print("No such command: {}".format(action[0]), file=sys.stderr)