Updated packager and worker for api changes

Signed-off-by: fly <merspieler@airmail.cc>
This commit is contained in:
fly 2021-04-18 00:00:22 +00:00
parent 387f4fc521
commit 3f74daf555
3 changed files with 79 additions and 112 deletions

View file

@ -58,7 +58,7 @@ def get_tile(lat, lon):
x = math.floor((lon - base_x) / tile_width) x = math.floor((lon - base_x) / tile_width)
return (int(lon + 180) << 14) + (int(lat + 90) << 6) + (y << 3) + x return (int(lon + 180) << 14) + (int(lat + 90) << 6) + (y << 3) + x
# Returns the area name ie. e145s17 # Returns the area name for a given lat lon ie. e145s17
def get_area_name(s, w, major=False): def get_area_name(s, w, major=False):
if s >= 0: if s >= 0:
ns = "n" ns = "n"
@ -76,6 +76,10 @@ def get_area_name(s, w, major=False):
s = s - (s % 10) s = s - (s % 10)
return ew + norm(abs(math.floor(w)), 3) + ns + norm(abs(math.floor(s)), 2) return ew + norm(abs(math.floor(w)), 3) + ns + norm(abs(math.floor(s)), 2)
# Returns the area name for a given tile
def get_area_name_by_tile(tile, major=False):
return get_area_name(get_south(tile), get_west(tile), major)
# Returns latitude of tiles SW corner in area scheme # Returns latitude of tiles SW corner in area scheme
def get_lat(tile): def get_lat(tile):
return ((16320 & int(tile)) >> 6) - 90 return ((16320 & int(tile)) >> 6) - 90
@ -164,51 +168,30 @@ def get_job(action, host, port, none_exit=True):
return ret return ret
# Gets new job from manager api # Gets new job from manager api
def api_get_job(action, api, token, none_exit=True): def api_get_job(action, new_status, api, token, none_exit=True):
try: try:
# TODO 'if' is temporary until the database is reworked response = requests.post(api, data={'auth': token, 'action': 'get-job', 'status': action, 'new-status': new_status}, headers={"lAccept": "application/json", "Content-Type": "application/x-www-form-urlencoded"})
if action == "done": if response.ok and response.json()["success"] == True:
response = requests.post(api, data={'auth': token, 'action': 'get-done'}, headers={"lAccept": "application/json", "Content-Type": "application/x-www-form-urlencoded"}) match = re.match(r"[ew]\d{3}[ns]\d{2}|[0-9]{1,7}|None", str(response.json()["job"]))
if response.ok and response.json()["success"] == True: if match != None:
match = re.match(r"[ew]\d{3}[ns]\d{2}|None", str(response.json()["job"])) ret = match.group(0)
if match != None: if ret == "None" and none_exit:
ret = match.group(0) print("No job got asigned. Exiting...")
if ret == "None" and none_exit: sys.exit(0)
print("No job got asigned. Exiting...")
sys.exit(0)
else:
print("Recived invalid job. Retrying in 10 seconds...")
sleep(10)
ret = api_get_job(action, api, token, none_exit)
return ret
else: else:
print("Unable to get job. Retrying in 60 seconds...") print("Recived invalid job. Retrying in 10 seconds...")
sleep(60) sleep(10)
ret = api_get_job(action, api, token, none_exit) ret = api_get_job(action, new_status, api, token, none_exit)
return ret return ret
else: else:
response = requests.post(api, data={'auth': token, 'action': 'get-job', 'type': action}, headers={"lAccept": "application/json", "Content-Type": "application/x-www-form-urlencoded"}) print("Unable to get job. Retrying in 60 seconds...")
if response.ok and response.json()["success"] == True: sleep(60)
match = re.match(r"[ew]\d{3}[ns]\d{2}|[0-9]{1,7}|None", str(response.json()["job"])) ret = api_get_job(action, new_status, api, token, none_exit)
if match != None: return ret
ret = match.group(0)
if ret == "None" and none_exit:
print("No job got asigned. Exiting...")
sys.exit(0)
else:
print("Recived invalid job. Retrying in 10 seconds...")
sleep(10)
ret = api_get_job(action, api, token, none_exit)
return ret
else:
print("Unable to get job. Retrying in 60 seconds...")
sleep(60)
ret = api_get_job(action, api, token, none_exit)
return ret
except (ConnectionError, OSError, IOError): except (ConnectionError, OSError, IOError):
print("Unable to get job. Retrying in 60 seconds...") print("Unable to get job. Retrying in 60 seconds...")
sleep(60) sleep(60)
ret = api_get_job(action, api, token, none_exit) ret = api_get_job(action, new_status, api, token, none_exit)
return ret return ret
# Gets status of a tile # Gets status of a tile

View file

