This repository has been archived by the owner on Sep 14, 2018. It is now read-only.
forked from matthiaskramm/mrscake
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautoupdate.py
76 lines (64 loc) · 1.94 KB
/
autoupdate.py
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
#!/usr/bin/python
import sys
import os
import time
import hashlib
import traceback
def run(cmd):
fi = os.popen(cmd, "rb")
output = fi.read()
status = fi.close()
if status is not None:
raise IOError("command '%s' returned non zero value %d" % (cmd, status))
return output
def exception_as_string():
s = "Exception "+str(sys.exc_info()[0])
info = sys.exc_info()[1]
if info:
s += " "+str(info)
s += "\n"
for l in traceback.extract_tb(sys.exc_info()[2]):
s += " File \"%s\", line %d, in %s\n" % (l[0],l[1],l[2])
s += " %s\n" % l[3]
return s.strip()
class Server:
def __init__(self):
pass
def start(self):
try:
self.pid = os.fork()
if not self.pid:
try:
os.execv("./mrscake-job-server", ["./mrscake-job-server"])
finally:
os.abort()
except:
print "Error spawning server"
self.pid = None
def stop(self):
if self.pid:
os.kill(self.pid, 9)
os.waitpid(self.pid, 0)
self.pid = None
# distribute the load to the git server, by delaying
# instance startup by a random number of seconds
time.sleep(ord(hashlib.md5(run("hostname")).digest()[0]))
server = Server()
server.start()
while 1:
current_revision = run("git rev-parse HEAD").strip()
run("git fetch origin master 2>&1")
server_revision = run("git rev-parse FETCH_HEAD").strip()
if current_revision != server_revision:
print "upgrading from revision %s to revision %s" % (current_revision, server_revision)
try:
print run("git merge FETCH_HEAD")
except:
print "Error merging revision %s" % server_revision
try:
server.stop()
run("make mrscake-job-server")
server.start()
except:
print exception_as_string()
time.sleep(60)