-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_lib.py
More file actions
125 lines (116 loc) · 4.55 KB
/
cmd_lib.py
File metadata and controls
125 lines (116 loc) · 4.55 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
# **************************************************************************** #
# #
# ::: :::::::: #
# cmd_lib.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: skhatir <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2016/06/30 10:05:09 by skhatir #+# #+# #
# Updated: 2016/06/30 10:05:16 by skhatir ### ########.fr #
# #
# **************************************************************************** #
import os
import task_lib
def post_init(cmd):
# OVER
if (cmd.umask > 7777):
cmd.state = task_lib.color_string("RED", "ERROR -> umask")
task_lib.log.warning(cmd.id + ': ' + cmd.state)
if (cmd.numprocs > 5000):
cmd.state = task_lib.color_string("RED", "ERROR -> too many processus")
task_lib.log.warning(cmd.id + ': ' + cmd.state)
if (cmd.workingdir[0] != "/" or os.access(cmd.workingdir, os.W_OK) == False):
cmd.state = task_lib.color_string("RED", "ERROR -> path")
task_lib.log.warning(cmd.id + ': ' + cmd.state)
if (cmd.stop_signal < 0 or cmd.stop_signal > 30):
cmd.state = task_lib.color_string("RED", "ERROR -> signaux")
task_lib.log.warning(cmd.id + ': ' + cmd.state)
# OPEN
if (cmd.state == "OK"):
oldmask = os.umask(cmd.umask)
try:
cmd.fdout = os.open(cmd.stdout, os.O_WRONLY | os.O_CREAT, 644)
except OSError, e:
cmd.state = "ERROR opening -> " + e.filename
cmd.stdout = task_lib.color_string("RED", str(cmd.stdout))
task_lib.log.warning(cmd.id + ': ' + cmd.state)
cmd.fdout = None
try:
cmd.fderr = os.open(cmd.stderr, os.O_WRONLY | os.O_CREAT, 644)
except OSError, e:
cmd.state = "ERROR opening -> " + e.filename
cmd.stderr = task_lib.color_string("RED", str(cmd.stderr))
task_lib.log.warning(cmd.id + ': ' + cmd.state)
cmd.fderr = None
os.umask(oldmask)
#CHECK_PATH
curr_cmd = cmd.path.split()[0]
if (curr_cmd[0] == '.' or curr_cmd[0] == '/'):
if os.access(curr_cmd, os.X_OK) == False:
cmd.state = task_lib.color_string("RED", "ERROR -> command")
task_lib.log.warning(cmd.id + ': ' + cmd.state)
else:
find = False
path_env = os.environ["PATH"]
path_env = path_env.split(":")
for path in path_env:
curr_path = path + "/" + curr_cmd
if os.access(curr_path, os.X_OK) == True:
find = True
break
if (find == False):
cmd.state = task_lib.color_string("RED", "ERROR -> command")
task_lib.log.warning(cmd.id + ': ' + cmd.state)
def special_params(cmd, params, name):
if (name == "autorestart"):
if ((type(params[name]) is not bool) and params[name] != "unexpected"):
cmd.state = task_lib.color_string("RED", "ERROR -> " + name)
task_lib.log.warning(cmd.id + ': ' + cmd.state)
return (False)
return (params[name])
elif (name == "exitcodes"):
if (type(params[name]) is int and params[name] == 0):
return (False)
elif (type(params[name]) is not list):
cmd.state = task_lib.color_string("RED", "ERROR -> " + name)
task_lib.log.warning(cmd.id + ': ' + cmd.state)
return (False)
for exit in params[name]:
if ((type(exit) is int and exit < 0) or type(exit) is not int):
cmd.state = task_lib.color_string("RED", "ERROR -> " + name)
task_lib.log.warning(cmd.id + ': ' + cmd.state)
return (False)
return (True)
def check_validity(cmd, params, name):
type_define = {
"cmd": str,
"workingdir": str,
"stopsignal": str,
"stdout": str,
"stderr": str,
"autostart": bool,
"starttime": int,
"startretries": int,
"stoptime": int,
"numprocs": int,
"umask": int,
"env": dict,
}
if (name == "autorestart" or name == "exitcodes"):
return (special_params(cmd, params, name))
elif (type(params[name]) is type_define[name]):
if (type_define[name] is int and params[name] < 0):
cmd.state = task_lib.color_string("RED", "ERROR -> " + name)
task_lib.log.warning(cmd.id + ': ' + cmd.state)
return (False)
elif (type(params[name]) is not type_define[name]):
cmd.state = task_lib.color_string("RED", "ERROR -> " + name)
task_lib.log.warning(cmd.id + ': ' + cmd.state)
return (False)
return (True)
def in_config(cmd, params, name):
if (params):
for k, v in params.iteritems():
if (k == name and check_validity(cmd, params, name) == True):
return (params[name])
return None