-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrun.py
More file actions
executable file
·165 lines (139 loc) · 4.49 KB
/
run.py
File metadata and controls
executable file
·165 lines (139 loc) · 4.49 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/python
# Standard Library
import os
import sys
import time
import subprocess
from threading import Thread
from Queue import Queue
# Third Party
import yaml
# Local
waitTime = 10
services = [
{
"name": "frontend",
"command": ["./main.py", "-e", "-d"],
"color": "1"
},
{
"name": "markcuban",
"command": ["./main.py", "-e", "-d"],
"color": "2"
},
{
"name": "jaunt",
"command": ["./main.py", "-e", "-d"],
"color": "3"
},
{
"name": "redshirt",
"command": ["./main.py", "-e", "-d"],
"color": "6"
},
{
"name": "flint",
"command": ["./main.py", "-e", "-d"],
"color": "5"
},
{
"name": "lego",
"command": ["./main.py", "-e", "-d"],
"color": "20"
},
{
"name": "wf-run",
"path": ["lego"],
"command": ["celery","-A","lib.runner","worker"],
"color": 22
}
]
def getTerminalSize():
env = os.environ
def ioctl_GWINSZ(fd):
try:
import fcntl, termios, struct, os
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,
'1234'))
except:
return
return cr
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
cr = (env.get('LINES', 25), env.get('COLUMNS', 80))
### Use get(key[, default]) instead of a try/catch
#try:
# cr = (env['LINES'], env['COLUMNS'])
#except:
# cr = (25, 80)
return int(cr[1]), int(cr[0])
if __name__ == "__main__":
conf = yaml.load(open("conf.yml"))
os.environ["ZKHOSTS"] = "|".join(conf["zkhosts"])
import mixingboard
for key, value in conf.items():
mixingboard.setConf(key, value)
currentDirectory = os.path.dirname(os.path.realpath(__file__))
logDirectory = os.path.join(currentDirectory, "logs")
os.environ["PYTHONPATH"] += ":%s:" % currentDirectory
try:
os.mkdir(logDirectory)
except OSError:
pass
def runService(service):
logFileName = os.path.join(logDirectory, "%s.log" % service['name'])
with open(logFileName,"a") as logFile:
baseDirectory = currentDirectory
serviceDirectory = ""
if "path" in service:
serviceDirectory = os.path.join(*([baseDirectory] + service['path']))
else:
serviceDirectory = os.path.join(baseDirectory, service['name'])
while True:
proc = subprocess.Popen(service['command'], cwd=serviceDirectory, bufsize=0,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
print "\033[48;5;%smStarted %s...\033[0m" % (service['color'], service['name'])
while True:
nextline = proc.stdout.readline()
if nextline == '' and proc.poll() != None:
break
(width, _) = getTerminalSize()
line = "\033[48;5;%sm%s:\033[0m " % (service['color'], service['name'])
extra = 13
while True:
nextLinePos = width - (len(line) - extra)
line += nextline[:nextLinePos]
sys.stdout.write(line)
nextline = nextline[nextLinePos:]
if len(nextline) > 0:
extra = 14
line = "\n\033[48;5;%sm%s \033[0m " % (service['color'], ' '*len(service['name']))
else:
sys.stdout.write('\n')
break
sys.stdout.flush()
logFile.write(nextline)
logFile.flush()
print "\033[48;5;%smProcess %s exited. Waiting %s seconds and restarting\033[0m" % (
service['color'],
service['name'],
waitTime
)
time.sleep(waitTime)
for service in services:
t = Thread(target=runService, args=(service,))
t.daemon = True
t.start()
time.sleep(0.5)
while True:
try:
time.sleep(10)
except:
sys.exit(0)