@ -23,27 +23,26 @@ import socket
from subprocess import run, Popen, STDOUT from subprocess import run, Popen, STDOUT
from time import sleep from time import sleep
from common import norm, send_status, get_job, api_get_job, api_send_status from common import norm, get_area_name_by_tile, api_get_job, api_send_status
host = socket.gethostname() host = socket.gethostname()
port = 12345 port = 12345
api = None api = None
api_token = None api_token = None
input_dir = ""
output = "" output = ""
tmp_dir = "/tmp/o2c-packager"
argc = len(sys.argv) argc = len(sys.argv)
i = 1 i = 1
first = 1 first = 1
while i < argc: while i < argc:
if sys.argv[i] == "--port": if sys.argv[i] == "-o" or sys.argv[i] == "--output":
i += 1
port = sys.argv[i]
elif sys.argv[i] == "--host":
i += 1
host = sys.argv[i]
elif sys.argv[i] == "-o" or sys.argv[i] == "--output":
i += 1 i += 1
output = sys.argv[i] output = sys.argv[i]
if sys.argv[i] == "-i" or sys.argv[i] == "--input":
i += 1
input_dir = sys.argv[i]
elif sys.argv[i] == "-a" or sys.argv[i] == "--api": elif sys.argv[i] == "-a" or sys.argv[i] == "--api":
i += 1 i += 1
api = sys.argv[i] api = sys.argv[i]
@ -54,9 +53,8 @@ while i < argc:
print("usage: worldbuild-packager.py [OPTIONS]") print("usage: worldbuild-packager.py [OPTIONS]")
print("Retrives done tiles from the manager and packages them") print("Retrives done tiles from the manager and packages them")
print("") print("")
print(" -i --input Input directory. Path MUST be absolute")
print(" -o, --output Output directory. Path MUST be absolute") print(" -o, --output Output directory. Path MUST be absolute")
print(" --host Manager host")
print(" --port Manager port")
print(" -a, --api Manager api url") print(" -a, --api Manager api url")
print(" -t, --api-token Manager api token") print(" -t, --api-token Manager api token")
print(" -h, --help Shows this help and exit") print(" -h, --help Shows this help and exit")
@ -74,6 +72,17 @@ if output == "":
print("ERROR: No output directory given") print("ERROR: No output directory given")
sys.exit(1) sys.exit(1)
if input_dir == "":
print("ERROR: No input directory given")
sys.exit(1)
if not api:
print("ERROR: No api given")
sys.exit(1)
if not api_token:
print("ERROR: No api token given")
def get_sha1(fname): def get_sha1(fname):
if os.path.isfile(fname): if os.path.isfile(fname):
sha1 = hashlib.sha1() sha1 = hashlib.sha1()
@ -146,64 +155,54 @@ while running:
try: try:
name = "None" name = "None"
while name == "None": while name == "None":
if api == None: name = api_get_job("done", "packaging", api, api_token, none_exit=False)
name = get_job("done", host, port, none_exit=False)
else:
name = api_get_job("done", api, api_token, none_exit=False)
if name == "None": if name == "None":
print("No job got asigned. Trying again in one hour.") print("No job got asigned. Trying again in one hour.")
sleep(3600) sleep(3600)
match = re.match(r"([ew])(\d{3})([ns])(\d{2})", name)
ew = match.group(1)
ew_val = int(match.group(2))
ns = match.group(3)
ns_val = int(match.group(4))
ew_val_major = int(ew_val / 10) * 10
if ew == "w":
if ew_val_major != ew_val:
ew_val_major += 10
ns_val_major = int(ns_val / 10) * 10
if ns == "s":
if ns_val_major != ns_val:
ns_val_major += 10
name_major = ew + norm(ew_val_major, 3) + ns + norm(ns_val_major, 2)
print("Packaging " + name + "...") print("Packaging " + name + "...")
for part in ["Buildings", "Details", "Pylons", "Roads"]: area = get_area_name_by_tile(name)
run("rm -f " + output + "/" + part + "/" + name_major + "/" + name + ".txz", shell=True) area_major = get_area_name_by_tile(name, major=True)
if os.path.exists("projects/worldbuild/scenery/" + part + "/" + name_major + "/" + name):
run("mkdir -p " + output + "/" + part + "/" + name_major, shell=True)
cmd = "bash -c 'cd projects/worldbuild/scenery/" + part + "/" + name_major + " && tar -cJf " + output + "/" + part + "/" + name_major + "/" + name + ".txz " + name + "/'" for part in os.listdir(input_dir):
#run("rm -f " + output + "/" + part + "/" + area_major + "/" + area + ".txz", shell=True)
run("mkdir -p " + tmp_dir + "/" + area)
# If there is an archive, we need to extract it first and remove old data
if os.path.exists(output + "/" + part + "/" + area_major + "/" + area + ".txz"):
run("bash -c 'cd " + tmp_dir + "/" + area + " && tar -xf " + output + "/" + part + "/" + area_major + "/" + area + ".txz && rm *" + name + "*'")
# If there's new scenery copy it into the tmp dir
if os.path.exists(input_dir + "/" + part + "/" + area_major + "/" + area + "/" + name + ".stg"):
run("cp " + input_dir + "/" + part + "/" + area_major + "/" + area + "/*" + name + "* " + tmp_dir + "/" + area + "/")
# If the scenery is not empty, package it
if os.listdir(tmp_dir + "/" + area):
run("mkdir -p " + output + "/" + part + "/" + area_major, shell=True)
cmd = "bash -c 'cd " + tmp_dir + " && tar -cJf " + output + "/" + part + "/" + area_major + "/" + area + ".txz " + area + "/'"
with open(os.devnull, 'w') as fp: with open(os.devnull, 'w') as fp:
package = Popen(cmd , shell=True, start_new_session=True, stdout=fp) package = Popen(cmd , shell=True, start_new_session=True, stdout=fp)
package.wait() package.wait()
update_index(output, part, name, name_major) update_index(output, part, area, area_major)
# Clean up
run("rm -r " + tmp_dir + "/" + area)
print("Packaging " + name + " done") print("Packaging " + name + " done")
if api == None: api_send_status(name, "packaged", api, api_token)
send_status(name, "packaged", host, port)
else:
api_send_status(name, "packaged", api, api_token)
except KeyboardInterrupt: except KeyboardInterrupt:
print("Graceful shutdown triggered. To force immedate stop, press Ctrl+C again") print("Graceful shutdown triggered. To force immedate stop, press Ctrl+C again")
running = False running = False
try: try:
package.wait() package.wait()
update_index(output, name, name_major) update_index(output, area, name_major)
if api == None: api_send_status(name, "packaged", api, api_token)
send_status(name, "packaged", host, port)
else:
api_send_status(name, "packaged", api, api_token)
except KeyboardInterrupt: except KeyboardInterrupt:
# TODO doesn't work # TODO doesn't work
print("Forcing shutdown...") print("Forcing shutdown...")

