-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.py
More file actions
146 lines (124 loc) · 6.1 KB
/
commands.py
File metadata and controls
146 lines (124 loc) · 6.1 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
146
from taskmaster import *
from execution import *
from reload import *
import logging
class colors :
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
endc = '\033[0m'
bold = '\033[1m'
cyan = '\u001b[36;1m'
red = '\u001b[31;1m'
class commands:
def __init__(self, command, procs):
self.command = command.split(" ")
self.procs = procs
self.commands()
def print_status(self):
print("--------------------------------------------------------------------------")
print(" " + colors.OKGREEN + "PROCESSES STATUS" + colors.endc + " ")
print("--------------------------------------------------------------------------")
for command in self.procs:
print(" " * 6,"Process: ",command.name, "| Pid: ",command.pid," | Status: ",end="")
if command.status == "STOPPED" or command.status == "FAILED":
print(colors.red + command.status + colors.endc,end="")
if command.status == "STARTED":
print(colors.OKBLUE+ command.status + colors.endc,end="")
if command.status == "RUNNING":
print(colors.OKGREEN+ command.status + colors.endc,end="")
if command.status == "EXITED":
print(colors.WARNING + command.status + colors.endc,end="")
if command.status == None:
print(colors.WARNING + "Not Started" + colors.endc,end="")
if command.exitcode == "?":
print(" | Exitcode:", command.exitcode)
elif command.exitcode != None:
print(" | Exitcode:", abs(command.exitcode))
else:
print(" | Exitcode:", 1)
def start_all(self):
logging.info("Starting all processes.")
print(colors.OKGREEN + "Starting all processes." + colors.endc)
for command in self.procs:
if command.status != "RUNNING" and command.status != "STARTED":
command.autostart = True
execution(command, "execute")
def start_process(self, cmd):
for command in self.procs:
if command.name == cmd:
if command.status != "RUNNING" and command.status != "STARTED":
command.autostart = True
print(colors.OKGREEN + "Starting {0}".format(cmd) + colors.endc)
logging.info("Starting {0}".format(cmd))
execution(command, "execute")
return
print(colors.red + "{0}: no such process".format(cmd) + colors.endc)
def restart_all(self):
print(colors.OKBLUE + "Restarting all processes." + colors.endc)
logging.info("Restarting all processes")
for command in self.procs:
if command.status == "STARTED" or command.status == "RUNNING" or command.status == "STOPPED":
command.proc.terminate()
execution(command, "execute")
def restart_process(self, cmd):
for command in self.procs:
if command.name == cmd:
if command.status == "STARTED" and command.status == "RUNNING" or command.status == "STOPPED":
os.kill(command.proc.pid, command.stopsig)
logging.info("Restarting {0}".format(cmd))
print(colors.OKBLUE + "Restarting {0}".format(cmd) + colors.endc)
execution(command, "execute")
return
print(colors.red + "{0}: no such process".format(cmd) + colors.endc)
def stop_all(self):
print(colors.red + "Stopping all processes." + colors.endc)
logging.info("Stopping all processes.")
for command in self.procs:
if command.status == "STARTED" or command.status == "RUNNING":
command.status = "STOPPED"
os.kill(command.proc.pid, command.stopsig)
time.sleep(0.1)
command.exitcode = command.proc.poll()
def stop_process(self, cmd):
for command in self.procs:
if command.name == cmd:
if command.status == "STARTED" or command.status == "RUNNING":
print(colors.red + "Stopping {0}".format(cmd) + colors.endc)
logging.info("Stopping {0}".format)
command.status = "STOPPED"
os.kill(command.proc.pid, command.stopsig)
time.sleep(0.1)
command.exitcode = command.proc.poll()
return
print(colors.red + "{0}: no such process".format(cmd) + colors.endc)
def commands(self):
if len(self.command) == 1:
if self.command[0] == "reload":
logging.info("Configuration file reloaded.")
reload(self.procs)
print(colors.WARNING + "Configuration file reloaded."+ colors.endc)
elif self.command[0] == "status":
self.print_status()
else:
print(colors.red + "{0}: command not found".format(self.command[0]) + colors.endc)
if len(self.command) > 1:
if self.command[0] == "start" and self.command[1] == "all":
self.start_all()
elif self.command[0] == "start":
for i in range(1, len(self.command)):
self.start_process(self.command[i])
elif self.command[0] == "restart" and self.command[1] == "all":
self.restart_all()
elif self.command[0] == "restart":
for i in range(1, len(self.command)):
self.restart_process(self.command[i])
elif self.command[0] == "stop" and self.command[1] == "all":
self.stop_all()
elif self.command[0] == "stop":
for i in range(1, len(self.command)):
self.stop_process(self.command[i])
else:
print(colors.red + "{0}: command not found".format(self.command[0]) + colors.endc)