2020-01-13 17:02:11 +00:00
|
|
|
#! /usr/bin/python3
|
2020-01-16 01:27:33 +00:00
|
|
|
# Copyright (C) 2018-2020 Merspieler, merspieler _at_ airmail.cc
|
2020-01-13 17:02:11 +00:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License as
|
|
|
|
# published by the Free Software Foundation; either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but
|
|
|
|
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
import os
|
2020-01-15 17:03:16 +00:00
|
|
|
from subprocess import run, Popen, STDOUT
|
2020-01-13 17:02:11 +00:00
|
|
|
import sys
|
|
|
|
import socket
|
|
|
|
import re
|
2020-03-17 17:44:28 +00:00
|
|
|
from time import sleep, strftime, time
|
2020-01-13 17:02:11 +00:00
|
|
|
|
2021-03-03 19:51:58 +00:00
|
|
|
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
|
2020-03-05 19:04:43 +00:00
|
|
|
|
2020-01-13 17:02:11 +00:00
|
|
|
action = "pending"
|
|
|
|
host = socket.gethostname()
|
2021-03-03 19:51:58 +00:00
|
|
|
api = None
|
|
|
|
api_token = None
|
2020-01-13 17:02:11 +00:00
|
|
|
port = 12345
|
|
|
|
prefix = ""
|
2020-02-15 20:38:50 +00:00
|
|
|
quiet = False
|
2020-03-17 17:44:28 +00:00
|
|
|
global_db = False
|
2020-01-13 17:02:11 +00:00
|
|
|
|
|
|
|
argc = len(sys.argv)
|
|
|
|
i = 1
|
|
|
|
first = 1
|
|
|
|
while i < argc:
|
|
|
|
if sys.argv[i] == "--port":
|
|
|
|
i += 1
|
|
|
|
port = sys.argv[i]
|
|
|
|
elif sys.argv[i] == "--host":
|
|
|
|
i += 1
|
|
|
|
host = sys.argv[i]
|
2021-03-03 19:51:58 +00:00
|
|
|
elif sys.argv[i] == "-a" or sys.argv[i] == "--api":
|
|
|
|
i += 1
|
|
|
|
api = sys.argv[i]
|
|
|
|
elif sys.argv[i] == "-t" or sys.argv[i] == "--api-token":
|
|
|
|
i += 1
|
|
|
|
api_token = sys.argv[i]
|
2020-01-13 17:02:11 +00:00
|
|
|
elif sys.argv[i] == "-p" or sys.argv[i] == "--prefix":
|
|
|
|
i += 1
|
|
|
|
prefix = sys.argv[i]
|
2020-03-17 17:44:28 +00:00
|
|
|
elif sys.argv[i] == "-g" or sys.argv[i] == "--global-db":
|
|
|
|
global_db = True
|
2020-02-15 20:46:06 +00:00
|
|
|
elif sys.argv[i] == "-q" or sys.argv[i] == "--quiet":
|
|
|
|
quiet = True
|
2020-01-13 17:02:11 +00:00
|
|
|
elif sys.argv[i] == "-a" or sys.argv[i] == "--action":
|
|
|
|
i += 1
|
|
|
|
if sys.argv[i] == "pending" or sys.argv[i] == "started" or sys.argv[i] == "rebuild" or sys.argv[i] == "skip":
|
|
|
|
action = sys.argv[i]
|
|
|
|
else:
|
|
|
|
print ("ERROR: Unknown action " + sys.argv[i])
|
|
|
|
sys.exit(1)
|
|
|
|
elif sys.argv[i] == "-h" or sys.argv[i] == "--help":
|
2020-01-15 11:33:31 +00:00
|
|
|
print("usage:worldbuild-worker.py [OPTIONS]")
|
|
|
|
print("Retrives jobs from the manager and executes them")
|
2020-01-13 17:02:11 +00:00
|
|
|
print("")
|
2020-03-17 17:44:28 +00:00
|
|
|
print(" -p, --prefix Database prefix to use")
|
|
|
|
print(" -g, --global-db Use global database")
|
|
|
|
print(" --host Manager host")
|
|
|
|
print(" --port Manager port")
|
2021-03-03 19:51:58 +00:00
|
|
|
print(" -a, --api Manager api url")
|
|
|
|
print(" -t, --api-token Manager api token")
|
2020-03-17 17:44:28 +00:00
|
|
|
print(" -q, --quiet Don't print messages")
|
|
|
|
print(" -a, --action Considered tiles for build. Can be:")
|
|
|
|
print(" pending: Only builds pending tiles. These are always build: default")
|
|
|
|
print(" started: Run tiles marked as started. CAUTION, use with care.")
|
|
|
|
print(" rebuild: Build tiles flaged for rebuild")
|
|
|
|
print(" skip: Force building tile that are marked for being skipped")
|
|
|
|
print(" -h, --help Shows this help and exit")
|
2020-01-15 11:33:31 +00:00
|
|
|
sys.exit(0)
|
2020-01-13 17:02:11 +00:00
|
|
|
else:
|
2020-02-15 20:46:06 +00:00
|
|
|
print("Unknown option " + sys.argv[i])
|
|
|
|
sys.exit(1)
|
2020-01-13 17:02:11 +00:00
|
|
|
i += 1
|
|
|
|
|
2021-03-03 19:51:58 +00:00
|
|
|
if api != None and api_token == None:
|
|
|
|
print("Error: API given but no token")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2020-01-15 13:54:27 +00:00
|
|
|
def cleanup():
|
|
|
|
if os.path.isfile("projects/worldbuild-" + name + "/osm2city-exceptions.log"):
|
2020-08-27 11:09:40 +00:00
|
|
|
run("mv projects/worldbuild-" + name + "/osm2city-exceptions.log projects/worldbuild/output/error/" + name + "-" + strftime("%Y%m%d-%H%M") + ".exceptions.log", shell=True)
|
2020-01-15 13:54:27 +00:00
|
|
|
|
|
|
|
run("rm -rf projects/worldbuild-" + name, shell=True)
|
|
|
|
|
2021-03-09 13:18:02 +00:00
|
|
|
build = None
|
2021-03-09 13:13:17 +00:00
|
|
|
try:
|
|
|
|
running = True
|
|
|
|
while running:
|
|
|
|
if api != None:
|
|
|
|
name = api_get_job(action, api, api_token)
|
2020-01-15 11:33:31 +00:00
|
|
|
else:
|
2021-03-09 13:13:17 +00:00
|
|
|
name = get_job(action, host, port)
|
|
|
|
|
|
|
|
run("mkdir -p projects/worldbuild-" + name, shell=True)
|
|
|
|
|
|
|
|
run("cp projects/worldbuild/params.ini projects/worldbuild-" + name + "/", shell=True)
|
|
|
|
|
|
|
|
if global_db:
|
|
|
|
db_name = prefix + "worldbuild"
|
2020-01-13 17:02:11 +00:00
|
|
|
else:
|
2021-03-09 13:13:17 +00:00
|
|
|
if get_south(name) >= 80:
|
|
|
|
db_name = prefix + "n-pole"
|
|
|
|
elif get_south(name) < -80:
|
|
|
|
db_name = prefix + "s-pole"
|
2020-01-15 11:33:31 +00:00
|
|
|
else:
|
2021-03-09 13:13:17 +00:00
|
|
|
match = re.match(r"[0-9]{1,7}", name)
|
|
|
|
if match == None:
|
|
|
|
print("ERROR: Invalid tile name")
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
db_name = prefix + get_area_name(get_south(name), get_west(name), major=True)
|
2020-01-15 11:33:31 +00:00
|
|
|
|
2021-03-09 13:13:17 +00:00
|
|
|
if get_west(name) < 0:
|
|
|
|
bounds = "bounds=*"
|
|
|
|
else:
|
|
|
|
bounds = "bounds="
|
|
|
|
|
|
|
|
bounds += str(get_west(name)) + "_" + str(get_south(name)) + "_" + str(get_east(name)) + "_" + str(get_north(name))
|
|
|
|
|
|
|
|
run("sed -i 's/DB_NAME.*/DB_NAME = \"" + db_name + "\"/' projects/worldbuild-" + name + "/params.ini", shell=True)
|
|
|
|
|
|
|
|
run("echo '" + bounds + "' > projects/worldbuild-" + name + "/settings", shell=True)
|
2020-01-15 11:33:31 +00:00
|
|
|
|
2021-03-09 13:13:17 +00:00
|
|
|
if not quiet:
|
|
|
|
print("Building " + name)
|
2020-01-15 11:33:31 +00:00
|
|
|
|
2021-03-09 13:13:17 +00:00
|
|
|
logpath = "projects/worldbuild/output/" + get_area_name(get_south(name), get_west(name), major=True) + "/" + get_area_name(get_south(name), get_west(name)) + "/"
|
|
|
|
run("mkdir -p " + logpath, shell=True)
|
|
|
|
with open(logpath + name + "-" + strftime("%Y%m%d-%H%M") + ".log", "w") as log_file:
|
|
|
|
build = Popen("./build -S 10 -t 1 worldbuild-" + name, stdout=log_file, stderr=STDOUT, shell=True, start_new_session=True)
|
2020-01-15 11:33:31 +00:00
|
|
|
|
|
|
|
build.wait()
|
2021-03-09 13:18:02 +00:00
|
|
|
build = None
|
2021-03-09 13:13:17 +00:00
|
|
|
|
2020-01-15 13:54:27 +00:00
|
|
|
cleanup()
|
2021-03-09 13:13:17 +00:00
|
|
|
|
2021-03-03 19:51:58 +00:00
|
|
|
if api != None:
|
|
|
|
api_send_status(name, "done", api, api_token)
|
|
|
|
else:
|
|
|
|
send_status(name, "done", host, port)
|
2021-03-09 13:13:17 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
if not quiet:
|
|
|
|
print("Graceful shutdown triggered. To force immedate stop, press Ctrl+C again")
|
|
|
|
running = False
|
|
|
|
try:
|
2021-03-09 13:18:02 +00:00
|
|
|
if build != None:
|
|
|
|
build.wait()
|
2021-03-09 13:13:17 +00:00
|
|
|
cleanup()
|
|
|
|
if api != None:
|
|
|
|
api_send_status(name, "done", api, api_token)
|
|
|
|
else:
|
|
|
|
send_status(name, "done", host, port)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
#TODO doesn't work
|
|
|
|
print("Forcing shutdown...")
|
|
|
|
build.terminate()
|
|
|
|
sleep(5)
|
|
|
|
build.kill()
|
|
|
|
sleep(5)
|
|
|
|
cleanup()
|
|
|
|
sys.exit(0)
|