Revision 13965151 snf-image-host/helper-monitor.py
b/snf-image-host/helper-monitor.py | ||
---|---|---|
24 | 24 |
import re |
25 | 25 |
|
26 | 26 |
LINESIZE = 512 |
27 |
BUFSIZE = 512 |
|
27 | 28 |
PROGNAME = os.path.basename(sys.argv[0]) |
28 | 29 |
STDERR_MAXLINES = 10 |
29 | 30 |
MAXLINES = 100 |
... | ... | |
71 | 72 |
error("File descriptor is not valid") |
72 | 73 |
|
73 | 74 |
lines_left = 0 |
74 |
stderr = "" |
|
75 |
|
|
76 | 75 |
line_count = 0 |
77 |
while 1: |
|
78 |
line = sys.stdin.readline(LINESIZE) |
|
76 |
stderr = "" |
|
77 |
line = "" |
|
78 |
while True: |
|
79 |
# Can't use sys.stdin.readline since I want unbuffered I/O |
|
80 |
new_data = os.read(sys.stdin.fileno(), BUFSIZE) |
|
81 |
|
|
82 |
if not new_data: |
|
83 |
if not line: |
|
84 |
break |
|
85 |
else: |
|
86 |
new_data = '\n' |
|
87 |
|
|
88 |
while True: |
|
89 |
split = new_data.split('\n', 1) |
|
90 |
line += split[0] |
|
91 |
if len(split) == 1: |
|
92 |
if len(line) > LINESIZE: |
|
93 |
error("Line size exceeded the maximum allowed size") |
|
94 |
break |
|
95 |
|
|
96 |
new_data = split[1] |
|
79 | 97 |
|
80 |
if not line: |
|
81 |
break |
|
82 |
else: |
|
83 | 98 |
line_count += 1 |
84 |
|
|
85 |
if line[-1] != '\n': |
|
86 |
# Line is too long... |
|
87 |
error("Too long line...") |
|
88 |
sys.exit(1) |
|
89 |
|
|
90 |
if lines_left > 0: |
|
91 |
stderr += line |
|
92 |
lines_left -= 1 |
|
93 |
if lines_left == 0: |
|
94 |
send(fd, "STDERR", stderr) |
|
95 |
stderr = "" |
|
96 |
continue |
|
97 |
|
|
98 |
if line_count >= MAXLINES + 1: |
|
99 |
error("Maximum allowed helper monitor number of lines exceeded.") |
|
100 |
|
|
101 |
line = line.strip() |
|
102 |
if len(line) == 0: |
|
103 |
continue |
|
104 |
|
|
105 |
if line.startswith("STDERR:"): |
|
106 |
m = re.match("STDERR:(\d+):(.*)", line) |
|
107 |
if not m: |
|
108 |
error("Invalid syntax for STDERR line") |
|
109 |
try: |
|
110 |
lines_left = int(m.group(1)) |
|
111 |
except ValueError: |
|
112 |
error("Second field in STDERR line must be an integer") |
|
113 |
|
|
114 |
if lines_left > STDERR_MAXLINES: |
|
115 |
error("Too many lines in the STDERR output") |
|
116 |
elif lines_left < 0: |
|
117 |
error("Second field of STDERR: %d is invalid" % lines_left) |
|
99 |
if line_count >= MAXLINES + 1: |
|
100 |
error("Exceeded maximum allowed number of lines: %d." % |
|
101 |
MAXLINES) |
|
118 | 102 |
|
119 | 103 |
if lines_left > 0: |
120 |
stderr = m.group(2) + "\n"
|
|
104 |
stderr += "%s\n" % line
|
|
121 | 105 |
lines_left -= 1 |
122 |
|
|
123 |
if lines_left == 0: |
|
124 |
send(fd, "STDERR", stderr) |
|
125 |
stderr = "" |
|
126 |
elif line.startswith("TASK_START:") or line.startswith("TASK_END:") \ |
|
127 |
or line.startswith("WARNING:") or line.startswith("ERROR:"): |
|
128 |
(msg_type, _, value) = line.partition(':') |
|
129 |
send(fd, msg_type, value) |
|
130 |
else: |
|
131 |
error("Unknown command!") |
|
106 |
if lines_left == 0: |
|
107 |
send(fd, "STDERR", stderr) |
|
108 |
stderr = "" |
|
109 |
line = "" |
|
110 |
continue |
|
111 |
|
|
112 |
line = line.strip() |
|
113 |
if len(line) == 0: |
|
114 |
continue |
|
115 |
|
|
116 |
if line.startswith("STDERR:"): |
|
117 |
m = re.match("STDERR:(\d+):(.*)", line) |
|
118 |
if not m: |
|
119 |
error("Invalid syntax for STDERR line") |
|
120 |
try: |
|
121 |
lines_left = int(m.group(1)) |
|
122 |
except ValueError: |
|
123 |
error("Second field in STDERR line must be an integer") |
|
124 |
|
|
125 |
if lines_left > STDERR_MAXLINES: |
|
126 |
error("Too many lines in the STDERR output") |
|
127 |
elif lines_left < 0: |
|
128 |
error("Second field of STDERR: %d is invalid" % lines_left) |
|
129 |
|
|
130 |
if lines_left > 0: |
|
131 |
stderr = m.group(2) + "\n" |
|
132 |
lines_left -= 1 |
|
133 |
|
|
134 |
if lines_left == 0: |
|
135 |
send(fd, "STDERR", stderr) |
|
136 |
stderr = "" |
|
137 |
elif line.startswith("TASK_START:") \ |
|
138 |
or line.startswith("TASK_END:") \ |
|
139 |
or line.startswith("WARNING:") \ |
|
140 |
or line.startswith("ERROR:"): |
|
141 |
(msg_type, _, value) = line.partition(':') |
|
142 |
send(fd, msg_type, value) |
|
143 |
else: |
|
144 |
error("Unknown command!") |
|
145 |
|
|
146 |
# Remove the processed line |
|
147 |
line = "" |
|
132 | 148 |
|
133 | 149 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : |
Also available in: Unified diff