-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.py
63 lines (51 loc) · 1.55 KB
/
start.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
# -*- coding: UTF-8 -*-
import socket
import os
import platform
import sys
try:
import consul
except:
os.system('pip install python-consul')
import consul
consulAddr = "192.168.150.191"
port = "8500"
gameName = "dinosaur"
def isopen(addr, port):
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
s.connect((addr,port))
s.shutdown(2)
return True
except:
return False
def getip():
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 80))
ip = s.getsockname()[0]
finally:
s.close()
return ip
def start(id, kind, tag):
exe = sys.path[0] + "/main"
system = platform.system()
consuld = consulAddr + ":" + port
if system == "Windows":
os.system("start cmd /k %s.exe -ID %s -kind %s -consul %s %s " % (exe, id, kind, consuld, tag))
else:
os.system("nohup %s -ID %s -kind %s -consul %s %s >>nohup.out_%s 2>&1 &" % (exe, id, kind, consuld, tag, tag))
if __name__ == '__main__':
c = consul.Consul(host=consulAddr, port=port)
data = c.agent.services()
ip = getip()
for k, v in data.items():
if not v.get("Service", "").startswith(gameName):
continue
if v.get("Address", "") == ip:
if isopen(ip, v.get("Port", 0)):
print("server is open " + v.get("ID", ""))
else:
flag = v.get("ID", "")
start(v["Meta"]["id"], v["Meta"]["kind"], flag)
os.system("ps aux | grep %s | grep -v grep" % gameName)