-
Notifications
You must be signed in to change notification settings - Fork 24
/
server.py
144 lines (128 loc) · 5.29 KB
/
server.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
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
from contextlib import closing
import requests
import json
import sys
import os
import subprocess
from subprocess import PIPE
import time
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
dbfile = "tr_sys/db.sqlite3"
python = sys.executable #python or python3
server = "http://localhost:8000"
def getUnsecret():
# syscall = ["curl", "-d", "@tr_sys/tr_ara_unsecret/unsecretAgent.json", server+"/api/agents"]
# fp = subprocess.run(syscall, stdout=PIPE)
# agent = json.loads(fp.stdout)
# assert agent["model"] == "tr_ars.agent"
# syscall = ["curl", "-d", "@tr_sys/tr_ara_unsecret/unsecretActor.json", server+"/api/actors"]
# fp = subprocess.run(syscall, stdout=PIPE)
# actor = json.loads(fp.stdout)
# assert actor["model"] == "tr_ars.actor"
response = requests.get(server+"/ars/api/actors")
actors = response.json()
actorpk = 0
for actor in actors:
if 'fields' in actor:
if actor['fields']['name'] == "ara-unsecret-runquery":
actorpk = actor['pk']
if actorpk == 0:
sys.stderr.write("Unsecret actor not found!\n")
assert actorpk > 0
return actorpk
def execUnsecret(unsecret):
retry_strategy = Retry(
total=5,
status_forcelist=[429, 500, 502, 503, 504],
backoff_factor=2,
method_whitelist=["HEAD", "GET", "OPTIONS"]
)
adapter = HTTPAdapter(max_retries=retry_strategy)
http = requests.Session()
http.mount("https://", adapter)
http.mount("http://", adapter)
querySubmit=requests.post(server+"/ars/api/submit",data=open('tr_sys/tr_ara_unsecret/unsecretStatusQueryTRAPIv1.json', 'rb'),timeout=10)
message =querySubmit.json()
assert message["model"] == "tr_ars.message"
for i in range(5):
time.sleep(i*i*5)
response = http.get(server+"/ars/api/messages/"+message["pk"]+"?trace=y",timeout=5)
chain = response.json()
#print(chain)
if 'children' not in chain:
continue
for child in chain["children"]:
if child["actor"]["pk"] == unsecret:
response = requests.get(server+"/ars/api/messages/"+child["message"])
print(str(response.json())[:500])
answer = response.json()
if answer["fields"]["status"] != "Running":
print("retrieved message: "+response.url)
assert len(answer["fields"]["data"]["message"]["results"]) > 1
return
sys.stderr.write("Could not find Unsecret message response!\n")
assert unsecret < 0
return message
def validateQuery():
url ='http://transltr.io:7071/validate_querygraph'
testJson = json.loads(open('./data/travis.json').read())
with closing(requests.post(url, json=testJson, stream=False)) as response:
status_code = response.status_code
assert status_code == 200
def runTests():
unsecret = getUnsecret()
execUnsecret(unsecret)
if __name__ == "__main__":
if len(sys.argv) > 1 and '--help' not in sys.argv and '-h' not in sys.argv:
if "--dbfile" in sys.argv:
dbfile = sys.argv[sys.argv.index("--dbfile")+1]
if "--python" in sys.argv:
python = sys.argv[sys.argv.index("--python")+1]
if "--server" in sys.argv:
server = sys.argv[sys.argv.index("--server")+1]
try:
subprocess.run([python, "--version"])
except AttributeError as err:
sys.stderr.write("Requires Python 3 ... maybe you are running Python 2?\n")
print("OS error: {0}".format(err))
sys.exit(1)
# setup db
if sys.argv[1] == 'prep' or sys.argv[1] == 'debug':
if os.path.exists(dbfile):
os.remove(dbfile)
subprocess.run([python, "tr_sys/manage.py", "makemigrations", "tr_ars"])
subprocess.run([python, "tr_sys/manage.py", "migrate"])
if sys.argv[1] == 'prep':
sys.exit()
# bring up server
serverfp = None
serverargs = [python, "tr_sys/manage.py", "runserver", "--noreload"]
# JH: see also "TR_ENV" and "maturity" in tr_sys/tr_smartapi_client/smart_api_discover.py
if sys.argv[1] == 'debug':
serverfp = subprocess.Popen(serverargs, stdout=PIPE, stderr=PIPE)
time.sleep(5)
if sys.argv[1] == 'prod':
serverfp = subprocess.run(serverargs, stdout=PIPE, stderr=PIPE)
# run tests
if sys.argv[1] == 'debug' or sys.argv[1] == 'test':
runTests()
if serverfp != None:
serverfp.terminate()
time.sleep(5)
sys.exit()
sys.stderr.write('''Usage: python server.py [mode] [opts]
... where [mode] is one of:
prep --- delete existing db file and prep a new db
test --- for running tests on a running localhost:8000 server with existing db
debug --- deletes existing db and starts a new server on localhost:8000 for testing
prod --- starts a new server only
options:
--server [uri]
server uri to test, e.g. http://localhost:8000
--dbfile [filename]
if needed to overwrite backend during testing (debug)
--python [path to exec]
python3 executable
\n''')
sys.exit(1)