View file

@ -25,10 +25,8 @@ from time import sleep, strftime, time
from common import send_status, get_job, norm, get_south, get_west, get_east, get_north, get_area_name, api_send_status, api_get_job from common import send_status, get_job, norm, get_south, get_west, get_east, get_north, get_area_name, api_send_status, api_get_job
action = "pending" action = "pending"
host = socket.gethostname()
api = None api = None
api_token = None api_token = None
port = 12345
prefix = "" prefix = ""
quiet = False quiet = False
global_db = False global_db = False
@ -37,13 +35,7 @@ argc = len(sys.argv)
i = 1 i = 1
first = 1 first = 1
while i < argc: while i < argc:
if sys.argv[i] == "--port": if sys.argv[i] == "-a" or sys.argv[i] == "--api":
i += 1
port = sys.argv[i]
elif sys.argv[i] == "--host":
i += 1
host = sys.argv[i]
elif sys.argv[i] == "-a" or sys.argv[i] == "--api":
i += 1 i += 1
api = sys.argv[i] api = sys.argv[i]
elif sys.argv[i] == "-t" or sys.argv[i] == "--api-token": elif sys.argv[i] == "-t" or sys.argv[i] == "--api-token":
@ -69,8 +61,6 @@ while i < argc:
print("") print("")
print(" -p, --prefix Database prefix to use") print(" -p, --prefix Database prefix to use")
print(" -g, --global-db Use global database") print(" -g, --global-db Use global database")
print(" --host Manager host")
print(" --port Manager port")
print(" -a, --api Manager api url") print(" -a, --api Manager api url")
print(" -t, --api-token Manager api token") print(" -t, --api-token Manager api token")
print(" -q, --quiet Don't print messages") print(" -q, --quiet Don't print messages")
@ -86,8 +76,12 @@ while i < argc:
sys.exit(1) sys.exit(1)
i += 1 i += 1
if api != None and api_token == None: if api != None:
print("Error: API given but no token") print("Error: No API url given")
sys.exit(1)
if api_token == None:
print("Error: No API token given")
sys.exit(1) sys.exit(1)
def cleanup(name): def cleanup(name):
@ -103,10 +97,7 @@ name = None
try: try:
running = True running = True
while running: while running:
if api != None: name = api_get_job(action, "started", api, api_token)
name = api_get_job(action, api, api_token)
else:
name = get_job(action, host, port)
run("mkdir -p projects/worldbuild-" + name, shell=True) run("mkdir -p projects/worldbuild-" + name, shell=True)
@ -151,10 +142,7 @@ try:
cleanup(name) cleanup(name)
if api != None: api_send_status(name, "done", api, api_token)
api_send_status(name, "done", api, api_token)
else:
send_status(name, "done", host, port)
except KeyboardInterrupt: except KeyboardInterrupt:
if not quiet: if not quiet:
print("Graceful shutdown triggered. To force immedate stop, press Ctrl+C again") print("Graceful shutdown triggered. To force immedate stop, press Ctrl+C again")
@ -163,10 +151,7 @@ except KeyboardInterrupt:
if build != None: if build != None:
build.wait() build.wait()
cleanup(name) cleanup(name)
if api != None: api_send_status(name, "done", api, api_token)
api_send_status(name, "done", api, api_token)
else:
send_status(name, "done", host, port)
except KeyboardInterrupt: except KeyboardInterrupt:
#TODO doesn't work #TODO doesn't work
print("Forcing shutdown...") print("Forcing shutdown...")